OpenGL(在Qt中)- 旋转问题

3

我在openGL中编写了一个简单的代码,你需要选择一张图片,然后使用以下按键进行旋转:

  • a、s -> 绕x轴旋转
  • d、f -> 绕y轴旋转
  • b、n -> 绕z轴旋转。

问题是z轴旋转总是正常的。但是,x、y轴的旋转不总是正确的。

测试:按下任意(一个)按钮,然后你可以看到其中一个轴根本没有移动,其他轴在移动。你可以检查所有三个轴。但是过一段时间后,尽管你试图围绕x轴旋转物体,你会发现所有三个轴都在移动。奇怪的是,围绕z轴旋转总是正常的。只有其他两个轴才会让人头痛。

这里是“main.cpp”文件:

#include <QApplication>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QMessageBox>
#include <QPainter>
#include <QTextStream>
#include <QtOpenGL/QGLWidget>
#include <QVector3D>
#include <QWidget>

class MyMessageBox:public QMessageBox
{
public:
    MyMessageBox(std::string message,QWidget *parent=0):QMessageBox(QMessageBox::NoIcon,QString("ErrorMessage"),QString(message.c_str()),QMessageBox::Ok,parent,Qt::Widget)
    {
    }
};

class MyOpenGL:public QGLWidget
{
    double x_Rot;
    double y_Rot;
    double z_Rot;
    QVector<GLuint *> textures;  // it does nothing. It got added just for the sake of the program run
    QVector<QImage> openGL_Images;
public:
    MyOpenGL(QWidget * parent);
    ~MyOpenGL();
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();
    void drawCube(QPoint upper_Left_Point,int length,int width,int height,QVector<GLuint *> textures);
    void drawAxis();
    QVector3D get_In_OpenGL_Coordinates(QPoint qwidget_Point);
    void keyPressEvent(QKeyEvent * event);
    void mousePressEvent(QMouseEvent * event);
};

MyOpenGL::MyOpenGL(QWidget *parent):QGLWidget(QGLFormat(QGL::SampleBuffers),parent)
{
    setAutoFillBackground(false);
}

MyOpenGL::~MyOpenGL()
{

}

void MyOpenGL::initializeGL()
{
    //textures.push_back(new GLuint);
    //textures.push_back(new GLuint);

    QString fileName=QFileDialog::getOpenFileName();
    openGL_Images.push_back(convertToGLFormat(QImage(fileName).scaled(QSize(256,256))));
    openGL_Images.push_back(convertToGLFormat(QImage(fileName).scaled(QSize(256,256))));
    openGL_Images.push_back(convertToGLFormat(QImage(fileName).scaled(QSize(256,256))));
    openGL_Images.push_back(convertToGLFormat(QImage(fileName).scaled(QSize(256,256))));
    openGL_Images.push_back(convertToGLFormat(QImage(fileName).scaled(QSize(256,256))));
    openGL_Images.push_back(convertToGLFormat(QImage(fileName).scaled(QSize(256,256))));

    //glGenTextures(1,textures[0]);
    //glBindTexture(GL_TEXTURE_2D,*textures[0]);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    glEnable(GL_TEXTURE_2D);

    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
}

void MyOpenGL::resizeGL(int w, int h)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(45,0,w,h);
    gluPerspective(45.0f,((double)width())/height(),1.0f,100.0f);
    //gluOrtho2D(-10.0f,10.0f,-10.0f,10.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    paintGL();
}

void MyOpenGL::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glTranslatef(0.0,0.0,-10.0);
    glRotatef(x_Rot,1.0f,0.0f,0.0f);
    glRotatef(y_Rot,0.0f,1.0f,0.0f);
    glRotatef(z_Rot,0.0f,0.0f,1.0f);

    drawAxis();

    //drawCube(QPoint(0,0),1,1,1,textures);
}

