Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/Tutorial/Media/DeferredShadingMedia/DeferredShading/post/hlsl/SinglePass_ps.hlsl @ 25

Last change on this file since 25 was 25, checked in by nicolasc, 17 years ago

added Media and Config

  • Property svn:executable set to *
File size: 4.5 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: Single pass
25*/
26sampler Tex0: register(s0);
27sampler Tex1: register(s1);
28
29float4x4 proj;
30
31float4 ambientColor;
32// Attributes of light 0
33float4 lightPos0;
34float4 lightDiffuseColor0;
35float4 lightSpecularColor0;
36// Attributes of light 1
37float4 lightPos1;
38float4 lightDiffuseColor1;
39float4 lightSpecularColor1;
40
41// Global parameters for lights
42struct LightGlobal
43{
44        float3 position;
45        float3 normal;
46        float3 viewDir;
47};
48
49// Current state of light
50struct LightAccum
51{
52        float3 light_diffuse;
53        float3 light_specular;
54};
55
56// Do lighting calculations for one light
57void processLight(
58        inout LightAccum accum,
59        LightGlobal global,
60        float4 lightPos,
61        float4 lightDiffuseColor,
62        float4 lightSpecularColor)
63{
64    float3 lightVec = lightPos - global.position;
65    float3 lightDir = normalize(lightVec);
66    accum.light_diffuse += max(0,dot(lightDir, global.normal)) * lightDiffuseColor;
67   
68    float3 h = normalize(global.viewDir + lightDir);
69    accum.light_specular += pow(dot(global.normal, h),32) * lightSpecularColor;
70}
71
72struct POUTPUT
73{
74        float4 colour: COLOR;
75        float depth: DEPTH;
76};
77
78POUTPUT main(float2 texCoord: TEXCOORD0, float3 projCoord: TEXCOORD1)
79{
80        POUTPUT o;
81       
82    float4 a0 = tex2D(Tex0, texCoord); // Attribute 0: Diffuse color+shininess
83    float4 a1 = tex2D(Tex1, texCoord); // Attribute 1: Normal+depth
84   
85    LightGlobal global;
86   
87    // Clip fragment if depth is too far, so the skybox can be rendered on the background
88    clip(a1.w-0.001);
89     
90    // Attributes
91    float3 colour = a0.rgb;
92    float alpha = a0.a;         // Specularity
93    float distance = a1.w;  // Distance from viewer -- is zero if no lighting wanted
94    //global.normal = normalize(a1.xyz); // normalizing done already
95    global.normal = a1.xyz;
96
97    // Acquire view space position via inverse projection transformation
98    //global.position = mul(invProj, float4(projCoord, 0, 1))*distance;
99    // Acquire view space position via inverse projection transformation
100    //float4 tpos;
101    //tpos = float4(projCoord, distance, 1.0);
102    //tpos = mul(invProj, tpos);
103    //tpos = tpos / tpos.w;
104    //global.position = tpos;
105    //global.position = float3(
106        //      invProj[0][0], // X vector component from X
107        //      invProj[1][1], // Y vector component from Y
108        //      invProj[2][3]  // Z vector component from W
109        //)*projCoord*distance;
110        global.position = projCoord*distance;
111   
112    // Apply light
113    LightAccum accum;
114    accum.light_diffuse = float3(0,0,0);
115    accum.light_specular = float3(0,0,0);
116    global.viewDir = -normalize(global.position);
117   
118    processLight(accum, global, lightPos0, lightDiffuseColor0, lightSpecularColor0);
119    processLight(accum, global, lightPos1, lightDiffuseColor1, lightSpecularColor1);
120   
121    // Calcalate total lighting for this fragment
122    float3 total_light_contrib;
123        total_light_contrib = ambientColor+accum.light_diffuse+alpha*accum.light_specular;
124       
125    o.colour = float4( total_light_contrib*colour ,0);
126    // Depth buffer value
127    // Transfering depth makes it possible to render particle effects and other transparent
128    // things unaffected by light in the postprocessing phase.
129    o.depth = projCoord.z*proj[2][2] + proj[2][3]/distance;
130    return o;
131}
132
Note: See TracBrowser for help on using the repository browser.