LINUX下QT+OpenGL编程初步我用的是ubuntu10.10;安装了qtcreator全套4.7.0;然后随便建一个工程,再pro文件中
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 很明了。我的环境和楼主基本一样。命令行:
sudo apt-get install glut3
提示:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package glut3
我换了源,还是这样的提示。这个问题怎么解决呢?多谢指点。
2 楼 yuanyu5237 2011-11-14 gistop 写道很明了。我的环境和楼主基本一样。
命令行:
sudo apt-get install glut3
提示:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package glut3
我换了源,还是这样的提示。这个问题怎么解决呢?多谢指点。
非常不好意思,我刚刚在虚拟机里试了下,也找不到,我猜应该是我当时写错了,时间长了不记得了,应该是
sudo apt-get install glutg3
sudo apt-get install glutg3-dev
希望对你有帮助,另外,像这种问题,如果命令不好解决,去新德里软件包里面找
3 楼 gistop 2011-11-14 多谢博主如此神速的指导,我去试试。 4 楼 gistop 2011-11-15 多谢博主的指点,安装好了。
两个小问题:
1 QT += openg 应该为 QT += opengl
2 gluPerspective好像opengl最新的版本不支持了(老版本OpenGL 2.1支持),编译时会提示‘gluPerspective’ was not declared in this scope这样的错误。
5 楼 gistop 2011-11-15 可以用glFrustum
