I'm working on a little app for a school project , which should be manipulating 3D objects. And i'm stuck with the rotation , as i'm facing what i think is called the gimball lock ? here's a video I uploaded that clarifies my problem : https://youtu.be/MT8nTAkvD6k
Now , from what I have understood , multiple rotations change the orientation of the object , but keep the X , Y , Z axis fixed , is that it ? Without knowing a lot about openGL , i thought that some projections like these would be enough : say Alpha is the angle following the rotation around X; The new Y and Z (let's Call the Y' and Z')axis would have this equation :
Y'=cos(Alpha)Y+sin(Alpha)Z
Z'=-sin(Alpha)Y+cos(Alpha)z
Based on this , i wrote this code
void Teapot::PrintObj() { glLoadName(_NameStack); glPushMatrix(); Translated(); RotatedX(); RotatedY(); RotatedZ(); glColor3ub(_Color.red,_Color.green,_Color.blue); glutSolidTeapot(2); if(_Selected){WiredTeapot();Guizmo();} glPopMatrix(); };
Now this is the RotatedX method :
void Object3D::RotatedX() { rotY.x=0; rotY.y=cos((angles.x/180.0f)*3.141592); rotY.z=sin((angles.x/180.0f)*3.141592); rotZ.x=0; rotZ.y=-sin((angles.x/180.0f)*3.141592); rotZ.z=cos((angles.x/180.0f)*3.141592); glRotated(angles.x,rotX.x,rotX.y,rotX.z); }
all the variables are declared within the class : the rotX , rotY and rotZ are the 3 vector axis , every time the object undergoes a rotation , these axis are modified;
The RotatedY method now :
void Object3D::RotatedY() { rotX.x=cos((angles.y/180.0f)*3.141592); rotX.y=0; rotX.z=-sin((angles.y/180.0f)*3.141592); rotZ.x=sin((angles.y/180.0f)*3.141592); rotZ.y=0; rotZ.z=cos((angles.y/180.0f)*3.141592); glRotated(angles.y,rotY.x,rotY.y,rotY.z); }
Same thing , if this method is called it means that the object is rotated around the Y axis , hence the modification of the X and Z axis position.
and now the RotatedZ :
void Object3D::RotatedZ() { rotX.x=cos((angles.z/180.0f)*3.141592); rotX.y=sin((angles.z/180.0f)*3.141592); rotX.z=0; rotY.x=-sin((angles.z/180.0f)*3.141592); rotY.y=cos((angles.z/180.0f)*3.141592); rotY.z=0; glRotated(angles.z,rotZ.x,rotZ.y,rotZ.z);
I'm really struggling to make this work ; I've looked on the net and saw things like Quaternions (i don't even know what that is).
Thanks for helping me !