opengl rotate an object around fixed axes

Hi I'm trying to implement a opengl program with rotation&translation. But I got this problem that, the world axes will rotating along with my object(a cube). It's like, first I rotate cube along Z-axis, it works fine, then I middle-click the mouse and want to rotate the cube along the original Y-axis. At the moment I click, the cube will stop rotating along Z and start to rotate along Y. But it turns out it will rotate along a "new and invisible" Y-axis. I figure out that's because when I rotate the cube along Z with glRotatef(), the other two axis:X,Y also rotate. How can I fix the axes when I'm rotating the cube. I know the glRotatef() will multiply all matrix in screen with a rotation axis, so I tried added a glLoadIdentity() in each rotation but it still not work. can anyone give me the solution?

the code is here for reference:

#include <stdlib.h>
#include <GL/glut.h>
#include <iostream>



GLfloat vertices[8][3] =
{ { -1.0, -1.0, -1.0 }, { 1.0, -1.0, -1.0 },
{ 1.0, 1.0, -1.0 }, { -1.0, 1.0, -1.0 }, { -1.0, -1.0, 1.0 },
{ 1.0, -1.0, 1.0 }, { 1.0, 1.0, 1.0 }, { -1.0, 1.0, 1.0 } };
GLuint listName;

GLfloat theta[3] = { 0.0, 0.0, 0.0 };
GLint axis = 2;
GLfloat delta = 0.02;
GLint stop = 0;
GLfloat distance = 0;

void face(int a, int b, int c, int d)
{
    glBegin(GL_POLYGON);
    //glColor3fv(colors[a]);
    glVertex3fv(vertices[a]);
    //glColor3fv(colors[b]);
    glVertex3fv(vertices[b]);
    //glColor3fv(colors[c]);
    glVertex3fv(vertices[c]);
    //glColor3fv(colors[d]);
    glVertex3fv(vertices[d]);
    glEnd();
}

void cube(void)
{
    glColor3f(1.0f,1.0f,1.0f);
    face(0, 3, 2, 1);
    face(2, 3, 7, 6);
    face(0, 4, 7, 3);
    face(1, 2, 6, 5);
    face(4, 5, 6, 7);
    face(0, 1, 5, 4);
    glutWireCube(2.5f);
    glutPostRedisplay();
}

void drawAxis(void){
    // save previous matrix
    glPushMatrix();
    // clear matrix
    glLoadIdentity();
    // draw our axes
    glRotatef(45.0, 1.0, 0.0, 0.0);
    glRotatef(45.0, 0.0, -1.0, 0.0);
    glBegin(GL_LINES);
    // draw line for x axis
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(10.0, 0.0, 0.0);
    // draw line for y axis
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 10.0, 0.0);
    // draw line for Z axis
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 10.0);
    glEnd();
    // load the previous matrix
    glPopMatrix();
    glutPostRedisplay();
}

void spinCube()
{


    theta[axis] += delta;
    if (theta[axis] > 360.0) theta[axis] -= 360.0;

    glutPostRedisplay();
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glRotatef(45.0, 1.0, 0.0, 0.0);
    glRotatef(45.0, 0.0, -1.0, 0.0);
    drawAxis();


    glPushMatrix();
    glRotatef(theta[0], 1.0, 0.0, 0.0); 
    glRotatef(theta[1], 0.0, 1.0, 0.0);
    glRotatef(theta[2], 0.0, 0.0, 1.0);
    glTranslatef(0.0, 0.0, distance + 2.0);
    glCallList(listName);
    glPopMatrix();

    glutSwapBuffers();
}

void myReshape(int w, int h)
{
    GLfloat aspect = (GLfloat) w / (GLfloat) h;
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho(-10.0, 10.0, -10.0 / aspect, 10.0 / aspect, -50.0, 50.0);
    else
        glOrtho(-10.0*aspect, 10.0*aspect, -10.0, 10.0, -50.0, 50.0);
    glMatrixMode(GL_MODELVIEW);
}

void mouse(int btn, int state, int x, int y)
{
    if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { axis = 0; 
    }
    if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) { axis = 1; 
    }
    if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { axis = 2; 
    }
}

void keyboard(unsigned char key, int x, int y)
{
    if (key == 'q' || key == 'Q') exit(0);
    if (key == ' ') { stop = !stop; }
    if (stop)
        glutIdleFunc(NULL);
    else
        glutIdleFunc(spinCube);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);

    glutInitWindowSize(600, 600);
    glutCreateWindow("cube");
    glutReshapeFunc(myReshape);

    glutDisplayFunc(display);
    glutIdleFunc(spinCube);

    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);


    //creating a display list:
    listName = glGenLists(1);
    glNewList(listName, GL_COMPILE);
    cube();
    glEndList();


    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
    return 0;
}

What you're after might be accumulating arbitrary rotations. This can't be done with euler angles, which gimbal lock. It's pretty common to have euler angle rotations and in most cases the issue is simply to do with the order they're applied in. The first thing I'd suggest is to reverse the order of your x/y/z rotations.

Next, if you want to accumulate rotations you'll really want to get into quaternions. This can be done with matrices but can easily become numerically unstable. Quaternions can be normalized which solves this issue.

If you rotate around X, the first call is of course, glRotatef(a, 1, 0, 0); draw() glRotatef(a, 1, 0, 0); draw() . Then you want to rotate the object and its current rotation around y. Note that the object and current rotation are grouped in this line of thinking. So you glRotatef(b, 0, 1, 0); glRotatef(a, 1, 0, 0); draw(); glRotatef(b, 0, 1, 0); glRotatef(a, 1, 0, 0); draw(); . Each time you rotate, you add the rotation behind the existing list of transforms. If you added in front, it'd transform the object in its local space and not global. What you could do is this (near-pseudo-code with an imaginary matrix implementation):

  • Keep a current object transform matrix M
  • In spinCube, M = rotationMatrix(delta, axis==0?1:0, axis==1?1:0, axis==2?1:0) * M (note it's rotation * M and not M * rotation .
  • Before you draw the cube, glMultMatrixf(M.data)
  • The problem is floating point error will build up over time and the matrix will start to skew/scale your object in weird ways. Instead, you'll want a quaternion implementation (again near-pseudo-code):

    Q = rotationQuaternion(delta, axis==0?1:0, axis==1?1:0, axis==2?1:0) * Q
    Q.normalize()
    ...
    glMultMatrixf(Q.toMatrix().data)
    
    链接地址: http://www.djcxy.com/p/14354.html

    上一篇: 在PyOpenGL中使用四元数旋转多维数据集

    下一篇: opengl围绕固定轴旋转对象