PDA

View Full Version : Problem about rotate and scale objects



peng
06-20-2002, 02:29 AM
When I program opengl,I find that the objects which I went them be scaled or rotated can not scale and rotate in it's center.
When scale and rotate object,object will scale in other pos.
any one can tell me how to solve this problem.
source as those:
//-------------------
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,400,300);
glOrtho(-200,200,-150,150,-150,150);
glTranslatef(X,Y,Z);
gluLookAt(0,100,0,0,0,0,0,0,-1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(PosX,PosY,PosZ);
glRotatef(RotX,-1,0,0);
DrawObject();
glPopMatrix();

Morglum
06-20-2002, 03:08 AM
In the last lines (between the pushmatrix and the popmatrix) : try calling glRotatef before glTranslatef, not after.

Relic
06-20-2002, 03:37 AM
Not quite. If PosXYZ is the objects center you need to put it into the origin, rotate there and move it back.

glTranslatef(PosX,PosY,PosZ);
glRotatef(RotX,-1,0,0);
glTranslatef(-PosX,-PosY,-PosZ);
DrawObject();

Matrices are left-multipled to vertices in OpenGL, which means in source code order the transformation nearest to the vertex is applied first
(In case you do lighting too and use glScale, don't forget to enable GL_NORMALIZE for correct lighting.)


[This message has been edited by Relic (edited 06-20-2002).]