我想在GLSL中实现一个简单的反射着色器。
我当前拥有的代码来自以下tutorials
VertexShader:
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;
out vec3 FragPos;
out vec3 Normal;
uniform mat4 MVP;
void main() {
FragPos = vec3(mat4(1.0f) * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse( mat4(1.0f)))) * aNormal;
gl_Position = MVP * vec4(aPos, 1.0);
};
FragmentShader:
#shader fragment
#version 330 core
out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
uniform vec3 viewPos;
void main() {
vec3 lightColor = vec3(1.0f, 1.0f, 1.0f);
vec3 lightPos = vec3(1.2f, 1.0f, 2.0f);
vec3 objectColor = vec3(1.0f, 0.5f, 0.31f);
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
// specular
float specularStrength = 0.5;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;
vec3 result = (ambient + diffuse + specular) * objectColor;
FragColor = vec4(result, 1.0);
};
我不确定这是否正确,但是我最初的想法是使用以下代码在片段着色器中进行反射:
vec3 I = normalize(FragPos - viewPos);
vec3 R = reflect(I, normalize(Normal));
这将从摄像机的视角给出归一化的反射矢量,但是我不知道如何将其与FragColor结合使用以获得合适的输出。