raytracing.github.io icon indicating copy to clipboard operation
raytracing.github.io copied to clipboard

Bug? Colors are specified in linear color space

Open code-monkey-101 opened this issue 2 years ago • 3 comments

I've noticed that all given colors are linear (non-SRGB).

I don't know if this is intentional, but usually colors are assumed to be SRGB colors and must be linearized before you make any calculations with them.

See for example Unity: https://docs.unity3d.com/ScriptReference/Color-linear.html https://docs.unity3d.com/ScriptReference/MaterialPropertyBlock.SetColor.html

code-monkey-101 avatar Jul 10 '22 01:07 code-monkey-101

Sort of.

Colors given are stated as-is, without any intent of being linear or sRGB. We don't discuss the difference between linear color spaces and gamma color spaces too much. And ultimately that's not the point of the book.

We could add some text specifying if a designated color is in linear or in sRGB.

What exactly would you recommend?

Keep on mind that it's ultimately more important that the book be a good learning tool than perfectly correct

trevordblack avatar Jul 10 '22 10:07 trevordblack

Since there is a chapter about gamma correction (book 1, chapter 8.3), I would at least mention it.

You are switching from a gamma workflow (sRGB colors in -> calculations in nonlinear space-> sRGB colors out) to a linear workflow without changing the values of the colors (sRGB colors in -> [missing linearization] -> calculations in linear space -> gamma conversion -> sRGB colors out). You can do "linear colors in -> calculations in linear space -> gamma conversion -> sRGB colors out", but the problem is that before chapter 8.3 you are interpreting the values as sRGB colors and after chapter 8.3 you are interpreting the same values as linear colors.

However, to do it correctly, you'd have to either adjust all the color values after chapter 8.3 or change all the pictures (everything becomes darker). So maybe just add a paragraph and mention that it's not entirely correct.

code-monkey-101 avatar Jul 10 '22 12:07 code-monkey-101

By the way, the problem is even more obvious when sampling the texture in book 2, chapter 6.1.

jpegs are usually in sRGB color space. For example, with DirectX, you'd usually load the texture as DXGI_FORMAT_R8G8B8A8_UNORM_SRGB format and the hardware would to the linearization for you when sampling it in the shader. But since you are doing it in software, you'd have to do it manually (or just use stbi_loadf instead of stbi_load).

code-monkey-101 avatar Jul 10 '22 12:07 code-monkey-101

Closing this out. There is a separate issue to track fixing this in book 2

trevordblack avatar Jul 25 '23 03:07 trevordblack