Gamma correction in OpenGL

Started by
11 comments, last by Triad_prague 12 years, 6 months ago
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:
the hardest part is the beginning...
Advertisement
Do it in your last screen-space shader or add one for that purpose. Just before presenting the image to the user.

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).
the hardest part is the beginning...
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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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:
the hardest part is the beginning...
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.
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.

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
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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.
I think what the OP was asking was: how did early pre-shader games implement their gamma controls? AFAICR Q3A had a gamma slider.

This topic is closed to new replies.

Advertisement