PDA

View Full Version : How to move object relative to origin, not origin and object!?



RJH
06-14-2001, 08:21 PM
Somebody please correct me if I'm wrong or confirm that it's impossible to move an object's position relative to the origin. The glTranslate function moves the coordinate system origin and the object with it! Example, if I define a GLQUADS in the X-Y plane V1=(4,4,0), V2=(0,4,0), V3=(0,0,0), V4=(4,0,0) I thought I could translate the GLQUAD so that the origin's X and Y axes pass through the center of the GLQUAD. WRONG!! It moved but the origin moved also. NOT GOOD! There is no way I can rotate the GLQUAD about an axis perpendicular (i.e., in the Z-Y plane) to it and running thru point (2,2,0). glRotate only works about vectors from the origin. Very disappointing <sigh>...

ffish
06-14-2001, 10:19 PM
You can move an object relative to any position you like. You have to think of your 3D geometry in either a global coordinate scheme or a local coordinate scheme (the red book discusses both). I assure you that it's possible to move an object relative to the origin. If you want to rotate an object relative to some other vector, you just have to get your transformations right. Here's a simple test you can do:



GLfloat rotate1(0.0f);
GLfloat rotate2(0.0f);

void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
// Translate the "camera" away from the scene.
glTranslatef(0.0f, 0.0f, -10.0f);
// Set color to blue.
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
// Draw a sphere around the origin.
glutSolidSphere(0.8f, 20, 20);
// Rotate the quad below.
glRotatef(rotate1, 0.0f, 1.0f, 0.0f);
// Rotate around the point (4, 0, 0).
glTranslatef(4.0f, 0.0f, 0.0f);
// Translate quad back to where it started.
glTranslatef(2.0f, 2.0f, 0.0f);
// Rotate quad around its central point.
glRotatef(-rotate2, 0.0f, 1.0f, 0.0f);
// Translate quad around a local origin.
glTranslatef(-2.0f, -2.0f, 0.0f);
// Draw the quad.
glBegin(GL_QUADS);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glVertex3f( 4.0f, 4.0f, 0.0f);
glVertex3f( 0.0f, 4.0f, 0.0f);
glVertex3f( 0.0f, 0.0f, 0.0f);
glVertex3f( 4.0f, 0.0f, 0.0f);
glEnd();

rotate1 += 0.1f;
rotate2 += 0.3f;
glutSwapBuffers();
}

Run that code and see what it does. I get rotation about the quad's centre and rotation about an arbitrary axis. If you don't use GLUT just swap the glutSolidSphere for another quad or something.

Hope that helps.

[This message has been edited by ffish (edited 06-15-2001).]

RJH
06-17-2001, 11:44 AM
Thanks for your reply ffish. I finally figured out what glTRanslate and glRotate really mean.