How to load OpenGLES texture from PNG file?

  • And I am searching for a way how to create OpenGLES texture from PNG file, I need a method like this:

    	struct TextureInfo
    		//! The texture handle
    		GLuint Id;
    		//! The width of the texture
    		unsigned int Width;
    		//! The height of the texture
    		unsigned int Height;
    TextureInfo LoadTextureFromFile(GLenum target, GLint level, const Path & file_name)

    the function can use QT internally (there should not be QT Widgets dependency, only QT Quick), but the loaded texture is used with raw OpenGLES 2.0 calls (without QT). What is the proper way to implement it? Is it possible to use libPNG (compiled with QT) directly?

    The function should do something like this:

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast<GLsizei>(image.width), static_cast<GLsizei>(image.height), 0,
        GL_RGBA, GL_UNSIGNED_BYTE, <???DATA???>);

  • You can check some tutorials I have here
    This should compatible with OpenGLES, and could easily be used without widgets.
    Note that I'm using QImage to load the png, but it should be trivial to use some other image library if you want to.

  • Lifetime Qt Champion


    Are you looking for something like described in the Textures Example ? i.e. QOpenGLTexture

  • @SGaist QOpenGLTexture is a good class but it does not have a 'detach' method, so I should keep its instance alive while I use the texture. Ideally I need only texture ID, width and height.

  • @johngod Thank you! It works! You saved me a lot of time!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.