4

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);
Michael IV
  • 11,016
  • 12
  • 92
  • 223

1 Answers1

3

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.

Community
  • 1
  • 1
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Ok , and the pixel data for each mipmap should be "trimmed" or offset too ? – Michael IV Apr 23 '13 at 10:06
  • @MichaelIV: If by trimming you mean *downsampled* then yes. However if you use mipmapping then the downsampling filter should be something significantly better than sparse box or linear sampling because you get that for free using GL_NEAREST or GL_LINEAR. For best visual quality you should downsample using a Lancosz (aka sinc) filter from the original signal. – datenwolf Apr 23 '13 at 12:27
  • @datenwolf do you have any screenshot comparing visual quality with or withohut sinc filter (using GL_LINEAR_MIPMAP_LINEAR in both cases). I just googled around without finding any screenshot – CoffeDeveloper Oct 25 '15 at 09:45