## Trouble with glRotatef()

Member
Posts: 61
Joined: 2007.01
Post: #1
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.

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)
Sage
Posts: 1,066
Joined: 2004.07
Post: #2
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.
Member
Posts: 61
Joined: 2007.01
Post: #3
Never mind!

I fixed it.

NOTE TO SELF: Translate, THEN Rotate, dumba\$\$
Member
Posts: 61
Joined: 2007.01
Post: #4
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.
Member
Posts: 100
Joined: 2006.05
Post: #5
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...