I have found very useful resource on the Swiftless website on OpenGL.

Unfortunately, I cannot manage to adapt a GLSL fragment shader to my project, which uses Cg. Here it is:

uniform sampler2D color_texture;  
uniform sampler2D normal_texture;  

void main() {  

// Extract the normal from the normal map  
vec3 normal = normalize(texture2D(normal_texture, gl_TexCoord[0].st).rgb * 2.0 - 1.0);  

// Determine where the light is positioned (this can be set however you like)  
vec3 light_pos = normalize(vec3(1.0, 1.0, 1.5));  

// Calculate the lighting diffuse value  
float diffuse = max(dot(normal, light_pos), 0.0);  

vec3 color = diffuse * texture2D(color_texture, gl_TexCoord[0].st).rgb;  

// Set the output color of our current pixel  
gl_FragColor = vec4(color, 1.0);  

I have tried something:

struct fsOutput {
    vec4 color : COLOR;

uniform sampler2D detailTexture : TEXUNIT0;
uniform sampler2D bumpTexture : TEXUNIT1;

fsOutput FS_Main(float2 detailCoords : TEXCOORD0,
            float2 bumpCoords: TEXCOORD1)

    fsOutput fragm;

    float4 anorm = tex2D(bumpTexture, bumpCoords);
    vec3 normal = normalize(anorm.rgb * 2.0f - 1.0f);
    vec3 light_pos = normalize(vec3(1.0f, 1.0f, 1.5f));
    float diffuse = max(dot(normal, light_pos), 0.0);
    vec3 color = diffuse * texture2D(detailTexture, detailCoords).rgb;
    fragm.color = vec4(color, 1.0f);
    return fragm;

But it doesn't work. To debug, I have a function that catches Cg errors, and my program breaks at this point. I have identified the two texture IDs in the main program. Can you suggest any improvement for this Cg shader?


1 Answer 1


I tried compiling it from the command-line as follows:

cgc -profile glslf -entry FS_Main test.cg

This gave the following error output:

test.cg(18) : error C1066: invalid type in type constructor
test.cg(18) : error C1010: expression left of ."rgb" is not a struct

This immediately highlights the fact that you used texture2D on line 18 instead of the correct Cg function tex2D. Fixing this error makes it compile correctly.

In the future, you should probably use the cgGetLastListing function when cgGetError returns CG_COMPILER_ERROR. This will allow you to print out the error listing in your application, which makes your shaders easier to debug.

On another note, you use vec3 and vec4 on various occasions. While the Cg compiler seems to accept this, the correct types are float3 and float4, I believe.

  • \$\begingroup\$ Thanks, I didn't know the command line method - but did you really compile the SECOND shader? The second is my Cg try, the first is the original GLSL. I have already put tex2D. The program crashes before it can display any error :( \$\endgroup\$
    – Ety
    Jul 11, 2014 at 14:01
  • \$\begingroup\$ Yes, I compiled the second shader, the Cg one. The second shader still contains one mention of texture2D as well as a mention of tex2D. \$\endgroup\$
    – rdb
    Jul 11, 2014 at 14:03
  • \$\begingroup\$ Aye Sir... You are totally right. The shader seems to compile fine now but the program keeps crashing for some reason. I will look this "cgc" command up to help me debug in the future. Thanks again \$\endgroup\$
    – Ety
    Jul 11, 2014 at 14:11
  • \$\begingroup\$ I should note that using cgc is not a substitution for proper use of the Cg Runtime's error checking API. All that cgc does can be done (and should be done) using simple API calls such as cgGetLastListing. It was merely a convenient way for me to see which errors it would produce, which would have equally been printed by a call to cgGetLastListing. \$\endgroup\$
    – rdb
    Jul 11, 2014 at 14:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .