Sign in to follow this  
whathappened

OpenGL Inverting matrix in Cg

Recommended Posts

Im writing a shader and in order to be able to transform some values, I would like to know if there are any functions in Cg for invertering/take the transpose of a matrix? If it isnt, is there any nice ways to do this in OpenGL?

Share this post


Link to post
Share on other sites
I don't have the Cg manual right here, but I'm sure there is a function at least for transposing a matrix. However, for performance reasons you'd better invert/transpose the matrix by yourself and send the matrix and its transpose / inverted counterpart to the shader.

AFAIK there are no options for inverting or transposing a matrix in OpenGL. You'd have to do that on your own.

Also check your code and find out if it is REALLY necessary to transpose the matrix. Some change in the code might result in transposing being unnecessary.

Share this post


Link to post
Share on other sites
Well I want to calculate a normal (and I can't let OpenGL do it for me since it's not the normal for the triangel currently being rendered). But I found a way,
cgGLSetStateMatrixParameter(*worldMatrixIT, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE);

Is it really better to do it by myself?

Another thing, is there any good Cg manuals out there? I cant find anything good on google.

Share this post


Link to post
Share on other sites
Quote:

cgGLSetStateMatrixParameter(*worldMatrixIT, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE);

Is it really better to do it by myself?


No, that should be fine if you just need the current modelview matrix. Just don't recalculate the inverted matrix every time the shader is executed, but calculate the inverse/transpose once (in software) and pass the result. The above Cg call should do this for you.

Note: Keep in mind that the first parameter is of type CGparameter, so no user defined matrix! Also, the modelview matrix in general will contain camera transformations as well, so it might not be that usefull for calculating normals in world space.

Quote:

Another thing, is there any good Cg manuals out there? I cant find anything good on google.

Well, I just use the CgUsersManual which is sufficient for me [smile].

Share this post


Link to post
Share on other sites
Keep in mind that calls like cgGLSetStateMatrixParameter internally does a glGetDoublev call which stalls the pipeline (killing performance).

You're better off inverting the matrix in your main app (search for fast matrix math libraries) and sending the result to Cg through a CGparameter.

I would advise against this function unless you really HAVE to use it for some reason.

Share this post


Link to post
Share on other sites
SirKnight: How do I send the different things as a CGparameter. Isnt that what Im doing? Also, is it possible to automatically pass the current transform to the shader, instead of using cgGLSetStateMatrixParameter all the time?

Basically what I do is to cast shadow volumes using a shader, and I need to discard the volumes that are casted by a front-facing polygon (therefore I send the normal of the polygon and the inverse/transpose to get the normal of the polygon (after transformation). But I need this when I draw the Volumes so I cant let OpenGL calculate the normal and send it.

Lord:Actually I split the camera and the world transformation. I do the camera transform in the beginning of my program, and then I set my cameraMatrix to that value using cgGLSetStateMatrixParameter(viewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);

Then, when I render something, I set the modelMatrix. And I use both of those in my shader (of course).

Share this post


Link to post
Share on other sites
Quote:

Lord:Actually I split the camera and the world transformation. I do the camera transform in the beginning of my program, and then I set my cameraMatrix to that value using cgGLSetStateMatrixParameter(viewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);

Actually there are 3 matrices: projection, view (camera) and model (world). modelview contains both, the camera matrix and the modelview matrix.

Quote:

SirKnight: How do I send the different things as a CGparameter. Isnt that what Im doing? Also, is it possible to automatically pass the current transform to the shader, instead of using cgGLSetStateMatrixParameter all the time?

No cgGLSetStateMatrixParameter does read back the state matrix from OpenGL and pass it to the shader. Use cgGLSetMatrixParameter instead and pass a pointer to your matrix.

Additionally, if you stick to OpenGL you could access the state matrices in your shader directly much like you do in GLSL.
@SirKnight: Do you know if there is a performance hit with this?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this