glGet for matrices doesn't seem to work

(Windows98, DevStudio, nVidia TNT2)

I am using this snippet of code (taken from a FAQ) to translate clicks on the screen/window to world coordinates:

GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy;
GLdouble wx, wy, wz;

glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
realy = viewport[3] - (GLint) ScrY - 1;

gluUnProject( (GLdouble)ScrX, (GLdouble) realy, 1.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

I do initialize the float arrays (code not shown here) for the matrices to set values and, after tracing through the calls which attempt to get the various matrices in the debugger, their values have NOT changed. I get zero, zip, nada results from any of the glGet calls.

This code is in a function that is called on a WM_TIMER message to the WndProc, is there a restriction of some sort on where the glGet* functions can be called from? Is there a particular pitfall to using the glGet* calls to get matrices? Do I have to activate a rendering context or something similar before they will work? Is there perhaps other code that can be called immediately before this that will cause the calls to not return a result?

I am not calling the glGet’s from within a glBegin/glEnd pair either.

Thanks for any help.

ARGH!
I SHOULD have said the code is being called from a WM_MOUSEMOVE event, NOT a WM_TIMER. And I do draw from within the WM_TIMER handler, so it’s entirely likely I AM in fact calling glGet* from within the begin/end pair. Error code investigations will probably confirm this.

Okay, new questions.

What is the cleanest way to insure that you do not call a glGet function from within the glBegin/glEnd pair?

Is there a preferable way to do OpenGL animation besides using the WM_TIMER messages? I suppose I could do the idle message loop thing in winmain, but I’ve always been told to avoid that.

I think your problem is that you grab the matrices at the wrong places where they might be really unchanged…I would get the Projection matrix right after the gl-calls which set up your projection matrix and I would grab the modelview-matrix right after your rotation-code…

Little example:

double modelv[16];

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotate(…);
All your rotate translate calls…
glGetDoublev(GL_MODELVIEW_MATRIX,modelv);

And I would not use doubles with OpenGl…There is no point where you really need the precision it just slows down things alot…(not in this example but if you do all your drawing commands with doubles…).
So I would use floats everywhere…

HTH,XBTC!

[This message has been edited by XBCT (edited 08-04-2000).]

That was definitely the problem. When I cache the matrices immediately after the calls which set them up, it works like it should.

Thanks.