6. Qt 和 openGL 顯示三維圖形


博客轉載自: https://blog.csdn.net/damoninhit/article/details/41078157

此項目轉載是為了學習 QT 和 openGL 配合顯示三維圖形和數據,熟悉openGL坐標系之間的關聯關系。

正常配置visual studio, 創建一個QT GUI程序即可,選擇依賴時候勾選,QtWidgets, QtOpenGL, QtGui和QtCore即可,Linker里面會自動添加對OpenGL庫 opengl32.lib 和 glu32.lib 的鏈接。

源代碼如下

main.cpp

#include "QtGuiCode/widget.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    Widget w;
    w.resize(300, 300);
    w.show();

    return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QGLWidget>

class Widget : public QGLWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
protected:
    void initializeGL();
    void resizeGL(int width, int height);
    void paintGL();
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
private:
    void draw();
    int faceAtPosition(const QPoint &pos);
    
    GLfloat rotationX;
    GLfloat rotationY;
    GLfloat rotationZ;
    
    QColor faceColors[4];
    QPoint lastPos;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include <QMouseEvent>
#include <QColorDialog>
#include <gl/GLU.h>

Widget::Widget(QWidget *parent): QGLWidget(parent)
{
    setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));
    
    rotationX = -21.0;
    rotationY = -57.0;
    rotationZ = -0.0;
    
    faceColors[0] = Qt::red;
    faceColors[1] = Qt::green;
    faceColors[2] = Qt::blue;
    faceColors[3] = Qt::yellow;
}

Widget::~Widget()
{
}

void Widget::initializeGL()
{
    qglClearColor(Qt::black);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
}

void Widget::resizeGL(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat x = GLfloat(width) / height;
    glFrustum(-x, +x, -1.0, +1.0, 4.0, 15.0);
    glMatrixMode(GL_MODELVIEW);
}

void Widget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    draw();
}

void Widget::draw()
{
    static const GLfloat P1[3] = { 0.0, -1.0, +2.0 };
    static const GLfloat P2[3] = { +1.73205081, -1.0, -1.0 };
    static const GLfloat P3[3] = { -1.73205081, -1.0, -1.0 };
    static const GLfloat P4[3] = { 0.0, 2.0, 0.0 };
    static const GLfloat *const coords[4][3] = {{ P1, P2, P3 }, { P1, P3, P4 }, { P1, P4, P2 }, { P2, P4, P3 }};

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -10.0);
    glRotatef(rotationX, 1.0, 0.0, 0.0);
    glRotatef(rotationY, 0.0, 1.0, 0.0);
    glRotatef(rotationZ, 0.0, 0.0, 1.0);
    for (int i = 0; i < 4; ++i)
    {
        glLoadName(i);
        glBegin(GL_TRIANGLES);
        qglColor(faceColors[i]);
        for (int j = 0; j <3; ++j)
        {
            glVertex3f(coords[i][j][0], coords[i][j][1], coords[i][j][2]);
        }
        glEnd();
    }
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    lastPos = event->pos();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
    GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
    
    if (event->buttons() & Qt::LeftButton)
    {
        rotationX -= 180 * dy;
        rotationY -= 180 * dx;
        updateGL();
    }
    else if (event->buttons() & Qt::RightButton)
    {
        rotationX -= 180 * dy;
        rotationZ -= 180 * dx;
        updateGL();
    }
    lastPos = event->pos();
}

void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
    int face = faceAtPosition(event->pos());
    if (face != -1)
    {
        QColor color = QColorDialog::getColor(faceColors[face], this);
        if (color.isValid())
        {
            faceColors[face] = color;
            updateGL();
        }
    }
}

int Widget::faceAtPosition(const QPoint &pos)
{
    const int MaxSize = 512;
    GLuint buffer[MaxSize];
    GLint viewport[4];
    makeCurrent();
    glGetIntegerv(GL_VIEWPORT, viewport);
    glSelectBuffer(MaxSize, buffer);
    glRenderMode(GL_SELECT);

    glInitNames();
    glPushName(0);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();

    gluPickMatrix(GLdouble(pos.x()), GLdouble(viewport[3] - pos.y()), 5.0, 5.0, viewport);
    GLfloat x = GLfloat(width()) / height();
    glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
    draw();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    if (!glRenderMode(GL_RENDER))
        return -1;
    return buffer[3];
}

