Gamma correction in OpenGL
#1 Members - Reputation: 125
Posted 21 October 2011 - 03:20 AM
#3 Members - Reputation: 125
Posted 21 October 2011 - 04:10 AM
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).
#4 Crossbones+ - Reputation: 5144
Posted 21 October 2011 - 04:12 AM
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 spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums
#5 Members - Reputation: 125
Posted 21 October 2011 - 04:36 AM
#6 Members - Reputation: 1771
Posted 21 October 2011 - 05:38 AM
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.
#7 Moderators - Reputation: 13463
#8 Members - Reputation: 785
Posted 21 October 2011 - 08:07 AM
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
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);
#11 Members - Reputation: 1937
Posted 21 October 2011 - 12:02 PM
This is something I understand runs completely in hardware nowadays without any special tweaks needed (and, also in absence of shaders).
#12 Members - Reputation: 785
Posted 21 October 2011 - 09:25 PM
of course I'm talking about Windows), use http://msdn.microsoft.com/en-us/library/dd372194%28VS.85%29.aspx
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);
#13 Crossbones+ - Reputation: 5144
Posted 21 October 2011 - 09:49 PM
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.
This article explains why you should perform your color blending in linear space.
I updated my engine to use linear lighting and it really made a difference. The colors are much more realistic now.
L. Spiro
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums







