Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/Tutorial/Media/DeferredShadingMedia/DeferredShading/post/glsl/LightMaterial_ps.glsl @ 44

Last change on this file since 44 was 44, checked in by bknecht, 17 years ago

merge from tutorial branche to trunk

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1/******************************************************************************
2Copyright (c) W.J. van der Laan
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of
5this software  and associated documentation files (the "Software"), to deal in
6the Software without restriction, including without limitation the rights to use,
7copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8Software, and to permit persons to whom the Software is furnished to do so, subject
9to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all copies
12or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE
19SOFTWARE 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*/
26uniform sampler2D tex0;
27uniform sampler2D tex1;
28
29varying vec2 texCoord;
30varying vec3 projCoord;
31
32// World view matrix to get object position in view space
33uniform mat4 worldView;
34
35// Attributes of light
36uniform vec3 lightDiffuseColor;
37uniform vec3 lightSpecularColor;
38uniform vec3 lightFalloff;
39
40void 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
Note: See TracBrowser for help on using the repository browser.