0

QT version: 5.12.0

OpenGL version: 4.5 core

Development environment: VS 2017

pro file:

QT += widgets
...

I have already add the glad.c file into my project, and ensure that the glad.h is included at the first place in every file.

But while I compile my project, all the other classes that using OpenGL functions are normal, except the class inherited from QOpenGLWidget.

MappingView.h

#include <glad/glad.h>
...
class MappingView : public QOpenGLWidget {
    Q_OBJECT

public:
    MappingView(QWidget * parent = nullptr);
    ~MappingView();

    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();
    ...
    GLuint m_pointer_texture;
}

MappingView.cpp

MappingView::MappingView(QWidget * parent) : QOpenGLWidget(parent) {
    
}

MappingView::~MappingView() {
    glDeleteTextures(1, &m_pointer_texture);
}

void MappingView::initializeGL() {
    // load glad
    if (!gladLoadGL()) {
        return;
    }

    glGenTextures(1, &m_pointer_texture);
}

Compile error: C3861, cann't find glDeleteTextures.

When I click to the definition of glDeleteTextures in VS, it will jump to QOpenGLFunctions, but not glad.h. It seems that the OpenGL in QT and glad is conflicting. But my project doesn't use QOpenGLFunctions.

When I try to add a macro in MappingView.h,

#define QT_NO_OPENGL

all OpenGL functions in MappingView can be successfully found in glad.h, but the base class QOpenGLWidget cannot be found instead, which means that the MappingView class cannot be inherited from QOpenGLWidget any more.

I would be very appreciated if somebody could help me to resolve the problem, thanks!

Ray
  • 61
  • 4
  • To access the Qt OpenGL binding, you have to use some flavor of [QOpenGLFunctions](https://doc.qt.io/qt-5/qopenglfunctions.html). You can use another GL binding by just not using `QOpenGLFunctions`. (I use my own GL binding with `QOpenGLWidget` for years now.) If `glDeleteTextures()` cannot be found by the compiler, I would check the includes (and, maybe, defines). (As it cannot be found you don't need to be afraid that a wrong one is used.) And, don't trust IntelliSense. It isn't bad but sometimes it's merely guessing and lying. ;-) – Scheff's Cat Apr 26 '21 at 12:47
  • Please provide a [mcve] that shows how you are using `glad` to initialize `OpenGL`. – G.M. Apr 26 '21 at 14:03
  • 1
    Also note, that even if this code compiles, you are calling `glGenTextures` before having a valid OpenGL context. – BDL Apr 26 '21 at 16:34
  • To BDL: My mistake, I put the glGenTextures in the constructor of MappingView when I try to explain my problem. But in my actual project, the glGenTextures is called after the gladLoadGL(). I have already reset its position in my question. But the problem still exists. – Ray Apr 27 '21 at 01:46
  • To Sceff: QOpenGLFunctions is an option. But in comparison with glad, I think it's more complicated. For all the class that need to call the opengl functions, not only the should be included, but also should be inherited from the base class QOpenGLFunctions. By contrast, I just need to include the when using glad. – Ray Apr 27 '21 at 01:56
  • I don't believe that `QOpenGLFunctions` does make things more complicated. I would expect that it would work smoother with the rest of Qt instead. However, I didn't tell you you have to use it. I even told you _I use **my own GL binding** with `QOpenGLWidget` for years now._ I had to care about some specific issues. (Qt doesn't like if you leave GL states changed after your own rendering. This in mind, and with some debugging, I finally got this solved.) – Scheff's Cat Apr 27 '21 at 08:00
  • Additionally... If you intend to address somebody's comment, please, prefix the name with `@`. This will cause a notification in the browser of the recipient. (It's not necessary if the recipient is the OP as OP is notified always but it would be @Ray in this case.) – Scheff's Cat Apr 27 '21 at 08:02

0 Answers0