本示例使用qt4.7.3编制,qt creator 2.2版本。
先看pro文件,和exampl文件夹里的不一样,这个简单,没有依赖关系。
#-------------------------------------------------
#
# Project created by QtCreator 2019-06-06T22:22:03
#
#-------------------------------------------------
QT += core gui
QT += opengl
TARGET = mynewgl
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp \
testgl.cpp
HEADERS += widget.h \
testgl.h
之后是main文件
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.setWindowTitle(QObject::tr("Tetrahedron"));
w.resize(500, 500);
w.show();
return a.exec();
}
首先生成widgets示例,之后show一下。
再看widget的头文件
#ifndef WIDGET_H
#define WIDGET_H
//#include <QtGui/QWidget>
#include <QWidget>
#include <QHBoxLayout>
#include "testgl.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private:
testgl *mygl;
QHBoxLayout *layout;
};
#endif // WIDGET_H
调用了testgl头文件,定义了构造函数,定了了两个私有指针。
再看widget实现文件
#include "widget.h"
// Constructor
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
// add mggl on layout, and add layout on widget
mygl = new testgl;
layout = new QHBoxLayout;
layout->addWidget(mygl);
setLayout(layout);
}
Widget::~Widget()
{
}
很简单,在构造函数里实现了一下testgl类,之后把它安装到layout上面。其实还可以更简单,直接显示testgl就得了。
在看testgl类的头文件
#ifndef TESTGL_H
#define TESTGL_H
#include <QGLWidget>
class testgl : public QGLWidget
{
Q_OBJECT
public:
explicit testgl(QWidget *parent = 0);
protected:
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
signals:
public slots:
private:
void draw();
GLfloat rotationX;
GLfloat rotationY;
GLfloat rotationZ;
QColor faceColors[4];
};
#endif // TESTGL_H
这规定了testgl类的成员函数和私有变量
再看testgl的实现文件
#include <QtGui>
#include <QtOpenGL>
#include "testgl.h"
//构造函数
testgl::testgl(QWidget *parent) :
QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer),parent)
{
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;
}
void testgl::initializeGL()
{
qglClearColor(Qt::gray);
// qglClearColor(Qt::black);
glShadeModel(GL_FLAT);
// glEnable(GL_DEPTH_TEST);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glColor4f(1.0f,1.0f,1.0f,1.0f);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
// glEnable(GL_CULL_FACE);
}
void testgl::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 testgl::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
}
void testgl::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();
}
}
在三维空间里绘制了一个正四面体,并以不同颜色显示。