summaryrefslogtreecommitdiffstats
path: root/openGL_tutorial/src/examples/lighting/lightingFragmentShader.fsh
blob: 77439f2b1dd41a6720fba0f610ea62ad752d4b74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#version 130

//! [0]
uniform vec4 ambientColor;
uniform vec4 diffuseColor;
uniform vec4 specularColor;
uniform float ambientReflection;
uniform float diffuseReflection;
uniform float specularReflection;
uniform float shininess;
uniform sampler2D texture;

in vec3 varyingNormal;
in vec3 varyingLightDirection;
in vec3 varyingViewerDirection;
in vec2 varyingTextureCoordinate;

out vec4 fragColor;

void main(void)
{
    vec3 normal = normalize(varyingNormal);
    vec3 lightDirection = normalize(varyingLightDirection);
    vec3 viewerDirection = normalize(varyingViewerDirection);
    vec4 ambientIllumination = ambientReflection * ambientColor;
    vec4 diffuseIllumination = diffuseReflection * max(0.0, dot(lightDirection, normal)) * diffuseColor;
    vec4 specularIllumination = specularReflection * pow(max(0.0, 
                                                             dot(-reflect(lightDirection, normal), viewerDirection)
                                                             ), shininess) * specularColor;
    fragColor = texture2D(texture, 
                          varyingTextureCoordinate) * (ambientIllumination + diffuseIllumination
                          ) + specularIllumination;
}
//! [0]