Created
June 13, 2012 11:08
-
-
Save rikusalminen/2923464 to your computer and use it in GitHub Desktop.
Simple GLSL lighting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 150 | |
layout(std140) uniform Camera | |
{ | |
mat4 view_matrix; | |
mat4 projection_matrix; | |
} camera; | |
layout(std140) uniform Light | |
{ | |
vec4 position; | |
vec4 direction; | |
vec4 diffuse; | |
vec4 specular; | |
vec4 ambient; | |
float shininess; | |
float attenuation; | |
float spot_cutoff, spot_exponent; | |
} light; | |
in vec4 pos; | |
in vec4 nor; | |
in vec2 uv; | |
out vec4 color; | |
void main() | |
{ | |
vec4 light_vec = (camera.view_matrix * light.position) - pos; | |
vec4 light_dir = normalize(light_vec); | |
float spot_cos = dot(-light_dir, camera.view_matrix * light.direction); | |
float spot = ((spot_cos < light.spot_cutoff) ? 0.0 : pow(spot_cos, light.spot_exponent)); | |
float light_attenuation = (1.0 / (1.0 + light.attenuation * dot(light_vec, light_vec))); | |
vec4 eye_vec = vec4(normalize(pos.xyz), 0.0); | |
float diffuse = max(0.0, dot(nor, light_dir)); | |
float specular = pow(max(0.0, dot(reflect(light_dir, nor), eye_vec)), light.shininess); | |
gl_FragColor = | |
light.ambient + | |
light_attenuation * | |
spot * | |
(diffuse * light.diffuse + specular * light.specular); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 150 | |
layout(std140) uniform Camera | |
{ | |
mat4 view_matrix; | |
mat4 projection_matrix; | |
} camera; | |
in vec4 position; | |
in vec4 normal; | |
in vec4 texcoord; | |
in mat4 model_matrix; | |
in mat4 normal_matrix; | |
out vec4 pos; | |
out vec4 nor; | |
out vec2 uv; | |
void main() | |
{ | |
pos = camera.view_matrix * model_matrix * position; | |
nor = camera.view_matrix * normal_matrix * normal; | |
uv = texcoord.xy; | |
gl_Position = camera.projection_matrix * pos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment