**已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
Improve this question
我已经在着色器中创建了一个点光源,但光线并不指向所有方向,我做错了什么?
光线似乎只从光源向右,没有光线向左。我假设这与光线方向参数有关,但不确定如何修复它,使它与所有方向都起作用。
顶点着色器
#version 330
layout (location=0) in vec3 position;
layout (location=1) in vec2 texCoord;
layout (location=2) in vec3 normal;
out VS_OUT
{
vec2 texCoord;
vec3 mv_normal;
vec3 mv_vertex;
} vs_out;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
void main()
{
vec4 mv = modelMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * mv;
vs_out.texCoord = texCoord;
vs_out.mv_normal = normalize(modelMatrix * vec4(normal, 0.0)).xyz;
vs_out.mv_vertex = mv.xyz;
}
片段着色器
#version 330
in VS_OUT {
vec2 texCoord;
vec3 mv_normal;
vec3 mv_vertex;
} fs_in;
out vec4 fragColor;
struct Attenuation
{
float constant;
float linear;
float exponent;
};
struct PointLight
{
vec3 colour;
// Light position is assumed to be in view coordinates
vec3 position;
float intensity;
Attenuation att;
};
struct Material
{
float reflectance;
};
uniform sampler2D texture_sampler;
uniform vec3 ambientLight;
uniform float specularPower;
uniform PointLight pointLight;
uniform Material material;
vec4 ambientC;
vec4 diffuseC;
vec4 speculrC;
void setupColours(vec2 textCoord)
{
ambientC = texture(texture_sampler, textCoord);
diffuseC = ambientC;
speculrC = ambientC;
}
vec4 calcPointLight(PointLight light, vec3 position, vec3 normal)
{
vec4 diffuseColour = vec4(0, 0, 0, 0);
vec4 specColour = vec4(0, 0, 0, 0);
// Diffuse Light
vec3 light_direction = light.position - position;
vec3 to_light_source = normalize(light_direction);
float diffuseFactor = max(dot(normal, to_light_source ), 0.0);
diffuseColour = diffuseC * vec4(light.colour, 1.0) * light.intensity * diffuseFactor;
// Specular Light
vec3 camera_direction = normalize(-position);
vec3 from_light_source = -to_light_source;
vec3 reflected_light = normalize(reflect(from_light_source, normal));
float specularFactor = max( dot(camera_direction, reflected_light), 0.0);
specularFactor = pow(specularFactor, specularPower);
specColour = speculrC * specularFactor * material.reflectance * vec4(light.colour, 1.0);
// Attenuation
float distance = length(light_direction);
float attenuationInv = light.att.constant + light.att.linear * distance +
light.att.exponent * distance * distance;
return (diffuseColour + specColour) / attenuationInv;
}
void main()
{
setupColours(fs_in.texCoord);
vec4 diffuseSpecularComp = calcPointLight(pointLight, fs_in.mv_vertex, fs_in.mv_normal);
fragColor = ambientC * vec4(ambientLight, 1) + diffuseSpecularComp;
}
我改变了光的方向为负,这将导致光的方向反转,但光仍然不去所有的方向。
1条答案
按热度按时间9vw9lbht1#
解决方案是将0.0改为1.0。
float diffuseFactor = max(dot(normal, to_light_dir), 1.0);