Qt_OpenGL小探1

68 篇文章 24 订阅
23 篇文章 0 订阅

工程目录QT += opengl  //widgets

包含头文件 #include <QGLWidget>//QGLWidget继承自QWidget

摘抄自帮助文档: 

QGLWidget provides three convenient virtual functions that you can reimplement in your subclass to perform the typical OpenGL tasks:

paintGL() - Renders the OpenGL scene. Gets called whenever the widget needs to be updated.

resizeGL() - Sets up the OpenGL viewport, projection, etc. Gets called whenever the widget has been resized (and also when it is shown for the first time because all newly created widgets get a resize event automatically).

initializeGL() - Sets up the OpenGL rendering context, defines display lists, etc. Gets called once before the first time resizeGL() or paintGL() is called.

paintGL() - 渲染OpenGL屏幕,当控件需要被更新时调用。

resizeGL() - 建立OpenGL视口、投影等。当空间大小改变时被调用(第一次显示时也被调用)。

initializeGL() - 初始化OpenGL渲染上下文,定义输出列表.....在第一次调用resizeGL()之前被调用或paintGL()被调用时被调用。

头文件glwidget.h:

#ifndef GLWIDGET_H
#define GLWIDGET_H
 
#include <QWidget>
#include <QGLWidget>
 
 
class GlWidget : public QGLWidget
{
    Q_OBJECT
 
public:
    GlWidget(QGLWidget *parent = 0);
    ~GlWidget();
 
protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int width,int height);
    void keyPressEvent(QKeyEvent *event);
    bool fullscreen;
private:
 
};
 
#endif // GLWIDGET_H
 源文件glwidget.cpp: 

#include "glwidget.h"
#include <QGLWidget>
#include <QtGui>
 
GlWidget::GlWidget(QGLWidget *parent)
    : QGLWidget(parent)
{
    fullscreen = false;
}
 
GlWidget::~GlWidget()
{
 
}
 
void GlWidget::initializeGL(){
    setGeometry(300,150,640,480);
    glShadeModel(GL_FLAT); //设置阴影为平滑模式
    glClearColor(0.5,1.0,0.2,0); //清除背景颜色
    glClearDepth(1.0); //设置深度缓存
    glEnable(GL_DEPTH_TEST); //允许深度测试
    glDepthFunc(GL_LEQUAL); //设置深度测试类型
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); //进行透视校正
}
 
void GlWidget::paintGL(){
    //glClear()清除为initializeGL()函数中设置的颜色和缓存深度
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity(); //replaces the current matrix with the identity matrix.
}
 
void GlWidget::resizeGL(int width, int height){
    if(0 == height){
        height = 1; //矩形得有高吧
        glViewport(0,0,(GLint)width,(GLint)height); //设置视口
        glMatrixMode(GL_PROJECTION); // 设置矩阵模式为将要设置的
        glLoadIdentity(); //重置原点
    }
}
 
void GlWidget::keyPressEvent(QKeyEvent *event){
    switch(event->key()){
    case Qt::Key_F1:
        fullscreen = !fullscreen;
        if(fullscreen)
            showFullScreen();
        else{
            setGeometry(300,150,640,480);
            showNormal();
        }
        updateGL();
        break;
    case Qt::Key_Escape:
        close();
        break;
    default:
        break;
    }
}
 
 

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值