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

Started by
5 comments, last by Edge Damodred 15 years, 8 months ago
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?
This is your life, and it's ending one minute at a time. - Fight club
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.
-----------------------Or, as I put it, MMORPG's are currently about attaining two primary things: strength and a shovel. The rest is you just shoveling sh** endlessly trying to get stronger to shovel more sh** so you can look for the next new shovel to shovel more sh** with. Once you are done, you can stand on top of a large pile of sh**, raise your golden sh** shoveler up high into the air and boast how proud you are to be the best sh** shoveler of them all. -Griffin_Kemp
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?
This is your life, and it's ending one minute at a time. - Fight club
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.
-----------------------Or, as I put it, MMORPG's are currently about attaining two primary things: strength and a shovel. The rest is you just shoveling sh** endlessly trying to get stronger to shovel more sh** so you can look for the next new shovel to shovel more sh** with. Once you are done, you can stand on top of a large pile of sh**, raise your golden sh** shoveler up high into the air and boast how proud you are to be the best sh** shoveler of them all. -Griffin_Kemp
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!
This is your life, and it's ending one minute at a time. - Fight club
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?
This is your life, and it's ending one minute at a time. - Fight club
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.
-----------------------Or, as I put it, MMORPG's are currently about attaining two primary things: strength and a shovel. The rest is you just shoveling sh** endlessly trying to get stronger to shovel more sh** so you can look for the next new shovel to shovel more sh** with. Once you are done, you can stand on top of a large pile of sh**, raise your golden sh** shoveler up high into the air and boast how proud you are to be the best sh** shoveler of them all. -Griffin_Kemp

This topic is closed to new replies.

Advertisement