void MyOpenGL::drawCube(QPoint upper_Left_Back_Point, int length, int width, int height,QVector<GLuint *> textures1)
{
    //glGenTextures(1,textures[0]);
    //glBindTexture(GL_TEXTURE_2D,*textures[0]);

    QVector3D starting_Point = get_In_OpenGL_Coordinates(upper_Left_Back_Point);

    double x=starting_Point.x();
    double y=starting_Point.y();
    double z=starting_Point.z();

    //glGenTextures(1,textures[0]);
    //glBindTexture(GL_TEXTURE_2D,*textures[0]);

     glTexImage2D(GL_TEXTURE_2D,0,3,openGL_Images[0].width(),openGL_Images[0].height(),0,GL_RGBA,GL_UNSIGNED_BYTE,openGL_Images[0].bits());
     glBegin(GL_QUADS);
//   glColor3f(1.0f,0.0f,0.0f);
     glTexCoord2f(0,1);glVertex3f(x-width,y+height,z-length);
     glTexCoord2f(1,1);glVertex3f(x+width,y+height,z-length);
     glTexCoord2f(1,0);glVertex3f(x+width,y+height,z+length);
     glTexCoord2f(0,0);glVertex3f(x-width,y+height,z+length);
     glEnd();

     //glGenTextures(1,textures[1]);
     //glBindTexture(GL_TEXTURE_2D,*textures[1]);
     glTexImage2D(GL_TEXTURE_2D,0,3,openGL_Images[1].width(),openGL_Images[1].height(),0,GL_RGBA,GL_UNSIGNED_BYTE,openGL_Images[1].bits());
     glBegin(GL_QUADS);
     //glColor3f(0.0f,1.0f,0.0f);
     glTexCoord2f(0,1);glVertex3f(x+width,y+height,z-length);
     glTexCoord2f(1,1);glVertex3f(x+width,y-height,z-length);
     glTexCoord2f(1,0);glVertex3f(x+width,y-height,z+length);
     glTexCoord2f(0,0);glVertex3f(x+width,y+height,z+length);
     glEnd();

     glTexImage2D(GL_TEXTURE_2D,0,3,openGL_Images[2].width(),openGL_Images[2].height(),0,GL_RGBA,GL_UNSIGNED_BYTE,openGL_Images[2].bits());
     glBegin(GL_QUADS);
     //glColor3f(0.0f,0.0f,1.0f);
     glTexCoord2f(0,1);glVertex3f(x+width,y-height,z-length);
     glTexCoord2f(1,1);glVertex3f(x-width,y-height,z-length);
     glTexCoord2f(1,0);glVertex3f(x-width,y-height,z+length);
     glTexCoord2f(0,0);glVertex3f(x+width,y-height,z+length);
     glEnd();


     glTexImage2D(GL_TEXTURE_2D,0,3,openGL_Images[3].width(),openGL_Images[3].height(),0,GL_RGBA,GL_UNSIGNED_BYTE,openGL_Images[3].bits());
     glBegin(GL_QUADS);
     //glColor3f(1.0f,1.0f,0.0f);
     glTexCoord2f(0,1);glVertex3f(x-width,y+height,z+length);
     glTexCoord2f(1,1);glVertex3f(x-width,y+height,z-length);
     glTexCoord2f(1,0);glVertex3f(x-width,y-height,z-length);
     glTexCoord2f(0,0);glVertex3f(x-width,y-height,z+length);
     glEnd();

     glTexImage2D(GL_TEXTURE_2D,0,3,openGL_Images[4].width(),openGL_Images[4].height(),0,GL_RGBA,GL_UNSIGNED_BYTE,openGL_Images[4].bits());
     glBegin(GL_QUADS);
     //glColor3f(1.0f,0.0f,1.0f);
     glTexCoord2f(0,1);glVertex3f(x-width,y+height,z-length);
     glTexCoord2f(1,1);glVertex3f(x+width,y+height,z-length);
     glTexCoord2f(1,0);glVertex3f(x+width,y-height,z-length);
     glTexCoord2f(0,0);glVertex3f(x-width,y-height,z-length);
     glEnd();

     glTexImage2D(GL_TEXTURE_2D,0,3,openGL_Images[5].width(),openGL_Images[5].height(),0,GL_RGBA,GL_UNSIGNED_BYTE,openGL_Images[5].bits());
     glBegin(GL_QUADS);
     //glColor3f(0,1,1);
     glTexCoord2f(0,1);glVertex3f(x-width,y+height,z+length);
     glTexCoord2f(1,1);glVertex3f(x+width,y+height,z+length);
     glTexCoord2f(1,0);glVertex3f(x+width,y-height,z+length);
     glTexCoord2f(0,0);glVertex3f(x-width,y-height,z+length);
     glEnd();
}

void MyOpenGL::drawAxis()
{
    //int length = 1;
    //int height = 1;
    //int width = 1;

    //int x = 0;
    //int y=0;
    //int z = 0;


    glBegin(GL_LINES);
    glColor3f(1,0,0);
    glVertex3f(0,0,0);
    glVertex3f(3,0,0);

    glColor3f(0,1,0);
    glVertex3f(0,0,0);
    glVertex3f(0,3,0);

    glColor3f(0,0,1);
    glVertex3f(0,0,0);
    glVertex3f(0,0,3);
    //glVertex3f(x-width,y+height,z+length);
    //glVertex3f(x+width,y+height,z+length);
    //glVertex3f(x+width,y-height,z+length);
    //glVertex3f(x-width,y-height,z+length);
    glEnd();
}

