CG Shader Woes

Started by
9 comments, last by evilsanta 14 years, 9 months ago
Hello, I have a 2d opengl game (orthogonal projection) that I am working on. I want to mess around with shaders so I attempted to implement CG into my program. I can compile and set the shaders just fine, however I am having some trouble actually altering anything with shaders. When I apply a shader to a quad for example, it tends to just disappear completely. I have a feeling this is because I am using orthogonal projection, do I need to tell CG my screen coordinates for example? Additionally I am not attempting any complex shaders, just things that should alter the color of a quad. Any help is appreciated.
Advertisement
When using shaders, you have to apply the modelview and projection matrices yourself, i.e. in your shader.

The output will be a coordinate in screen coordinates, i.e. in range [-1,1] for x and y and [0,1] for z. If a vertex lies outside that range it will be clipped away. If all of your quad's vertices lie outside, your quad might get clipped away completely.

As a short test (to get your shaders working) render a triangle with coordinates (-0.5,-0.5,0.0), (0.5,-0.5,0.0), (0.0, 0.5, 0.0) which should appear right in the center of the screen if you don't apply any transformations in the shader (note that this assumes that front facing polies are CCW wound).
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Thanks for your reply. When I disabled orthogonal projection, I was able to render the triangle like you suggested. This worked, my shader turned the triangle green.

But how would I apply the modelview and projection matricies when using orthogonal projection?
Generally it works the same way. Your orthogonal projection matrix might be faulty, however. How did you set it up?
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Hi, the following is my code to enable 2d.


	GLint iViewport[4];	glGetIntegerv( GL_VIEWPORT, iViewport );	glMatrixMode( GL_PROJECTION );	glPushMatrix();	glLoadIdentity();	glOrtho( iViewport[0], iViewport[0]+iViewport[2],			 iViewport[1]+iViewport[3], iViewport[1], -1, 1 );	glMatrixMode( GL_MODELVIEW );	glPushMatrix();	glLoadIdentity();	glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT );	glDisable( GL_DEPTH_TEST );	glDisable( GL_LIGHTING );



But I think it's like you said, I need to apply the modelview and projection matrices myself in my shader. Right now the shader I am using doesn't appear to be handling any of that. I am using a sample shader provided with the cg library. I am just unsure how to have the shader modified so that it outputs the correct screen coordinates. Right now when I apply the shader to a quad in 2d mode the quad appears a lot bigger than it should be, it's green, but the location and size is not what it should be.

The following code is my shader, if anyone can give me any help with getting this to work I would appreciate it, even if it's just the commands required to get it to work, as right now I am almost completely clueless.

// This is C2E1v_green from "The Cg Tutorial" (Addison-Wesley, ISBN// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 38.struct C2E1v_Output {  float4 position : POSITION;  float3 color    : COLOR;};C2E1v_Output C2E1v_green(float2 position : POSITION){	  C2E1v_Output OUT;  OUT.position = float4(position,0,1);  OUT.color = float3(0,1,0);  return OUT;	}


[Edited by - evilsanta on June 25, 2009 1:04:29 AM]
First, your shader need the modelview and projection matrices as uniform parameters, next you need to apply them. Since the combined modelview projection matrix (mvp) doesn't change during shader execution you might calculate it on the CPU and pass the result to the shader.

// This is C2E1v_green from "The Cg Tutorial" (Addison-Wesley, ISBN// 0321194969) by Randima Fernando and Mark J. Kilgard.  See page 38.struct C2E1v_Output {  float4 position : POSITION;  float3 color    : COLOR;};C2E1v_Output C2E1v_green(float2 position : POSITION, uniform float4x4 mvp){	  C2E1v_Output OUT;  OUT.position = mul(mvp, float4(position,0,1));  OUT.color = float3(0,1,0);  return OUT;	}
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Hello again,

I modified my shader code to match yours. I still seem to be having trouble getting it to work though. The following is my code that multiplies the two matrices. It seems like it is correct, am I using the correct set parameter command?

	GLfloat modelview[16];	glGetFloatv(GL_MODELVIEW_MATRIX , modelview);	glMatrixMode(GL_PROJECTION_MATRIX);	glMultMatrixf(modelview);	GLfloat output[16];	glGetFloatv(GL_MATRIX_MODE, output);        cgGLSetParameterArray4f( mvp,0,16,output);
It looks like your shader is a vertex shader, you probably want to make a fragment shader instead...

If you don't activate a vertex shader, fixed function pipeline will take care of the projection matrices for you.
You might use cgGLSetMatrixParameterfc(...) or - for simplicity - cgGLSetStateMatrixParameter(<your parameter>, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY); With the second, CG will handle the reading and multiplication for you.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Quote:Original post by Lord_Evil
You might use cgGLSetMatrixParameterfc(...) or - for simplicity - cgGLSetStateMatrixParameter(<your parameter>, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY); With the second, CG will handle the reading and multiplication for you.


I use that and it works great... saves a lot of math for me... although the inverse modelview matrix calc was broken until last release.

------------------------------

redwoodpixel.com

This topic is closed to new replies.

Advertisement