Pages: [1] 2   Go Down
  Print  
Author Topic: The importance of being linear  (Read 5569 times)
April 16, 2008, 03:02:15 pm
Did you know that the final image you watch on your monitor has some gamma correction in it? This means that most of the photos and images you use have gamma correction applied to them.

The graphics calculations like lighting and other calculations are calculated in linear color space so the textures you feed them should be in linear color space too! Since we do not know the gamma that was applied to the images you use you can just take a value of 2.2 which is ok for most images. Alpha maps and normal maps usually already are in linear color space so they do not need correction.

linearcolor = pow(originalcolor,2.2)

When the final image is finished we should apply the gamma to the entire result so we end up with a gamma correct image.

correctedcolor = pow(finalcolor,1/2.2)

If you use these rules you end up with much more realistic materials that behave like real materials and look less "computer generated". Look at the attached examples. The incorrect images was originally much darker so I had to brighten it to make it easier view.

The images use 3 spotlights with shadow map.


* correctgamma.jpg (15.24 KB, 640x404 - viewed 370 times.)

* incorrectgamma.jpg (14.25 KB, 640x404 - viewed 366 times.)
April 16, 2008, 03:12:22 pm
OK a quick example that shows the difference. Let's say we have a shader that calculates the average of two pixel values (like a blur for example shader).

We have a pixel value of 0.0 and a pixel value of 1.0

The expected end result ofcourse is:

(0.0+1.0)/2 = 0.5

With gamma correction the final correct result is: 0.7297400

When the gamma correction is in the textures and not applied to the final image the result is:
(0.0+1.0)/2 = 0.5

This is wrong! The linear answer is 0.5 but the final value on your monitor should be 0.7297400. Applying gamma correction at the end here does not help because the input values are wrong and there is no way to fix this.

More info can be found in the book GPU gems 3


April 16, 2008, 03:20:16 pm
In Quest3D there are three ways to do gamma correction. Remember the correct gamma value is 2.2! So the gamma correction value should be 1/2.2 = 0.4545454.

1) Use the Brightness, Contrast, Gamma channel. This channel only works with full screen projects but the result is fast and easy.
2) Use the gamma setting of the HDR processor. If you are rendering in HDR anyway this is a good option. HDR is slower dan LDR so if you do not need it, avoid it.
3) Use the color correction post-processing template. This post-process example has a gamma setting.

The textures should preferrably be corrected in photoshop. In CS3 you can click Adjustment->Exposure. Use 0.454545 as the value for gamma.
April 16, 2008, 04:15:09 pm
nice one.

I used a value of 2.2 in the hdr rendering because the result look like in your fist image.
So now i known why.

thanks

April 17, 2008, 02:00:13 am
maybe setting gamma on a videocard also works?
April 17, 2008, 10:34:48 am
Yes this also works Cheesy
April 23, 2008, 05:42:38 pm
Pitty that not so much developers are aware of that. For example Vray community is using a lot Linear workflow. But baking in Linear workflow with Vray creates a lot of annoying bugs. It's fine if you do regular baking into one lightmap but create issue when you need three lightmaps for directional radiosity lightmaps (((

Anyway the more you use postprocessing more important linear approach become.

P.S. Remko, inspired by GPU GEMS 3 ?  Smiley

Upgrade to PSSM is available for SSAO customers. Check http://www.3dvrm.com/shadows_solution/ for details.
April 24, 2008, 09:57:21 am
Here's another example that shows the dramatic differences. Also notice how the non gamma corrected images have that typical computer generated look.

Textures and photo's are usually pre-gamma corrected. This is WRONG! You need to have textures that use a linear scale. The will look too dark on your monitor but when you use them in your 3D scene and apply gamma correction afterwards the result is perfect.

How to apply gamma correction to your textures?
1. Clone the layer in photoshop
2. Set layer transparency to 'Multiply'
3. Save your image

This removes a gamma of 2.0 which is roughly close to the 2.2 or 1.8 that is used normally. Because there is no way to figure out the original gamma that was applied using a gamma of 2.0 is fine.

UPDATE(26/09/2008):
The easiest way to deal with gamma is this:
Dot All photo/image material needs 'gamma uncorrection'. Example: a wood texture, photo....
Dot Normal, light, depth maps are all linear so no correction needed
Dot Only artist generated images are in non linear-space
Dot If you uncorrect the gamma in photoshop you get banding errors (on 8 bit images)
Dot The best way to do the uncorrection is to do it in your shader!!! here's the trick:
Code:
sampler2D SamDiffuseTexture0 = sampler_state
{
    Texture = <DiffuseTexture0>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    SRGBTEXTURE = TRUE;
};

Simply use this sampler state for any texture you suspect will not be in linear space.

Now look at the images I created. They show all 4 possible cases and only one is correct!





The First image show the result if all the calculations and the textures are correct but there is no gamma applied to the final image. This image is simply too dark.

