// glwidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QtCore>
#include <QtGui>
#include <QtOpenGL>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
GLWidget(int timerInterval = 0, QWidget *parent = 0);
protected:
virtual void initializeGL() = 0;
virtual void resizeGL(int width, int height) = 0;
virtual void paintGL() = 0;
virtual void keyPressEvent(QKeyEvent *e);
virtual void timeOut();
protected slots:
virtual void timeOutSlot();
private:
QTimer *m_timer;
};
#endif
// glwidget.cpp
#include "glwidget.h"
GLWidget::GLWidget(int timerInterval, QWidget *parent)
: QGLWidget(parent)
{
if (timerInterval == 0) {
m_timer = 0;
} else {
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(timeOutSlot()));
m_timer->start(timerInterval);
}
}
void GLWidget::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Escape:
close();
}
}
void GLWidget::timeOut()
{
}
void GLWidget::timeOutSlot()
{
timeOut();
}
// glwidget4.h
#ifndef GLWIDGET4_H
#define GLWIDGET4_H
#include "../glwidget.h"
class GLWidget4 : public GLWidget
{
// Q_OBJECT //! 注意:此处不能有Q_OBJECT
public:
GLWidget4(int timerInterval = 0, QWidget *parent = 0);
protected:
virtual void initializeGL();
virtual void resizeGL( int width, int height );
virtual void paintGL();
virtual void timeOut();
private:
GLfloat rtri, rquad;
};
#endif // GLWIDGET4_H
// glwidget4.cpp
#include "glwidget4.h"
GLWidget4::GLWidget4(int timerInterval, QWidget *parent)
: GLWidget(timerInterval, parent)
{
rtri = rquad = 0.0f;
}
void GLWidget4::initializeGL()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void GLWidget4::resizeGL(int width, int height)
{
height = height?height:1;
glViewport( 0, 0, (GLint)width, (GLint)height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget4::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存
glLoadIdentity(); // 重置当前的模型观察矩阵
// 三角形采用平滑着色
glTranslatef(-1.5f,0.0f,-6.0f); // 相对当前所在的屏幕位置移动
glRotatef(rtri, 0.0f, 1.0f, 0.0f); // 绕y轴从左向右旋转三角形
glBegin(GL_TRIANGLES); // 开始绘制三角形
glColor3f(1.0f, 0.0f, 0.0f); // 红色
glVertex3f( 0.0f, 1.0f, 0.0f); // 上顶点
glColor3f(0.0f, 1.0f, 0.0f); // 绿色
glVertex3f(-1.0f,-1.0f, 0.0f); // 左下顶点
glColor3f(0.0f, 0.0f, 1.0f); // 蓝色
glVertex3f( 1.0f,-1.0f, 0.0f); // 右下顶点
glEnd(); // 三角形绘制结束
/* 使用顺时针次序画正方形(左上->右上->右下->左下), 采用顺时针绘制的
是对象的后表面.
*/
// 四边形采用平面着色
glLoadIdentity(); // 重置模型观察矩阵
glTranslatef(1.5f,0.0f,-6.0f); // 右移1.5单位,并移入屏幕6.0单位
glRotatef(rquad, 1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS); // 开始绘制四边形
glColor3f(0.5f, 0.5f, 1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f); // 左上顶点
glColor3f(0.1f, 0.2f, 0.3f);
glVertex3f( 1.0f, 1.0f, 0.0f); // 右上顶点
glColor3f(0.2f, 0.3f, 0.4f);
glVertex3f( 1.0f,-1.0f, 0.0f); // 右下顶点
glColor3f(0.4f, 0.1f, 0.3f);
glVertex3f(-1.0f,-1.0f, 0.0f); // 左下顶点
glEnd(); // 四边形绘制结束
}
void GLWidget4::timeOut()
{
rtri += 0.5f;
rquad -= 0.25f;
updateGL();
}
// main.cpp
#include "glwidget4.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GLWidget *w = new GLWidget4(50);
w->show();
return a.exec();
}