glLoadMatrix fails to update current matrix

So I’m trying to draw a shape using a matrix that I define. I do this


glMatrixMode(GL_MODELVIEW);
float m[16] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
glPushMatrix();
glLoadMatrixf(m);

It is my understanding that the current matrix should now be all 2’s. However, when I use the following code to check the current matrix, it returns the identity matrix.


glGetFloatv (GL_MODELVIEW_MATRIX, m1);

After this code runs, m1 SHOULD contain all 2’s, but instead it is the identity matrix, just as it is before I call glLoadMatrixf (I run that last line in a few places to see what the current matrix is). Can anyone see what I’m doing wrong?

I am able to draw a scene fine if I use a scene graph, or use the rotate, scale, transform functions on a matrix, but the load matrix method doesn’t seem to do anything.

For the specific job I’m working on, I need to be able to load custom matrices, not just manipulate them by rotating, scaling, translating. Any help would be welcome. Thanks.

Are you sure that you use glGetFloatv(…) the right way and actually pass a pointer to an array of floats (allocated to the correct size) as the second argument? Otherwise your m1 variable could still be initialized with default values.

yeah, I allocate it correctly and everything. If that were the case, I would think that it would give me close-to-zero values instead of the identity matrix. I never set it to the identity matrix, it just becomes that after the call to glGetFloatv…

glLoadMatrix expects a pointer to an array of 16 floats.
In your code you don’t supply a pointer, so the function call is invalid.


glMatrixMode(GL_MODELVIEW);
float m[16] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
glPushMatrix();
glLoadMatrixf(m);

BionicBytes, m is a pointer. This is even how C/C++ manages arrays: like a pointer.

Is there a GL context present? Did you make it current with wglMakeCurrent? Are you able to render object in your window?

http://www.opengl.org/wiki/FAQ#What_is_an_OpenGL_context.3F

@arts
fair play; I don’t code with C++ so it didn’t look right to me.

Sorry. I didn’t know you don’t program with C. Just thought you made a mistake by quickly read the post (it happens to me often), nothing bad :slight_smile:

Some things to check:

(1) Before reading the matrix back, retrieve the current matrix mode just to be sure…

(2) Maybe you’re out of matrix stacks? This can happen if you render complex hierarchical models. Minimum size is 32 entries for the modelview matrix stack. Try checking with

GLint depth;
glGetIntergerv ( GL_MODELVIEW_STACK_DEPTH, &depth );

This situation should, however, raise errors of type GL_STACK_UNDERFLOW when calling glPushMatrix(). Maybe check for that.

and in addition (if you haven’t already) bracket all your calls with glGetError, and see what it says


// glGetError 1
glMatrixMode(GL_MODELVIEW);
// glGetError 2
float m[16] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
glPushMatrix();
// glGetError 3
glLoadMatrixf(m);
// glGetError 4

this will give you the error state before and after every GL call on the way through your code…

I didn’t use any calls to wglMakeCurrent, but I am able to render a scene. This particular code is inside the creation of a display list (I just comment out the usual way of doing things to try this instead - my goal is to imitate in opengl the way I’ve been doing raytracing in order to troubleshoot that - and part of that is doing collision detection with a stored matrix (rather than a series of translate/rotate/scale calls) so I want to see what the stored matrix renders like to ensure it’s not different than the equivalent translate/rotate/scale).

Would the fact that it’s in a display list make a difference? And do I actually need to use wglMakeCurrent? I just create my window doing the usual glutinit() stuff, then create my display lists from my scenegraph, then call glutMainLoop() to draw it.

charlie, could you explain a bit more how to use that glGetError function? I’ve never used it before. Would I just uncomment those calls? Then how do I actually see the errors? Do I have to make some other function call to read the error state?

so… anyone?

Simply don’t do it inside display lists. This has no real sense since display lists are stored in server side, and such commands you are using are queries to be stored on client side.

thank you. Taking those function calls out of a display list fixed my issue. Could you explain a bit more what you mean though about server/client side calls? I’ve never heard those terms used in this context. I figured everything would be client side since I’m not networking to any other computer or anything.