LINUX下QT+OpenGL编程初步
我用的是ubuntu10.10;安装了qtcreator全套4.7.0;然后随便建一个工程,再pro文件中加入:
?
#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; }}?这个主要是记录一下,没什么可用价值
1 楼 gistop 2011-11-14 很明了。我的环境和楼主基本一样。