PDA

View Full Version : Setting actors in a world

Structural
11-15-2002, 03:20 AM
I'm new to OpenGL (first came in contact a few days ago) and I've been experimenting quite a bit the past few days.
I'm so far that I can make spinning cubes and set my camera with GLUlookat to view the cube (inc lighting) at any point.

However, I tried to make two cubes spinning independantly just a few minutes ago, and I found out that the function "glRotatef" rotates the entire axis system.
So what I did next was setting the axis-system back in it's original position, and then translated it over the X-axis to draw the next cube to keep it from spinning all over the place. http://www.opengl.org/discussion_boards/ubb/wink.gif

Actually, this works quite properly. But I just read a thread here about optimisation of applications and read that rotating the axis-system consumes some processor-power...
Is there a way to do this more efficiently? For example that only rotates a certain set of polygons, instead of the entire system while drawing?

I'm not asking this to optimise my application (as I'm far from being experienced enough to worry about optimisation) but I want to learn this the proper way instead of creating that much overhead.

bakery2k
11-15-2002, 03:32 AM
You can use glPushMatrix and glPopMatrix.
OpenGL has a matrix stack. PushMatrix saves the current matrix by pushing it onto the stack, and PopMatrix will restore the previous matrix. So, in your case you could do:

glTranslatef(0.0f, 0.0f, -10.0f); //translate into screen
glPushMatrix(); //save current matrix *
glRotate() //rotate cube 1
//draw cube 1
glPopMatrix(); //restore matrix from *
glRotate(); rotate cube 2
//draw cube 2

Similarly, for three cubes, sandwich the first two with push/pop Matrix.

nexusone
11-15-2002, 04:01 AM
Here is my example:

glRotatef(....); // Effects everything forward

glPushMatrix(); //Save matrix
glTranslatef(.......) // Note order, opengl operates last function first
glRotatef(.......) // Rotate around axis
draw_cube1();
glPopMatrix(); //Restore matrix to state before Push, rotate/translate inside here does not effect next operations.

glPushMatrix(); //Save matrix
glTranslatef(.......)
glRotatef(.......)
draw_cube2();
glPopMatrix(); //Restore matrix to state before Push

[This message has been edited by nexusone (edited 11-15-2002).]

Structural
11-15-2002, 04:05 AM
YOU LOT ROCK!!!

Thanks http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by Structural (edited 11-15-2002).]