Qt5.12+OpenGL可编程管线之创建OpenGL窗口

本文介绍如何使用Qt5.12结合OpenGL3.x以上版本进行图形编程,通过创建自定义的QOpenGLWidget来实现窗口的初始化、调整及绘制,详细解析了代码实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击上方蓝字可直接关注!方便下次阅读。如果对你有帮助,可以点个在看,让它可以帮助到更多老铁~

今天正式进入Qt5.12 + OpenGL 3.x以上可编程管线的代码部分。

程序主要是参考learnopengl,更多关于OpenGL的说明也可参考他们的文章。

https://learnopengl-cn.github.io/01%20Getting%20started/03%20Hello%20Window/

 

由于顶点着色器与片段着色器程序需要我们自己去实现,反而创建窗体的程序就比较少了。

 

 

.h部分:

#include <QOpenGLWidget>
#include <QOpenGLExtraFunctions>


class MyGLWidget : public QOpenGLWidget, protected QOpenGLExtraFunctions
{
    Q_OBJECT


public:
    MyGLWidget(QWidget *parent = nullptr);


    ~MyGLWidget();


protected:
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();


    void keyPressEvent( QKeyEvent *e );
};

.cpp部分:

#include <QKeyEvent>


MyGLWidget::MyGLWidget(QWidget *parent)
    : QOpenGLWidget(parent)
{
//    setGeometry( 0, 0, 800, 600 );
    resize(800, 600);
    setWindowTitle( "A goose's OpenGL Framework" );
}


MyGLWidget::~MyGLWidget()
{


}


void MyGLWidget::initializeGL()
{
    //要在调用任何gl函数前调用该函数!!
    this->initializeOpenGLFunctions();
}


//改变窗口大小时调用
void MyGLWidget::resizeGL(int w, int h)
{
    //开始渲染前,必须告诉OpenGL渲染窗口的尺寸大小,即视口(Viewport),
    //这样OpenGL才只能知道怎样根据窗口大小显示数据和坐标
    glViewport(0, 0, w, h);
    qDebug() << "w h" << w << h;
}




//每一帧绘制的函数
void MyGLWidget::paintGL()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); //清空屏幕所用的颜色,参数分别为红、绿、蓝以及透明度
                                     //使用三种颜色的分量可配置出超过1600万种不同的颜色
    glClear(GL_COLOR_BUFFER_BIT);        //清空屏幕的颜色缓冲
}


void MyGLWidget::keyPressEvent(QKeyEvent *e)
{
    //ESC 按下后,关闭窗体
    switch ( e->key() )
    {
    case Qt::Key_Escape:
      close();
        break;
    }
}

程序效果:

小结:涉及到图形学编程,尤其是之前从未接触过的,入门还是有一定难度。我也是从0开始学习的,大家可以一起交流!

     对于程序中 文字解释的排版欢迎大家多提出宝贵的意见,感谢!

最后,如需程序源码可在公众号后台留言

学不可以已

 

This directory contains the Qt3D project for Qt5: * Qt3D QML bindings and * Qt3D C++ APIs Building Qt3D ================== Qt5 is a rapidly changing bleeding edge environment. This branch is our initial support for it and thus is also rapidly changing and bleeding edge. This branch is experimental, and unsupported. This information is provided for advanced use only. No guarantees about API stability or even if this works at all are supplied, use at your own risk. First fetch the Qt5 source tree and Qt3D master branch: cd ~/depot git clone ssh://codereview.qt-project.org:29418/qt/qt5.git cd qt5 ./init-repository --codereview-username \ --module-subset=qtbase,qtsvg,qtdeclarative,qttools,qtxmlpatterns,qtdoc,qlalr,qtrepotools,qtqa,qtlocation,qt3d git submodule foreach "git fetch gerrit && git reset --hard gerrit/master" cd qt3d scp -p -P 29418 codereview.qt-project.org:hooks/commit-msg .git/hooks/ git fetch gerrit git checkout --track -b master gerrit/master If you are reading this file then somehow you probably already got this far anyway. Now build Qt5, which will also build Qt3D as a module: cd ~/build mkdir qt5 cd qt5 ~/depot/qt5/configure -developer-build -opensource -confirm-license -no-webkit -no-phonon -nomake tests \ -nomake examples -declarative -opengl -svg && make -j 4 What's in Qt3D ================== Directory structure: src/threed/ This is the main library of the Qt3D project, containing abstractions for cross-platform GL, shaders, lighting models, and so on. src/plugins/ Scene format loading plugins. src/imports/ QML import plugins. util/ Various utilities that are useful when working with Qt3D. examples/ Some examples of using Qt3D QML bindings and Qt3D C++ API. demos/ Some more complex demos of using Qt3D QML bindings and Qt3D C++ API. tests/auto/qml3d/ Unit tests for the QML bindings. tests/auto/threed/ Unit tests for the C++ API doc/ Documentation. devices/symbian/ Symbian deployment file Documentation ============= The documentation can be generated with "make docs". It will be placed into "doc/html" in the build directory. Packages ======== This section is only for those developing Qt3D. Read on to discover how the building of packages works. This section is also important if you want to change how the structure of the Qt3D pro files work. To build Qt3D, run: qmake && make The .pro files will cause the toolchain to place the libraries, QML files and meshes of Qt3D directly into place, as part of the compile process. The files go into the bin/ directory, and the executables can be run directly from there. If you are doing a developer build, plugins will be installed in such a way that Qt will find them. After building the tree the install step is invoked using the INSTALL_ROOT environment export to cause the installation rules to place all the files into a sandboxed install tree, ready for packaging: INSTALL_ROOT=tmp make install Examples ======== Some examples require assimp library to parse the content. Go to http://assimp.sourceforge.net/ and build and install the assimp library. Then configure Qt3D to include assimp and run qmake && make.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值