Qt--OpenGL加載OBJ 3D模型

原始出處:https://github.com/kadie16/3D-Model-Viewer

效果圖:

在這裏插入圖片描述原始代碼是基於MAC的在windows上面沒法用:
需要稍微修改一下:
.pro文件添加

#-------------------------------------------------
#
# Project created by QtCreator 2020-06-09T11:42:35
#
#-------------------------------------------------

QT       += core gui opengl

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = OpenglOBJ
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp \
    camera.cpp \
    objload.cpp \
    face.cpp \
    vertex.cpp \
    glwidget.cpp

HEADERS += \
        mainwindow.h \
    camera.h \
    objload.h \
    face.h \
    vertex.h \
    glwidget.h

FORMS += \
        mainwindow.ui

LIBS += -lOpengl32

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

還有就是glwidget.h文件需要修改:

#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QWidget>
#include <QGLWidget>
#include <QT>
#include <QTimer>
#include <QVector2D>
#include <QMouseEvent>
#include <QEvent>
#include <QDebug>
#include "objload.h"
#include "vertex.h"
#include "face.h"
#include "camera.h"
#include <QQuaternion>
#include <QObject>
#include <QMatrix4x4>
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QGLFramebufferObjectFormat>

class GLWidget : public QGLWidget
{
    Q_OBJECT
public:
    explicit GLWidget(QWidget *parent = nullptr);
    bool toggleRotation();
    bool toggleCulling();
    bool toggleTranslation();
    double increaseScale();
    double decreaseScale();
    void setScale();
    void initializeGL();
    void paintGL();
    void drawObject();
    void drawAxes();
    void resetView();
    void grabObj(objLoad objFile);
    void grabColor(double r, double g, double b);
    void resizeGL(int w, int h);
    void mouseMoveEvent(QMouseEvent *e);
    QQuaternion drag2Rotate(float dx, float dy);
    void drag2Translate(float dx, float dy);
    void drag2Zoom(float dy);

private:
    QTimer timer;
    /* .obj Information */
    objLoad *objPtr = 0;
    std::vector<Vertex> vertices;
    std::vector<face> faces;
    std::vector<float> center;
    std::vector<float> maxCoords;
    std::vector<float> minCoords;
    QQuaternion currQ;
    /* Frustrum Things */
    float radius;
    float fdist;
    double dNear;
    double dFar;
    double viewAngle;
    float w0;
    float h0;
    /* User Control */
    camera cam;
    /* Rotation */
    bool mouseHeld;
    bool rotationOK;
    /* Culling */
    bool cullingOK;
    bool translateOK;
    bool scaleOK;
    /* Zoom */
    bool zoomOK;
    float zoomF;
    double scale;
    /* Color Pick */
    double red,green,blue;
    bool needsReset;
    QVector3D axisOfRotation;
    int x,y,dx,dy,x0,y0;
    int prevPos[2];
    float mag;
signals:
    void Mouse_Pressed();
    void Mouse_Pos();
    void Mouse_Released();
protected:
    void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;

};

#endif // GLWIDGET_H

其他不變,編譯沒有問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章