衰减未正确应用

时间:2016-06-03 15:05:06

标签: opengl lighting

我无法理解光衰减是否正确应用。

enter image description here

似乎不是因为位于(-500, 100, 100)(左侧)的立方体钢架的光泽太突出 - 我认为必须少一点,考虑到光线位置(白色立方体)是(100.0, 100.0f, 100.0f)

我使用这个公式来获得衰减

1.0 / (constant + linear * distance + quadratic * (distance^2))

片段着色器:

#version 450 core

struct Material {
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};

struct Light {
    vec3 position;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;    
    float constant;
    float linear;
    float quadratic;
};

uniform vec3 cameraPosition;
uniform Material material;
uniform Light light;    
in vec3 vNormal;
in vec2 vTexCoord;
in vec4 fragPosition;
out vec4 color;

void main() {
    vec3 norm = vNormal;
    vec3 lightDir = normalize(light.position - vec3(fragPosition.xyz));
    vec3 viewDir = normalize(cameraPosition - vec3(fragPosition.xyz));
    vec3 reflectDir = reflect(-lightDir, norm);

    vec3 ambient  = light.ambient * vec3(texture(material.diffuse, vTexCoord));
    vec3 diffuse  = light.diffuse * max(dot(norm, lightDir), 0.0) * vec3(texture(material.diffuse, vTexCoord));
    vec3 specular = light.specular * pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess) * vec3(texture(material.specular, vTexCoord));

    float d = length(lightDir);
    float attenuation = 1.0f / (light.constant + light.linear * d + light.quadratic * (d * d));


    ambient *= attenuation;
    diffuse *= attenuation;
    specular *= attenuation;

    color = vec4(ambient + diffuse + specular, 1.0f);
}

将以下值传递给程序

glUniform1f(lightConstantLoc,  1.0);
glUniform1f(lightLinearLoc,    0.1);
glUniform1f(lightQuadraticLoc, 0.01);

同样的位置但改变了视角

enter image description here 那有什么不对吗?我的计算错了吗?去那里的是什么?

0 个答案:

没有答案