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: Multipass, one light |
---|
25 | */ |
---|
26 | sampler Tex0: register(s0); |
---|
27 | sampler Tex1: register(s1); |
---|
28 | |
---|
29 | // Attributes of light 0 |
---|
30 | float4 lightPos0; |
---|
31 | float4 lightDiffuseColor0; |
---|
32 | float4 lightSpecularColor0; |
---|
33 | |
---|
34 | // Global parameters for lights |
---|
35 | struct LightGlobal |
---|
36 | { |
---|
37 | float3 position; |
---|
38 | float3 normal; |
---|
39 | float3 viewDir; |
---|
40 | }; |
---|
41 | |
---|
42 | // Current state of light |
---|
43 | struct LightAccum |
---|
44 | { |
---|
45 | float3 light_diffuse; |
---|
46 | float3 light_specular; |
---|
47 | }; |
---|
48 | |
---|
49 | // Do lighting calculations for one light |
---|
50 | void 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 | |
---|
65 | float4 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 | |
---|