Problem with a simple Cg shader

This topic is 4524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello, I am new to Cg programming, and I am trying to run a cg shader program on a basic one-triangle-scene. I encountered some problems, so I made the shader as simple as possible : here is the vertex program :
struct VP_Output
{
float4 pos	: POSITION;
float4 color	: COLOR0;
};

struct VP_Input
{
float4 pos	: POSITION;
float4 color	: COLOR0;
};

VP_Output main ( VP_Input IN, uniform float4x4 modelViewProj )
{
VP_Output OUT;

OUT.pos = mul(modelViewProj, IN.pos );
OUT.color = IN.color;

//OUT.color.r = 0.5;

return OUT;
}


and the fragment program :
void main ( VP_Output IN, out float4 outCol : COLOR )
{
outCol = IN.color;
}

As you can see it is quite basic :) It works fine, untill I uncomment one line in the vertex program :
OUT.color.r = 0.5;

Then the triangle i draw with this shader program is not rendered anymore .. The triangle disappears the same way whatever value i assign to OUT.color, or when i add a TEXCOORD0 input parameter that i try to modify manually. I can't figure out what is wrong ; have you got any clue ?

Share on other sites
Hi,

As a quick suggestion, have you tried 0.5f instead of 0.5?
I will try to test your shader later on and see if there are any other problems, but I am busy at the moment and not near my main development pc.

All the best,
ViLiO

Share on other sites
hmmmm...that is odd. I used your source and was able to compile it with the compiler in the /bin directory that same with the Cg sdk. I got this output from the compiler:

23 lines, 0 errors.
vs.1.1
// DX8 Vertex shader generated by NVIDIA Cg compiler
// cgc version 1.4.0000, build date Sep 26 2005 21:50:31
// command line args:
// source file: C:\Documents and Settings\Main\Desktop\test.cg
// nv30vp backend compiling 'main' program
def c4, 0.5, 0, 0, 0
//vendor NVIDIA Corporation
//version 1.0.02
//profile vs_1_1
//program main
//semantic main.modelViewProj
//var float4x4 modelViewProj : : c[0], 4 : 1 : 1
//var float4 IN.pos : $vin.POSITION : ATTR0 : 0 : 1 //var float4 IN.color :$vin.COLOR0 : ATTR5 : 0 : 1
//var float4 main.pos : $vout.POSITION : HPOS : -1 : 1 //var float4 main.color :$vout.COLOR0 : COL0 : -1 : 1
//const c[4] = 0.5 0 0 0
dp4 oPos.x, c0, v0
dp4 oPos.y, c1, v0
dp4 oPos.z, c2, v0
dp4 oPos.w, c3, v0
mov oD0.yzw, v5
mov oD0.x, c4.x
// 6 instructions
// 0 temp registers

This might not be off any use to you since you said that it works if that one line is uncommented, but are you sure that the context has been setup properly? I think your best bet would be to try to run it through the stand-alone compiler and see if it generates any errors there.

Share on other sites

I did try 0.5f, 1.0f or 0.0f instead of 0.5, but nothing changed.
It looks like every manual modification the color value makes my triangle not being displayed ( I mean that if I multiply OUT.color with the projection matrix, the color doesnt make any sense but the triangle is rendered anyway ; but setting the red value to 0.5 causes the problem )

I also compiled the program with the cgc compiler, using vs_1_1 profile for the vertex program and arbfp1 for the fragment program : I got no error output ..

I load the cg program into my opengl application with the cg runtime library, and I setup the vertex and fragment profiles with a call to cgGLGetLatestProfile(..) for each one. Could there be a problem in the opengl app ?

Share on other sites
does the triangle still not visible if you do something to the effect of:

OUT.color = float4( 0.5, IN.color.g, IN.color.b, 1.0 );

This is indeed a stange problem. It seems that you have setup your context correctly if you can render the triangle without that line of code. If you like, post a copy of your source and I'll take a look at it.

Share on other sites
I did what you suggest, and the triangle disappeared once more ..

Assigning new values or 'manually' modifying variables always causes this problem.
Anyway, I can get past it by setting up a matrix in the opengl app, passing it to the shader program and using it to work with my variables - which, i guess, is also quicker. But I can see no reason why the above shader shouldn't work :(

I also noticed the same thing happened when i made a similar modification in various example cg shaders downloaded here and there. Is my problem linked with a cg language restriction, or could my graphic card or driver cause it ?

• 23
• 10
• 19
• 15
• 14