LINUX下QT+OpenGLプログラミング初歩
私はubuntu 10を使っています.10;qtcreatorの4.7.0セットがインストールされています.それから勝手に工事を建てて、proファイルに参加します.
クラスのヘッダファイルに次を追加します.
コンパイルが表示されるとglが見つかりません.hファイルのエラーは、ライブラリがいくつか欠けています.インストールが必要です.次のコマンドを実行します.
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3
sudo apt-get install freeglut3-dev
sudo apt-get install glutg3
sudo apt-get install glutg3-dev
sudo apt-get install libglut3
sudo apt-get install libglut3-dev
次に、QTベースのopenglプログラムを最初に書きます.
これは主に記録されていて、何の価値もありません.
QT += opengl
クラスのヘッダファイルに次を追加します.
#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 glutg3
sudo apt-get install glutg3-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;
}
}
これは主に記録されていて、何の価値もありません.