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;
|
2019-03-18 11:48:11 +01:00
|
|
|
in layout(location = 3) vec4 color;
|
|
|
|
in layout(location = 4) vec3 tangent;
|
|
|
|
in layout(location = 5) vec3 bitangent;
|
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;
|
2019-03-19 20:15:13 +01:00
|
|
|
layout(binding = 3) uniform sampler2D reflectionTexture;
|
2019-03-16 20:12:35 +01:00
|
|
|
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;
|
|
|
|
|
2019-03-19 20:11:52 +01:00
|
|
|
// material
|
|
|
|
uniform float opacity;
|
2019-03-18 11:48:11 +01:00
|
|
|
uniform float shininess;
|
2019-03-19 20:15:13 +01:00
|
|
|
uniform float reflexiveness;
|
2019-03-19 20:11:52 +01:00
|
|
|
uniform vec3 diffuse_color;
|
|
|
|
uniform vec3 specular_color;
|
|
|
|
uniform vec3 emissive_color;
|
2019-03-16 20:12:35 +01:00
|
|
|
|
2019-03-14 12:43:41 +01:00
|
|
|
uniform bool isIlluminated;
|
|
|
|
uniform bool isTextured;
|
2019-03-19 20:11:52 +01:00
|
|
|
uniform bool isVertexColored;
|
2019-03-14 12:43:41 +01:00
|
|
|
uniform bool isNormalMapped;
|
2019-03-16 20:12:35 +01:00
|
|
|
uniform bool isDisplacementMapped;
|
2019-03-19 20:15:13 +01:00
|
|
|
uniform bool isReflectionMapped;
|
2019-03-14 12:43:41 +01:00
|
|
|
uniform bool isInverted;
|
|
|
|
|
2019-03-15 16:43:51 +01:00
|
|
|
// 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)
|
2019-03-19 20:11:52 +01:00
|
|
|
vec3 color;
|
2019-03-16 20:12:35 +01:00
|
|
|
|
2019-03-15 16:43:51 +01:00
|
|
|
bool is_spot; // false means point light
|
2019-03-19 20:11:52 +01:00
|
|
|
vec3 spot_direction;
|
|
|
|
float spot_cuttof_cos;
|
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-18 11:48:11 +01:00
|
|
|
out vec4 color_out;
|
2019-02-19 16:16:13 +01:00
|
|
|
|
|
|
|
|
2019-03-19 20:15:13 +01:00
|
|
|
vec3 reflection(vec3 basecolor, vec3 nnormal) {
|
|
|
|
vec3 up = normalize(vec3(MVnormal * vec4(vec3(0.0, 0.0, 1.0), 1.0)));
|
|
|
|
vec3 north = normalize(vec3(MVnormal * vec4(vec3(1.0, 0.0, 0.0), 1.0)));
|
|
|
|
float u = acos(dot(reflect(normalize(vertex), nnormal), north)) / -3.141592;
|
|
|
|
float v = acos(dot(reflect(normalize(vertex), nnormal), up )) / -3.141592;
|
|
|
|
vec3 reflection = texture(reflectionTexture, vec2(u, v)).rgb;
|
|
|
|
return (reflexiveness < 0)
|
|
|
|
? basecolor * mix(vec3(0.0), reflection, -reflexiveness)
|
|
|
|
: mix(basecolor, reflection, reflexiveness);
|
|
|
|
}
|
|
|
|
|
2019-03-19 20:11:52 +01:00
|
|
|
vec3 phong(vec3 basecolor) {
|
2019-03-16 20:12:35 +01:00
|
|
|
vec3 nnormal; // normalized normal
|
2019-03-14 12:43:41 +01:00
|
|
|
if (isNormalMapped) {
|
2019-03-17 15:17:44 +01:00
|
|
|
mat3 TBN;
|
|
|
|
if (isDisplacementMapped) {
|
|
|
|
float o = texture(displacementTexture, UV).r * 2.0 - 1.0;
|
|
|
|
float u = (texture(displacementTexture, UV + vec2(0.0001, 0.0)).r*2.0-1.0 - o) / 0.0004; // magic numbers are great
|
|
|
|
float v = (texture(displacementTexture, UV + vec2(0.0, 0.0001)).r*2.0-1.0 - o) / 0.0004; // magic numbers are great
|
|
|
|
TBN = mat3(
|
|
|
|
normalize(tangent + normal*u),
|
|
|
|
normalize(bitangent + normal*v),
|
|
|
|
normalize(cross(tangent + normal*u, bitangent + normal*v))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TBN = mat3(
|
|
|
|
normalize(tangent),
|
|
|
|
normalize(bitangent),
|
|
|
|
normalize(normal)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
nnormal = TBN * normalize(texture(normalTexture, UV).rgb * 2.0 - 1.0);
|
2019-03-16 20:12:35 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-03-17 15:17:44 +01:00
|
|
|
if (isDisplacementMapped) {
|
|
|
|
float o = texture(displacementTexture, UV).r * 2.0 - 1.0;
|
2019-03-19 20:11:52 +01:00
|
|
|
float u = (texture(displacementTexture, UV + vec2(0.00001, 0.0)).r*2.0-1.0 - o) / 0.00004;
|
|
|
|
float v = (texture(displacementTexture, UV + vec2(0.0, 0.00001)).r*2.0-1.0 - o) / 0.00004;
|
2019-03-17 15:17:44 +01:00
|
|
|
nnormal = normalize(cross(tangent + normal*u, bitangent + normal*v));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nnormal = normalize(normal);
|
|
|
|
}
|
2019-03-14 12:43:41 +01:00
|
|
|
}
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-19 20:11:52 +01:00
|
|
|
vec3 diffuse_component = vec3(0.0);
|
|
|
|
vec3 specular_component = vec3(0.0);
|
|
|
|
float diffuse_i_sum = 0.0;
|
|
|
|
//vec3 emissive_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++) {
|
2019-03-15 17:29:59 +01:00
|
|
|
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-19 20:11:52 +01:00
|
|
|
if (dot(light[i].spot_direction, -L) < light[i].spot_cuttof_cos) {
|
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);
|
2019-03-17 15:17:44 +01:00
|
|
|
float specular_i = dot(reflect(-L, nnormal), -normalize(vertex));
|
2019-03-16 20:50:39 +01:00
|
|
|
specular_i = (specular_i>0)
|
2019-03-18 11:48:11 +01:00
|
|
|
? pow(specular_i, shininess)
|
2019-03-16 20:50:39 +01:00
|
|
|
: 0;
|
2019-02-19 16:16:13 +01:00
|
|
|
|
2019-03-19 20:11:52 +01:00
|
|
|
specular_component += specular_color * light[i].color * specular_i * attenuation;
|
|
|
|
if (diffuse_i>0) diffuse_component += diffuse_color * light[i].color * diffuse_i * attenuation;
|
|
|
|
//emissive_component += emissive_color*light[i].color*attenuation;
|
2019-02-19 16:16:13 +01:00
|
|
|
}
|
|
|
|
|
2019-03-19 20:11:52 +01:00
|
|
|
basecolor *= (emissive_color + diffuse_component);
|
2019-03-19 20:15:13 +01:00
|
|
|
|
|
|
|
if (isReflectionMapped)
|
|
|
|
basecolor = reflection(basecolor, nnormal);
|
|
|
|
|
2019-03-19 20:11:52 +01:00
|
|
|
return basecolor + specular_component;
|
2019-03-14 12:43:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
2019-03-19 20:11:52 +01:00
|
|
|
vec4 c = vec4(vec3(1.0), opacity);
|
|
|
|
if (isVertexColored) c *= color;
|
|
|
|
if (isTextured) c *= texture(diffuseTexture, UV);
|
|
|
|
if (isInverted) c.rgb = 1 - c.rgb;
|
|
|
|
if (isIlluminated) c.rgb = phong(c.rgb);
|
2019-03-19 20:15:13 +01:00
|
|
|
else{
|
|
|
|
c.rgb *= diffuse_color;
|
|
|
|
if (isReflectionMapped)
|
|
|
|
c.rgb = reflection(c.rgb, normalize(normal));
|
|
|
|
}
|
2019-03-19 20:11:52 +01:00
|
|
|
//c.rgb = diffuse_color;
|
|
|
|
//c.rgb = emissive_color;
|
|
|
|
//c.rgb = specular_color;
|
2019-03-18 11:48:11 +01:00
|
|
|
color_out = c;
|
2019-02-04 18:32:08 +01:00
|
|
|
}
|