就在OpenGL其自身的轴线旋转对象 [英] Rotate an object on its own axes in OpenGL

查看:221
本文介绍了就在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.

我的图纸code是如下:

My drawing code is as follows:

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();

<一个href="http://stackoverflow.com/questions/7724840/is-it-possible-to-rotate-an-object-around-its-own-axis-and-not-around-the-base-c">This问题似乎说明pretty的多正是我试图做的,接受的答案似乎正是我想做的事,但他提供的链接已经死了。

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),我会绕着(-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_x cube_y cube_z 来握住我的cube.They当前轴初始化如下:

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};

早在我的绘图功能,我已经改变了它,这样的旋转围绕这些轴,而不是因为我有previously全球轴进行。执行三个旋转后,我称之为 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.

编辑:解决方法:

在下面他的意见,加文提到,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.

推荐答案

你的注视后,必须由多维数据集的消极立场翻译,做旋转,再转换回立方体的位置。

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天全站免登陆