Search code examples
openglopengl-4

Updating OpenGL mipmapped texture


There is something I fail to understand completely about texture update in OpenGL.Let's say I create OpenGL texture with mipmaps like this:

        ///.. tex gen and bind here .... ///

        glTexStorage2D(GL_TEXTURE_2D,numMipMapLevels,GL_RGBA8,imageInfo.Width,imageInfo.Height);
        glTexSubImage2D(GL_TEXTURE_2D,0,0,0,Width,Height,GL_BGRA,GL_UNSIGNED_BYTE,data);
        glGenerateMipmap(GL_TEXTURE_2D);

Now the first question: Does it mean ,when I want to update this texture including all the mipmap level ,do I have to loop over each level and call glTexSubImage2D with appropriate mipmap dimension and image bytes?If the answer is YES , then I have:

second question: How do I retrieve each mipmap dimension and data to pass into the texture?

Alternatively , can I just do the update :

 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,Width,Height,GL_BGRA,GL_UNSIGNED_BYTE,data);

And re-generate mipmaps immediately afterwards?

    glGenerateMipmap(GL_TEXTURE_2D);

Solution

  • Answer to the first question

    Yes

    Answer to the second question

    Each mipmap level has exactly half the width and height of the mipmap level above it. So for mipmap level n the dimensions are w·2-n and h·2-n where w and h are the size of level 0.

    It can be easily programmed by recalling that bit shifting (the >> and << operators in C like languages) are power of 2 operators. So a << n = a·2n and a >> n = a·2-n

    Answer to the third qiestion

    Yes, but the quality of the minification filter used by the particular OpenGL implementation may not meet one's demands.