Trouble with glRotatef()
Hi,
I've been trying to rotate a 2D object - a simple GL_QUAD (I'm following the example 3-2 from the Redbook, or at least I think I am).
When I do the rotation, the size of the quad gets smaller and if the angle of rotation is greater than 45 degrees it simply disappears - no longer visible on the screen.
vPosition.x and vPosition.y are the coordinates I draw the quad at and size is simply how large I want the quad to be.
Does anyone have an idea as to what I'm doing wrong?
(sorry about the code formatting, I can't clean it up much further)
I've been trying to rotate a 2D object - a simple GL_QUAD (I'm following the example 3-2 from the Redbook, or at least I think I am).
When I do the rotation, the size of the quad gets smaller and if the angle of rotation is greater than 45 degrees it simply disappears - no longer visible on the screen.
vPosition.x and vPosition.y are the coordinates I draw the quad at and size is simply how large I want the quad to be.
Code:
glPushMatrix();
// rotate(angle (degrees), x?, y?, z?)
glRotatef(45, 1.0f, 0.0f, 0.0f);
glTranslatef(vPosition.x, vPosition.y, 0);
glColor3f(1.0f, 0.5f, 0.0f);
glBegin(GL_QUADS);
// top right
glVertex2f(size, 0);
// top left
glVertex2f(0, 0);
// bottom left
glVertex2f(0, size);
// bottom right
glVertex2f(size, size);
glEnd();
glPopMatrix();
Does anyone have an idea as to what I'm doing wrong?
(sorry about the code formatting, I can't clean it up much further)
Try glRotatef(45, 0, 0, 1). For a 2D game, you are likely using an orthographic projection matrix. Then to rotate an object you are rotating it about the Z axis. In your code you are rotating your object about the X axis.
Quote:Try glRotatef(45, 0, 0, 1). For a 2D game, you are likely using an orthographic projection matrix. Then to rotate an object you are rotating it about the Z axis. In your code you are rotating your object about the X axis.
Thanks for your reply Nick. I copped that I should be rotating about the Z and not X eventually.
Sorry I didn't see your post before I posted my last one, I appreciate the reply I hope you didn't think I was rude in saying "Never mind!" - I didn't realise anyone had replied.
If it's confusing why you should put glTranslate before glRotatef, it's because OpenGL 'applies' these commands in reverse order. This may seem weird but it's linear algebra, matrix multiplication takes place from right to left (it is not commutative). So if I am applying a rotation matrix R and a translation matrix T to a vertex V, the multiplication with the modelview matrix M would go like this:
M = T (glTranslate)
M = T*R (glRotate)
V' = M*V = T*R*V = T*(R*V) (glVertex)
Hope that helps...
M = T (glTranslate)
M = T*R (glRotate)
V' = M*V = T*R*V = T*(R*V) (glVertex)
Hope that helps...
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
gluLookAt vs. glRotatef/glTranslatef...etc | hangt5 | 9 | 8,335 |
Mar 15, 2005 12:54 PM Last Post: OneSadCookie |