Need help clarifying something about matrix stack manipulation

Started by
1 comment, last by CrimsonMemory 16 years, 11 months ago
Ok, the best way for me to show what I'm asking is through an example: Let's say that I want to display a wall clock. Therefore, it's just a circle with three lines, each line rotating a different amount from the origin point (which, in this case, will be when it is pointing to the 15). Simple so far, yes? Okay, well, what I'd like to do now is to rotate the ENTIRE clock about the y-axis as the hands are still rotating themselves. Well, I figured that all I would need to do is to rotate the clock, then push the matrix, rotate the second hand, pop matrix, push again, rotate minute hand, pop matrix, push once more, rotate hour hand, and pop matrix in order for it to display correctly. Lemme show you some sample code just to clarify:

glLoadIdentity();

glRotatef(clockTheta, 0.0, 1.0, 0.0);
drawClock();

glPushMatrix();
glRotatef(secondsTheta, 0.0, 0.0, 1.0);
drawHand();
glPopMatrix();

glPushMatrix();
glRotatef(minutesTheta, 0.0, 0.0, 1.0);
drawHand();
glPopMatrix();

glPushMatrix();
glRotatef(hoursTheta, 0.0, 0.0, 1.0);
drawHand();
glPopMatrix;


It seems logical to me that the above should do what I wanted. However, when I run the program, only the clock circle and the seconds hand rotate correctly. The minute and hour hands turn as if the entire clock wasn't rotating at all! So, I messed with my code to see if I could get it to work correctly. I decided to comment out every call for pushMatrix() after the first call and see what happens. And, lo and behold, it worked! But does that make any sense?! How can I be popping 3 times when I push only once? Am I missing something here?
I jumped in a river and what did I see? Black-eyed angels swam with me.- Thom Yorke
Advertisement
Oh, and I apologize, I mixed something up at the end of my post, but it's now edited. This is just in case someone saw it before the edit.
I jumped in a river and what did I see? Black-eyed angels swam with me.- Thom Yorke
OKAY, I actually found the answer. It seems that I had left out that VERY LAST popMatrix() call - that fixed everything. Funny that I typed it correctly in the sample code and not in the actual code. Thanks anyway, guys!
I jumped in a river and what did I see? Black-eyed angels swam with me.- Thom Yorke

This topic is closed to new replies.

Advertisement