雙擊四面體某個面之后,顯示圖像如下

源代碼分析

  1. 窗口坐標系

窗口水平向右為X軸正向,窗口豎直向下為Y軸正向,指向窗口內部為Z軸正向,從draw()函數中P1~P4點的坐標可知,另外在面的描述中每個三角形三個角點的順序都是順時針(面向該面),沒記錯的話這樣才能表示面的外表面。如下是四面體四個頂點的坐標

   static const GLfloat P1[3] = {0.0, -1.0, +2.0};
    static const GLfloat P2[3] = {+1.73205081, -1.0, -1.0};
    static const GLfloat P3[3] = {-1.73205081, -1.0, -1.0};
    static const GLfloat P4[3] = {0.0, 2, 0.0};
    static const GLfloat *const coords[4][3] = {{P1, P2, P3}, {P1, P3, P4}, {P1, P4, P2}, {P2, P4, P3}};

  2. 顯示四面體代碼如下

    for(int i = 0; i < 4; ++i)
    {
        glLoadName(i);
        glBegin(GL_TRIANGLES);
        qglColor(faceColors[i]);
        for(int j = 0; j <3; ++j)
        {
            glVertex3f(coords[i][j][0], coords[i][j][1], coords[i][j][2]);
        }
        glEnd();
    }

其中glLoadName()的作用是替換堆棧頂部的那個值,從而為每次操作生成的物體提供一個唯一的編號(Name),通過qglColor(faceColors[i])命令,就將第i個面與第i號顏色對應起來。
glVertex3f的函數原型是:void glVertex3f(GLfloat x,GLfloat y,GLfloat z);這里x,y,z分別對應coords[i][j][0], coords[i][j][1], coords[i][j][2],例如i=0,j=0時就是P1點坐標(coords[0][0][0], coords[0][0][1], coords[0][0][2]),然后j++,取到P2,再j++,取到P3,從而構成一個面,面的顏色采用faceColor[0]。之后i++,j又從0開始遞增,繼續取到P1,P3,P4……以此類推。由此可見,coords中點的排序是有規律的。
  3. 改變面的顏色

這一功能主要是通過int Widget::faceAtPosition(const QPoint &pos)函數實現的。在鼠標雙擊后,將鼠標指針所在位置傳遞給該函數,在聲明了一些變量后,函數執行了如下語句:

glGetIntegerv(GL_VIEWPORT, viewport);

glGetIntegerv是一個用來獲取參數的函數。宏定義GL_VIEWPORT說明這里獲取的是視口的參數。The params parameter returns four values: the x and y window coordinates of the viewport, followed by its width and height. x、y的原點都是視口左下角,默認值都是(0,0),視口的長寬也就是所在窗體的長寬。返回的參數存入voewport四維GLint向量。
然后是:

<span style="font-size:12px;">glSelectBuffer(MaxSize, buffer);</span>

glSelectBuffer的作用是為選擇模式值建立一個緩沖區,MaxSize為緩沖區大小,buffer返回選擇數據。

glRenderMode(GL_SELECT);
glInitNames();//初始化名稱堆棧
glPushName(0);//將名稱0推入棧頂
glMatrixMode(GL_PROJECTION);//將之后的矩陣操作應用到投影矩陣堆棧
glPushMatrix();
glLoadIdentity();

然后是:

gluPickMatrix(GLdouble(pos.x()), GLdouble(viewport[3] - pos.y()), 5.0, 5.0, viewport);

gluPickMatrix定義了一個選擇區域。函數原型為:void gluPickMatrix(GLdouble x,GLdouble y,GLdouble height,GLdouble width,GLint viewport[4]);
其中x表示選擇區域在窗口中的x坐標,在這里就是pos.x(),而需要注意的是選擇區域y坐標為viewport[3] - pos.y(),height和width表示選擇區域長寬,這里取5像素,實際驗證表明,即使鼠標指針不在三角面上,但5X5像素的選擇區域與面有交集,則也能選中該面。

GLfloat x = GLfloat(width()) / height();//求窗口橫縱比
glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);

glFrustum將當前矩陣乘一個透視矩陣

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM