#version 330 core in vec3 frag_pos; in vec3 frag_normal; in vec2 frag_uv; out vec4 color; layout (std140) uniform matrices { mat4 view_xform; mat4 proj_xform; mat4 normal_xform; }; uniform mat4 node_xform; uniform sampler2D sampler; struct PointLight { vec3 pos; vec3 color; float intensity; }; struct DirectionalLight { vec3 direction; vec3 color; float intensity; }; // FIXME: probably want a buffer-backed UBO for lights in case we have more // than one shader that does lighting calculations //uniform PointLight lights[32]; // FIXME: hard-coded values for testing const DirectionalLight dl1 = DirectionalLight(vec3(-2, 1, 3), vec3(1,1,1), 1); const vec4 ambient_color = vec4(0.1, 0.1, 0.1, 1); const PointLight pl1 = PointLight(vec3(-20, 10, 30), vec3(1, 1, 1), 800); void main() { #if 1 // directional light vec3 light_direction = normalize(dl1.direction); float diffuse_factor = clamp(dot(frag_normal, light_direction), 0, 1); vec4 diffuse_color = dl1.intensity * vec4(dl1.color * diffuse_factor, 1); #else // point light vec3 light_direction = pl1.pos - frag_pos; float len = length(light_direction); light_direction = normalize(light_direction); float diffuse_factor = dot(frag_normal, light_direction) / pow(len, 2); diffuse_factor = clamp(diffuse_factor, 0, 1); vec4 diffuse_color = pl1.intensity * vec4(pl1.color * diffuse_factor, 1); #endif color = (ambient_color + diffuse_color) * texture(sampler, frag_uv.st); }