Need help clarifying something about matrix stack manipulation

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

Recommended Posts

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?

Share on other sites
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.

Share on other sites
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!

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 47
• Forum Statistics

• Total Topics
632636
• Total Posts
3007575
• Who's Online (See full list)

There are no registered users currently online

×