diff --git a/f_shader.glsl b/f_shader.glsl new file mode 100644 index 0000000..a548c54 --- /dev/null +++ b/f_shader.glsl @@ -0,0 +1,33 @@ + +uniform vec4 ambient, diffuse, specular; +uniform float shininess; + +varying vec3 pos_i; +varying vec3 normal_i; + +void main(void) +{ + vec3 n, lightDir; + vec4 color; + float NdotL, RdotEye; + + lightDir = normalize(vec3(-0.1, 0, -0.9)); + color = vec4(0.2, 0.2, 0.2, 1.0) * ambient; /* ambient light */ + n = normalize(normal_i); + + NdotL = max(dot(n, -lightDir), 0.0); + + if (NdotL > 0.0) + { + /* diffuse component */ + color += diffuse * NdotL; + /* specular component */ + RdotEye = dot(normalize(-pos_i), normalize(reflect(-lightDir, n))); + if (RdotEye > 0.0) + { + color += clamp(specular * pow(RdotEye, shininess), 0.0, 1.0); + } + } + + gl_FragColor = color; +} diff --git a/f_tex_shader.glsl b/f_tex_shader.glsl new file mode 100644 index 0000000..d561d66 --- /dev/null +++ b/f_tex_shader.glsl @@ -0,0 +1,35 @@ + +uniform vec4 ambient, specular; +uniform float shininess; +uniform sampler2D tex; + +varying vec3 pos_i; +varying vec3 normal_i; +varying vec2 tex_coord_i; + +void main(void) +{ + vec3 n, lightDir; + vec4 color; + float NdotL, RdotEye; + + lightDir = normalize(vec3(-0.1, 0, -0.9)); + color = vec4(0.2, 0.2, 0.2, 1.0) * ambient; /* ambient light */ + n = normalize(normal_i); + + NdotL = max(dot(n, -lightDir), 0.0); + + if (NdotL > 0.0) + { + /* diffuse component */ + color += texture2D(tex, tex_coord_i) * NdotL; + /* specular component */ + RdotEye = dot(normalize(-pos_i), normalize(reflect(-lightDir, n))); + if (RdotEye > 0.0) + { + color += clamp(specular * pow(RdotEye, shininess), 0.0, 1.0); + } + } + + gl_FragColor = color; +} diff --git a/v_shader.glsl b/v_shader.glsl new file mode 100644 index 0000000..7d88a5f --- /dev/null +++ b/v_shader.glsl @@ -0,0 +1,13 @@ + +attribute vec3 pos; +attribute vec3 normal; + +varying vec3 pos_i; +varying vec3 normal_i; + +void main(void) +{ + gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1); + pos_i = gl_Position.xyz; + normal_i = gl_NormalMatrix * normal; +} diff --git a/v_tex_shader.glsl b/v_tex_shader.glsl new file mode 100644 index 0000000..9be4096 --- /dev/null +++ b/v_tex_shader.glsl @@ -0,0 +1,16 @@ + +attribute vec3 pos; +attribute vec3 normal; +attribute vec2 tex_coord; + +varying vec3 pos_i; +varying vec3 normal_i; +varying vec2 tex_coord_i; + +void main(void) +{ + gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1); + pos_i = gl_Position.xyz; + tex_coord_i = tex_coord; + normal_i = gl_NormalMatrix * normal; +}