3

我正在寻找调试我的 GLSL phong 着色代码的帮助。这是我的顶点着色器:

layout(std140) uniform Matrices {
    mat4 model[1024];
};

layout(location = 0) in vec4 vertexCoord;
layout(location = 2) in vec3 vertexNormal;

uniform mat4 view; // from lookAt() 
uniform mat4 projection; // perspective projection 

out vec3 Position;
out vec3 Normal;
out vec4 lightPosEye;

void main() {
    mat4 modelView = view * model[gl_InstanceID];
    mat3 normalMatrix = mat3(transpose(inverse(modelView)));
    //mat3 normalMatrix = mat3(modelView);

    vec4 lightPos = vec4(350, 350, 350, 1);
    lightPosEye = modelView * lightPos;

    Position = vec3(modelView * vertexCoord);
    Normal = normalize(normalMatrix * vertexNormal);

    gl_Position = projection * vec4(Position, 1.0);
}

这是我的片段着色器:

in vec3 Position;
in vec3 Normal;
in vec4 lightPosEye;

layout(location = 0) out vec4 FragColor;

vec3 ads() {
    vec3 Ka = vec3(0, 0.5, 0);
    vec3 Kd = vec3(0, 0.5, 0);
    vec3 Ks = vec3(0, 0.1, 0);
    vec3 intensity = vec3(0.3, 0.5, 0.0);
    float shininess = 0.1;

    vec3 n = normalize(Normal);
    vec3 s = normalize(vec3(lightPosEye) - Position);
    vec3 v = normalize(vec3(-Position));
    vec3 r = reflect(-s, n);

    return intensity * (Ka + Kd * max(dot(s, n), 0.0) + Ks * pow(max(dot(r,v), 0.0), shininess));
}

void main() {
    FragColor = vec4(ads(), 1);
}

这是结果的屏幕截图(出于调试目的,我还使用另一个几何着色器渲染法线):

渲染结果

立方体上的伪影是错误的,当我移动相机(更改视图矩阵)时,圆圈上的“阴影”也会移动一点。

我的 GLSL 代码中有任何明显的错误吗?

4

0 回答 0