# OpenGL Rotations, translations, pops and pushes

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

## Recommended Posts

Hi, I'm trying to draw a quad in 3d space. I'm having problems getting it to place and orient itself correctly, and I think it's due to the order I'm performing the operations, or the push/pop stack.
void PlanePhysicsClass::Render()
{

float xRot = 90.0f - RADIAN_TO_DEGREE * acos(Dot(this->mNormal, Vector3(1.0f,0.0f,0.0f)));
float yRot = 90.0f - RADIAN_TO_DEGREE * acos(Dot(this->mNormal, Vector3(0.0f,1.0f,0.0f)));
float zRot = 90.0f - RADIAN_TO_DEGREE * acos(Dot(this->mNormal, Vector3(0.0f,0.0f,1.0f)));

mCorners[0].x = this->mPos.x - this->mExtents.x;
mCorners[0].z = this->mPos.z - this->mExtents.y;
mCorners[0].y = 0.0f;

mCorners[1].x = this->mPos.x - this->mExtents.x;
mCorners[1].z = this->mPos.z + this->mExtents.y;
mCorners[1].y = 0.0f;

mCorners[2].x = this->mPos.x + this->mExtents.x;
mCorners[2].z = this->mPos.z + this->mExtents.y;
mCorners[2].y = 0.0f;

mCorners[3].x = this->mPos.x + this->mExtents.x;
mCorners[3].z = this->mPos.z - this->mExtents.y;
mCorners[3].y = 0.0f;
////
glPushMatrix();
//glRotatef(xRot, 1.0f,0.0f,0.0f);
//glRotatef(yRot, 0.0f,1.0f,0.0f);
//glRotatef(zRot, 0.0f,0.0f,1.0f);
glTranslatef(this->mPos.x, this->mPos.y, this->mPos.z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(mCorners[0].x, mCorners[0].y, mCorners[0].z);	// Point 1 (Back)
glTexCoord2f(1.0f, 1.0f); glVertex3f(mCorners[1].x, mCorners[1].y, mCorners[1].z);	// Point 2 (Back)
glTexCoord2f(0.0f, 1.0f); glVertex3f(mCorners[2].x, mCorners[2].y, mCorners[2].z);	// Point 3 (Back)
glTexCoord2f(0.0f, 0.0f); glVertex3f(mCorners[3].x, mCorners[3].y, mCorners[3].z);	// Point 4 (Back)
glEnd();
glPopMatrix();

}


I'm not sure on the order of translation, rotation, and push/pop. If I have any rotations in there at all it seems to displace the object strangely (I initially thought the collision detection code was wrong, but my current line of reasoning leads me to think I'm rendering it in the wrong place). Should I rotate then translate? Or translate then rotate? Does it even matter (I know it does in matrix maths on paper, but I don't know if GL behaves differently)? What about push/pop? (I did originally post a predecessor to this problem in the maths & physics section, but I think the current problem is more OpenGL related than mathematics. I apologise if this is considered as cross posting)

##### Share on other sites
I have a separate function in itself that builds the quard and stuffs it into the a display list inside the class so then inside your render all I'd do is:
1) bind any textures you may have
2) push matrix
3) do translations
4) do my rotations
5) call the display list ( glCallList( m_nDisplayList );
6) pop matrix

I hope this helps

##### Share on other sites
Your OpenGL functions are correct, but is there an out-side source maybe causing the problem? May we see the entire drawScene() (or whatever you named it ;p ) code? Oh, and you would use glTranslatef() then glRotatef(). When you rotate, you are changing the x,y,z axis. So when you translate along the new axis, you'll end up going where you didn't mean to go visually. So when you translate on the default axis (either 0, 0, 0, or 1, 1, 1) you'll end up on the spot you most likely meant to go. Then when you rotate, it won't alter the axis. Maybe a picture will help:

Translating then rotating...
http://jerome.jouvie.free.fr/images/OpenGl/Lessons/Lesson2-TranslationRotation.png

Rotating then translating...
http://jerome.jouvie.free.fr/images/OpenGl/Lessons/Lesson2-RotationTranslation.png

I hope I've helped some!

##### Share on other sites
int DrawGLScene()										// Here's Where We Do All The Drawing{	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer	glLoadIdentity();			// Reset The View		myCamera->SetPrespective();	aSphere->Render();	aPlane->Render();	aPlane2->Render();		ResolveInput(anInputManager->GetKeyboardState(), anInputManager->GetMouseState());	TextOutput();	anInputManager->ClearKeys();	return TRUE;									}

aSphere->Render()

void SpherePhysicsClass::Render(){	glPushMatrix();		glBindTexture(GL_TEXTURE_2D, NULL);		glTranslatef(this-&gt;mPos.x, this-&gt;mPos.y, this-&gt;mPos.z);		gluSphere(mQuadratic, mRadius, 15, 15);	glPopMatrix();}

##### Share on other sites
Quote:
 Original post by WinegumsShould I rotate then translate? Or translate then rotate? Does it even matter (I know it does in matrix maths on paper, but I don't know if GL behaves differently)? What about push/pop?

• ### What is your GameDev Story?

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

• 11
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015656
×