在 OpenGL 中在其自己的轴上旋转对象 [英] Rotate an object on its own axes in OpenGL

查看:24
本文介绍了在 OpenGL 中在其自己的轴上旋转对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个立方体,我希望围绕它的三个轴(立方体的轴,而不是窗口)中的任何一个旋转.正如许多其他类似问题所述,只要我只向一个方向旋转,我的旋转就会起作用,但是当我开始混合它们时,我会得到奇怪的结果.特别是,无论立方体如何旋转,绕 y 轴的旋转始终绕窗口的 y 轴旋转.

So I have a cube that I wish to rotate around any of its three axes (the axes of the cube, not the window). As many other similar questions have stated, my rotations work as long as I am only rotating in one direction, but when I start mixing them, I get strange results. In particular, the rotation about the y-axis always rotates about the y-axis of the window, regardless of how the cube has been rotated.

我的绘图代码如下:

glMatrixMode(GL_MODELVIEW) ;
glLoadIdentity();
gluLookAt(0.0, 5.0, 15.0,  
          0.0, 0.0, 0.0,  
          0.0, 1.0, 0.0);
glPushMatrix();
glRotatef(theta_y,0,1,0); 
glRotatef(theta_x,1,0,0);
glRotatef(theta_z,0,0,1);
draw_cube();
glPopMatrix();

这个 问题似乎几乎准确地描述了我想要做的事情,接受的答案似乎是我想要做的,但是他提供的链接已经失效.

This question seems to describe pretty much exactly what I am trying to do, and the accepted answer seems to be what I want to do, however the link he provides is dead.

根据我在链接问题中可以收集到的信息,首先执行我的 z 轴旋转,这意味着在我的 x 轴旋转(下一个)中,我需要旋转(1,0,0),而不是旋转(1,0,0),我会围绕 (-sin(theta_z),cos(theta_z),0) 旋转.但是对于第三次轮换,他只给出了链接,说它变得非常复杂".现在链接失效了,我不知道如何进行第三次旋转.

From what I can gather in the linked question, my z-rotation is performed first, which means in my x-rotation (which would be next) I need to, instead of rotating about (1,0,0), I would rotate about (-sin(theta_z),cos(theta_z),0). But then for the third rotation, he only gives the link, saying it gets "very complicated." Now that the link is dead, I'm not sure how to go about this third rotation.

根据回复,我添加了三个向量:cube_xcube_ycube_z 来保存我的立方体的当前轴.它们被初始化如下:

Based on the replies, I have added three vectors: cube_x, cube_y, and cube_z to hold the current axes of my cube.They are initialized as follows:

float cube_x[] = {1.0f,0.0f,0.0f,0.0f};
float cube_y[] = {0.0f,1.0f,0.0f,0.0f};
float cube_z[] = {0.0f,0.0f,1.0f,0.0f};

回到我的绘图函数中,我对其进行了更改,以便围绕这些轴而不是像以前那样围绕全局轴进行旋转.执行三个旋转后,我调用 glGetFloatv 来获取当前矩阵并使用一个新函数 update_vector 用它们的新值填充我的立方体向量.这一切都包括在下面:

Back in my drawing function, I have changed it so that the rotations are done around these axes rather than the global axes as I had previously. After performing the three rotations, I call glGetFloatv to get the current matrix and use a new function, update_vector to fill my cube vectors with their new values. This all is included below:

void update_vector(float vector[],float matrix[]) {
    vector[0] = matrix[0]*vector[0] + matrix[1]*vector[1] + matrix[2]*vector[2] + matrix[3]*vector[3];
    vector[1] = matrix[4]*vector[0] + matrix[5]*vector[1] + matrix[6]*vector[2] + matrix[7]*vector[3];
    vector[2] = matrix[8]*vector[0] + matrix[9]*vector[1] + matrix[10]*vector[2] + matrix[11]*vector[3];
    vector[3] = matrix[12]*vector[0] + matrix[13]*vector[1] + matrix[14]*vector[2] + matrix[15]*vector[3];

}

void my_display(void) {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;

    glMatrixMode(GL_MODELVIEW) ;
    glLoadIdentity();
    gluLookAt(0.0, 5.0, 15.0,  
          0.0, 0.0, 0.0,  
          0.0, 1.0, 0.0); 
    glPushMatrix();
    glRotatef(theta_y,cube_y[0],cube_y[1],cube_y[2]); 
    glRotatef(theta_x,cube_x[0],cube_x[1],cube_x[2]);
    glRotatef(theta_z,cube_z[0],cube_z[1],cube_z[2]);

    //get the current matrix
    float my_matrix[16];
    glGetFloatv(GL_MODELVIEW_MATRIX, my_matrix);

    //Multiply the matrix by each of my vectors
    update_vector(cube_x,my_matrix);
    update_vector(cube_y,my_matrix);
    update_vector(cube_z, my_matrix);

    make_cube();
    glPopMatrix();

    /* buffer is ready */
    glutSwapBuffers();

    return ;
}

现在,当我尝试绕 y 轴旋转时,立方体似乎会沿几个轴摆动一点点,然后最终确定绕 x 轴旋转.绕 Z 旋转做同样的事情.似乎只能绕 x 轴旋转.

Now when I try to rotate about the y axis, the cube seems to wobble around along a few axes for a little bit before finally settling into a rotation about the x-axis. Rotating about Z does the same thing. Only rotating about the x-axis seems to work.

解决方案:

在下面的评论中,Gavin 提到 Y 轴旋转总是首先发生.这是整个问题.通过添加标志 ROT 并根据我尝试围绕哪个轴旋转来设置它,我可以对旋转进行排序,以便最后完成旋转的轴.

In his comment below, Gavin mentioned that the Y-axis rotation was always happening first. This is the whole issue. By adding a flag ROT and setting it based on which axis I am attempting to rotate around, I am able to order the rotations so that the axis being rotated about is done last.

推荐答案

在 LookAt 之后,您必须平移立方体的负位置,进行旋转,然后平移回立方体的位置.

After your LookAt you must translate by the negative position of the cube, do the rotations, then translate back to the cube's position.

这篇关于在 OpenGL 中在其自己的轴上旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