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);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    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???>);
    glGenerateMipmap(GL_TEXTURE_2D);
    
    


  • You can check some tutorials I have here https://bitbucket.org/joaodeusmorgado/opengltutorials/src/83630ec69fce6c237f7a23367aa6859a2184a73f/T07_ImageTexture/?at=master
    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

    Hi,

    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.