8 changed files with 152 additions and 22 deletions
@ -0,0 +1,60 @@ |
|||||||
|
#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); |
||||||
|
} |
||||||
@ -0,0 +1,27 @@ |
|||||||
|
#version 330 core |
||||||
|
|
||||||
|
layout (location = 0) in vec3 position; |
||||||
|
layout (location = 1) in vec3 normal; |
||||||
|
layout (location = 2) in vec2 uv; |
||||||
|
|
||||||
|
out vec3 frag_pos; |
||||||
|
out vec3 frag_normal; |
||||||
|
out vec2 frag_uv; |
||||||
|
|
||||||
|
layout (std140) uniform matrices |
||||||
|
{ |
||||||
|
mat4 view_xform; |
||||||
|
mat4 proj_xform; |
||||||
|
mat4 normal_xform; |
||||||
|
}; |
||||||
|
|
||||||
|
uniform mat4 node_xform; |
||||||
|
|
||||||
|
|
||||||
|
void main() |
||||||
|
{ |
||||||
|
frag_pos = (node_xform * vec4(position, 1)).xyz; |
||||||
|
frag_uv = uv; |
||||||
|
frag_normal = normalize(mat3(node_xform) * normal); |
||||||
|
gl_Position = proj_xform * view_xform * node_xform * vec4(position, 1); |
||||||
|
} |
||||||
Loading…
Reference in new issue