Approximate Average Brightness Of Rendered Image

Started by
9 comments, last by Hodgman 7 years, 9 months ago

So in GLSL shader I have an FBO RGB image. What I need to find out is how bright it is. I need to do it real-time inside shader. How to do it?

Advertisement

Generate mips. The bottom mip level is the average value.

Generate mips. The bottom mip level is the average value.

That'd be the average RGB, wouldn't it? If the mipmap doesn't do anything fancy, and just averages each channel independently, you could take that value and then apply some multipliers to get the "brightness".

@OP: the three different color-channels contribute different values to what humans consider "brightness", since our eyes deal with the color channels differently.

In my code I use something like:


float brightness = (red * 0.21f) + (green * 0.71f) + (blue * 0.07f);

...which only adds up to 0.99. :ph34r:

But here's some more accurate numbers.

Yeah, you can do the dot product after mipping, or you can do it beforehand (into a single-channel texture) and then mip that.

If this is for HDR, the geometric mean is often more useful / representative. You can compute it by doing the dot first, then a log (into a single channel texture), then mipping down to 1x1, then an exp to undo the log.

Yeah, you can do the dot product after mipping, or you can do it beforehand (into a single-channel texture) and then mip that.

If this is for HDR, the geometric mean is often more useful / representative. You can compute it by doing the dot first, then a log (into a single channel texture), then mipping down to 1x1, then an exp to undo the log.

But don't forget to add a black offset (you don't want to add any -infinity values into your average)

Phuh... That's a complex thing. I don't know how to access mipmaps, I'm not such experienced. I just do some simple experiments.

Phuh... That's a complex thing. I don't know how to access mipmaps, I'm not such experienced. I just do some simple experiments.

Within a shader you can simply use textureLod (with a sufficiently high value for the lod) to access specific mipmap levels.

Thanks for info. What versions of GLSL does it work in? It also seems to be a good thing for blur, right?

Another question:

In # version 120 and older there was a gl_TexCoord[0] param. That's all nice. In next version it got removed. For meshes this is fine as I can just use texture coordinate attribute. However, how do I find out the current texture coordinates in a 2D filter?

You can use gl_FragCoord to get the window coordinates in the fragment shader.

Not in # version 130 and above. Instead I found that # version 150 compatibility works fine.

This topic is closed to new replies.

Advertisement