Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a square at the origin. First, I want to rotate it.Then, translate it 5 units away from the origin.

After rotation I want to translate it for a distance of 5. But when I implement the code below. It does not translate it to where I want. What is the correct way to Translate. glTranslatef( 5,5, 5); glRotatef(45, x,y,z);

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
194 views
Welcome To Ask or Share your Answers For Others

1 Answer

If your square center is at the origin, glTranslatef then glRotatef.

To rotate about a custom center, here is the generic formula :

TranslationFromRotCenterToOrigin * RotationMatrix * TranslationFromOriginToRotCenter

which gives something in reverse order in OpenGL ( cause glTranslatef/glRotatef multiplies the current OpenGL matrix by the specified translation/rotation matrix )

glLoadIdentity();
glTranslatef( 5, 5, 5);
glTranslatef( toRotCenterX, toRotCenterY, toRotCenterZ );
glRotatef( 45, x, y, z );
glTranslatef( -toRotCenterX, -toRotCenterY, -toRotCenterZ );

Hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...