Grabbing a Matrix from GL

Started by
6 comments, last by Disgruntled Gamer 17 years, 11 months ago
Hey, Is there any way to retrieve a matrix from OpenGL, by any chance? At the moment I'm using several calls to glMultMatrix to concatenate animation data on the GPU, but I've reached a point where I need to know the contents of it. The glMultMatrix calls are made at various points at initialisation and it would be irritating to have to rewrite a load of it. Also, I'm trying to perform as much as possible on the GPU, for obvious reasons. Obviously I considered removing the many glMultMatrix calls and use my own matrix class on the CPU and pass it around, but that seems not only a waste of time but also messy. If there's no way to do it though I guess that's how it has to be :) Any ideas chaps? Cheers, Peter
__________________________________Peter Lewis ([email=me_AT_pjblewis.com]E-mail[/email] | Portfolio)
Advertisement
double matrix[16];
glGetDoublev(GL_MODELVIEW_MATRIX, matrix);
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
You rock <3
__________________________________Peter Lewis ([email=me_AT_pjblewis.com]E-mail[/email] | Portfolio)
I have some old docs, so maybe this has changed. But make sure you use glPushMatrix() before your glGetDoublev() call because glGet retrieves the modelview matrix from the top of the matrix stack.

Hope this helps.
Quote:Original post by Disgruntled Gamer
I have some old docs, so maybe this has changed. But make sure you use glPushMatrix() before your glGetDoublev() call because glGet retrieves the modelview matrix from the top of the matrix stack.

Hope this helps.
Yes, it gets the current matrix at the top of the modelview matrix stack (or projection, texture, etc depending on what you use for pname) but it doesn't modify it, so pushing and popping is not needed.
Quote:Original post by Kalidor
Yes, it gets the current matrix at the top of the modelview matrix stack (or projection, texture, etc depending on what you use for pname) but it doesn't modify it, so pushing and popping is not needed.


I wasn't thinking of glPushMatrix() as a way of saving the matrix from being modified. I was doing it to put my current matrix on top of the stack so when I call glGet it gets the matrix on top of the stack which is the one I just pushed there.

This is how I think it works, but I could be wrong.
Quote:Original post by Disgruntled Gamer
I wasn't thinking of glPushMatrix() as a way of saving the matrix from being modified. I was doing it to put my current matrix on top of the stack so when I call glGet it gets the matrix on top of the stack which is the one I just pushed there.

This is how I think it works, but I could be wrong.
The current matrix is already at the top of the stack. glPushMatrix adds a copy of the current matrix to the top of the stack so that there are two identical matrices at the top and just below the top. This lets you alter the matrix at the top (the current matrix) however you want and then you can pop it to get back to the previous matrix. Calling glPushMatrix too many times without associated glPopMatrix calls will produce a GL_STACK_OVERFLOW error, because you are only adding extra matrices to the stack and never removing them so it will eventually fill up.

You can also query OpenGL for the maximum stack depth using glGet* with pname set to one of the GL_MAX_*_STACK_DEPTH constants.

EDIT: So to sum up, the top of the current matrix mode's (ie: GL_MODELVIEW, GL_PROJECTION, etc) stack is always the current matrix. It will be the matrix that is altered by any call to functions such as glRotate*, glMultMatrix*, glFrustum, gluLookAt, etc.
Thanks for the clarification Kalidor, I just did a few glGet calls without the glPushMatrix calls and I was getting new versions of my matrix after calls to glRotate of gluLookAt. I just didn't realize there was already a copy of the current matrix on the stack, but it does make sense. And I do know you need to call glPopMatrix for every glPushMatrix or you can overload the stack. Sorry if I caused any confusion in the thread.

This topic is closed to new replies.

Advertisement