QT中第一个OpenGL程序

   

学习QT已经有一段时间了,为了可以进一步的学习QT,并学习在QT在使用OpenGL。将定期按照NeheOpenGL教程,将功能在QT中全部实现。本博客发布的代码,在Window XP + Visual 2005 SP1 + QT 4.6.0编译通过。非常感谢Nehe对于本人学习OpenGL的帮助。

需要在QT中创建OpenGL应用程序,通常需要从QGLWidget类继承出自己的类。QGLWidgetQWidget派生,提供了在QT应用程序中显示OpenGL图形的能力。QGLWidget累提供了三个虚函数来完成OpenGL的绘图任务。initializeGL()完成OpenGL环境的初始化,paintGL()绘制OpenGL图形,resizeGL()在窗口发生改变时运行。以下代码利用此方法,在窗口中显示一个简单的山角形和五角星。

main.cpp

 

#include "wglwidget.h" #include <QtGui/QApplication> #include <QtGui/QtGui> int main(int argc, char *argv[]) { QApplication app(argc, argv); QTextCodec::setCodecForTr(QTextCodec::codecForLocale()); // 安装QT自身翻译文件 QTranslator translatorQT; { QStringList environment = QProcess::systemEnvironment(); QString str; bool bFinded = false; foreach(str, environment) { if(str.startsWith("QTDIR=")) { bFinded = true; break; } } if(bFinded) { str = str.mid(6); bFinded = translatorQT.load("qt_" + QLocale::system().name(),str.append("/translations/")); if(bFinded) app.installTranslator(&translatorQT); else qDebug() <<QObject::tr("Can't find the translation file for Chinese!"); } else { qDebug() << QObject::tr("Please set the environment variable QTDIR"); } } WGLWidget w; w.show(); return app.exec(); }

 

wglwidget.h

 

#ifndef WGLWIDGET_H #define WGLWIDGET_H #include <QtGui/QWidget> #include <QtOpenGL/QtOpenGL> #include "ui_wglwidget.h" class WGLWidget : public QGLWidget , public Ui::WGLWidgetClass { Q_OBJECT public: WGLWidget(QWidget *parent = 0, const QGLWidget* shareEidget = 0,Qt::WFlags flags = 0); ~WGLWidget(); protected: virtual void initializeGL(); virtual void resizeGL(int w, int h); virtual void paintGL(); virtual void mouseDoubleClickEvent(QMouseEvent *event); virtual void closeEvent(QCloseEvent *event); private: GLfloat Point[5][3]; ///< 五角星坐标 }; #endif // WGLWIDGET_H

 

wglwidget.cpp

 

#include "wglwidget.h" #include <math.h> const GLfloat PI = 3.1415926536f; WGLWidget::WGLWidget(QWidget *parent /* = 0 */, const QGLWidget* shareEidget /* = 0 */,Qt::WFlags flags /* = 0 */) : QGLWidget(parent, shareEidget, flags) { setupUi(this); setMinimumSize(320,240); resize(640,480); setWindowTitle(tr("第一个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; // Z值 angle += 72; } } WGLWidget::~WGLWidget() { } void WGLWidget::initializeGL() { glShadeModel(GL_SMOOTH); glClearColor(0.0,0.0,0.0,0.0); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3,GL_FLOAT,0,Point); } void WGLWidget::resizeGL(int w, int h) { glViewport(0,0,(GLint)width(),(GLint)height()); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,(GLfloat)width() / (GLfloat)height(),0.1,100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void WGLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(-1.5,0.0,-6.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(); glTranslatef(3.0,0.0,0.0); glBegin(GL_LINE_LOOP); glArrayElement(1); glArrayElement(4); glArrayElement(2); glArrayElement(0); glArrayElement(3); glEnd(); } void WGLWidget::mouseDoubleClickEvent(QMouseEvent *event) { if(windowState() & Qt::WindowFullScreen) showNormal(); else showFullScreen(); } void WGLWidget::closeEvent(QCloseEvent *event) { QMessageBox box; box.setWindowTitle(tr("退出")); box.setIcon(QMessageBox::Warning); box.setText(tr("确定要关闭OpenGL窗口吗?")); box.setStandardButtons(QMessageBox::Yes | QMessageBox::No); if(QMessageBox::No == box.exec()) { event->ignore(); return; } QWidget::closeEvent(event); }

 

程序中,利用QTranslator安装翻译文件,为了可以讲QMessageBox弹出对话框中的YesNo翻译为中文,至于具体做法,可参照本博客其他文章。本程序运行结果如下:

(图片上传功能暂时关闭,稍微补充)

1.1 第一个OpenGL程序

 

 

 

你可能感兴趣的:(UI,System,buffer,qt,图形,translation)