Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL: Gamma corrected image doesn't appear linear

I'm using OpenGL for rendering, and when I write linear values to the default framebuffer (without any gamma correction) they appear linear on my monitor. This goes against everything I thought I knew about gamma correction (as explained here: http://gamedevelopment.tutsplus.com/articles/gamma-correction-and-why-it-matters--gamedev-14466 ). Without gamma correction, I would expect to see mid-range colors darkened non-linearly by my monitor.

But here is what I actually see; first with no gamma correction on my part, then with gamma correction: enter image description here

Here's my fragment shader without gamma correction (drawn on a fullscreen quad to the default framebuffer). This results in the linear image on the left:

out vec4 fsOut0;

void main( void )
{
    // split the screen into 10 discrete color bands
    float yResolution = 768.0;
    int intVal = int(gl_FragCoord.y / yResolution * 10.0);
    fsOut0.rgb = vec3( float(intVal) / 10.0 );

    fsOut0.a = 1.0;
}

And here's the shader with added gamma correction (from linear space to sRGB). This results in the brighter-than-linear image on the right:

out vec4 fsOut0;

void main( void )
{
    // split the screen into 10 discrete color bands
    float yResolution = 768.0;
    int intVal = int(gl_FragCoord.y / yResolution * 10.0);
    fsOut0.rgb = vec3( float(intVal) / 10.0 );

    // gamma correction
    fsOut0.rgb = pow( fsOut0.rgb, vec3(1.0/2.2) );

    fsOut0.a = 1.0;
}

I'm verifying whether or not the colors are linear just by looking at them, and by using the color picker in Photoshop and looking at the differences in RGB values between color bands. For the linear-looking image the difference between each color is (mostly) constant.

I have also tried requesting an sRGB-capable default framebuffer. In this case, writing linear values with no gamma correction looks like the second image (non-linear).

What am I doing wrong? Or could it be that my two monitors are both miscalibrated AND that Photoshop does not pick colors in linear space? Or is my "non-linear" image actually the correct linear result, but it just doesn't seem linear to my eyes?

My question is sort of a duplicate of this: Do I need to gamma correct the final color output on a modern computer/monitor Unfortunately the accepted answer is extremely confusing and the parts of it I was able to follow seem contradictory, or at least not fully explained for someone less knowledgeable than the answerer.

like image 301
KTC Avatar asked Nov 10 '22 17:11

KTC


1 Answers

Well, both your left and right pictures are as is to be expected. They are perfectly fine, and yes, I know my stuff.

It's just that our eyes are not very linear either, so e.g. 1/5th of linear intensity (luminous intensity) is perceived as "half as bright as white". This is what you see on the right, in the corrected image, near the bottom.

This is the reason for gamma being there in the first place - to help encoding by mimicking the eye's response. IOW, gamma makes the non-linear ramp look linear.

However, a physically linear ramp (as on the right) is therefore quite counter to being perceived as linear. Remember that the real world has a quite large dynamic range (in terms of luminous intensity), and our eyes are compensating for that. This is confusing you, but unlike many others, you actually got the numbers right.

like image 186
Simon Thum Avatar answered Nov 15 '22 06:11

Simon Thum