我用的是ubuntu10.10;安装了qtcreator全套4.7.0;然后随便建一个工程,再pro文件中加入:
QT += openg
在类的头文件中加入:
#include <QtOpenGL>?
如果编译出现,找不到gl.h文件的错误,则是缺少了一些库;需要安装,执行下面的命令:
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3
sudo apt-get install freeglut3-dev
sudo apt-get install glut3
sudo apt-get install glut3-dev
sudo apt-get install libglut3
sudo apt-get install libglut3-dev
然后,写第一个基于QT的opengl程序:
?
#ifndef NEHEWIDGET_H
#define NEHEWIDGET_H
#include <QtOpenGL>
class NeHeWidget : public QGLWidget
{
Q_OBJECT
public:
NeHeWidget( QWidget* parent = 0 );
~NeHeWidget();
protected:
void initializeGL();
void paintGL();
void resizeGL( int width, int height );
void keyPressEvent( QKeyEvent *e );
private:
int base;
};
#endif//NEHEWIDGET_H
?
?
#include "nehewidget.h"
NeHeWidget::NeHeWidget( QWidget* parent )
: QGLWidget( parent )
{
setGeometry( 150, 100, 1024, 576 );
}
NeHeWidget::~NeHeWidget()
{
}
void NeHeWidget::initializeGL()
{
glShadeModel( GL_SMOOTH ); // Enables Smooth Shading
glClearColor( 0.0, 0.0, 0.0, 0.0 ); // Black Background
glClearDepth( 1.0 ); // Depth Buffer Setup
glEnable( GL_DEPTH_TEST ); // Enables Depth Testing
glDepthFunc( GL_LEQUAL ); // The Type Of Depth Test To Do
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); // Really Nice Perspective Calculations
}
void NeHeWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glTranslatef( -1.5, 0.0, -6.0 );
glBegin( GL_TRIANGLES );
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.0, 1.0, 0.0 );
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -1.0, -1.0, 0.0 );
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 1.0, -1.0, 0.0 );
glEnd();
glTranslatef( 3.0, 0.0, 0.0 );
glColor3f( 0.5, 0.5, 1.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();
}
void NeHeWidget::resizeGL( int width, int height )
{
if ( height == 0 )
{
height = 1;
}
glViewport( 0, 0, width, height ); // (GLint)
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (float)width/(float)height, 0.1, 100.0 ); // GLfloat
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void NeHeWidget::keyPressEvent( QKeyEvent *e )
{
switch ( e->key() )
{
case Qt::Key_F2: //F2 key, full screen switch
break;
case Qt::Key_Escape: // Esc key, close window
{
close();
break;
}
default:
break;
}
}
?这个主要是记录一下,没什么可用价值