Public Group

Rotation of an element in OpenGL

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

Recommended Posts

Hello. Im making a simple program in OpenGL for practice that renders a simple cube. I made the cube using GL_QUADS. The problem is it rotates around origin on the y axis, rather than the center of itself. I also had this problem when turning a character around using WASD back last year.
void CGfxOpenGL::Render()
{
//clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//clear matrix

glPushMatrix();
// position the cube
glTranslatef(0.0f, 0.0f, -5.0f);
// perform the rotations
glRotatef(35.0f, 1.0, 0.0, 0.0);
glRotatef(rotationAngle, 0.0, 1.0, 0.0);
// draw the cube
DrawCube();
glPopMatrix();
}


The rotation angle gets updated in Prepare()
void CGfxOpenGL::Prepare(float dt)
{
rotationAngle += 50.0f * dt;
if (rotationAngle >= 360.0f)
{
rotationAngle = 0.0f;
}
}



Share on other sites
You need to rotate the cube before translating. If you translate first it is no longer centered around the origin and will be incorrect for what you're looking for.

In general you'd want to do linear transformations in this order: Scale, Rotate, Translate. There are exceptions (like the orbit of a planet or something similar), but for many applications the SRT (Scale, Rotate, Translate) rule applies.

Share on other sites
Quote:
 Original post by dashurcYou need to rotate the cube before translating. If you translate first it is no longer centered around the origin and will be incorrect for what you're looking for.In general you'd want to do linear transformations in this order: Scale, Rotate, Translate. There are exceptions (like the orbit of a planet or something similar), but for many applications the SRT (Scale, Rotate, Translate) rule applies.
It looks to me like the OP is in fact rotating, then translating, so I'm guessing the problem is elsewhere.

Share on other sites
Quote:
 Original post by jykIt looks to me like the OP is in fact rotating, then translating, so I'm guessing the problem is elsewhere.

The OP's code is
// position the cubeglTranslatef(0.0f, 0.0f, -5.0f);// perform the rotationsglRotatef(35.0f, 1.0, 0.0, 0.0);glRotatef(rotationAngle, 0.0, 1.0, 0.0);
So he does indeed translate before rotating.

To the OP: In OpenGL (and DirectX) transformations are always done around the origin of the world, not the origin of the object the transformations are applied to.

If you want to rotate around the origo (or any other point) of the mesh you must translate that point to the origo of the world before rotating. If you object is centered around the origo in model space (this is usually the case) the object is also centered around the world origo until you do any transformations.

What all this boils down to is that you must rotate your object before you translate it, like dashurc wrote. Otherwise your object will not just rotate but also move when you rotate.

Share on other sites
I tried rotating before translating, adjusted the camera position with gluLookAt(). Now instead of spinning around where one of its corners is the center(just the way i drew the cube), it does wide laps around it!

void CGfxOpenGL::Render(){	//clear screen and depth buffer	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	//clear matrix	glLoadIdentity();	gluLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);	glPushMatrix();		// perform the rotations		glRotatef(35.0f, 1.0, 0.0, 0.0);		glRotatef(rotationAngle, 0.0, 1.0, 0.0);		// position the cube		glTranslatef(0.0f, 0.0f, -4.0f);		// draw the cube		DrawCube();	glPopMatrix();}

Thanks for your help guys. FYI this is a more in depth version im doing of OnYourOwn from chapter 4 of Beginning Game Programming OpenGL

Share on other sites
I was on NeHe's website, and realized that the tutorials have the rotation I want. Heres there code:

int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing{	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer	glLoadIdentity();									// Reset The Current Modelview Matrix	glTranslatef(-1.5f,0.0f,-6.0f);						// Move Left 1.5 Units And Into The Screen 6.0	glRotatef(rtri,0.0f,1.0f,0.0f);						// Rotate The Triangle On The Y axis ( NEW )	glBegin(GL_TRIANGLES);								// Start Drawing A Triangle		glColor3f(1.0f,0.0f,0.0f);						// Set Top Point Of Triangle To Red		glVertex3f( 0.0f, 1.0f, 0.0f);					// First Point Of The Triangle		glColor3f(0.0f,1.0f,0.0f);						// Set Left Point Of Triangle To Green		glVertex3f(-1.0f,-1.0f, 0.0f);					// Second Point Of The Triangle		glColor3f(0.0f,0.0f,1.0f);						// Set Right Point Of Triangle To Blue		glVertex3f( 1.0f,-1.0f, 0.0f);					// Third Point Of The Triangle	glEnd();											// Done Drawing The Triangle	glLoadIdentity();									// Reset The Current Modelview Matrix	glTranslatef(1.5f,0.0f,-6.0f);						// Move Right 1.5 Units And Into The Screen 6.0	glRotatef(rquad,1.0f,0.0f,0.0f);					// Rotate The Quad On The X axis ( NEW )	glColor3f(0.5f,0.5f,1.0f);							// Set The Color To Blue One Time Only	glBegin(GL_QUADS);									// Draw A Quad		glVertex3f(-1.0f, 1.0f, 0.0f);					// Top Left		glVertex3f( 1.0f, 1.0f, 0.0f);					// Top Right		glVertex3f( 1.0f,-1.0f, 0.0f);					// Bottom Right		glVertex3f(-1.0f,-1.0f, 0.0f);					// Bottom Left	glEnd();											// Done Drawing The Quad	rtri+=0.2f;											// Increase The Rotation Variable For The Triangle ( NEW )	rquad-=0.15f;										// Decrease The Rotation Variable For The Quad ( NEW )	return TRUE;										// Keep Going}

I tried removing the x axis rotation i do before the y axis to see if it someway effected it, but no luck.

Share on other sites
Quote:
 Original post by PromethiumThe OP's code is*** Source Snippet Removed ***So he does indeed translate before rotating.
Actually, the rotation is applied first, followed by the translation, as I stated previously. (Take another look at the OpenGL specification/documentation if you're not sure why this is the case.)

1. 1
Rutin
64
2. 2
3. 3
4. 4
5. 5

• 16
• 10
• 29
• 20
• 9
• Forum Statistics

• Total Topics
633414
• Total Posts
3011764
• Who's Online (See full list)

There are no registered users currently online

×