Hi
I have been programming for a little while in OpenGL but I seem to have a misconception about how the pipeline manages the order of rendering from Model to View to World.
In everything I have read, including the Red Book and SuperBible, it states that (for example) transforms preceeding a mesh object occur in that object’s Model space (local object space).
ie.
glPushMatrix();
glRotatef(45, 1, 0, 0);
<draw_object>
glPopMatrix();
will apply a rotation of 45 degrees on the object’s local space along the X vector.
So, if we had the pseudo-code of:
glViewport(...);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
SetPerspective(...);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(...);
SetLights(...);
glPushMatrix();
LookAt(...);
glPushMatrix();
glRotatef(45, 1, 0, 0);
<draw_object>
glPopMatrix();
...
glPopMatrix();
We would end up with “object” rotated 45 degrees in the view.
My (apparently incorrect) understanding was that within this set of code, we would have three distinct spaces.
Defines World space:
glViewport(...);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
SetPerspective(...);
Defines our Camera/Eye (View) space:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(...);
SetLights(...);
glPushMatrix();
LookAt(...);
Occurs at local Model (object) space:
glPushMatrix();
glRotatef(45, 1, 0, 0);
<draw_object>
glPopMatrix();
I always assumed that the glRotatef would modify only the object vertex data so the result would be a correctly lit object.
However, I must be incorrect and apparently View and Model are within the same space, as the rotated object will also have rotated lighting on it, in other words, it’s lighting will be incorrect (rotated 45 degrees away from the actual light position set in SetLights(…);).
SetLights(…); occurs in the Identity Matrix of ModelView so it is a fixed (stationary) light source (see RedBook 6th Edition pg.202).
So am I to understand that there is no real “local object space” for direct object transformations, just World and ModelView space, and any transforms such as rotation that are to be applied to a model prior to lighting must be done independantly and prior to sending the object to OpenGL, ie. through directly manipulating the object’s vertex array prior to DrawElements or whatever is being used for the object.
I have an understanding of matrices etc., and I believe I see why this would be occurring, however I was under the assumption that OpenGL rendered in an in-to-out and bottom-to-top order. So I assumed that the object’s glRotate would occur on its vertex data array prior to the View space matrix set in LookAt.
But apparently the matrix in glRotate is multiplied with LookAt before being applied to the object vertex data so the result is the entire ModelView space is modified for this object, hence the light direction within this rotated space also changes, and the lighting then also rotates 45 degrees (??).
Am I correct in my last assumption, or did I just confuse you all.
I hope I was clear in all of this.
Thanks.