C++ 如何围绕对象旋转';是否使用glRotatef设置局部(非全局)轴?

C++ 如何围绕对象旋转';是否使用glRotatef设置局部(非全局)轴?,c++,opengl,freeglut,C++,Opengl,Freeglut,我有一个网格/游戏对象要使用glRotatef旋转,使用FreeGLUT库: glPushMatrix(); glTranslatef(GlobalPos_x, GlobalPos_y, GlobalPos_z); glRotatef(Global_Yaw,0,1,0); glRotatef(Global_Pitch,1,0,0); glRotatef(Global_Roll,0,0,1); RenderMesh(mesh_Plane); glPopM

我有一个网格/游戏对象要使用glRotatef旋转,使用FreeGLUT库:

glPushMatrix();
    glTranslatef(GlobalPos_x, GlobalPos_y, GlobalPos_z);

    glRotatef(Global_Yaw,0,1,0);
    glRotatef(Global_Pitch,1,0,0);
    glRotatef(Global_Roll,0,0,1);

    RenderMesh(mesh_Plane);
glPopMatrix();
开始时,对象与前Z轴对齐; Y轴向上,X轴向左


“控件”:

W:每帧俯仰+5度

S:节距-每帧5度

A:每帧偏航+5度

D:偏航-每帧5度


目标:全局偏航、全局俯仰和全局横滚值必须以某种方式从这些控制中增加(范围分别为-180到+180、-90到+90和-180到+180)。

问题是,如果飞机已经从地面倾斜90度 它原来的位置“偏航”会让它旋转 Y轴(万向节锁),仅在第一个- 人枪手

所以只需执行“if(GetAsyncKeyState(Key_A))Global_Yaw+=5;”这不是我要找的


编辑:正方形2 在tkausl的提醒下,我找到了我以前的一次尝试。我的想法是将飞机当前的全局角度转换成一个矩阵,将该矩阵与玩家关键输入生成的另一个矩阵相乘,然后以某种方式使用该矩阵获得新的全局偏航、全局俯仰和全局横摇值:

// Convert ORIENTATION to MATRIX
Omatrix00 = cos(Global_Pitch)*cos(Global_Yaw);
Omatrix01 = sin(Global_Pitch);
Omatrix02 = cos(Global_Pitch)*sin(Global_Yaw);
Omatrix03 = 0;
Omatrix10 = -cos(Global_Roll)*sin(Global_Pitch)*cos(Global_Yaw)+sin(Global_Roll)*sin(Global_Yaw);
Omatrix11 = cos(Global_Roll)*cos(Global_Pitch);
Omatrix12 = -cos(Global_Roll)*sin(Global_Pitch)*sin(Global_Yaw)-sin(Global_Roll)*cos(Global_Yaw);
Omatrix13 = 0;
Omatrix20 = -sin(Global_Roll)*sin(Global_Pitch)*cos(Global_Yaw)-cos(Global_Roll)*sin(Global_Yaw);
Omatrix21 = sin(Global_Roll)*cos(Global_Pitch);
Omatrix22 = cos(Global_Roll)*cos(Global_Yaw)-sin(Global_Roll)*sin(Global_Pitch)*sin(Global_Yaw);
Omatrix23 = 0;
Omatrix30 = 0;
Omatrix31 = 0;
Omatrix32 = 0;
Omatrix33 = 1;


// Convert Commanded Turn Angles to MATRIX
matrix00 = cos(pitch_speed)*cos(yaw_speed);
matrix02 = cos(pitch_speed)*sin(yaw_speed);
matrix01 = sin(pitch_speed);
matrix03 = 0;
matrix10 = -cos(roll_speed)*sin(pitch_speed)*cos(yaw_speed)+sin(roll_speed)*sin(yaw_speed);
matrix11 = cos(roll_speed)*cos(pitch_speed);
matrix12 = -cos(roll_speed)*sin(pitch_speed)*sin(yaw_speed)-sin(roll_speed)*cos(yaw_speed);
matrix13 = 0;
matrix20 = -sin(roll_speed)*sin(pitch_speed)*cos(yaw_speed)-cos(roll_speed)*sin(yaw_speed);
matrix21 = sin(roll_speed)*cos(pitch_speed);
matrix22 = cos(roll_speed)*cos(yaw_speed)-sin(roll_speed)*sin(pitch_speed)*sin(yaw_speed);
matrix23 = 0;
matrix30 = 0;
matrix31 = 0;
matrix32 = 0;
matrix33 = 1;

// Next step: Somehow using this matrix to get new Global_Yaw, Global_Pitch and Global_Roll values...

我甚至不确定我在代码中是否正确地乘以了矩阵。

必须先绕x轴或z轴旋转,最后绕y轴旋转,才能得到正确的结果。在大多数类型的游戏中,你不需要掷骰子,先绕x轴旋转,然后绕z轴旋转,一切都按预期旋转。

你必须先绕x轴或z轴旋转,最后绕y轴旋转,才能得到正确的结果。在大多数类型的游戏中,你不需要掷骰子,你先绕x旋转,然后再绕z旋转,所有东西都按预期旋转。

这对我没有多大帮助。。。我的问题是,全局横摆、全局俯仰和全局横摇值必须以某种方式从WASD控件中增加。(分别从-180到+180,-90到+90和-180到+180)。不过,我会修改我的问题,以更好地反映我的问题。哦,我想我明白了。在这种情况下,您必须使用四元数和矩阵来防止万向节锁,这对我没有多大帮助。。。我的问题是,全局横摆、全局俯仰和全局横摇值必须以某种方式从WASD控件中增加。(分别从-180到+180,-90到+90和-180到+180)。不过,我会修改我的问题,以更好地反映我的问题。哦,我想我明白了。在这种情况下,必须使用四元数和矩阵来防止万向节锁定