-
Vertex shader:
#version 130
in vec4 s_vPosition;
in vec4 s_vNormal;
uniform mat4 mM; // model matrix
uniform mat4 mV; // camera view matrix
uniform mat4 mP; // perspective matrix
uniform mat4 mRotations; // all model rotations matrix
uniform vec4 vLight; // direction of light
out vec3 fN; // output of the normal
out vec3 fL; // output of light vector
out vec3 fE; // vector between the camera and a vertex
void main () {
// Rotate the normal and pass on as vec3
fN = (mRotations*s_vNormal).xyz;
fL = vLight.xyz;
// vertex is relative to the camera:
fE = (mV*mM*s_vPosition).xyz;
// From local, to world, to camera, to NDCs
gl_Position = mP*mV*mM*s_vPosition;
}
|
-
Fragment shader:
#version 130
in vec3 fN;
in vec3 fL;
in vec3 fE; // interpolated from the vertex shader
out vec4 fColor;
void main () {
// ALWAYS normalize the vectors after interpolation:
vec3 N = normalize(fN);
vec3 L = normalize(fL);
// Reverse E: becomes vector from pixel to camera:
vec3 E = normalize(-fE);
vec3 H = normalize( L + E ); // Create the half vector
float diffuse_intensity = max(dot(N, L), 0.0);
vec4 diffuse_color = vec4( 0.5, 0.2, 0.0, 1.0 ); // dark orange
diffuse_color = diffuse_color * diffuse_intensity;
float shininess = 30.0;
float specular_intensity = pow(max(dot(N, H), 0.0), shininess);
vec4 specular_color = vec4( 0.9, 0.1, 0.1, 1.0 ); // red
specular_color = specular_intensity * specular_color;
//fColor = diffuse_color; // only diffuse light
//fColor = specular_color; // only specular light
// dark orange with bright specular red:
fColor = diffuse_color + specular_color;
}
|