The Second image shows how it should be. The colors of the texture look nice and all the light blending and gradients are exacly as in real life.

In most games and 3D applications the 3rd image is the case: no gamma correction is applied at all! No correction in the textures and no correction in the final image. The color of the texture is ok but all the light blending and the gradient caused by lighting looks wrong. Also blending of two lights looks wrong! Look at the lighting of the ball and compare that to the second shot. Which one looks more real to you?

The Fourth image shows what happens if you correct the final  image but you forget to modify your textures. All the calculations are fine. The lighting is fine and the blending is fine. The only problem is that tha floor texture looks a bit washed out. Maybe the floor should be a bit washed out but if we compare it to the original texture we can see this is not the case. The image looks fine except for the fact that the original color of the floor is not reproduced. Removing the gamma correction from the texture solves this(See second image).


* Gamma_NotCorrect_TextureCorrect.jpg (107.1 KB, 640x428 - viewed 331 times.)

* Gamma_Correct.jpg (110.51 KB, 640x428 - viewed 321 times.)

* Gamma_IncorrectTextureAndGamma.jpg (104.14 KB, 640x428 - viewed 310 times.)

* Gamma_IncorrectTexture.jpg (104.3 KB, 640x428 - viewed 301 times.)
April 26, 2008, 05:07:22 pm
Thanks Remko
Very important tips.
To adjust correct gamma for a texture is it possible to use auto levels in photoshop?
Is there any way to make sure that our texture is 100% gamma correct? Any tip?

ali-rahimi.net
April 27, 2008, 02:12:11 am
Auto levels has nothing to do with gamma. In short all pictures made for sceen viewing have a gamma of 2.0 or so. All other pictures like normal maps and some light maps are already linear.

Well, a correct gamma is completely subjective. You can only guess what the gamma of an image is. Usually it's 2.2 because that's what's mostly used. You can easily test the gamma of your monitor by creating a square that has 0.5 0.5 0.5 as color and then generate an image of alternating black and white pixels. When you squint your eyes the black/white should appear the same as the gray color since gray is the average of black and white. If you create an image like this in photoshop you will see that the result is incorrect! When you apply the gamma correction in Quest using the methods described above you should be able to find a gamma value that is correct. In practice you can use a gamma of 2.0 because that is close enough. When I tried this my monitor appears to have a gamma of 1.8.
May 02, 2008, 10:30:18 am
For 4.1 we added sRGB support for textures. Turning on this switch will automatically compensate for a gamma of 2.2 in the image (Which is the standard for most images). More info about sRGB here: http://en.wikipedia.org/wiki/SRGB_color_space

Use this in your shader to sample a gamma corrected with linear samples. The sRgb feature of the texture can be left off in this case because the shader takes care of the conversion.
sampler2D SamDiffuseTexture0 = sampler_state
{
    Texture = <DiffuseTexture0>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    SRGBTEXTURE = TRUE;
};
May 02, 2008, 11:16:23 am
Thats realy good remko.  Smiley
So now we dont need to adjust texture brightness in photoshop?
Is there any chance to see screen space ambiant occ soon?
Also i have a Suggestion for HDR post process channel. It would be nice if we could have color correction within HDR post process itself.

ali-rahimi.net
May 02, 2008, 12:06:46 pm
Quote
screen space ambiant occ soon

makes only really sense if we have MRT's.

Then it is a matter of days to implement it in an more speedy way  Grin

May 02, 2008, 08:13:22 pm
Remko Jacobs

Maybe it's a stupid question but how we can actually create proper sRGB images? As i understood you need precision more than 8 bits for every color channel to create sRGB image, thats what i found in OpenGL specification on sRGB. Another issue is that gamma 2.2 is true for old CRT displays, on LCD gamma with factory settings can be even 1.2, that what we found several times on Vray forum when people was cheking gamma of their monitors. So basically image created with profile different than 2.2 gamma will have a wrong data when loaded as sRGB.

Edit: after reading article in MSDN, it sounds that it's better to calibrate your monitor to 2.2 gamma and than use sRGBs.

Upgrade to PSSM is available for SSAO customers. Check http://www.3dvrm.com/shadows_solution/ for details.
May 04, 2008, 11:14:31 pm
Well, what gamma you use is up to you but most systems now use 2.2. On a Mac it's 1.6 I believe. Some formats such as pdf and psd have color profiles in them and you can convert any color profile image to sRGB. For jpg and other formats you'll have to guess. In general just assuming a gamma of 2.2 works.

We did some work on SSAO but the shadow maps have priority. SSAO effectively is a modified shadowmap routine so when that's in SSAO is not far away. NVIDIA has some pretty advanced stuff about this that looks much better than the Crysis version but on the other hand the Crysis version is very fast in comparison.
Pages: [1] 2   Go Down
  Print  
 
Jump to: