Qt下OpenGL框架

  相对于Win32下,或者MFC下的GL框架,Qt下的则显得相对简单了,因为Qt有个现成的GLWidget。实现一个继承自GLWidget的Widget,在main里产生一个Widget就好了。

  废话少说,贴上代码。
  NeHeWidget.h内容:

 

#ifndef NEHEWIDGET_H
#define NEHEWIDGET_H

#include
#include

class NeHeWidget : public QGLWidget
{
    Q_OBJECT
public:
    NeHeWidget( QWidget* parent = 0, bool fs = false );
    //NeHeWidget( QWidget* parent = 0, const char* name = 0, bool fs = false );
    ~NeHeWidget();

protected:
  void initializeGL();
  void paintGL();
  void resizeGL( int width, int height );
  void keyPressEvent( QKeyEvent *e );

protected:
  bool fullscreen;
  GLfloat rTri;
  GLfloat rQuad;
  QTimer* t;

};

#endif // NEHEWIDGET_H

 

NeHeWidget.cpp内容:

#include "nehewidget.h"
#include
#include

NeHeWidget::NeHeWidget( QWidget* parent,bool fs )
    : QGLWidget(parent)
{
    rTri = 0.0;
    rQuad = 0.0;

    fullscreen = fs;
    setGeometry( 0, 30, 640, 480 );
    //setCaption( "NeHe's OpenGL Framework" );
    setWindowTitle("NeHe's OpenGL Framework");
    if ( fullscreen )
        showFullScreen();

    t = new QTimer(this);
    connect(t, SIGNAL(timeout()), this, SLOT(updateGL()));
    t->start(10);

}

NeHeWidget::~NeHeWidget()
{
}

void NeHeWidget::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 );
}

void NeHeWidget::paintGL()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();

    //renderText(10,20,"Hello,My Dear");

    glTranslatef( -1.5,  0.0, -6.0 );
    glRotatef( rTri,  0.0,  1.0,  0.0 );

    glBegin( GL_TRIANGLES );

    //front
    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,  1.0 );
    glColor3f( 0.0, 0.0, 1.0 );
    glVertex3f(  1.0, -1.0,  1.0 );

    //right
    glColor3f( 1.0, 0.0, 0.0 );
    glVertex3f(  0.0,  1.0,  0.0 );
    glColor3f( 0.0, 0.0, 1.0 );
    glVertex3f(  1.0, -1.0,  1.0 );
    glColor3f( 0.0, 1.0, 0.0 );
    glVertex3f(  1.0, -1.0, -1.0 );

    //back
    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, -1.0 );
    glColor3f( 0.0, 0.0, 1.0 );
    glVertex3f( -1.0, -1.0, -1.0 );

    //left
    glColor3f( 1.0, 0.0, 0.0 );
    glVertex3f(  0.0,  1.0,  0.0 );
    glColor3f( 0.0, 0.0, 1.0 );
    glVertex3f( -1.0, -1.0, -1.0 );
    glColor3f( 0.0, 1.0, 0.0 );
    glVertex3f( -1.0, -1.0,  1.0 );

    glEnd();

    glLoadIdentity();

    glTranslatef(  1.5,  0.0,  -7.0 );
    glRotatef( rQuad,  1.0,  0.0,  0.0 );

    glBegin( GL_QUADS );

    //top
    glColor3f( 0.0, 1.0, 0.0 );
    glVertex3f(  1.0,  1.0, -1.0 );
    glVertex3f( -1.0,  1.0, -1.0 );
    glVertex3f( -1.0,  1.0,  1.0 );
    glVertex3f(  1.0,  1.0,  1.0 );

    //bottom
    glColor3f( 1.0, 0.5, 0.0 );
    glVertex3f(  1.0, -1.0,  1.0 );
    glVertex3f( -1.0, -1.0,  1.0 );
    glVertex3f( -1.0, -1.0, -1.0 );
    glVertex3f(  1.0, -1.0, -1.0 );

    //front
    glColor3f( 1.0, 0.0, 0.0 );
    glVertex3f(  1.0,  1.0,  1.0 );
    glVertex3f( -1.0,  1.0,  1.0 );
    glVertex3f( -1.0, -1.0,  1.0 );
    glVertex3f(  1.0, -1.0,  1.0 );

    //back
    glColor3f( 1.0, 1.0, 0.0 );
    glVertex3f(  1.0, -1.0, -1.0 );
    glVertex3f( -1.0, -1.0, -1.0 );
    glVertex3f( -1.0,  1.0, -1.0 );
    glVertex3f(  1.0,  1.0, -1.0 );

    //left
    glColor3f( 0.0, 0.0, 1.0 );
    glVertex3f( -1.0,  1.0,  1.0 );
    glVertex3f( -1.0,  1.0, -1.0 );
    glVertex3f( -1.0, -1.0, -1.0 );
    glVertex3f( -1.0, -1.0,  1.0 );

    //right
    glColor3f( 1.0, 0.0, 1.0 );
    glVertex3f(  1.0,  1.0, -1.0 );
    glVertex3f(  1.0,  1.0,  1.0 );
    glVertex3f(  1.0, -1.0,  1.0 );
    glVertex3f(  1.0, -1.0, -1.0 );

    glEnd();

    rTri += 2;
    rQuad -= 1.5;

}

void NeHeWidget::resizeGL( int width, int height )
{
    if ( height == 0 )
    {
        height = 1;
    }
    glViewport(0,0,(GLint)width,(GLint)height );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

void NeHeWidget::keyPressEvent(QKeyEvent *e)
{
    switch ( e->key() )
    {
    case Qt::Key_F2:
        fullscreen = !fullscreen;
        if ( fullscreen )
        {
            showFullScreen();
        }
        else
        {
            showNormal();
            setGeometry( 0, 0, 640, 480 );
        }
        updateGL();
        break;
      case Qt::Key_Escape:
        close();
    }
}

main.cpp内容:

#include
#include "nehewidget.h"
#include

int main(int argc, char *argv[])
{
/*    QApplication a(argc, argv);
    NeHeWidget w;
    w.show();
    return a.exec();
    */
    bool fs = false;
    QApplication a(argc,argv);
    switch(QMessageBox::information( 0,
                                     "Start FullScreen?",
                                     "Would You Like To Run In Fullscreen Mode?",
                                     QMessageBox::Yes,
                                     QMessageBox::No | QMessageBox::Default ) )
   {
      case QMessageBox::Yes:
            fs = true;
            break;
      case QMessageBox::No:
            fs = false;
            break;
   }
    NeHeWidget w(0,fs );

    w.show();
    return a.exec();

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值