summaryrefslogtreecommitdiffstats
path: root/src/render/shaders/es2/normaldiffusespecularmap.frag
blob: 9db1ef21ac41dee350a469ed36917e43fc76647c (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#version 120

varying vec3 lightDir;
varying vec3 viewDir;
varying vec2 texCoord;

uniform sampler2D diffuseTexture;
uniform sampler2D specularTexture;
uniform sampler2D normalTexture;

// TODO: Replace with a uniform block
uniform vec4 lightPosition = vec4(0.0, 0.0, 0.0, 1.0);
uniform vec3 lightIntensity = vec3(1.0, 1.0, 1.0);

// TODO: Replace with a struct
uniform vec3 ka;            // Ambient reflectivity
uniform float shininess;    // Specular shininess factor

vec3 adsModel( const vec3 norm, const vec3 diffuseReflect, const vec3 specular )
{
    // Reflection of light direction about normal
    vec3 r = reflect( -lightDir, norm );

    // Calculate the ambient contribution
    vec3 ambient = lightIntensity * ka;

    // Calculate the diffuse contribution
    float sDotN = max( dot( lightDir, norm ), 0.0 );
    vec3 diffuse = lightIntensity * diffuseReflect * sDotN;

    // Sum the ambient and diffuse contributions
    vec3 ambientAndDiff = ambient + diffuse;

    // Calculate the specular highlight contribution
    vec3 spec = vec3( 0.0 );
    if ( sDotN > 0.0 )
        spec = lightIntensity * ( shininess / ( 8.0 * 3.14 ) ) * pow( max( dot( r, viewDir ), 0.0 ), shininess );
    return (ambientAndDiff + spec * specular.rgb);
}

void main()
{
    // Sample the textures at the interpolated texCoords
    vec4 diffuseTextureColor = texture2D( diffuseTexture, texCoord );
    vec4 specularTextureColor = texture2D( specularTexture, texCoord );
    vec4 normal = 2.0 * texture2D( normalTexture, texCoord ) - vec4( 1.0 );

    // Calculate the lighting model
    gl_FragColor = vec4( adsModel( normalize( normal.xyz ), diffuseTextureColor.xyz, specularTextureColor.xyz ), 1.0 );
}