Search code examples
c++opengltexturesshader

How to create mipmaps based on maximum value rather than default merging?


I have a 3D texture. Each texel contains a transparency value on the alpha channel.

I need to generate my mipmaps in such a way that it always takes the values of the texel with he maximum alpha value.

In other words if there are 4 texels 3 with a transparency value of 0 and one with a transparency value of 1 the resulting mipmap texel should be 1.

How can I achieve this?

If I need to write my own shaders, what is the optimal way to do it?

EDIT:

My question, to put it more clearly is:

Do I need to manually create a shader that does this or is there a way to use built in functions of opengl to save me the trouble?


Solution

  • In order to do that, you'll need to render to each layer of each mipmap with a custom shader that computes max of 8 samples from the upper level. This can be done by attaching each layer of the rendered mipmap to a framebuffer (using glFramebufferTexture3D), and, in the shader, sampling from the same texture by using texelFetch (lod parameter specifies the mipmap to sample from).