QVector3D MyOpenGL::get_In_OpenGL_Coordinates(QPoint qwidget_Point)
{
    return QVector3D(0,0,0);
}

void MyOpenGL::keyPressEvent(QKeyEvent * event)
{
    switch(event->key())
    {
        case Qt::Key_A:
            x_Rot-=5;
            break;
        case Qt::Key_S:
            x_Rot+=5;
            break;
        case Qt::Key_D:
            y_Rot+=5;
            break;
        case Qt::Key_F:
            y_Rot-=5;
            break;
        case Qt::Key_B:
            z_Rot+=5;
            break;
        case Qt::Key_N:
            z_Rot-=5;
            break;
        default:
            break;
    }
    updateGL();
}

void MyOpenGL::mousePressEvent(QMouseEvent *event)
{
    double x = event->pos().x();
    double y = event->pos().y();
    double z=-1;

    glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&z);

    double projection[16];
    glGetDoublev(GL_PROJECTION_MATRIX,projection);
    double modelView[16];
    glGetDoublev(GL_MODELVIEW_MATRIX,modelView);

    int viewPort[4];
    glGetIntegerv(GL_VIEWPORT,viewPort);

    double x_Gl;
    double y_Gl;
    double z_Gl;
    gluUnProject(x,y,z,modelView,projection,viewPort,&x_Gl,&y_Gl,&z_Gl);

    QString ss;
    QTextStream ss_Text(&ss);
    ss_Text << x_Gl << " " << y_Gl << " " << z_Gl ;
    MyMessageBox mm(ss.toStdString());
    mm.exec();
}

int main(int argc,char * argv[])
{
    QApplication app(argc,argv);

    MyOpenGL * f = new MyOpenGL(NULL);
    f->show();

    return app.exec();
}

这里是 .pro 文件。

SOURCES += \
    main.cpp

QT += opengl

请有人帮我解决这个问题。


你期望发生什么? - Mikola
@Mikola 我期望在其他两个轴上看到z轴的行为。 - prabhakaran
对不起,那不是很具体。你所说的“z轴行为”是什么意思? - Mikola
@Mikola 上述应用程序代码只绘制了三个轴。当我围绕一个轴旋转轴时,其中一个应该保持不变,另外两个必须围绕它旋转。但是,在上面的代码中,所有三个轴都在旋转。当旋转围绕z轴时,它完美地按照我的预期工作。 - prabhakaran
2
好的,根据你所描述的情况,你想要逐步应用旋转。因为在三维空间中旋转是不可交换的,你不能仅仅独立地对x、y、z轴进行旋转然后将它们相加。如果你需要这种行为,你需要在客户端累积所有的旋转到一个矩阵/四元数中;然后在绘制之前按照该矩阵/四元数进行旋转。我回答中的链接应该会有所帮助。 - Mikola
2个回答

4

首先,对于您的代码有一些观察:

  1. 您在哪里初始化了x_Rot、y_Rot和z_Rot?我没有看到这个。

接下来,还有一些更一般的评论:

从技术上讲,您现在正在围绕x、y和z旋转。如果您想要做其他事情,比如说每次按按钮逐渐旋转,则需要仔细考虑您要做的事情。一种方法是逐步应用OpenGL中的旋转矩阵。另一个可能性是在客户端上存储四元数/矩阵,然后每帧重置视图变换。还有一种可能性是通过计算每次旋转时x/y/z角度的正确偏差来解决一堆三角函数(不推荐)。一旦您理解了实际要做的事情,就可以执行其中任何一项或更多操作。为了达到这个目标,您将不得不阅读一些资料(抱歉,这就是数学的工作方式)。作为起点,以下是一些基本的维基页面,以帮助您入门:

http://en.wikipedia.org/wiki/Rotation_representation_%28mathematics%29

http://en.wikipedia.org/wiki/Rotation_matrix

http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation


1

看起来你的问题是万向锁(Gimbal Lock)

使用四元数是解决这个问题最简单的方法之一。


+1 对万向锁的解决方案。请注意,仅在每个帧中将旋转存储在矩阵中并连续更新可能会解决问题。四元数适用于插值旋转,但在只需要逐步旋转某物时,不一定必要使用它们。 - Macke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接