Files
3dobs/shaders/frag.glsl
T

224 lines
6.0 KiB
GLSL
Raw Normal View History

2023-10-14 22:41:16 +02:00
#version 330 core
in vec3 fragNormals;
in vec3 fragPos;
in vec2 texCoords;
2023-11-04 21:40:01 +02:00
in mat3 fragTBN;
2023-10-14 22:41:16 +02:00
out vec4 FragColor;
struct Material {
2023-11-02 03:38:14 +02:00
sampler2D texture_diffuse;
sampler2D texture_specular;
sampler2D texture_ambient;
2023-11-02 22:02:00 +02:00
sampler2D texture_emission;
2023-11-04 21:40:01 +02:00
sampler2D texture_normal;
2023-10-14 22:41:16 +02:00
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
2023-11-02 03:38:14 +02:00
float opacity;
2023-10-14 22:41:16 +02:00
};
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
float quadratic;
};
struct SpotLight {
vec3 position;
vec3 direction;
float cutOff;
float outerCutOff;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float constant;
float linear;
float quadratic;
};
#define NR_POINT_LIGHTS 4
#define blinn true
2023-10-14 22:41:16 +02:00
uniform vec3 viewPos;
uniform Material material;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform SpotLight spotLight;
2023-11-02 03:38:14 +02:00
uniform bool useTextures;
2023-11-02 22:02:00 +02:00
uniform bool hasEmissionTexture;
2023-11-04 21:40:01 +02:00
uniform bool hasNormalTexture;
2023-10-14 22:41:16 +02:00
vec3 CalculateDirLight(DirLight light, vec3 normal, vec3 viewDir) {
vec3 lightDir = normalize(-light.direction);
float diff = max(dot(lightDir, normal), 0.0);
float spec = 0.0f;
if (blinn) {
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), material.shininess);
} else {
vec3 reflectDir = reflect(-lightDir, normal);
spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
}
2023-10-14 22:41:16 +02:00
2023-11-02 03:38:14 +02:00
vec3 ambient = vec3(0.0, 0.0, 0.0);
vec3 diffuse = vec3(0.0, 0.0, 0.0);
vec3 specular = vec3(0.0, 0.0, 0.0);
if (useTextures) {
2023-11-04 21:40:01 +02:00
ambient = light.ambient * vec3(texture(material.texture_ambient, texCoords));
diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse, texCoords));
specular = light.specular * spec * vec3(texture(material.texture_specular, texCoords));
2023-11-02 03:38:14 +02:00
} else {
ambient = light.ambient * material.ambient;
diffuse = light.diffuse * (diff * material.diffuse);
specular = light.specular * (spec * material.specular);
}
2023-10-14 22:41:16 +02:00
return (ambient + diffuse + specular);
}
vec3 CalculatePointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 lightDir = normalize(light.position - fragPos);
float diff = max(dot(lightDir, normal), 0.0);
float spec = 0.0f;
if (blinn) {
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), material.shininess);
} else {
vec3 reflectDir = reflect(-lightDir, normal);
spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
}
2023-10-14 22:41:16 +02:00
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance + distance));
2023-11-02 03:38:14 +02:00
vec3 ambient = vec3(0.0, 0.0, 0.0);
vec3 diffuse = vec3(0.0, 0.0, 0.0);
vec3 specular = vec3(0.0, 0.0, 0.0);
if (useTextures) {
2023-11-04 21:40:01 +02:00
ambient = light.ambient * vec3(texture(material.texture_ambient, texCoords));
diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse, texCoords));
specular = light.specular * spec * vec3(texture(material.texture_specular, texCoords));
2023-11-02 03:38:14 +02:00
} else {
ambient = light.ambient * material.ambient;
diffuse = light.diffuse * (diff * material.diffuse);
specular = light.specular * (spec * material.specular);
}
2023-10-14 22:41:16 +02:00
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
vec3 CalculateSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 lightDir = normalize(light.position - fragPos);
float diff = max(dot(lightDir, normal), 0.0);
float spec = 0.0f;
if (blinn) {
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), material.shininess);
} else {
vec3 reflectDir = reflect(-lightDir, normal);
spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
}
2023-10-14 22:41:16 +02:00
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance + distance));
// spotlight
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.cutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
2023-11-02 03:38:14 +02:00
vec3 ambient = vec3(0.0, 0.0, 0.0);
vec3 diffuse = vec3(0.0, 0.0, 0.0);
vec3 specular = vec3(0.0, 0.0, 0.0);
if (useTextures) {
2023-11-04 21:40:01 +02:00
ambient = light.ambient * vec3(texture(material.texture_ambient, texCoords));
diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse, texCoords));
specular = light.specular * spec * vec3(texture(material.texture_specular, texCoords));
2023-11-02 03:38:14 +02:00
} else {
ambient = light.ambient * material.ambient;
diffuse = light.diffuse * (diff * material.diffuse);
specular = light.specular * (spec * material.specular);
}
2023-10-14 22:41:16 +02:00
diffuse *= intensity;
specular *= intensity;
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
void main()
{
2023-11-02 22:02:00 +02:00
// discard completely transparent fragments, such as the ones used in
// leaves or grass textures
if (texture(material.texture_diffuse, texCoords).a == 0.0) {
discard;
}
2023-10-14 22:41:16 +02:00
vec3 norm = normalize(fragNormals);
2023-11-04 21:40:01 +02:00
// use normal map if available
if (useTextures && hasNormalTexture) {
vec3 normal = texture(material.texture_normal, texCoords).rgb;
normal = normal * 2.0 - 1.0;
norm = normalize(fragTBN * normal);
}
2023-10-14 22:41:16 +02:00
vec3 viewDir = normalize(viewPos - fragPos);
// direction light
vec3 result = CalculateDirLight(dirLight, norm, viewDir);
// point lights
for (int i = 0; i < NR_POINT_LIGHTS; i++) {
result += CalculatePointLight(pointLights[i], norm, fragPos, viewDir);
}
result += CalculateSpotLight(spotLight, norm, fragPos, viewDir);
2023-11-04 21:40:01 +02:00
// emission
2023-11-02 22:02:00 +02:00
if (useTextures && hasEmissionTexture) {
result += texture(material.texture_emission, texCoords).rgb;
}
2023-11-02 03:38:14 +02:00
FragColor = vec4(result, material.opacity);
2023-10-14 22:41:16 +02:00
}