Jump to content
  • Advertisement
Sign in to follow this  

OpenGL [SOLVED]Cg and OpenGL Rendering Error - Need Help badly

This topic is 3503 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

Hey all. I started adding Cg to my OpenGL based rendering engine yesterday and so far i have had an incredible hassle at the first hurdle, just getting Cg to draw. The engine is built upon classes, theres a class for objects in the scene (holds tansformation data and matrices), a class for imported meshes these pass the mesh data to the objects so that they can be draw and transformed and there s class for shaders. Each shader class holds the Cg parameters and initialises it's own Cg context and program, the shader class (like the mesh class) can be attached to an object in the scene, when it is the Cg shader's enable and bind functions are called at the start of the object's render and unbind etc.. is called at the end of the render. My problem is that this sometime's draws and sometimes does not, most of the time it doesn't, this is the part that really has me stuck, if it was a straight error then nothing would work, but there are no error messages from Cg or OpenGL, if it never drew then there would obviously be an error in the rendering code or the shader, but it sometimes draws and runs perfectly, that has me stumped. Right now I am only running the most basic of basic vertex shaders just to get it to draw, all it does is multiple the out position by the modelview projection matrix and the incomnig position. So I think the problem must be in the multiplication of the modelview projection matrix, its the only thing that's there to be wrong. But i can't solve it no matter how many ways I try. Below is the current Cg vertex shader:

 void C2E1v_green(	float4 position : POSITION,
					float3 color	:	COLOR,

					out float4 oPosition	: POSITION,
					out float3 oColor		: COLOR,

             uniform float4x4 modelViewProj)
  // Transform position from object space to clip space
  oPosition = mul(glstate.matrix.mvp, position);
  oColor	= float3(0,1,0);

Below is the shader class binding function:

			// Enable the profile
			checkForCgError("Enabling vertex profile", myProgramName,  myCgContext);

			// Bind the program
			checkForCgError("Binding vertex program", myProgramName,  myCgContext);

(As the shader calls the glstate.matrix.mvp there is no need to pass in a matrix from OpenGL through Cg parameters Below is the object rendering function:

		// Set the modelview matrix

		// Set the colour to white
		glColor3f(1.0f, 1.0f, 1.0f);

		// Enable the texturing

		// Counter clockwise faces are front facing

		// Push the matrix

			// Multiple the matrices

			// Check the Bone Handler
			if(pBoneHandler != NULL)
				// Bind the skin

				// Draw the bones (depending on their own render state

			// Bind the texture
			if(pShader != NULL)
				// Set up the textures
				glBindTexture(GL_TEXTURE_2D, pShader->GetTextureName());

				// Bind the shader

			// If there is a mesh then render it, if not then draw the axis
			if(pMesh != NULL)
				// Enable the array client states.

				// Draw the elements
				glVertexPointer(3, GL_FLOAT, 0, pMesh->GetVertices());
				glNormalPointer(GL_FLOAT, 0, pMesh->GetNormals());
				glTexCoordPointer(2, GL_FLOAT, 0, pMesh->GetTextureCoords());
				glDrawElements(GL_TRIANGLES, pMesh->GetIndexSize(), GL_UNSIGNED_INT, pMesh->GetIndices());

				// Disable the array client states.

			// Check if a Camera is pointed to
			if(pCamera != NULL)

			// Check the bone handler
			if(pBoneHandler != NULL)
				// Bind the skin

			// UnBind the shader
			if(pShader != NULL)

		// Pop the matrix

Previously I have tried to pass in the matrix to the uniform float4x4 modelviewproj Cg param of the shader and to mulitply that. I have passed it in using the following methods:

// Set the CgParameters	
		cgGLSetStateMatrixParameter(myCgVertexParam_modelViewProj, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);


// Create the model matrix
			multMatrix(ModelMatrix, pObject->GetTranslateMatrix(), pObject->GetRotateMatrix());
			multMatrix(ModelMatrix, ModelMatrix, pObject->GetScaleMatrix());

			// Create the view matrix
			multMatrix(ViewMatrix, pCamera->GetInverseTranslateMatrix(), pCamera->GetInverseRotateMatrix());

			// Create the model view matrix
			multMatrix(ModelViewMatrix, ViewMatrix, ModelMatrix);

			// Get the Modelview and projection matrices
			glGetFloatv(GL_PROJECTION_MATRIX, ProjectionMatrix);

			// Create the ModelViewProjectionMatrix
			multMatrix(ModelViewProjectionMatrix, ModelViewMatrix ,ProjectionMatrix);

cgSetMatrixParameterfr(myCgVertexParam_modelViewProj, ModelViewProjectionMatrix);

And now calling
oPosition = mul(glstate.matrix.mvp, position);

In the shader All have the same result, sometimes it renders and sometimes it does. Please help, I'm really stumped. [Edited by - RoadToRuin on November 19, 2008 3:59:31 PM]

Share this post

Link to post
Share on other sites
Just a quick thought all, this code is currently running our toolset and this toolset has multiple other windows all running their own instances of OpenGL. Is there any chance that Cg is getting confused as to which instance of OpenGL to either get the state of (glstate.) or to output to?

It would explain the why it sometimes renders and sometimes does not.
If it is the case, is there any way that this can be limited?

Thanks in advance.

Share this post

Link to post
Share on other sites

I am nervous writing this because usually when it works it promptly stops again, but after ten straight working compiles, it works!!

Ok for anyone in the future who gets issues with Cg (possibly other shaders too), shader classes and win32 wrapper classes, the solution is to set and initialise Cg in the wrapper class, just after the window is created. Have the wrapper class output the Cg Context, vertex and fragment programs and profiles and any other needed shader parameters that you may have, then use those outputs to set the contexts, profiles and programs in your shader class. This will work.

The problem would appear to have been setting the contexts, programs etc.. in the shader class (in its constructor), as it was isolated from a specific OpenGL initialisation and therefore would output to any which one.

I should have known better really, buts its been months since I last worked shaders and as soon as it didn't work I should dumped it and tried a different method as soon as it failed, but unfortunately the first time it actually happened to output to the correct screen was the first time I tried the method, so I was dead set that it should work, oh well.

Anyway I hope this can help someone else out in the future!
Thanks to everyone who looked and tried to think of a way to help.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!