awdorrin's picture

Brightness Control

When I had my project implemented with the Tao Framework, I used the following code to adjust the 'brightness' of my rendered scene.
Basically blending a gray quad over my final rendering to control the brightness:

private void AdjustBrightness()
  float adj = 0.25f + (brightness * 0.75); // brightness is between 0.0 and 1.0
  GL.BlendFunc( BlendingFactorSrc.Zero, BeldningFactorDest.SrcColor);
  GL.Color3(adj, adj, adj);
  GL.Vertex2(-6.0f, -9.75f);
  GL.Vertex2(-6.0f, 9.75f);
  GL.Vertex2(6.0f, 9.75f);
  GL.Vertex2(6.0f, -9.75f);

After moving to OpenTK I have run into a strange situation where sometimes (currently all the time) the blending occurs and always results in a completely black rendered scene.

I have been running this through the debugger and have verified that the brightness value (and resulting adj is never zero) - it seems that once the scene becomes entirely black, it never comes back. As if the colors are getting cached, but I'm not sure if its OpenTK, or my C# code.

Anyone have any ideas?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kvark's picture

What do you expect from that code?
Your blending equation is "0 * dst_color + src_color*dst_color" = "src_color * dst_color" or simple multiplication (modulation in terms of artists).
Once you have zero, it will always produce zero in the multiplication.

awdorrin's picture

My intention with the code is to take the color of the source (what has been rendered up to that point) and multiply it by a "grey" full-screen quad, where the level of grey is somewhere between 0.25 (dark grey) and 1.0 (white.)

So, if brightness were set to 1.0, the resulting color would be the same as the source, if it were 0.25, everything would be scaled down to 25% of the original brightness.

It seems that what is happening under OpenTK is that the overall brightness goes down until it does reach zero. That the 'source' color is not being reset on each rendering loop, but that the next 'source' is carried over from the blend.

This is different behavior than what I saw under Tao, which makes me think that OpenTK is caching something somewhere.

Snorkel's picture

Maybe you have the wrong GL.BlendEquation(...).
Did you disable textures before rendering the quad ?

the Fiddler's picture

Your code looks correct. I just copy-pasted the snippet into QuickStart.sln and it worked fine, which means the issue lies in something other than the blend equation itself. From your description, one possibility is that you are not resetting the vertex color back to white before rendering again:

  GL.Color3(1f, 1f, 1f); // Add this

The vertex color is indeed "cached" (retained) by the OpenGL driver and will affect all subsequent operations.

Note that OpenTK itself doesn't have any say in the process. It passes your parameters down to OpenGL directly, without editing, caching or performing any other dark magic. :)

awdorrin's picture

Ah, Snorkel figured it out, thank you!

I was not disabling textures before rendering the quad, when I added the code to do that, everything started working again.

What I'm guessing, is that I must have I accidentally deleted a GL.Disable(EnableCaps.Texture2d); line somewhere when I was migrating the code and trying to clean things up.

One more problem down, probably a few dozen to go - hah.

Thanks everyone, maybe i'll figure this OpenGL stuff out before I get driven insane