PDA

View Full Version : how to rotate a cube an efficent rotate ???!!!!!!

03-28-2010, 10:53 AM
i know that many people may see this topic bit strange,but of course the problem is not to rotate a cube actually.

i made an application to rotate a cube and i know that it is very simple one but the problem that when i rotate the cube it rotate the (X,Y,Z) axis with it !!!!
u can see it when i rotate the cube with angle 90 around Y-axis then when i rotate around X-axis it rotate around Z-axis because i rotate the axis with the cube ?!!!
Sample of the Code :
Gl.glPushMatrix();
Gl.glScalef(45, 45, 45);
Gl.glRotatef(cubeAngley, 0, 1, 0);
Gl.glRotatef(cubeAnglex, 1, 0, 0);
DrawCube();
Gl.glPopMatrix();

private void Oglctrl_KeyDown(object sender, KeyEventArgs e)
{
// rotating cube
if (e.KeyCode == Keys.W)
{
cubeAnglex -= 5;
}
else if (e.KeyCode == Keys.S)
{
cubeAnglex += 5;
}
else if (e.KeyCode == Keys.A)
{
cubeAngley -= 5;
}
else if (e.KeyCode == Keys.D)
{
cubeAngley += 5;
}
}

wistful
03-29-2010, 07:23 AM
Hi, I think the problem is because some time after your rotation, the variance cubeAnglex and cubeAngley become nono-zero, subsequently lead the rotation angle become not x-axis nor y-axis but their combination. Wish to help you^_^

Bloodtoes
04-05-2010, 11:21 PM
If I understand correctly, the reason is because when you call the first glRotatef it multiplies the result against the modelview matrix, so the next time you call glRotatef(cubeAnglex) it's multiplying against the rotated matrix. I think what you want to do is create an independent rotate matrix then multiply it in later.

Gl.glPushMatrix();
float rotateYMatrix[16], rotateXMatrix[16], rotateMatrix[16];

Gl.glPushMatrix();
Gl.glRotatef(cubeAngley, 0, 1, 0);
Gl.glGetFloatv( GL_MODELVIEW_MATRIX, rotateYMatrix );

Gl.glRotatef(cubeAnglex, 1, 0, 0);
Gl.glGetFloatv( GL_MODELVIEW_MATRIX, rotateXMatrix );
Gl.glMultMatrixf( rotateYMatrix );
Gl.glGetFloatv( GL_MODELVIEW_MATRIX, rotateMatrix );
Gl.glPopMatrix();

Gl.glScalef(45, 45, 45);
Gl.glMultMatrixf( rotateMatrix );

DrawCube();
Gl.glPopMatrix();

That might be excessive but it should do what you're looking for. Shouldn't it? Hrm.. if that doesn't do it, try reversing the order of the X and Y rotations.