PDA

View Full Version : rotate ..translate...what is?



11-14-2001, 10:38 PM
Ok, I am confused with how objects and camera gets assigned rotations and translations. I searched everywhere and read lots of tutorials but things still dont quite make sence to me.

I know there is just 1 matrix, the current matrix, but how do you tell opengl to spin one thing translate something else and move camera? I put glLoadIdentity() in between my objects but glLoadIdentity() seems to just make things dissapear.

The following code works fine but the sphere does not rotate, it is in some strange orbit, like it was effected by my camera translation.

Stars rotate with camera fine, terran generation works fine, but sphere is not rotating like i want. I tried that push and pop stuff but seemed to have no effect.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();

// Move camera and draw stars
glRotatef(rotate_view, 0.0f, 1.0f, 0.0f);
DrawStars(); // Stars just follow translation
glTranslatef(x_cam, y_cam, z_cam);

// Rotating and drawing a sphere
RotateObject += 0.1f;
glRotatef(RotateObject, 0.0f, 1.0f, 0.0f);
DrawSphere(2.0f, 0.0f, 0.5f, 4.0f, 1);

// Create the terrain
glTranslatef(0.0f, B, A);
CreateTerrain();

Can someone explain what i am doing wrong or a good expanation on how openGL can know what to trans + rotate and how. Keep in mind I am very new to openGL.

Thanx in advance!

Firestar
11-15-2001, 12:52 AM
The glRotate and glTranslate just multiplies the current matrix with the rotation(translation) matrix. So, the reason why your sphere is affected by the translation is, because you call glTranslate which translates everything you draw after this. The rotation is then also affected by the translation. So instead you would rotate the object in object space, you rotate it in world space. What you should do is:

load the identity matrix
translate to the camera position
rotate the camera

1.) push the current matrix
2.) translate the object(to put it onto a custom position in the world)
3.) rotate the object
4.)draw the object
5.) pop the matrix

Repeat steps 1-5 for all objects.

Hope it helps

Firestar

11-15-2001, 05:51 PM
Thanx for the tip but adding pop and push did nothing. My sphere still orbits around my camera. The following is the code updated:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Move camera and draw stars
glRotatef(rotate_view, 0.0f, 1.0f, 0.0f);
DrawStars(); // Stars just follow translation
glTranslatef(x_cam, y_cam, z_cam);

glPushMatrix();
// Rotating and drawing a sphere
RotateObject += 0.1f;
glRotatef(RotateObject, 0.0f, 1.0f, 0.0f);
DrawSphere(2.0f, 0.0f, 0.5f, 4.0f, 1);
glPopMatrix();

glPushMatrix();
// Create the terrain
glTranslatef(0.0f, B, A);
CreateTerrain();
glPopMatrix();

hmm. I also have another question, why do my stars translate when the translate command is AFTER the call to draw the stars. It appears from tutorials you must translate FIRST then draw object. This stuff seems illogical.