Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/Tutorial/Media/DeferredShadingMedia/DeferredShading/post/hlsl/GlobalLight_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: 3.6 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: Multipass, one light
25*/
26sampler Tex0: register(s0);
27sampler Tex1: register(s1);
28
29// Attributes of light 0
30float4 lightPos0;
31float4 lightDiffuseColor0;
32float4 lightSpecularColor0;
33
34// Global parameters for lights
35struct LightGlobal
36{
37        float3 position;
38        float3 normal;
39        float3 viewDir;
40};
41
42// Current state of light
43struct LightAccum
44{
45        float3 light_diffuse;
46        float3 light_specular;
47};
48
49// Do lighting calculations for one light
50void processLight(
51        inout LightAccum accum,
52        LightGlobal global,
53        float4 lightPos,
54        float4 lightDiffuseColor,
55        float4 lightSpecularColor)
56{
57    float3 lightVec = lightPos - global.position;
58    float3 lightDir = normalize(lightVec);
59    accum.light_diffuse += max(0,dot(lightDir, global.normal)) * lightDiffuseColor;
60   
61    float3 h = normalize(global.viewDir + lightDir);
62    accum.light_specular += pow(dot(global.normal, h),32) * lightSpecularColor;
63}
64
65float4 main(float2 texCoord: TEXCOORD0, float3 projCoord: TEXCOORD1) : COLOR
66{
67    float4 a0 = tex2D(Tex0, texCoord); // Attribute 0: Diffuse color+shininess
68    float4 a1 = tex2D(Tex1, texCoord); // Attribute 1: Normal+depth
69   
70    LightGlobal global;
71   
72    // Clip fragment if depth is too close, so the skybox can be rendered on the background
73    //clip(a1.w-0.001);
74     
75    // Attributes
76    float3 colour = a0.rgb;
77    float alpha = a0.a;         // Specularity
78    float distance = a1.w;  // Distance from viewer
79    //global.normal = normalize(a1.xyz);
80    global.normal = a1.xyz;
81
82    // Acquire view space position via inverse projection transformation
83    //global.position = mul(invProj, float4(projCoord, 0, 1))*distance;
84    global.position = projCoord*distance;
85   
86    // Apply light
87    LightAccum accum;
88    accum.light_diffuse = float3(0,0,0);
89    accum.light_specular = float3(0,0,0);
90    global.viewDir = -normalize(global.position);
91   
92    processLight(accum, global, lightPos0, lightDiffuseColor0, lightSpecularColor0);
93   
94    // Calcalate total lighting for this fragment
95    float3 total_light_contrib;
96        total_light_contrib = accum.light_diffuse+alpha*accum.light_specular;
97    return float4( total_light_contrib*colour ,0);
98    //return float4(accum.light_diffuse,0);
99    //return float4(global.position/1000.0,0);
100}
101
Note: See TracBrowser for help on using the repository browser.