1.环境
windows 10平台,Qt5+Qt Creator 4.9.1+ OpenGL(QT 自带的OpenGL库)
2.Qt Creator 4.9.1创建Qt Widgets Application
3.pro工程文件配置
QT += core gui opengl
LIBS+=-lopengl32 -lglu32
4.在main.cpp中,main函数第一行添加如下代码:
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
5.工程中新建一个c++ class, 名称为GLWidget
6.实现GLWidget类
1>glwidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class GLWidget : public QGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit GLWidget(QGLWidget *parent = nullptr);
~GLWidget();
signals:
public slots:
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void mouseDoubleClickEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *e);
private:
GLfloat Point[5][3];
};
#endif // GLWIDGET_H
2>glwidget.cpp
#include "glwidget.h"
const GLfloat PI = 3.1415926536f;
GLWidget::GLWidget(QGLWidget *parent) : QGLWidget (parent)
{
setMinimumSize(320,240);
resize(640,480);
setWindowTitle(tr("Hello OpenGL"));
short angle = 18;
for(short i=0; i<5; i++) {
Point[i][0] = cos(angle * PI/180);
Point[i][1] = sin(angle * PI/180);
Point[i][2] = 0.0;
angle += 72;
}
}
GLWidget::~GLWidget()
{
}
void GLWidget::initializeGL()
{
// 初始化函数
initializeOpenGLFunctions();
glClearColor(0.0,0.0,0.0,1.0);
glShadeModel(GL_SMOOTH);
glClearDepth(1.0);
glEnable(GL_DEPTH);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
}
void GLWidget::paintGL()
{
//清除之前图形并将背景设置为 blue
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef( -1.5, 0.0, -6.0 );
glBegin( GL_QUADS );
glVertex3f( -1.0, 1.0, 0.0 );
glVertex3f( 1.0, 1.0, 0.0 );
glVertex3f( 1.0, -1.0, 0.0 );
glVertex3f( -1.0, -1.0, 0.0 );
glEnd();
glTranslatef( 3.0, 0.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f( 0.0, 1.0, 0.0 );
glVertex3f( -1.0, -1.0, 0.0 );
glVertex3f( 1.0, -1.0, 0.0 );
glEnd();
}
void GLWidget::resizeGL(int width, int height)
{
// 窗口大小变化时 调整大小
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble fovy=45.0;
GLdouble glWidth=static_cast(width);
GLdouble glHeight=static_cast(height);
GLdouble aspect=glWidth/glHeight;
GLdouble zNear=0.1;
GLdouble zFar=100.0;
gluPerspective(fovy,aspect,zNear,zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::mouseDoubleClickEvent( QMouseEvent *event )
{
Q_UNUSED(event);
if(windowState() & Qt::WindowFullScreen)
showNormal();
else
showFullScreen();
}
void GLWidget::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Escape)
close();
}
3>main.cpp
#include "widget.h"
#include
#include "glwidget.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
QApplication a(argc, argv);
// Widget w;
// w.show();
GLWidget w;
w.show();
return a.exec();
}
7.源码下载:https://download.csdn.net/download/happyrabbit456/11656402