140 lines
4.4 KiB
GLSL
140 lines
4.4 KiB
GLSL
#version 330 core
|
|
|
|
struct Material {
|
|
vec3 ambient;
|
|
vec3 diffuse;
|
|
vec3 specular;
|
|
float shininess;
|
|
};
|
|
|
|
struct DirLight {
|
|
vec3 direction;
|
|
|
|
vec3 ambient;
|
|
vec3 diffuse;
|
|
vec3 specular;
|
|
};
|
|
|
|
struct PointLight {
|
|
vec3 position;
|
|
|
|
float constant;
|
|
float linear;
|
|
float quadratic;
|
|
|
|
vec3 ambient;
|
|
vec3 diffuse;
|
|
vec3 specular;
|
|
};
|
|
|
|
struct SpotLight {
|
|
vec3 position;
|
|
vec3 direction;
|
|
float cutOff;
|
|
float outerCutOff;
|
|
|
|
float constant;
|
|
float linear;
|
|
float quadratic;
|
|
|
|
vec3 ambient;
|
|
vec3 diffuse;
|
|
vec3 specular;
|
|
};
|
|
|
|
in vec3 FragPos;
|
|
in vec3 Normal;
|
|
in vec2 TexCoord;
|
|
uniform sampler2D tex;
|
|
|
|
uniform vec3 viewPos;
|
|
//直射光
|
|
uniform DirLight dirLight;
|
|
//点光源
|
|
uniform PointLight pointLight;
|
|
//材质
|
|
uniform Material material;
|
|
|
|
out vec4 FragColor;
|
|
|
|
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
|
|
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
|
|
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
|
|
|
|
void main() {
|
|
|
|
vec3 norm = normalize(Normal);
|
|
vec3 viewDir = normalize(viewPos - FragPos);
|
|
// 第一阶段:定向光照
|
|
vec3 result = CalcDirLight(dirLight, norm, viewDir);
|
|
// 第二阶段:点光源
|
|
result += CalcPointLight(pointLight, norm, FragPos, viewDir);
|
|
|
|
if (texture(tex, TexCoord).a < 0.7)
|
|
discard;
|
|
FragColor = vec4(result,1.0) * texture(tex, TexCoord);
|
|
}
|
|
|
|
// calculates the color when using a directional light.
|
|
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
|
|
{
|
|
vec3 lightDir = normalize(-light.direction);
|
|
// diffuse shading
|
|
float diff = max(dot(normal, lightDir), 0.0);
|
|
// specular shading
|
|
vec3 reflectDir = reflect(-lightDir, normal);
|
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
|
// combine results
|
|
vec3 ambient = light.ambient * material.ambient * vec3(texture(tex, TexCoord));
|
|
vec3 diffuse = light.diffuse * diff *material.diffuse * vec3(texture(tex, TexCoord));
|
|
vec3 specular = light.specular * spec * material.specular * vec3(texture(tex, TexCoord));
|
|
return (ambient + diffuse + specular);
|
|
}
|
|
|
|
// calculates the color when using a point light.
|
|
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
|
|
{
|
|
vec3 lightDir = normalize(light.position - fragPos);
|
|
// diffuse shading
|
|
float diff = max(dot(normal, lightDir), 0.0);
|
|
// specular shading
|
|
vec3 reflectDir = reflect(-lightDir, normal);
|
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
|
// attenuation
|
|
float distance = length(light.position - fragPos);
|
|
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
|
// combine results
|
|
vec3 ambient = light.ambient * material.ambient * vec3(texture(tex, TexCoord));
|
|
vec3 diffuse = light.diffuse * diff *material.diffuse * vec3(texture(tex, TexCoord));
|
|
vec3 specular = light.specular * spec * material.specular * vec3(texture(tex, TexCoord));
|
|
ambient *= attenuation;
|
|
diffuse *= attenuation;
|
|
specular *= attenuation;
|
|
return (ambient + diffuse + specular);
|
|
}
|
|
|
|
// calculates the color when using a spot light.
|
|
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
|
|
{
|
|
vec3 lightDir = normalize(light.position - fragPos);
|
|
// diffuse shading
|
|
float diff = max(dot(normal, lightDir), 0.0);
|
|
// specular shading
|
|
vec3 reflectDir = reflect(-lightDir, normal);
|
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
|
// attenuation
|
|
float distance = length(light.position - fragPos);
|
|
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
|
|
// spotlight intensity
|
|
float theta = dot(lightDir, normalize(-light.direction));
|
|
float epsilon = light.cutOff - light.outerCutOff;
|
|
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
|
|
// combine results
|
|
vec3 ambient = light.ambient * material.ambient * vec3(texture(tex, TexCoord));
|
|
vec3 diffuse = light.diffuse * diff *material.diffuse * vec3(texture(tex, TexCoord));
|
|
vec3 specular = light.specular * spec * material.specular * vec3(texture(tex, TexCoord));
|
|
ambient *= attenuation * intensity;
|
|
diffuse *= attenuation * intensity;
|
|
specular *= attenuation * intensity;
|
|
return (ambient + diffuse + specular);
|
|
} |