TDT4230_final_project/res/shaders/simple.frag

102 lines
2.7 KiB
GLSL
Raw Normal View History

2019-02-04 18:32:08 +01:00
#version 430 core
2019-03-16 20:12:35 +01:00
in layout(location = 0) vec3 vertex;
in layout(location = 1) vec3 normal;
2019-03-14 12:43:41 +01:00
in layout(location = 2) vec2 UV;
in layout(location = 3) mat3 TBN;
2019-03-15 16:26:31 +01:00
2019-03-14 12:43:41 +01:00
layout(binding = 0) uniform sampler2D diffuseTexture;
layout(binding = 1) uniform sampler2D normalTexture;
2019-03-16 20:12:35 +01:00
layout(binding = 2) uniform sampler2D displacementTexture;
uniform float displacementCoefficient;
2019-03-14 12:43:41 +01:00
2019-03-15 16:26:31 +01:00
uniform mat4 MVP;
uniform mat4 MV;
uniform mat4 MVnormal;
uniform float shinyness;
2019-03-16 20:12:35 +01:00
2019-03-14 12:43:41 +01:00
uniform bool isIlluminated;
uniform bool isTextured;
uniform bool isNormalMapped;
2019-03-16 20:12:35 +01:00
uniform bool isDisplacementMapped;
2019-03-14 12:43:41 +01:00
uniform bool isInverted;
// lights
2019-03-16 20:12:35 +01:00
struct Light { // point lights, coordinates in MV space
2019-02-19 16:16:13 +01:00
vec3 position;
2019-03-16 20:12:35 +01:00
vec3 attenuation; // 1 / (x + y*l + z*l*l)
vec3 color_emissive;
vec3 color_diffuse;
vec3 color_specular;
bool is_spot; // false means point light
2019-03-16 20:12:35 +01:00
vec3 spot_target;
float spot_cuttof_angle;
2019-02-19 16:16:13 +01:00
};
2019-03-16 20:12:35 +01:00
#define N_LIGHTS 1
2019-02-19 16:16:13 +01:00
uniform Light light[N_LIGHTS];
2019-02-04 18:32:08 +01:00
2019-03-16 20:12:35 +01:00
out vec4 color;
2019-02-19 16:16:13 +01:00
2019-03-16 20:12:35 +01:00
vec4 phong(vec4 basecolor) {
vec3 nnormal; // normalized normal
2019-03-14 12:43:41 +01:00
if (isNormalMapped) {
vec3 tangential = texture(normalTexture, UV).rgb * 2.0 - 1.0;
2019-03-16 20:12:35 +01:00
nnormal = TBN * normalize(tangential);
}
else {
2019-03-14 12:43:41 +01:00
nnormal = normalize(normal);
}
2019-02-19 16:16:13 +01:00
2019-03-16 20:12:35 +01:00
vec3 emmissive_component = vec3(0.0);
vec3 diffuse_component = vec3(0.0);
vec3 specular_component = vec3(0.0);
2019-02-19 16:16:13 +01:00
2019-03-16 20:12:35 +01:00
for (int i = 0; i<N_LIGHTS; i++) {
vec3 L = light[i].position - vertex;
2019-03-14 12:43:41 +01:00
float l = length(L);
2019-02-19 16:16:13 +01:00
L = normalize(L);
if (light[i].is_spot) {
2019-03-16 20:12:35 +01:00
if (dot(normalize(light[i].position - light[i].spot_target), L) < light[i].spot_cuttof_angle) {
2019-02-19 16:16:13 +01:00
continue;
}
}
2019-03-16 20:12:35 +01:00
float attenuation = clamp(1 / (
light[i].attenuation.x +
light[i].attenuation.y * l +
light[i].attenuation.z * l * l
), 0.0, 1.25);
2019-02-19 16:16:13 +01:00
2019-03-16 20:12:35 +01:00
float diffuse_i = dot(nnormal, L);
float specular_i = dot(reflect(-L, nnormal), vec3(0.0)-normalize(vertex));
specular_i = (specular_i>0)
? pow(specular_i, shinyness)
: 0;
2019-02-19 16:16:13 +01:00
2019-03-16 20:12:35 +01:00
emmissive_component += light[i].color_emissive;
if (diffuse_i>0) diffuse_component += light[i].color_diffuse * diffuse_i * attenuation;
specular_component += light[i].color_specular * specular_i * attenuation;
2019-02-19 16:16:13 +01:00
}
2019-03-16 20:12:35 +01:00
return vec4(basecolor.rgb * (emmissive_component + diffuse_component) + specular_component, basecolor.a);
2019-03-14 12:43:41 +01:00
}
void main() {
if(isIlluminated) {
if (isTextured) {
2019-03-16 20:12:35 +01:00
color = phong(texture(diffuseTexture, UV));
2019-03-14 12:43:41 +01:00
} else {
2019-03-16 20:12:35 +01:00
color = phong(vec4(1.0));
2019-03-14 12:43:41 +01:00
}
} else {
color = texture(diffuseTexture, UV);
}
if (isInverted) color.rgb = 1 - color.rgb;
2019-02-04 18:32:08 +01:00
}