PDA

View Full Version : Rotation

Mukund
05-18-2010, 02:24 PM
Hello everyone.
Im trying to construct a cube using quads.ie translate and rotate quads.

Now i want to rotate the cube as well.Im not able to figure out how i can achieve that.Can anyone please help?

void Cube()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();

glMatrixMode(GL_MODELVIEW);
glRotatef(X += 10 , 1, 0, 1);

for(int i = 1; i <= 6; i++)
DrawCube(i);

Sleep(1000);

}

void DrawCube(int side)
{

if(side == 1){
glTranslatef(0 , 10, 0);
glRotatef(90 , 1, 0, 0);
}
if(side == 2){
glTranslatef(0 , -10, 0);
glRotatef(90 , 1, 0, 0);
}else if(side == 3){
glTranslatef(-10 , 0, 0);
glRotatef(90 , 0, 1, 0);
}else if(side == 4){
glTranslatef(10 , 0, 0);
glRotatef(90 , 0, 1, 0);
}else if(side == 5){
glTranslatef(0 , 0, 10);
}else if(side == 6){
glTranslatef(0 , 0, -10);
}

glColor3f(1, 1 , 0);
glVertex3i(-10, -10, 0);
glColor3f(1, 1 , 0);
glVertex3i(-10, 10, 0);
glColor3f(1, 0 , 1);
glVertex3i(10, 10, 0);
glColor3f(1, 0 , 0);
glVertex3i(10, -10, 0);
glEnd();
glFlush();

}

Cube() is my idle function.So basically, i want to rotate the cube.( a 20x20x20 cube)

Yomboprime
05-18-2010, 02:59 PM
Simply do this changes to DrawCube (marked <--)

void DrawCube(int side)
{

glPushMatrix(); <-- add this line

if(side == 1){
glTranslatef(0 , 10, 0);
glRotatef(90 , 1, 0, 0);
}
if(side == 2){
glTranslatef(0 , -10, 0);
glRotatef(90 , 1, 0, 0);
}else if(side == 3){
glTranslatef(-10 , 0, 0);
glRotatef(90 , 0, 1, 0);
}else if(side == 4){
glTranslatef(10 , 0, 0);
glRotatef(90 , 0, 1, 0);
}else if(side == 5){
glTranslatef(0 , 0, 10);
}else if(side == 6){
glTranslatef(0 , 0, -10);
}

glColor3f(1, 1 , 0);
glVertex3i(-10, -10, 0);
glColor3f(1, 1 , 0);
glVertex3i(-10, 10, 0);
glColor3f(1, 0 , 1);
glVertex3i(10, 10, 0);
glColor3f(1, 0 , 0);
glVertex3i(10, -10, 0);
glEnd();
glFlush();

glLoadIdentity(); <-- delete this line

glPopMatrix(); <-- add this line

}

What does this do? Your Cube() function defines a rotation, and then calls the DrawCube() to draw the faces right? Well, using the glPush/glPop, you save the current rotation and work with a copy of it in the stack. Between the glPush/glPop you combine the current matrix with another rotation and then draw the face. Then, when the glPop is executed, the previous saved matrix (the one for the entire cube) is restored and you can again call another DrawCube() for the next face.

Ah, another point is to call glFluch when you're done rendering all of your geometry, put it at the end.

Mukund
05-18-2010, 03:25 PM
Thanks for the reply Yomboprime.

Yes i want to rotate the cube(which im drawing using quads).

So once im done creating the object, i need to rotate it.
I changed it as per your suggestions.

But its still not working.Well, im getting two quads,one remains stationary and another one behind it is moving!

Im not able to figure out what the problem is. Can you please tell where im going wrong?

Mukund
05-18-2010, 03:33 PM
Im sorry.i had made a minor error..Its working.

Thanks a tonne! :)

Well in you post, you mentioned that i need to call glFlush()
in the end.

Thanks a lot!

Yomboprime
05-19-2010, 01:45 PM
Read what glFlush does:
http://www.opengl.org/sdk/docs/man/xhtml/glFlush.xml

I think it also swap the buffers for showing the next frame (I'm not sure) So it should be used only when finished drawing the frame, not between drawing calls of the same frame.

Rosario Leonardi
05-19-2010, 02:45 PM
Most driver use a queue of commands to avoid to pass from user space and system space at every gl command.
So when you draw some polygons these are not drawn immediately but the command can sleep in a queue for some time. This is not a big problem in application that refresh the scene very often (like interactive application with some animation). After some time the queue is filled and the openGL commands are executed.
But in program where you draw the scene only when something change this can be a problem cause your command can remain in the queue for a long time.
Usually you must use glFlush only in single buffer applications. If you are using double buffering, the swap buffers command already send a glFlush for you before return.