source: OGRE/trunk/ogrenew/Samples/Media/DeferredShadingMedia/DeferredShading/post/glsl/SinglePass_ps.glsl @ 692

Revision 692, 3.7 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

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: Single pass
25*/
26uniform sampler2D tex0;
27uniform sampler2D tex1;
28
29varying vec2 texCoord;
30varying vec3 projCoord;
31
32uniform mat4 proj;
33
34uniform vec3 ambientColor;
35// Attributes of light 0
36uniform vec4 lightPos0;
37uniform vec3 lightDiffuseColor0;
38uniform vec3 lightSpecularColor0;
39// Attributes of light 1
40uniform vec4 lightPos1;
41uniform vec3 lightDiffuseColor1;
42uniform vec3 lightSpecularColor1;
43
44// Global parameters for lights
45struct LightGlobal
46{
47        vec3 position;
48        vec3 normal;
49        vec3 viewDir;
50};
51
52// Current state of light
53struct LightAccum
54{
55        vec3 light_diffuse;
56        vec3 light_specular;
57};
58
59// Do lighting calculations for one light
60void processLight(
61        inout LightAccum accum,
62        LightGlobal global,
63        vec4 lightPos,
64        vec3 lightDiffuseColor,
65        vec3 lightSpecularColor)
66{
67    vec3 lightVec = vec3(lightPos) - global.position;
68    vec3 lightDir = normalize(lightVec);
69    accum.light_diffuse += max(0.0,dot(lightDir, global.normal)) * lightDiffuseColor;
70   
71    vec3 h = normalize(global.viewDir + lightDir);
72    accum.light_specular += pow(dot(global.normal, h),32.0) * lightSpecularColor;
73}
74
75void main()
76{
77        vec4 a0 = texture2D(tex0, texCoord); // Attribute 0: Diffuse color+shininess
78    vec4 a1 = texture2D(tex1, texCoord); // Attribute 1: Normal+depth
79   
80    // Clip fragment if depth is too far, so the skybox can be rendered on the background
81    if(a1.w==0.0)
82                discard;
83     
84    // Attributes
85    vec3 colour = a0.rgb;
86    float alpha = a0.a;         // Specularity
87    float distance = a1.w;  // Distance from viewer -- is zero if no lighting wanted
88   
89    LightGlobal global;
90    global.normal = a1.xyz;
91        global.position = projCoord*distance;
92   
93    // Apply light
94    LightAccum accum;
95    accum.light_diffuse = vec3(0,0,0);
96    accum.light_specular = vec3(0,0,0);
97    global.viewDir = -normalize(global.position);
98   
99    processLight(accum, global, lightPos0, lightDiffuseColor0, lightSpecularColor0);
100    processLight(accum, global, lightPos1, lightDiffuseColor1, lightSpecularColor1);
101   
102    // Calcalate total lighting for this fragment
103    vec3 total_light_contrib;
104        total_light_contrib = ambientColor+accum.light_diffuse+alpha*accum.light_specular;
105       
106        // Calculate colour of fragment
107    gl_FragColor = vec4( total_light_contrib*colour ,0);
108   
109    // Calculate depth of fragment; GL requires a 2.0* here as the range is [-1, 1]
110    gl_FragDepth = projCoord.z*proj[2][2] + proj[3][2]/(2.0*distance);
111}
Note: See TracBrowser for help on using the repository browser.