Advertisement Jump to content
Sign in to follow this  
EmptyVoid

How to get a CG vertex and pixel shader to work together?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't seem to figure out how to make a CG vertex and pixel shader work together. I create the shaders like this:
cgContext = cgCreateContext();
cg_vs = cgCreateProgramFromFile(cgContext, CG_SOURCE, "Data/Shaders/default.cg", cgGLGetLatestProfile(CG_GL_VERTEX), "VS", 0);
cgGLLoadProgram(cg_vs);
cg_ps = cgCreateProgramFromFile(cgContext, CG_SOURCE, "Data/Shaders/default.cg", cgGLGetLatestProfile(CG_GL_FRAGMENT), "PS", 0);
cgGLLoadProgram(cg_ps);
position		= cgGetNamedParameter(cg_vs, "IN.position");
color			= cgGetNamedParameter(cg_vs, "IN.color");
modelViewMatrix	= cgGetNamedParameter(cg_vs, "ModelViewProj");




Then I render them like this:
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0f, 25.0f, -45.0f, 0.0f, 0.0f, 0.0f, 0, 1, 0);
cgGLSetStateMatrixParameter(modelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLBindProgram(cg_vs);
cgGLBindProgram(cg_ps);
cgGLSetParameter4f(color, 0.5f, 1.0f, 0.5f, 1.0f);
	glTranslatef(0.0f,0.0f,0.0f);
	glBegin(GL_TRIANGLES);
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glVertex3f(-1.0f,-1.0f, 0.0f);
		glVertex3f( 1.0f,-1.0f, 0.0f);
	glEnd();	
SwapBuffers(hDC);
}




I have no idea what I need to do to make them work together. Can someone set me on the right path?

Share this post


Link to post
Share on other sites
Advertisement
In your rendering code before you draw you need to call cgGLEnableProfile() for both the vertex and fragment shaders. You can store the profiles in a CGprofile parameter, one for each. You can get this once when you're creating the shaders.

Share this post


Link to post
Share on other sites
Quote:
Original post by Edge Damodred
In your rendering code before you draw you need to call cgGLEnableProfile() for both the vertex and fragment shaders. You can store the profiles in a CGprofile parameter, one for each. You can get this once when you're creating the shaders.


Ok will that make them work together?

Share this post


Link to post
Share on other sites
Quote:
Original post by Edge Damodred
What do you mean by making them work together? It might help if you posted your shader code and what you're trying to make happen.


cgGLEnableProfile did work thanks!

Share this post


Link to post
Share on other sites
Actually I have one more question, when I remove both cgGLBindProgram statements it seems to still work fine. So my question is what does cgGLBindProgram do?

Share this post


Link to post
Share on other sites
cgGLBindProgram() basically tells the rendering pipeline to ignore OpenGL's Fixed Function commands. If all your shaders are doing is passing stuff through the vertex and fragment processors, you're not going to notice any difference between the shader code and OpenGL's pipeline. However if in your vertex shader you tell it to specifically assign a specific color to a vertex, it won't matter what color you tell OpenGL that vertex should be, your shader will override it as long as the vertex program is bound.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!