Sign in to follow this  
CrimsonMemory

Need help clarifying something about matrix stack manipulation

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 this post


Link to post
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this