TDT4230_final_project/res/shaders/simple.frag

106 lines
2.6 KiB
GLSL
Raw Normal View History

2019-02-04 18:32:08 +01:00
#version 430 core
in layout(location = 0) vec3 normal;
2019-02-19 16:16:13 +01:00
in layout(location = 1) vec3 vertex;
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-15 16:26:31 +01:00
uniform mat4 MVP;
uniform mat4 MV;
uniform mat4 MVnormal;
2019-03-14 12:43:41 +01:00
uniform bool isIlluminated;
uniform bool isTextured;
uniform bool isNormalMapped;
uniform bool isInverted;
// lights
struct Light { // coordinates in MV space
2019-02-19 16:16:13 +01:00
vec3 position;
vec3 spot_target;
bool is_spot; // false means point light
2019-02-19 16:16:13 +01:00
};
2019-03-14 12:43:41 +01:00
//named
2019-02-19 16:16:13 +01:00
#define N_LIGHTS 3
uniform Light light[N_LIGHTS];
2019-02-04 18:32:08 +01:00
out vec4 color;
2019-02-19 16:16:13 +01:00
// constants
float shininess = 15;
vec3 c_diffuse = vec3(0.75390625, 0.4296875, 0.4375);
vec3 c_emissive = vec3(0.01171875, 0.0, 0.15234375);
vec3 c_specular = vec3(0.9453125, 0.94921875, 0.84765625);
2019-03-14 12:43:41 +01:00
float spot_cuttof_angle = cos(1.5 / 180.0 * 3.1415926535);
2019-02-19 16:16:13 +01:00
2019-03-14 12:43:41 +01:00
void main_(vec4 basecolor) {
vec3 nnormal;
if (isNormalMapped) {
vec3 tangential = texture(normalTexture, UV).rgb * 2.0 - 1.0;
nnormal = TBN * -normalize(tangential);
} else {
nnormal = normalize(normal);
}
2019-02-19 16:16:13 +01:00
float diffuse_intensity = 0.0;
float specular_intensity = 0.0;
for (int i = 0; i<3; i++) {
vec3 L = light[i].position - vertex;
2019-03-14 12:43:41 +01:00
float l = length(L);
float attenuation = clamp(2000/(1 + 1*l + 0.1*l*l), 0.0, 1.25);
2019-02-19 16:16:13 +01:00
L = normalize(L);
if (light[i].is_spot) {
vec3 L2 = normalize(light[i].position - light[i].spot_target);
2019-02-19 16:16:13 +01:00
if (dot(L2, L) < spot_cuttof_angle) {
continue;
}
2019-03-14 12:43:41 +01:00
attenuation *= 30;
2019-02-19 16:16:13 +01:00
}
float diffuse_i = dot(nnormal, L);
float specular_i = pow(dot(reflect(-L, nnormal), normalize(vec3(0,0,0) - vertex)), shininess);
if (diffuse_i > 0) diffuse_intensity += attenuation*diffuse_i;
if (specular_i > 0) specular_intensity += attenuation*specular_i;
}
//diffuse_intensity *= 1.0 / N_LIGHTS;
//specular_intensity *= 1.0 / N_LIGHTS;
//float intensity = dot(normalize(normal), normalize(light_pos_2));
//color = vec4(0.5 * normal + 0.5, 1.0);
2019-03-14 12:43:41 +01:00
color = basecolor * vec4(c_emissive
+ c_diffuse*diffuse_intensity, 1.0f)
+ vec4(c_specular*specular_intensity, 1.0f);
}
void main() {
if(isIlluminated) {
if (isTextured) {
c_diffuse = vec3(0.9);
c_emissive = vec3(0.2);
main_(texture(diffuseTexture, UV));
} else {
main_(vec4(1.0));
}
} else {
color = texture(diffuseTexture, UV);
}
2019-03-15 21:22:41 +01:00
if (isInverted) color.rgb = 1- color.rgb;
2019-02-04 18:32:08 +01:00
}