0

我正在 OpenGL 中实现镜面高光作为 phong 模型的一部分。目前,当我的相机移动时,高光会移动,而不是变得更亮或更暗,这显然不是预期的结果。我已经按照 learnopengl.com 上的教程进行操作


#version 330 core

in vec3 outColor;
in vec3 outNormal;
in vec3 fragPos;

out vec4 FragColor;

uniform vec3 lightPos = vec3(-2.0f, 0.0f, 0.0f);
uniform vec3 lightColor = vec3(1.0, 0.5f, 1.0f);
uniform vec3 viewPos;


float ambientStrength = 0.2;
float specularStrength = 0.5;


uniform sampler2D tex;

void main()
{
    vec3 normal = normalize(outNormal);
    vec3 lightDir = normalize(lightPos - fragPos);

    //ambient lighitng
    vec3 ambient = ambientStrength * lightColor;

    //diffuse lighting
    float diffuseFactor = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = diffuseFactor * lightColor;

    //specular lighting
    vec3 viewDir = normalize(viewPos - fragPos);
    vec3 reflectDir = reflect(-lightDir, normal);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 128);
    vec3 specular = specularStrength * spec * lightColor;


    vec3 result = (ambient + diffuse + specular) * outColor;
    FragColor = vec4(result, 1.0f);// texture(tex, outTexCoords);// vec4(0.0f, 1.0f, 0.0f, 1.0f);
}
4

0 回答 0