PDA

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



Nader El Masry
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.glLoadIdentity();
Gl.glRotatef(cubeAngley, 0, 1, 0);
Gl.glGetFloatv( GL_MODELVIEW_MATRIX, rotateYMatrix );

Gl.glLoadIdentity();
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.

Nader El Masry
04-08-2010, 06:06 PM
thank u soo much , it really solved my problem but there is one little problem that it solved the problem from one side but the other side still as it is .
i mean by ur code above if u i rotate 90 around y then 90 around there is no problem but by vece versa there is a problem (roatate x then y)

MaxH
04-08-2010, 07:35 PM
This topic comes up every once in a while. I think Bloodtoes is on the right track. I posted a demo that does different types of rotations. It's at -

http://www.mfwweb.com/OpenGL/Special_Rotations

Download the file 'source.c'. Compile, link, and run. See if that helps you.