c++ - 使用 glad with a QOpenGLWidget 加载 OpenGL 函数指针

标签 c++ qt opengl

所以我正在阅读一些 OpenGL,我想使用 QOpenGLWidget 进行绘图,以便稍后创建一些其他有用的 UI 元素。我正在使用 glad用于解析指向 OpenGL 的函数指针,但我不知道如何使用 Qt 的 getProcAddress 函数!

在我的 QOpenGLWidget 子类的 initializeGL() 函数中,我尝试过:

if(gladLoadGLloader((GLADloadproc) currentContext()->getProcAddress) {}

但由于 Qt 的函数重载,所以没有成功。当我使用

if(gladLoadGL()) {}

也不行。我的包括:

#include <glad\glad.h>
#include "OpenGLViewport.h"
#include <QDebug>
#include <QOpenGLContext>

我搜索了 Mr. Google,并仔细查看了 Qt 文档,但一无所获。我想使用 GLAD 只是为了让我的渲染代码不会太紧密地绑定(bind)到 Qt,以防我以后想切换。

编辑:我的目标是将非实例化 OpenGL 函数与 Qt 一起使用(尽管如果我没记错的话,文档中另有建议)。因为那样我就可以无缝地切换到 GLFW 来提供窗口等。

最佳答案

将解决方案从问题移动到答案:

ANSWER: So it turns out I just had some things mixed up, this is how I got it to work, in case anyone has the same problem:

  1. add glad.c in your project
  2. add the necessary headers to your include directory
  3. the .cpp file of your QOpenGLWidget subclass should have following components:

// Subclass.cpp
#include <glad/glad.h>
// important, subclass header file after glad!!
// otherwise glad won't "hook"
#include "Subclass.h"

void Subclass::initializeGL()
{
    if(gladLoadGL()) initialized = true; // initialized is a class member here
    else; // handle errors
}

void Subclass::paintGL()
{
    if(initialized); // render here
}

关于c++ - 使用 glad with a QOpenGLWidget 加载 OpenGL 函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44968303/

相关文章:

c++ - 在纹理中存储 float 数组并使用纹理坐标从着色器访问 float

linux - 在 linux 中使用 QGLFrameBufferObject 渲染的错误颜色

c++ - 不确定为什么我的函数一直返回相同的值

c++ - gdb 用户定义函数 : how to pass complex argument?

c++ - C/C++ 中的常量

c++ - 在没有停靠栏图标的 Mac OS X 中启动 GUI 进程

c++ - 为什么未分配的内存标记为 0xCC?

c++ - Qt C++ MySQL 到 QTableView 多个连接表为空

c++ - Qt QTableWidget QDoubleSpinBoxes/QSpinBoxes 作为通过迭代访问值的元素

c# - 将 OpenGL 场景绘制到 C# 位图;屏幕外被剪辑