原文出處:https://www.infotutoriales.info/2016/10/opengl-con-qt.html?m=1
效果圖,使用三個滑塊來調節3D圖像的三個軸,來讓3D圖像旋轉,然後在實際中三個滑塊的值可以通過其他數據來加載。
添加類mywidget
- 右鍵單擊項目,添加新的…選擇C ++類
- 給Widget命名,在“ 自定義” 基類中寫入WidgetOpenGL
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QGLFramebufferObjectFormat>
class MyWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
public:
explicit MyWidget(QWidget *parent = 0);
protected:
void initializeGL() Q_DECL_OVERRIDE;
void resizeGL(int w, int h)Q_DECL_OVERRIDE;
void paintGL()Q_DECL_OVERRIDE;
void graficarLineas();
public:
double rotate_y=0;
double rotate_x=0;
double rotate_z=0;
private:
};
#endif // MYWIDGET_H
mywidget.c
#include "mywidget.h"
MyWidget::MyWidget(QWidget *parent):QOpenGLWidget(parent)
{
}
void MyWidget::initializeGL()
{
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
}
void MyWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void MyWidget::paintGL()
{
// 清除屏幕和Z緩衝區
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
// 重置轉換
glLoadIdentity();
// 當用戶更改“ rotate_x”和“ rotate_y”時旋轉
glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 1.0, 0.0 );
glRotatef( rotate_z, 0.0, 0.0, 1.0 );
// 前側:多色側
glBegin(GL_POLYGON); // 顯示一個矩形,就使用GL_POLYGON
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, -0.5, -0.5 ); // P1 是紅色
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( 0.5, 0.5, -0.5 ); // P2 是綠色
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( -0.5, 0.5, -0.5 ); // P3 是藍色
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( -0.5, -0.5, -0.5 ); // P4 是紫色
glEnd();
// 背面:白色面
glBegin(GL_POLYGON);
glColor3f( 1.0, 1.0, 1.0 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glEnd();
// 右側:紫色側
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glEnd();
// 左側:綠色側
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
// 頂部:藍色面
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glEnd();
// 下側:紅色側
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
graficarLineas();
glFlush();
this->makeCurrent();
}
void MyWidget::graficarLineas()
{
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(20,0,0);
glColor3f(1,1,0);
glVertex3f(0,0,0);
glVertex3f(0,20,0);
glColor3f(0,1,1);
glVertex3f(0,0,0);
glVertex3f(0,0,20);
glEnd();
}
修改.pro文件–添加opengl和庫
#-------------------------------------------------
#
# Project created by QtCreator 2016-10-18T20:43:46
#
#-------------------------------------------------
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = opengl
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
mywidget.cpp
HEADERS += mainwindow.h \
mywidget.h
FORMS += mainwindow.ui
LIBS += -lOpengl32
DISTFILES +=
將窗體組件提升(Protmote to)爲上面的那個自定義的類
https://github.com/hollowmaster1496/QtOpenGL-OBJ-LOADER