0

I have a small application on OpenGL+GLEW. Now, I am trying to rewrite it with QT(instead of GLEW). But I have a problem. IDE writes:

'glActiveTexture' was not declared in this scope glActiveTexture(TextureUnit); ^

Here is that code in .cpp file:

#include <iostream>
#include "texture.h"

Texture::Texture(GLenum TextureTarget, std::string& FileName)
{
    m_textureTarget = TextureTarget;
    m_fileName      = FileName;
}

bool Texture::Load()
{
    // A lot of code for reading the picture.
}

void Texture::Bind(GLenum TextureUnit)
{
    glActiveTexture(TextureUnit);
    glBindTexture(m_textureTarget, m_textureObj);
}

Here is code from .h file.

#ifndef TEXTURE_H
#define TEXTURE_H

#include <string>
#include <QGLWidget>

class Texture
{
public:
    Texture(GLenum TextureTarget, std::string& FileName);

    bool Load();

    void Bind(GLenum TextureUnit);

private:
    std::string m_fileName;
    GLenum m_textureTarget;
    GLuint m_textureObj;
    unsigned int width, height;
    unsigned char * data;
};


#endif  /* TEXTURE_H */

I am starting to think that Qt doesn't present such capabilities. How can I solve this problem? I would be glad to any ideas.

Denis
  • 719
  • 2
  • 8
  • 23

1 Answers1

1

For anything beyond GL 1.1 (and glActiveTexture is beyond that), you have to use OpenGL's extension mechanism. Qt can do that for you all under the hood, have a look at the QAbstractOpenGLFunctions class hierarchy

You can get the context the widget has created via QOpenGLWidget::context and the QAbstractOpenGLFunctions of the context via QOpenGLContext::versionFunctions(). There is also the older QOpenGLFunctions class available via QOpenGLContext::functions() which is limited to GL ES 2.0 (and the smathcing ubset of desktop GL 2.0), but would be enough for glActiveTexture().

derhass
  • 43,833
  • 2
  • 57
  • 78
  • Should I just add `#include` and write something like `QOpenGLFunctions_4_3_Core::glActiveTexture(TextureUnit);`. Or does it work in a unusual way? – Denis May 02 '15 at 23:55
  • Read the documentation I linked. You cannot just use this statically. These pointers have to be queried at runtime, after the GL context was created. – derhass May 03 '15 at 00:00
  • @user3051029: maybe you also find also [this blog post](http://www.kdab.com/opengl-in-qt-5-1-part-1/) helpful (especially the "Functions, functions everywhere!" section). – derhass May 03 '15 at 00:06
  • The old class actually loads glActiveTexture only on GLES 2 capable systems in release build. Debug build somehow does in any case, but release got null pointer – Swift - Friday Pie Jan 29 '21 at 09:53