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: Single pass
|
---|
25 | */
|
---|
26 | sampler Tex0: register(s0);
|
---|
27 | sampler Tex1: register(s1);
|
---|
28 |
|
---|
29 | float4x4 proj;
|
---|
30 |
|
---|
31 | float4 ambientColor;
|
---|
32 | // Attributes of light 0
|
---|
33 | float4 lightPos0;
|
---|
34 | float4 lightDiffuseColor0;
|
---|
35 | float4 lightSpecularColor0;
|
---|
36 | // Attributes of light 1
|
---|
37 | float4 lightPos1;
|
---|
38 | float4 lightDiffuseColor1;
|
---|
39 | float4 lightSpecularColor1;
|
---|
40 |
|
---|
41 | // Global parameters for lights
|
---|
42 | struct LightGlobal
|
---|
43 | {
|
---|
44 | float3 position;
|
---|
45 | float3 normal;
|
---|
46 | float3 viewDir;
|
---|
47 | };
|
---|
48 |
|
---|
49 | // Current state of light
|
---|
50 | struct LightAccum
|
---|
51 | {
|
---|
52 | float3 light_diffuse;
|
---|
53 | float3 light_specular;
|
---|
54 | };
|
---|
55 |
|
---|
56 | // Do lighting calculations for one light
|
---|
57 | void 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 |
|
---|
72 | struct POUTPUT
|
---|
73 | {
|
---|
74 | float4 colour: COLOR;
|
---|
75 | float depth: DEPTH;
|
---|
76 | };
|
---|
77 |
|
---|
78 | POUTPUT 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 |
|
---|