使用QOpenGLWidget高兴地加载OpenGL函数指针

时间:2017-07-07 10:16:33

标签: 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>

我搜索了谷歌先生,我已经仔细查看了Qt文档但没有发现任何内容。我想使用GLAD只是因为我的渲染代码没有太严格地绑定到Qt,以防我想稍后切换。

编辑:我的目标是在Qt中使用非实例化的OpenGL函数(尽管如果我没记错的话,文档会另外推荐)。因为那时我能够无缝地切换到GLFW以提供窗口等。

1 个答案:

答案 0 :(得分:2)

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

  

答案:所以事实证明我只是混淆了一些事情,这就是我如何让它发挥作用,万一有人遇到同样的问题:

     
      
  1. 在您的项目中添加glad.c
  2.   
  3. 将必要的标头添加到您的包含目录
  4.   
  5. QOpenGLWidget子类的.cpp文件应包含以下组件:
  6.         
    // 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
    }
    
相关问题