简体   繁体   English

纯色的OpenGL着色器伽玛

[英]OpenGL shader gamma on solid color

I want to implement gamma correction on my OpenGL 3D renderer, I understand that it's absolutely relevant on texture loaded in sRGB, so I do this: 我想在我的OpenGL 3D渲染器上实现伽马校正,我知道它与sRGB中加载的纹理完全相关,所以我这样做:

vec4 texColor;
texColor = texture(src_tex_unit0, texCoordVarying);
texColor = vec4(pow(texColor.rgb,vec3(2.2)),1.0);
vec4 colorPreGamma = texColor * (vec4(ambient,1.0) + vec4(diffuse,1.0));
fragColor =  vec4(pow(colorPreGamma.rgb, vec3(1.0/gamma)),1.0);

But my question is about solid color, when the surface of the 3D object I want lit is not textured but just colored by a per vertex RGB value. 但我的问题是关于纯色,当我想要点亮的3D对象的表面没有纹理但只是按每个顶点RGB值着色。 In this case, do I have to transform my color in Linear space, and after the lighting operation, transform back to gamma space like I do for a texture? 在这种情况下,我是否必须在线性空间中转换颜色,并且在照明操作之后,像我对纹理一样转换回伽玛空间?

Does this apply when my light are colored? 当我的灯光着色时,这是否适用?

In this case, do I have to transform my color in Linear space, and after the lighting operation, transform back to gamma space like I do for a texture? 在这种情况下,我是否必须在线性空间中转换颜色,并且在照明操作之后,像我对纹理一样转换回伽玛空间?

That depends: what colorspace are your colors in? 这取决于:你的颜色是什么颜色空间?

You're not doing this correction because of where they come from; 你不是因为他们来自哪里而进行这种修正; you're doing it because of what the colors actually are. 你是因为颜色实际上是这样做的。 If the value is not linear, then you must linearize it before using it, regardless of where it comes from. 如果该值不是线性的,那么在使用它之前必须将其线性化,无论它来自何处。

You are ultimately responsible for putting that color there. 你最终要把那种颜色放在那里。 So you must have to know whether that color is in linear RGB or sRGB colorspace. 因此,您必须知道该颜色是否为线性RGB或sRGB颜色空间。 And if the color is not linear, then you have to linearize it before you can get meaningful numbers from it. 如果颜色不是线性的,那么你必须先将其线性化,然后才能从中获得有意义的数字。

In OpenGL there isn't a huge distinction between color data and other kinds of data: if you have a vec3 you can access components as .xyz or .rgb . 在OpenGL中,颜色数据和其他类型的数据之间没有太大的区别:如果你有一个vec3你可以访问.xyz.rgb组件。 It's all just data. 这些都只是数据。

So ask yourself this: "Do I have to gamma correct my vertex positions in the vertex shader?" 所以问问自己:“我是否需要在顶点着色器中对顶点位置进行伽玛校正?”

Of course not, because your vertex positions are already in linear space. 当然不是,因为你的顶点位置已经在线性空间中。 So if you are similarly setting your vertex colors in linear space, again no gamma correction is needed. 因此,如果您在线性空间中类似地设置顶点颜色,则不需要进行伽马校正。

In other words, do you imagine vec3(0.5, 0.5, 0.5) as being a gray that is visually halfway between black and white? 换句话说,你vec3(0.5, 0.5, 0.5)是一个灰色,在黑色和白色之间的视觉中间? Then you need gamma correction. 然后你需要伽马校正。

Do you imagine it as being mathematically halfway between black and white (in terms of measurable light intensity)? 你是否认为它在数学上处于黑白之间(就可测量的光强度而言)? Then it's already linear. 然后它已经是线性的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM