# OpenGL [Solved] Problem understanding glLoadIdentity and more

This topic is 3328 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! Once upon a time I thought I had this down, but apparently I'm wrong. And confused. I had thought that calling glLoadIdentity(); clears the current matrix. Say if I have the MODELVIEW matrix active and call a glLoadIdentity, that matrix would be cleared. Which is why I call it every frame. To clear the matrix and make the frame ready to be drawn again. Now. I draw two spheres and a text. I push the matrix before calling my display lists and pop it afterwards. This would make the transformations and so on only apply to the active object. I thought. When I render my text I can only render it if I call the text drawing function at the bottom (before glFlush()), or else it won't show up. And regardless of what I do to the matrix it keeps rotating with the camera. If I try to clear the matrix the text disappears. This is true for everything but the "stars sphere". If I call glLoadIdentity(); on the "globe sphere" it disappears, if I call it on the text it disappears, but if I call it on the stars they just won't rotate with the camera, but they won't disappear (which is good). The problem is: I want the text to stay put and not rotate with the camera I have misunderstood the glLoadIdentity(); call or how to use it I have misunderstood what the glPush/PopMatrix(); call does The easy answer here is "just read up on it!" and I agree with that, but I think I need simpler language to understand it, since neither books nor online texts have been enough, it would seem ... Code:
void drawGlobe(float size, int slices){
glPushMatrix();
glTranslatef(potX, potY, 0.0f);

glRotatef(-angle, 0.0f, 1.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glCallList(globe);
glPopMatrix();
}

void drawStars(float size, int slices){
glPushMatrix();
glDisable(GL_LIGHTING);

glColor3f(1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, texture[1].texID);
glCallList(stars);

glEnable(GL_LIGHTING);
glPopMatrix();
}

void drawText(void){
glPushMatrix();
glColor3f(1, 1, 1);
glRasterPos2f(8, .5);
freetype::print(our_font, "Invasion");
glPopMatrix();
}

void draw(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glTranslatef(0.0f, 0.0f, -20.0f);

lighting();

drawGlobe(4.0f, 100);

drawStars(200.0f, 10);

drawText();

glFlush();
}


Any functions not included in the code will be provided if needed. Thanks in advance, Marcus Axelsson Edit: Even though this thread is now "Solved", I have to add that I've been having much trouble understanding this very simple concept. Since I began about a year ago with C++ and OpenGL, some theory just won't ... take? Thanks for helping me with these questions. [Edited by - tre on December 7, 2009 1:07:11 PM]

##### Share on other sites
You rotate the scene outside of everything, of course everything will be rotated.

push means saving the current transformation, which includes your rotation, translation too. pop means loading the saved transformation.
glLoadIdentity();apply_camera_transform()glPushMatrix();transform_obj_1();draw_objects_1();glPopMatrix();glPushMatrix();transform_obj_2();draw_objects_2();glPopMatrix();glLoadIdentity();draw_text();

##### Share on other sites
Quote:
 Original post by szecsYou rotate the scene outside of everything, of course everything will be rotated.push means saving the current transformation, which includes your rotation, translation too. pop means loading the saved transformation.***

Allright. So, if I translate 20 to the left on X and glPushMatrix();, the transformation is saved but is still "active". And if I then glPopMatrix(); the saved transformation is loaded again. So within these two lines I could translate, rotate and scale objects and do whatever, when I load the saved transformation the only thing left is the "translate 20 to the left" action.
If I want a "clean slate" when I push a matrix, I call a glLoadIdentity(), this removes the "translate 20 to the left" action. So if I want to keep the text on X -20 I have to do another translate.

Have I got it right? Because this code now works, thanks to you :)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();								glTranslatef(0.0f, 0.0f, -20.0f);RotateScene(EYE_RADIUS, RotateCamY, RotateCamX, 180.0);lighting();glPushMatrix();	drawGlobe(4.0f, 100);glPopMatrix();glPushMatrix();	drawStars(200.0f, 10);glPopMatrix();glPushMatrix();	glLoadIdentity();		glTranslatef(0.0f, 0.0f, -20.0f);		drawText();glPopMatrix();glFlush();

##### Share on other sites
You don't have to do this:

glPushMatrix();
glTranslatef(0.0f, 0.0f, -20.0f);
drawText();
glPopMatrix();

Since you will glLoadIdentity(); at the beginning. So it's meaningless to save/restore the matrix.

Play around with the push/pop/transformations, and you will get used to them quickly.

##### Share on other sites
Quote:
 Original post by szecsYou don't have to do this:glPushMatrix(); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -20.0f); drawText();glPopMatrix();Since you will glLoadIdentity(); at the beginning. So it's meaningless to save/restore the matrix.Play around with the push/pop/transformations, and you will get used to them quickly.

True. Man, I can't understand why it's so difficult for me to grasp this concept. Insane.
Thanks for helping!

##### Share on other sites
Try to understand the meaning of the matrix itself. As long as you consider it some magical black box that only computer hardware can understand, you are missing a lot of actually very trivial facts that make life a lot easier.

Global and local coordinate system (or world and object coordinates) are the first step. Take a bunch of pens or your fingers (or even better, some rulers) and make it form a coordinate system (obviously three axes and an origin).

No pretend your room is a coordinate system and where your pens are is also the "worlds" origin. Whenever you specify a vertex, you give object coordinates (ie. relative to your pens).

The modelview is doing nothing but changing those pens and because sometimes even math can be great, it also happens to _describe_ those pens in a very primitive way. By consisting of four vectors that happen to be the x-pen, the y-pen, the z-pen and the origin of your pen-system, all expressed in world coordinates.

The identity is boring. But it doesn't "clean" anything and it is no "empty" matrix. It is the "1" of multiplication. It's the "if you multiply with me, nothing will happen" matrix. Also, it is (1,0,0) (0,1,0) (0,0,1) (0,0,0). Surprise, the axes are the worlds axes and the origin is at 0 (ignore the w component for now). You can call glTranslate(10,0,0), you can create a translation matrix (1,0,0)(0,1,0)(0,0,1)(10,0,0) and multiply with it. If you didn't do anything else before (ie. still identity), you could even _set_ the modelview to this matrix.

Okay, rotation involves trigonometry, but it's easy to see that rotating 90° around y will turn x into (0,0,-1) and z will become (0,0,1).

Saying "vertex(10,0,2)" boils down to: (10*x-pen) + (0*y-pen) + (2*z-pen) + origin (and yes, in a rather simplified way, that is what happens when you multiply a vector with a transformation matrix).

I think books about 3D graphics should give a simplified view of the model(view) matrix. Just like nobody cares about the mathematical background of quaternions and just cares about the "it's an axis and the angle around it" part. For starters, that's enough to get a simple working image of it.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634072
• Total Posts
3015341
×