#version 430 core in layout(location = 0) vec3 vertex; in layout(location = 1) vec3 normal; in layout(location = 2) vec2 UV; in layout(location = 3) vec4 color; in layout(location = 4) vec3 tangent; in layout(location = 5) vec3 bitangent; layout(binding = 0) uniform sampler2D diffuseTexture; layout(binding = 1) uniform sampler2D normalTexture; layout(binding = 2) uniform sampler2D displacementTexture; uniform float displacementCoefficient; uniform mat4 MVP; uniform mat4 MV; uniform mat4 MVnormal; // material uniform float opacity; uniform float shininess; uniform vec3 diffuse_color; uniform vec3 specular_color; uniform vec3 emissive_color; uniform bool isIlluminated; uniform bool isTextured; uniform bool isVertexColored; uniform bool isNormalMapped; uniform bool isDisplacementMapped; uniform bool isInverted; // lights struct Light { // point lights, coordinates in MV space vec3 position; vec3 attenuation; // 1 / (x + y*l + z*l*l) vec3 color; bool is_spot; // false means point light vec3 spot_direction; float spot_cuttof_cos; }; #define N_LIGHTS 1 uniform Light light[N_LIGHTS]; out vec4 color_out; vec3 phong(vec3 basecolor) { vec3 nnormal; // normalized normal if (isNormalMapped) { 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); } else { if (isDisplacementMapped) { float o = texture(displacementTexture, UV).r * 2.0 - 1.0; 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; nnormal = normalize(cross(tangent + normal*u, bitangent + normal*v)); } else { nnormal = normalize(normal); } } vec3 diffuse_component = vec3(0.0); vec3 specular_component = vec3(0.0); float diffuse_i_sum = 0.0; //vec3 emissive_component = vec3(0.0); for (int i = 0; i0) ? pow(specular_i, shininess) : 0; 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; } basecolor *= (emissive_color + diffuse_component); return basecolor + specular_component; } void main() { 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); else c.rgb *= diffuse_color; //c.rgb = diffuse_color; //c.rgb = emissive_color; //c.rgb = specular_color; color_out = c; }