PDA

View Full Version : Rotation

Mukund
05-18-2010, 01: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, 01:59 PM
Simply do this changes to DrawCube (marked <--)

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();

}

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, 02:25 PM

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, 02:33 PM

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, 12:45 PM