Jump to content
  • Advertisement
Sign in to follow this  
Triad_prague

OpenGL Gamma correction in OpenGL

This topic is 2607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just realized that some games provide the user with the capability to set the "brightness" or gamma correction (CMIIW). I want to implement it but I wonder how would I go for it in OpenGL? Do you know if such a way exist? Thanks before :lol:

Share this post


Link to post
Share on other sites
Advertisement
Do it in your last screen-space shader or add one for that purpose. Just before presenting the image to the user.

Share this post


Link to post
Share on other sites

Do it in your last screen-space shader or add one for that purpose. Just before presenting the image to the user.


duh, I don't use shaders atm. and the games I've played (that supports gamma correction) don't use shaders too (it's old games).

Share this post


Link to post
Share on other sites
There is a bit more to it than that.

Firstly your framework will need a gamma value. Default it to 2.2, but of course let the users change it.

After that, all of the colors you send to your shaders need to be sent in linear format. That includes your lights, materials, and especially your textures.
You will have to either convert the texels to linear space manually or use the GL_EXT_texture_sRGB extension (and why wouldn’t you?), but be aware that it prefers 2.4 for the gamma value over 2.2, and it uses a slightly more complicated conversion formula.
For materials and lights, don’t send the raw RGB values to the shaders. Use powf( 2.2 ) to convert to linear space (or 2.4).


Perform all of your blending in linear space.

Finally, once you are done rendering the scene, your last post-processing operation should be to gamma-correct the screen.
For each pixel, convert back into sRGB space via pow( THISPIXEL.rgb, 1.0f / GAMMA ).


Someone feel free to fact-check me on 1 point:
#1: Lights and materials might already be considered to be in linear space, in which case they should not be converted. But if they are, then you would get a value of 0.72974f back when you originally set the material value to 0.5f. So which way do we go? Convert only textures or convert them all?


L. Spiro

Share this post


Link to post
Share on other sites
wow that's a long response....umm I don't use shaders lol. the games that I talked about (NFS Porsche Unleashed) doesn't use shaders too (DX7 era). and I think it can be done without shaders. I think I can use a fullscreen quad and render it using additive blending/subtractive (depends on need). but that's too much fillrate intensive. and I thought I've ever read that you can use pixels bit shifting (I don't remember but it said that it shifts pixel color so pixels will get brighter). But I don't remember what api to use :huh:

Share this post


Link to post
Share on other sites
Gamma correction is a non-linear operation, so I doubt that you can do it with additive or subtractive blending or even another blending.

However, I'd say that gamma correction shouldn't be looked at in OpenGL at all. IMHO it is part of the windowing system or monitor device settings.

Share this post


Link to post
Share on other sites
As alluded to above, all modern graphics programming is based on shaders.

[s]You can find the old-school gamma extension here.[/s] Note, this isn't part of OpenGL, but Windows-OpenGL (wgl).
You can also find the legacy windows function here.

Share this post


Link to post
Share on other sites

As alluded to above, all modern graphics programming is based on shaders.

You can find the old-school gamma extension here. Note, this isn't part of OpenGL, but Windows-OpenGL (wgl).


I've never seen any PCs that support WGL_I3D_gamma

Share this post


Link to post
Share on other sites
I've never seen any PCs that support WGL_I3D_gamma
Ah, you're right -- that's a defunct 3dfx Voodoo specific extension. Edited my above post.

Share this post


Link to post
Share on other sites
I think what the OP was asking was: how did early pre-shader games implement their gamma controls? AFAICR Q3A had a gamma slider.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!