1 | /******************************************************************************
|
---|
2 | Copyright (c) W.J. van der Laan
|
---|
3 |
|
---|
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of
|
---|
5 | this software and associated documentation files (the "Software"), to deal in
|
---|
6 | the Software without restriction, including without limitation the rights to use,
|
---|
7 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
---|
8 | Software, and to permit persons to whom the Software is furnished to do so, subject
|
---|
9 | to the following conditions:
|
---|
10 |
|
---|
11 | The above copyright notice and this permission notice shall be included in all copies
|
---|
12 | or substantial portions of the Software.
|
---|
13 |
|
---|
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
---|
15 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
---|
16 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
17 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
---|
18 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE
|
---|
19 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
20 | ******************************************************************************/
|
---|
21 | /** Deferred shading framework
|
---|
22 | // W.J. :wumpus: van der Laan 2005 //
|
---|
23 |
|
---|
24 | Post shader: Light geometry material
|
---|
25 | */
|
---|
26 | uniform sampler2D tex0;
|
---|
27 | uniform sampler2D tex1;
|
---|
28 |
|
---|
29 | varying vec2 texCoord;
|
---|
30 | varying vec3 projCoord;
|
---|
31 |
|
---|
32 | // World view matrix to get object position in view space
|
---|
33 | uniform mat4 worldView;
|
---|
34 |
|
---|
35 | // Attributes of light
|
---|
36 | uniform vec3 lightDiffuseColor;
|
---|
37 | uniform vec3 lightSpecularColor;
|
---|
38 | uniform vec3 lightFalloff;
|
---|
39 |
|
---|
40 | void main()
|
---|
41 | {
|
---|
42 | vec4 a0 = texture2D(tex0, texCoord); // Attribute 0: Diffuse color+shininess
|
---|
43 | vec4 a1 = texture2D(tex1, texCoord); // Attribute 1: Normal+depth
|
---|
44 |
|
---|
45 | // Attributes
|
---|
46 | vec3 colour = a0.rgb;
|
---|
47 | float alpha = a0.a; // Specularity
|
---|
48 | float distance = a1.w; // Distance from viewer (w)
|
---|
49 | vec3 normal = a1.xyz;
|
---|
50 |
|
---|
51 | // Calculate position of texel in view space
|
---|
52 | vec3 position = projCoord*distance;
|
---|
53 |
|
---|
54 | // Extract position in view space from worldView matrix
|
---|
55 | //vec3 lightPos = vec3(worldView[0][3],worldView[1][3],worldView[2][3]);
|
---|
56 | vec3 lightPos = vec3(worldView[3][0],worldView[3][1],worldView[3][2]);
|
---|
57 |
|
---|
58 | // Calculate light direction and distance
|
---|
59 | vec3 lightVec = lightPos - position;
|
---|
60 | float len_sq = dot(lightVec, lightVec);
|
---|
61 | float len = sqrt(len_sq);
|
---|
62 | vec3 lightDir = lightVec/len;
|
---|
63 |
|
---|
64 | /// Calculate attenuation
|
---|
65 | float attenuation = dot(lightFalloff, vec3(1, len, len_sq));
|
---|
66 |
|
---|
67 | /// Calculate diffuse colour
|
---|
68 | vec3 light_diffuse = max(0.0,dot(lightDir, normal)) * lightDiffuseColor;
|
---|
69 |
|
---|
70 | /// Calculate specular component
|
---|
71 | vec3 viewDir = -normalize(position);
|
---|
72 | vec3 h = normalize(viewDir + lightDir);
|
---|
73 | vec3 light_specular = pow(dot(normal, h),32.0) * lightSpecularColor;
|
---|
74 |
|
---|
75 | // Calcalate total lighting for this fragment
|
---|
76 | vec3 total_light_contrib;
|
---|
77 | total_light_contrib = light_diffuse;
|
---|
78 | // Uncomment next line if specular desired
|
---|
79 | //total_light_contrib += alpha * light_specular;
|
---|
80 |
|
---|
81 | gl_FragColor = vec4(total_light_contrib*colour/attenuation, 0);
|
---|
82 | //gl_FragColor = vec4(1.0/attenuation, 0.0,0.0,0.0);
|
---|
83 | //gl_FragColor = vec4(a1.xyz, 0.0);
|
---|
84 | }
|
---|
85 |
|
---|