# OpenGL Rotatate a triangle, N00by Question

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

## Recommended Posts

I'm using OpenGL for 2 weeks now, so this may be a very n00by question. This is my drawing code: glColor4f(col.Red(),col.Green(),col.Blue(),col.Alpha()); glBindTexture(GL_TEXTURE_2D, sprite); glRotatef(0.5f, 0.0f, 0.0f, 0.0f); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex2f(x, y); glTexCoord2f(1.0, 0.0); glVertex2f(x+width, y); glTexCoord2f(1.0, 1.0); glVertex2f(x+width, y+height); glTexCoord2f(0.0, 1.0); glVertex2f(x, y+height); glEnd (); glBindTexture(GL_TEXTURE_2D, 0); The function glRotatef, does rotate something, but I want it to rotate the Quad only, not everything. I have more drawing code after this, and that is rotated as well! I only want this quad (rectangle) in this code to be rotated. Please help me out! Thanx. BTW: I'm just drawing 2d. 3d is not involved.

##### Share on other sites
Before you call glRotatef, you could call glPushMatrix in order to save the current matrix. Once your current rendering segment is complete, call glPopMatrix to restore the previous matrix, which will be the one before the rotation was applied.

##### Share on other sites
Also, you're not specifying an axis to rotate about. Whether you only ue 2D or not is irrelevant, because 2D is only a special case of 3D but it is not treated as a special case by OpenGL. You still need to treat everything as 3D. The axis of rotation should, in your case, probably be the Z-axis.
glRotatef(0.5, 0, 0, 1);

##### Share on other sites
Should i put PushMatrix before my other drawing codes, and put PopMatrix before the code of a quad to be rotated and PushMatrix after it again? Like this:

PushMatrix();

glColor4f(col.Red(),col.Green(),col.Blue(),col.Alpha());
glBindTexture(GL_TEXTURE_2D, sprite);
glRotatef(0.5f, 0.0f, 0.0f, 0.0f);
glTexCoord2f(0.0, 0.0); glVertex2f(x, y);
glTexCoord2f(1.0, 0.0); glVertex2f(x+width, y);
glTexCoord2f(1.0, 1.0); glVertex2f(x+width, y+height);
glTexCoord2f(0.0, 1.0); glVertex2f(x, y+height);
glEnd ();
glBindTexture(GL_TEXTURE_2D, 0);

PopMatrix();

##### Share on other sites
Thank you all! I tried to put PushMatrix before all my drawing codes and PopMatrix after, and it worked!

Thank you all for the help!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015215
×