How to do it...

Adding gamma correction to an OpenGL program can be as simple as carrying out the following steps:

  1. Set up a uniform variable named Gamma and set it to an appropriate value for your system.
  2. Use the following code or something similar in a fragment shader:
vec3 color = lightingModel( ... ); 
FragColor = vec4( pow( color, vec3(1.0/Gamma) ), 1.0 ); 

If your shader involves texture data, care must be taken to make sure that the texture data is not already gamma-corrected so that you don't apply gamma correction twice (refer to the There's more... section of this recipe).

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.