我正在寻找一个合适的简单示例。
现在我有一些基于教程的东西,但我没有看到三角形中间的变化。看起来每个三角形都会改变颜色,但只是整体改变。
out vec3 vertex_normal;
out vec3 vertex_light_position;
..在顶点着色器上。
与
vertex_normal = normalize(NormalMatrix * in_Normal);
// old gl_NormalMatrix: "transpose of the inverse of the upper
// leftmost 3x3 of gl_ModelViewMatrix"
mat3 NormalMatrix = transpose(
inverse(
mat3(
ModelViewMatrix
)
)
);
以及片段着色器:
float diffuse_value = MAX(dot(vertex_normal, vertex_light_position), 0.0);
gl_FragColor = out_Color * diffuse_value
但正如我所说,每个三角形似乎都是纯色(仅整体变化)。
我听说规范化可能会发挥作用,但如果我规范化()vertex_normal,它会得到相同的结果。
最佳答案
检查 vertex_normal
和 vertex_light_position
的值。您可以通过使片段着色器执行以下操作来做到这一点:
gl_FragColor = vertex_normal
我不确定 vertex_light_position
是什么,但听起来它应该是从顶点到光源的法线,而不是光源的绝对位置。
编辑: 请参阅http://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/lighting.php
关于opengl - 现代 GLSL 中的每像素光照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3739150/