| 1 |
#version 150 core |
| 2 |
|
| 3 |
uniform vec4 lightPosition = vec4(1.0, 1.0, 0.0, 1.0); |
| 4 |
uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0); |
| 5 |
|
| 6 |
uniform vec3 ka; // Ambient reflectivity |
| 7 |
uniform vec3 ks = vec3(1.0, 1.0, 1.0); // Specular reflectivity |
| 8 |
uniform float shininess = 150.0; // Specular shininess factor |
| 9 |
|
| 10 |
in vec3 kd; // Diffuse reflectivity |
| 11 |
in vec3 position; |
| 12 |
in vec3 normal; |
| 13 |
|
| 14 |
out vec4 fragColor; |
| 15 |
|
| 16 |
vec3 adsModel( const in vec3 pos, const in vec3 n ) |
| 17 |
{ |
| 18 |
// Calculate the vector from the light to the fragment |
| 19 |
vec3 s = normalize( vec3( lightPosition ) - pos ); |
| 20 |
|
| 21 |
// Calculate the vector from the fragment to the eye position |
| 22 |
// (origin since this is in "eye" or "camera" space) |
| 23 |
vec3 v = normalize( -pos ); |
| 24 |
|
| 25 |
// Reflect the light beam using the normal at this fragment |
| 26 |
vec3 r = reflect( -s, n ); |
| 27 |
|
| 28 |
// Calculate the diffuse component |
| 29 |
float diffuse = max( dot( s, n ), 0.0 ); |
| 30 |
|
| 31 |
// Calculate the specular component |
| 32 |
float specular = 0.0; |
| 33 |
if ( dot( s, n ) > 0.0 ) |
| 34 |
specular = pow( max( dot( r, v ), 0.0 ), shininess ); |
| 35 |
|
| 36 |
// Combine the ambient, diffuse and specular contributions |
| 37 |
return lightIntensity * ( ka + kd * diffuse + ks * specular ); |
| 38 |
} |
| 39 |
|
| 40 |
void main() |
| 41 |
{ |
| 42 |
fragColor = vec4( adsModel( position, normalize( normal ) ), 1.0 ); |
| 43 |
} |