Qtで最初のOpenglプログラム
7871 ワード
1.目的:QtでOpengl関数を使って三角形を描く;
2.次のように実現される.
エラー解決:
エラー1:
コンパイル時にgluPerspectiveで次のエラーが発生しました:C:UsersAdministratorDesktoptornadomeetopenglopengl_nehe_01\glwidget.cpp:46: error: C 3861:「gluPerspective」:識別子が見つかりません.多くの人がQTでOpenGLプログラムを書くときにgluの先頭の関数を使う必要があるが、どうしても使えないことに気づいた.例えば、gluPerspective関数は、透視投影マトリクスを構築するために使われている.実はQTだけでなく、VCコンパイラも含めて、上記の問題が発生します.エラー:エラー:C 3861:'gluPerspective':identifier not found解決方法:proファイルに追加:LIBS+=glut.lib glut 32.libしかし、この2つのlibファイルをqtのbinディレクトリの下に入れることを前提としています.私のパソコンにインストールされている対応ディレクトリは、C:QtSDKSimulatorQtmingwlibです.そして、対応する関数にヘッダファイルを追加します.
エラー2:
実行中に次のエラーが発生しました:myqtopenglwindow.obj:error LNK 209:解析できない外部シンボル"_declspec(dllimport)public:virtual __thiscall QGLWidget::~QGLWidget(void)"(__imp_??1QGLWidget@@UAE@XZ)で、この記号は関数"public: virtual __thiscall myQtOpenglWindow::~myQtOpenglWindow(void)"(??1myQtOpenglWindow@@UAE@XZ)で参照されるmyqtopenglwindow.obj:error LNK 209:解析できない外部記号"_declspec(dllimport)public:_thiscall QGLWidget::QGLWidget(class QWidget *,class QGLWidget const *,class QFlags)" (__imp_??0QGLWidget@@QAE@PAVQWidget@@PBV0@V?$QFlags@W4WindowType関数"public: __thiscall myQtOpenglWindow::myQtOpenglWindow(class QWidget *,char const *,bool)"(?? 0myQtOpenglWindow@@QAE@PAVQWidget@@PBD_N@Z)で参照されるmyqtopenglwindow.obj:error LNK 2001:解析できない外部シンボル"protected:virtual bool__thiscall QGLWidget::event(class QEvent *)"(?event@QGLWidget@@MAE_NPAVQEvent@@Z)myqtopenglwindow.obj:error LNK 2001:解析できない外部シンボル"protected:virtual void__thiscall QGLWidget::paintEvent(class QPaintEvent *)"(?paintEvent@QGLWidget@@MAEXPAVQPINTEvent@@Z)myqtopenglwindow.obj:error LNK 2001:解析できない外部シンボル"protected:virtual void__thiscall QGLWidget::resizeEvent(class QResizeEvent *)"(?resizeEvent@QGLWidget@@MAEXPAVQResizeEvent@@Z)myqtopenglwindow.obj:error LNK 2001:解析できない外部記号"public:virtual void__thiscall QGLWidget::updateGL(void)"(?updateGL@QGLWidget@@UAEXXZ)myqtopenglwindow.obj:error LNK 2001:解析できない外部記号"public:virtual void__thiscall QGLWidget::updateOverlayGL(void)"(?updateOverlayGL@QGLWidget@@UAEXXZ)myqtopenglwindow.obj:error LNK 2001:解析できない外部シンボル"protected:virtual void__thiscall QGLWidget::initializeOverlayGL(void)"(?initializeOverlayGL@QGLWidget@@MAEXXZ)myqtopenglwindow.obj:error LNK 2001:解析できない外部シンボル"protected:virtual void__thiscall QGLWidget::resizeOverlayGL(int,int)"(?resizeOverlayGL@QGLWidget@@MAEXHH@Z)myqtopenglwindow.obj:error LNK 2001:解析できない外部シンボル"protected:virtual void__thiscall QGLWidget::paintOverlayGL(void)"(?paintOverlayGL@QGLWidget@@MAEXXZ)myqtopenglwindow.obj:error LNK 2001:解析できない外部シンボル"protected:virtual void__thiscall QGLWidget::glInit(void)"(?glInit@QGLWidget@@MAEXXZ)myqtopenglwindow.obj:error LNK 2001:解析できない外部シンボル"protected:virtual void__thiscall QGLWidget::glDraw(void)"(?glDraw@QGLWidget@@MAEXXZ)myqtopenglwindow.obj:error LNK 2001:解析不可能な外部シンボル"public:virtual class QpaintEngine* __thiscall QGLWidget::paintEngine(void)const "(?paintEngine@QGLWidget@@UBEPAVQPaintEngine@@XZ) moc_myqtopenglwindow.obj:error LNK 209:解析できない外部シンボル"_declspec(dllimport)public: virtual void * __thiscall QGLWidget::qt_metacast(char const *)"(__imp_? qt_metacast@QGLWidget@@UAEPAXPBD@Z)で、この記号は関数"public:virtual void*_thiscall myQtOpenglWindow::qt_metacast(char const *)"(?qt_metacast@myQtOpenglWindow@@UAEPAXPBD@Z)で参照されるmoc_myqtopenglwindow.obj:error LNK 209:解析できない外部シンボル"_declspec(dllimport)public: virtual int __thiscall QGLWidget::qt_metacall(enum QMetaObject::Call,int,void * *)"(__imp_? qt_metacall@QGLWidget@@UAEHW4Call@QMetaObject@@HPAPAX@Z)で、この記号は関数"public:virtual int __thiscall myQtOpenglWindow::qt_metacall(enum QMetaObject::Call,int,void * *)"(? qt_metacall@myQtOpenglWindow@@UAEHW4Call@QMetaObject@@HPAPAX@Z)で参照されるmoc_myqtopenglwindow.obj:error LNK 2001:解析できない外部記号"_declspec(dllimport)public:static struct QMetaObject const QGLWidget::staticMetaObject"(__imp_? staticMetaObject@QGLWidget@@2 UQMetaObject@@B)debugtest.exe:fatal error LNK 1120:16個解析不能外部コマンド
解決方法:外部ファイルの追加:QT/QT 5.3.1/5.3/msvc 2010_Opengl/lib/Qt 5 OpenGL.lib、もう一度やってみて、だめです.debugとreleaseフォルダを削除して、プロジェクトを整理して、プロジェクトを再構築して、もう一度実行して、あるいはプロジェクトを整理した後、qmakeを実行して、もう一度実行してみてください.
エラー3:
QtcreatorでQtデザイナーインタフェースクラスを追加する場合、このクラスでオブジェクトを宣言すると、コンパイルが発生する可能性があります:Qtcreatorでコンパイルすると解析できない外部記号が発生し、ファイルが見つかりません:mainwindow.objのようなエラー、解決方法:プロジェクトディレクトリの下のreleaseとdubugフォルダを削除し、プロジェクトを整理し、再構築し、qmakeを実行します.参照先:http://tieba.baidu.com/p/2567360449 注意:qmakeを実行し、再実行すればいいです.
参考記事:
http://blog.csdn.net/yibulianhua/article/details/7597205
http://blog.csdn.net/alicehyxx/article/details/5251962
http://www.cnblogs.com/tornadomeet/archive/2012/08/22/2651574.html
http://www.cnblogs.com/tornadomeet/archive/2012/08/24/2654327.html
2.次のように実現される.
// myQtOpenglWindow.h
#ifndef MYQTOPENGLWINDOW_H
#define MYQTOPENGLWINDOW_H
#include <QtOpenGL/QtOpenGL>
#include <QtOpenGL/QGLWidget>
#include <QWidget>
class myQtOpenglWindow : public QGLWidget
{
Q_OBJECT
public:
explicit myQtOpenglWindow( QWidget* parent = 0, const char* name = 0, bool fs = false );\
~myQtOpenglWindow();
protected:
void initializeGL();
void paintGL();
void resizeGL( int width, int height );
void keyPressEvent(QKeyEvent *);
protected:
bool fullScreen;
signals:
public slots:
};
#endif // MYQTOPENGLWINDOW_H
// myQtOpenglWindow.cpp
#include "myqtopenglwindow.h"
#include <gl/GLU.h>
myQtOpenglWindow::myQtOpenglWindow( QWidget* parent, const char* name, bool fs )
:QGLWidget( parent )
{
// 。
fullScreen = fs;
// , (0,0) , 640*480。
setGeometry( 0, 0, 640, 480 );
// “My's OpenGL Framework”。
//setCaption( "My's OpenGL Framework" );
// fullscreen , 。
if ( fullScreen )
{
showFullScreen();
}
}
myQtOpenglWindow::~myQtOpenglWindow()
{
}
void myQtOpenglWindow::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 );
return;
}
void myQtOpenglWindow::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();
return;
}
void myQtOpenglWindow::resizeGL(int width, int height)
{
// height 0
if( height == 0 )
{
height =1;
}
// (Viewport)
glViewport( 0, 0, (GLint)width, (GLint)height );
// 。
glMatrixMode( GL_PROJECTION );
//
glLoadIdentity();
//
gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
//gluPerspective(45.0,(GLfloat)width() / (GLfloat)height(),0.1,100.0);
//
glMatrixMode( GL_MODELVIEW );
// 。
glLoadIdentity();
return;
}
void myQtOpenglWindow::keyPressEvent(QKeyEvent * e)
{
switch ( e->key() )
{
// F2 , 。 ,
。
case Qt::Key_F2:
fullScreen = !fullScreen;
if ( fullScreen )
{
showFullScreen();
}
else
{
showNormal();
setGeometry( 0, 0, 640, 480 );
}
updateGL();
break;
// Escape , 。
case Qt::Key_Escape:
close();
}
return;
}
:
myQtOpenglWindow *poglwindow = new myQtOpenglWindow;
poglwindow->show();
Opengl ,
エラー解決:
エラー1:
コンパイル時にgluPerspectiveで次のエラーが発生しました:C:UsersAdministratorDesktoptornadomeetopenglopengl_nehe_01\glwidget.cpp:46: error: C 3861:「gluPerspective」:識別子が見つかりません.多くの人がQTでOpenGLプログラムを書くときにgluの先頭の関数を使う必要があるが、どうしても使えないことに気づいた.例えば、gluPerspective関数は、透視投影マトリクスを構築するために使われている.実はQTだけでなく、VCコンパイラも含めて、上記の問題が発生します.エラー:エラー:C 3861:'gluPerspective':identifier not found解決方法:proファイルに追加:LIBS+=glut.lib glut 32.libしかし、この2つのlibファイルをqtのbinディレクトリの下に入れることを前提としています.私のパソコンにインストールされている対応ディレクトリは、C:QtSDKSimulatorQtmingwlibです.そして、対応する関数にヘッダファイルを追加します.
エラー2:
実行中に次のエラーが発生しました:myqtopenglwindow.obj:error LNK 209:解析できない外部シンボル"_declspec(dllimport)public:virtual __thiscall QGLWidget::~QGLWidget(void)"(__imp_??1QGLWidget@@UAE@XZ)で、この記号は関数"public: virtual __thiscall myQtOpenglWindow::~myQtOpenglWindow(void)"(??1myQtOpenglWindow@@UAE@XZ)で参照されるmyqtopenglwindow.obj:error LNK 209:解析できない外部記号"_declspec(dllimport)public:_thiscall QGLWidget::QGLWidget(class QWidget *,class QGLWidget const *,class QFlags
解決方法:外部ファイルの追加:QT/QT 5.3.1/5.3/msvc 2010_Opengl/lib/Qt 5 OpenGL.lib、もう一度やってみて、だめです.debugとreleaseフォルダを削除して、プロジェクトを整理して、プロジェクトを再構築して、もう一度実行して、あるいはプロジェクトを整理した後、qmakeを実行して、もう一度実行してみてください.
エラー3:
QtcreatorでQtデザイナーインタフェースクラスを追加する場合、このクラスでオブジェクトを宣言すると、コンパイルが発生する可能性があります:Qtcreatorでコンパイルすると解析できない外部記号が発生し、ファイルが見つかりません:mainwindow.objのようなエラー、解決方法:プロジェクトディレクトリの下のreleaseとdubugフォルダを削除し、プロジェクトを整理し、再構築し、qmakeを実行します.参照先:http://tieba.baidu.com/p/2567360449 注意:qmakeを実行し、再実行すればいいです.
参考記事:
http://blog.csdn.net/yibulianhua/article/details/7597205
http://blog.csdn.net/alicehyxx/article/details/5251962
http://www.cnblogs.com/tornadomeet/archive/2012/08/22/2651574.html
http://www.cnblogs.com/tornadomeet/archive/2012/08/24/2654327.html