Jump to content

  • Log In with Google      Sign In   
  • Create Account


Gamma correction in OpenGL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Triad_prague   Members   -  Reputation: 140

Like
1Likes
Like

Posted 21 October 2011 - 03:20 AM

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...

Sponsor:

#2 pcmaster   Members   -  Reputation: 659

Like
1Likes
Like

Posted 21 October 2011 - 03:27 AM

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

#3 Triad_prague   Members   -  Reputation: 140

Like
0Likes
Like

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

#4 L. Spiro   Crossbones+   -  Reputation: 13268

Like
1Likes
Like

Posted 21 October 2011 - 04:12 AM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
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
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 Triad_prague   Members   -  Reputation: 140

Like
0Likes
Like

Posted 21 October 2011 - 04:36 AM

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...

#6 haegarr   Crossbones+   -  Reputation: 4171

Like
1Likes
Like

Posted 21 October 2011 - 05:38 AM

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.

#7 Hodgman   Moderators   -  Reputation: 29459

Like
1Likes
Like

Posted 21 October 2011 - 06:44 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).
You can also find the legacy windows function here.

#8 V-man   Members   -  Reputation: 805

Like
0Likes
Like

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
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);

#9 Hodgman   Moderators   -  Reputation: 29459

Like
0Likes
Like

Posted 21 October 2011 - 08:22 AM

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.

#10 raigan   Members   -  Reputation: 670

Like
1Likes
Like

Posted 21 October 2011 - 11:54 AM

I think what the OP was asking was: how did early pre-shader games implement their gamma controls? AFAICR Q3A had a gamma slider.

#11 samoth   Crossbones+   -  Reputation: 4684

Like
0Likes
Like

Posted 21 October 2011 - 12:02 PM

Maybe I'm not getting this right, but isn't proper gamma correction in OpenGL as much as using the *_SRGB constant for defining textures (to convert to linear space), and creating a SRGB framebuffer (to convert back)?

This is something I understand runs completely in hardware nowadays without any special tweaks needed (and, also in absence of shaders).

#12 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 21 October 2011 - 09:25 PM

Old games (
of course I'm talking about Windows), use http://msdn.microsoft.com/en-us/library/dd372194%28VS.85%29.aspx
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);

#13 L. Spiro   Crossbones+   -  Reputation: 13268

Like
0Likes
Like

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
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
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#14 Triad_prague   Members   -  Reputation: 140

Like
0Likes
Like

Posted 23 October 2011 - 04:03 AM

Thanks guys, in the end I finally cheat and use SDL gamma functions to do the work hehe sorry to bother you all :P
the hardest part is the beginning...




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS