1 | //--------------------------------------------------------------------------------------
|
---|
2 | // File: EnvMap.fx
|
---|
3 | //
|
---|
4 | // The effect file for the OptimizedMesh sample.
|
---|
5 | //
|
---|
6 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
7 | //--------------------------------------------------------------------------------------
|
---|
8 |
|
---|
9 |
|
---|
10 | /// size of the cube map taken from the reference point of the object
|
---|
11 | #define CUBEMAP_SIZE 128
|
---|
12 | /// size of the cube map for diffuse/glossy reflections
|
---|
13 | int LR_CUBEMAP_SIZE;
|
---|
14 | #define PI 3.14159f
|
---|
15 |
|
---|
16 |
|
---|
17 | //--------------------------------------------------------------------------------------
|
---|
18 | // Global variables
|
---|
19 | //--------------------------------------------------------------------------------------
|
---|
20 |
|
---|
21 |
|
---|
22 | float4x4 World; ///< World matrix for the current object
|
---|
23 | float4x4 WorldIT; ///< World matrix IT (inverse transposed) to transform surface normals of the current object
|
---|
24 | float4x4 WorldView; ///< World * View matrix
|
---|
25 | //float4x4 WorldViewIT; ///< World * View IT (inverse transposed) to transform surface normals of the current object
|
---|
26 | float4x4 WorldViewProjection; ///< World * View * Projection matrix
|
---|
27 |
|
---|
28 | float texel_size; ///< upload this constant every time the viewport changes
|
---|
29 |
|
---|
30 | float4 eyePos; ///< current eye (camera) position
|
---|
31 | float4 reference_pos; ///< Reference point for the last cube map generation.
|
---|
32 |
|
---|
33 | int nFace; ///<
|
---|
34 | int iShowCubeMap; ///<
|
---|
35 | float4 objColor;
|
---|
36 |
|
---|
37 | float intensity, shininess, brightness;
|
---|
38 |
|
---|
39 |
|
---|
40 | //--------------------------------------------------------------------------------------
|
---|
41 | // Textures & texture samplers
|
---|
42 | //--------------------------------------------------------------------------------------
|
---|
43 |
|
---|
44 |
|
---|
45 | texture EnvironmentMap, SmallEnvironmentMap, PreconvolvedEnvironmentMap, Decoration;
|
---|
46 |
|
---|
47 | sampler EnvironmentMapSampler = sampler_state
|
---|
48 | {
|
---|
49 | /*MinFilter = LINEAR;
|
---|
50 | MagFilter = LINEAR;
|
---|
51 | MipFilter = LINEAR;*/
|
---|
52 | Texture = <EnvironmentMap>;
|
---|
53 | AddressU = WRAP;
|
---|
54 | AddressV = WRAP;
|
---|
55 | };
|
---|
56 |
|
---|
57 | sampler PreconvolvedEnvironmentMapSampler = sampler_state
|
---|
58 | {
|
---|
59 | MinFilter = LINEAR;
|
---|
60 | MagFilter = LINEAR;
|
---|
61 | //MipFilter = LINEAR;
|
---|
62 | Texture = <PreconvolvedEnvironmentMap>;
|
---|
63 | AddressU = WRAP;
|
---|
64 | AddressV = WRAP;
|
---|
65 | };
|
---|
66 |
|
---|
67 | sampler SmallEnvironmentMapSampler = sampler_state
|
---|
68 | {
|
---|
69 | // MinFilter = Point;
|
---|
70 | // MagFilter = Point;
|
---|
71 |
|
---|
72 | MinFilter = LINEAR;
|
---|
73 | MagFilter = LINEAR;
|
---|
74 |
|
---|
75 | //MipFilter = Point;
|
---|
76 | Texture = <SmallEnvironmentMap>;
|
---|
77 | AddressU = WRAP;
|
---|
78 | AddressV = WRAP;
|
---|
79 | };
|
---|
80 |
|
---|
81 | sampler DecorationSampler = sampler_state
|
---|
82 | {
|
---|
83 | Texture = <Decoration>;
|
---|
84 | MinFilter = LINEAR;
|
---|
85 | MagFilter = LINEAR;
|
---|
86 | //MipFilter = LINEAR;
|
---|
87 | AddressU = CLAMP; //WRAP;
|
---|
88 | AddressV = CLAMP; //WRAP;
|
---|
89 | };
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 | //--------------------------------------------------------------------------------------
|
---|
94 | // Shader programs
|
---|
95 | //--------------------------------------------------------------------------------------
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 | void ReduceTextureVS( float4 position : POSITION,
|
---|
100 | float4 color0 : COLOR0,
|
---|
101 | float3 Normal : NORMAL,
|
---|
102 | float2 Tex : TEXCOORD0,
|
---|
103 | out float4 hposition : POSITION,
|
---|
104 | out float4 color : COLOR0,
|
---|
105 | out float2 oTex : TEXCOORD0,
|
---|
106 | out float4 pos : TEXCOORD1 )
|
---|
107 | {
|
---|
108 | pos = position;
|
---|
109 | hposition = pos;
|
---|
110 | color = color0;
|
---|
111 | oTex = Tex;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | \brief Downsamples a cube map face.
|
---|
116 | */
|
---|
117 | #define _ReduceTexturePS( M ) \
|
---|
118 | float4 ReduceTexture##M##PS( float2 Tex : TEXCOORD0, \
|
---|
119 | float4 pos : TEXCOORD1, \
|
---|
120 | float4 color0 : COLOR0 ) : COLOR0 \
|
---|
121 | { \
|
---|
122 | /* offset to texel center */ \
|
---|
123 | pos.xy += float2(1/(float)CUBEMAP_SIZE, -1/(float)CUBEMAP_SIZE); \
|
---|
124 | /* transform position into texture coord */ \
|
---|
125 | float2 tpos = pos.xy/2+0.5; /* rescale from -1..1 into range 0..1 */ \
|
---|
126 | tpos.y = 1-tpos.y; \
|
---|
127 | \
|
---|
128 | float2 t; \
|
---|
129 | float4 color = 0; \
|
---|
130 | const int RATE = CUBEMAP_SIZE / M; \
|
---|
131 | \
|
---|
132 | for (int i = 0; i < RATE; i++) \
|
---|
133 | for (int j = 0; j < RATE; j++) \
|
---|
134 | { \
|
---|
135 | t.x = tpos.x + i/(float)CUBEMAP_SIZE; \
|
---|
136 | t.y = tpos.y + j/(float)CUBEMAP_SIZE; \
|
---|
137 | color += tex2D(DecorationSampler, t) / (RATE * RATE); \
|
---|
138 | } \
|
---|
139 | return color; \
|
---|
140 | } // end of macro definition
|
---|
141 |
|
---|
142 | _ReduceTexturePS( 2 );
|
---|
143 | _ReduceTexturePS( 4 );
|
---|
144 | _ReduceTexturePS( 8 );
|
---|
145 | _ReduceTexturePS( 16 );
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 | //--------------------------------------------------------------------------------------
|
---|
150 | // Method #0: CLASSIC (pre-convolved)
|
---|
151 | //--------------------------------------------------------------------------------------
|
---|
152 |
|
---|
153 |
|
---|
154 |
|
---|
155 | /// \brief Returns the precalculated contribution of a texel with regard to the specified query direction.
|
---|
156 | ///
|
---|
157 | /// \param q <b>query direction</b> (i.e. surface normal in diffuse case, ideal reflection direction in specular case).
|
---|
158 | /// \param L vector pointing to the texel center
|
---|
159 | float4 GetContr(float3 q, float3 L)
|
---|
160 | // Lin * a * ( dw )
|
---|
161 | // -- actually, dw is calculated by the caller --
|
---|
162 | {
|
---|
163 | //float shininess = 1;
|
---|
164 | float fcos = max(dot(L, q), 0);
|
---|
165 | // diffuse
|
---|
166 | if (shininess <= 0)
|
---|
167 | return 0.2 * fcos * texCUBE( SmallEnvironmentMapSampler, L);
|
---|
168 | else
|
---|
169 | {
|
---|
170 | // some ad-hoc formula to avoid darkening
|
---|
171 | float brightness = (pow(shininess,0.8)*0.2);
|
---|
172 | return brightness * pow(fcos, shininess) * texCUBE( SmallEnvironmentMapSampler, L);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /// \brief Input for vertex shader ConvolutionVS().
|
---|
177 | struct _ConvolutionVS_input {
|
---|
178 | float4 Position : POSITION;
|
---|
179 | };
|
---|
180 |
|
---|
181 | /// \brief Input for pixel shader ::_ConvolutionPS().
|
---|
182 | struct _ConvolutionVS_output {
|
---|
183 | float4 hPosition : POSITION;
|
---|
184 | float3 Position : TEXCOORD0;
|
---|
185 | };
|
---|
186 |
|
---|
187 | _ConvolutionVS_output ConvolutionVS(_ConvolutionVS_input IN) {
|
---|
188 | _ConvolutionVS_output OUT;
|
---|
189 | OUT.hPosition = IN.Position;
|
---|
190 |
|
---|
191 | float2 pos = IN.Position.xy; // -1..1
|
---|
192 |
|
---|
193 | pos.x += 0.5f / LR_CUBEMAP_SIZE;
|
---|
194 | pos.y -= 0.5f / LR_CUBEMAP_SIZE;
|
---|
195 |
|
---|
196 | if (nFace == 0) OUT.Position = float3(1, pos.y, -pos.x);
|
---|
197 | if (nFace == 1) OUT.Position = float3(-1, pos.y, pos.x);
|
---|
198 | if (nFace == 2) OUT.Position = float3(pos.x, 1, -pos.y);
|
---|
199 | if (nFace == 3) OUT.Position = float3(pos.x,-1, pos.y);
|
---|
200 | if (nFace == 4) OUT.Position = float3(pos.xy, 1);
|
---|
201 | if (nFace == 5) OUT.Position = float3(-pos.x, pos.y,-1);
|
---|
202 |
|
---|
203 | return OUT;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | \brief Convolves the values of a cube map of resoultion MxM.
|
---|
208 |
|
---|
209 | Calculates the diffuse/specular irradiance map of resolution #LR_CUBEMAP_SIZE by summing up the contributions of all cube map texels
|
---|
210 | with regard to the current query direction.
|
---|
211 | */
|
---|
212 |
|
---|
213 | #define _ConvolutionPS( M ) \
|
---|
214 | float4 Convolution##M##PS( _ConvolutionVS_output IN ) : COLOR \
|
---|
215 | { \
|
---|
216 | /* input position = query direction for the result */ \
|
---|
217 | float3 q = normalize( IN.Position ); \
|
---|
218 | float4 color = 0; \
|
---|
219 | \
|
---|
220 | for (int i = 0; i < M; i++) \
|
---|
221 | for (int j = 0; j < M; j++) \
|
---|
222 | { \
|
---|
223 | float u = (i+0.5) / (float)M; \
|
---|
224 | float v = (j+0.5) / (float)M; \
|
---|
225 | float3 pos = float3( 2*u-1, 1-2*v, 1 ); \
|
---|
226 | \
|
---|
227 | float r = length(pos); \
|
---|
228 | pos /= r; \
|
---|
229 | \
|
---|
230 | float4 dcolor = 0; \
|
---|
231 | float3 L; \
|
---|
232 | L = float3(pos.z, pos.y, -pos.x); dcolor += GetContr( q, L ); \
|
---|
233 | L = float3(-pos.z, pos.y, pos.x); dcolor += GetContr( q, L ); \
|
---|
234 | L = float3(pos.x, pos.z, -pos.y); dcolor += GetContr( q, L ); \
|
---|
235 | L = float3(pos.x, -pos.z, pos.y); dcolor += GetContr( q, L ); \
|
---|
236 | L = float3(pos.x, pos.y, pos.z); dcolor += GetContr( q, L ); \
|
---|
237 | L = float3(-pos.x, pos.y, -pos.z); dcolor += GetContr( q, L ); \
|
---|
238 | \
|
---|
239 | float dw = 4 / (r*r*r); \
|
---|
240 | color += dcolor * dw; \
|
---|
241 | } \
|
---|
242 | \
|
---|
243 | return color / (M * M); \
|
---|
244 | } /* end of macro definition */
|
---|
245 |
|
---|
246 | _ConvolutionPS( 2 );
|
---|
247 | _ConvolutionPS( 4 );
|
---|
248 | _ConvolutionPS( 8 );
|
---|
249 | _ConvolutionPS( 16 );
|
---|
250 |
|
---|
251 | /// \brief Input for vertex shader EnvMapVS().
|
---|
252 | struct _EnvMapVS_input
|
---|
253 | {
|
---|
254 | float4 Position : POSITION;
|
---|
255 | float3 Normal : NORMAL;
|
---|
256 | float2 TexCoord : TEXCOORD0;
|
---|
257 | };
|
---|
258 |
|
---|
259 | /// \brief Input for pixel shaders EnvMapDiffuseClassicPS(), ::_EnvMapDiffuseLocalizedPS(), EnvMapDiffuseLocalized5TexPS().
|
---|
260 | struct _EnvMapVS_output
|
---|
261 | {
|
---|
262 | float4 hPosition : POSITION;
|
---|
263 | float2 TexCoord : TEXCOORD0;
|
---|
264 | float3 Normal : TEXCOORD1;
|
---|
265 | float3 View : TEXCOORD2;
|
---|
266 | float3 Position : TEXCOORD3;
|
---|
267 | };
|
---|
268 |
|
---|
269 | _EnvMapVS_output EnvMapVS( _EnvMapVS_input IN )
|
---|
270 | {
|
---|
271 | _EnvMapVS_output OUT;
|
---|
272 |
|
---|
273 | OUT.Position = mul( IN.Position, World ).xyz; // scale & offset
|
---|
274 | OUT.View = normalize( OUT.Position - eyePos );
|
---|
275 | //OUT.Normal = IN.Normal;
|
---|
276 | OUT.Normal = mul( IN.Normal, WorldIT ).xyz; // allow distortion/rotation
|
---|
277 |
|
---|
278 | OUT.TexCoord = IN.TexCoord;
|
---|
279 |
|
---|
280 | OUT.hPosition = mul( IN.Position, WorldViewProjection );
|
---|
281 | return OUT;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /// \brief Determines diffuse or specular illumination with a single lookup into #PreconvolvedEnvironmentMap.
|
---|
285 | /// PreconvolvedEnvironmentMap is bound to EnvMap::pCubeTexturePreConvolved (cube map of resolution #LR_CUBEMAP_SIZE).
|
---|
286 | float4 EnvMapDiffuseClassicPS( _EnvMapVS_output IN ) : COLOR
|
---|
287 | {
|
---|
288 | IN.View = normalize( IN.View );
|
---|
289 | IN.Normal = normalize( IN.Normal );
|
---|
290 |
|
---|
291 | float3 R = reflect(IN.View, IN.Normal);
|
---|
292 |
|
---|
293 | if (shininess <= 0) // diffuse
|
---|
294 | return intensity * texCUBE(PreconvolvedEnvironmentMapSampler, IN.Normal) *2;
|
---|
295 | else // specular
|
---|
296 | return intensity * texCUBE(PreconvolvedEnvironmentMapSampler, R) *2;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 |
|
---|
301 | //--------------------------------------------------------------------------------------
|
---|
302 | // Method #1-#2: OUR METHOD
|
---|
303 | //--------------------------------------------------------------------------------------
|
---|
304 |
|
---|
305 |
|
---|
306 |
|
---|
307 | /// \brief Calculates the contribution of a single texel of #SmallEnvironmentMap to the illumination of the shaded point.
|
---|
308 | /// To compute reflectivity, precalculated integral values are used.
|
---|
309 | ///
|
---|
310 | /// \param L vector pointing to the center of the texel under examination. We assume that the largest coordinate component
|
---|
311 | /// of L is equal to one, i.e. L points to the face of a cube of edge length of 2.
|
---|
312 | /// \param pos is the position of the shaded point
|
---|
313 | /// \param N is the surface normal at the shaded point
|
---|
314 | /// \param V is the viewing direction at the shaded point
|
---|
315 |
|
---|
316 |
|
---|
317 | float4 GetContr(int M, float3 L, float3 pos, float3 N, float3 V) // Phong-Blinn
|
---|
318 | // L is strictly non-normalized
|
---|
319 | {
|
---|
320 | float l = length(L);
|
---|
321 | L = normalize(L);
|
---|
322 |
|
---|
323 | //Lin
|
---|
324 | float4 Lin = texCUBE(SmallEnvironmentMapSampler, L);
|
---|
325 |
|
---|
326 | //dw
|
---|
327 | float dw = 4 / (M*M*l*l*l + 4/2/3.1416f);
|
---|
328 |
|
---|
329 | float dws = dw;
|
---|
330 |
|
---|
331 | //r
|
---|
332 | float doy = texCUBE(SmallEnvironmentMapSampler, L).a;
|
---|
333 | float dxy = length(pos - L * doy);
|
---|
334 |
|
---|
335 | //dws
|
---|
336 | //dws = (doy*doy * dw) / (dxy*dxy*(1 - dw/3.1416f) + doy*doy*dw/3.1416f); // localization:
|
---|
337 | //dws = (doy*doy * dw) / (dxy*dxy*(1 - dw/2/3.1416f) + doy*doy*dw/2/3.1416f); // localization:
|
---|
338 |
|
---|
339 | float den = 1 + doy*doy / (dxy*dxy) * ( (2*3.1416f)*(2*3.1416f) / ((2*3.1416f-dw)*(2*3.1416f-dw)) - 1 );
|
---|
340 | dws = 2*3.1416f * (1 - 1/sqrt(den));
|
---|
341 |
|
---|
342 | float3 LL = L * doy - pos; // L should start from the object (and not from the reference point) !!!
|
---|
343 | LL = normalize(LL);
|
---|
344 |
|
---|
345 | float3 H = normalize(L + V); // halfway vector
|
---|
346 | float3 R = reflect(-V, N); // reflection vector
|
---|
347 |
|
---|
348 | // from texture
|
---|
349 |
|
---|
350 | float4 color = 0;
|
---|
351 |
|
---|
352 | float cos_value;
|
---|
353 | if (shininess <= 0)
|
---|
354 | cos_value = dot(N,L); // diffuse
|
---|
355 | else cos_value = dot(R,L); // specular
|
---|
356 |
|
---|
357 | float2 tex;
|
---|
358 | tex.x = (cos_value + 1)/2;
|
---|
359 | tex.y = dws/2/PI;
|
---|
360 |
|
---|
361 | // lookup into precalculated reflectivity values
|
---|
362 | cos_value = tex2D(DecorationSampler, tex).g * 3;
|
---|
363 | color = Lin * 0.5 * cos_value;
|
---|
364 |
|
---|
365 | return color;
|
---|
366 | }
|
---|
367 |
|
---|
368 | // Method #1
|
---|
369 |
|
---|
370 | /// \brief Calculates diffuse or specular contributions of all texels in #SmallEnvironmentMap to the current point.
|
---|
371 | /// For each texel of #SmallEnvironmentMap, function GetContr(int,float3,float3,float3,float3) is called.
|
---|
372 |
|
---|
373 | #define _EnvMapDiffuseLocalizedPS( M ) \
|
---|
374 | float4 EnvMapDiffuseLocalized##M##PS( _EnvMapVS_output IN ) : COLOR \
|
---|
375 | { \
|
---|
376 | IN.View = -normalize( IN.View ); \
|
---|
377 | IN.Normal = normalize( IN.Normal ); \
|
---|
378 | IN.Position -= reference_pos.xyz; /* relative to the ref.point */ \
|
---|
379 | \
|
---|
380 | float3 R = -reflect( IN.View, IN.Normal ); /* reflection direction */ \
|
---|
381 | \
|
---|
382 | float4 I = 0; \
|
---|
383 | \
|
---|
384 | for (int x = 0; x < M; x++) /* foreach texel */ \
|
---|
385 | for (int y = 0; y < M; y++) \
|
---|
386 | { \
|
---|
387 | /* compute intensity for 6 texels with equal solid angles */ \
|
---|
388 | \
|
---|
389 | float2 tpos = float2( (x+0.5f)/M, (y+0.5f)/M ); /* texture coord (0..1) */ \
|
---|
390 | \
|
---|
391 | float2 p = float2(tpos.x, 1-tpos.y); \
|
---|
392 | p.xy = 2*p.xy - 1; /* position (-1..1) */ \
|
---|
393 | \
|
---|
394 | I += GetContr( M, float3(p.x, p.y, 1), IN.Position, IN.Normal, IN.View ); \
|
---|
395 | I += GetContr( M, float3(p.x, p.y, -1), IN.Position, IN.Normal, IN.View ); \
|
---|
396 | I += GetContr( M, float3(p.x, 1, p.y), IN.Position, IN.Normal, IN.View ); \
|
---|
397 | I += GetContr( M, float3(p.x, -1, p.y), IN.Position, IN.Normal, IN.View ); \
|
---|
398 | I += GetContr( M, float3(1, p.x, p.y), IN.Position, IN.Normal, IN.View ); \
|
---|
399 | I += GetContr( M, float3(-1, p.x, p.y), IN.Position, IN.Normal, IN.View ); \
|
---|
400 | } \
|
---|
401 | \
|
---|
402 | return intensity * I; \
|
---|
403 | } // end of macro definition
|
---|
404 |
|
---|
405 | _EnvMapDiffuseLocalizedPS( 2 );
|
---|
406 | _EnvMapDiffuseLocalizedPS( 4 );
|
---|
407 | _EnvMapDiffuseLocalizedPS( 8 );
|
---|
408 | _EnvMapDiffuseLocalizedPS( 16 );
|
---|
409 |
|
---|
410 |
|
---|
411 | // Method #2
|
---|
412 |
|
---|
413 | /// \brief Calculates diffuse or specular contributions of the 5 "most important" texels of #SmallEnvironmentMap to the current point.
|
---|
414 | /// For these texels, function GetContr(int,float3,float3,float3,float3) is called.
|
---|
415 |
|
---|
416 | float4 EnvMapDiffuseLocalized5TexPS( _EnvMapVS_output IN ) : COLOR
|
---|
417 | {
|
---|
418 | IN.View = -normalize( IN.View );
|
---|
419 | IN.Normal = normalize( IN.Normal );
|
---|
420 | // translate reference point to the origin
|
---|
421 | IN.Position -= reference_pos.xyz;
|
---|
422 |
|
---|
423 | float3 R = -reflect( IN.View, IN.Normal ); // reflection direction
|
---|
424 |
|
---|
425 | float4 I = 0;
|
---|
426 |
|
---|
427 | float3 q;
|
---|
428 | if ( shininess <= 0 )
|
---|
429 | q = IN.Normal; // diffuse
|
---|
430 | else
|
---|
431 | q = R;
|
---|
432 |
|
---|
433 | float rr = max( max(abs(q.x), abs(q.y)), abs(q.z) ); // select the largest component
|
---|
434 | q /= rr; // scale the largest component to value +/-1
|
---|
435 |
|
---|
436 | float3 offset1 = float3(1,0,0); // default: largest: z
|
---|
437 | float3 offset2 = float3(0,1,0); // select: x,y
|
---|
438 |
|
---|
439 | if (abs(q.x) > abs(q.y) && abs(q.x) > abs(q.z)) { // largest: x
|
---|
440 | offset1 = float3(0,0,1); // select y,z
|
---|
441 | }
|
---|
442 | if (abs(q.y) > abs(q.x) && abs(q.y) > abs(q.z)) { // largest: y
|
---|
443 | offset2 = float3(0,0,1); // select x,z
|
---|
444 | }
|
---|
445 |
|
---|
446 | I += GetContr( LR_CUBEMAP_SIZE, q, IN.Position, IN.Normal, IN.View );
|
---|
447 | I += GetContr( LR_CUBEMAP_SIZE, q + offset1*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
|
---|
448 | I += GetContr( LR_CUBEMAP_SIZE, q - offset1*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
|
---|
449 | I += GetContr( LR_CUBEMAP_SIZE, q + offset2*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
|
---|
450 | I += GetContr( LR_CUBEMAP_SIZE, q - offset2*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
|
---|
451 |
|
---|
452 | // since only 5 texels are considered, the result gets darker.
|
---|
453 | // LR_CUBEMAP_SIZE is present to compensate this.
|
---|
454 | return intensity * I * LR_CUBEMAP_SIZE / 2;
|
---|
455 | }
|
---|
456 |
|
---|
457 |
|
---|
458 | //--------------------------------------------------------------------------------------
|
---|
459 | // Shading the environment
|
---|
460 | //--------------------------------------------------------------------------------------
|
---|
461 |
|
---|
462 | /// \brief Input for vertex shader IlluminatedSceneVS().
|
---|
463 | struct _IlluminatedSceneVS_input {
|
---|
464 | float4 Position : POSITION;
|
---|
465 | float3 Normal : NORMAL;
|
---|
466 | float2 TexCoord : TEXCOORD0;
|
---|
467 | };
|
---|
468 |
|
---|
469 | /// \brief Input for pixel shader IlluminatedScenePS().
|
---|
470 | struct _IlluminatedSceneVS_output {
|
---|
471 | float4 hPosition : POSITION;
|
---|
472 | float2 TexCoord : TEXCOORD0;
|
---|
473 | float3 Position : TEXCOORD1;
|
---|
474 | };
|
---|
475 |
|
---|
476 | _IlluminatedSceneVS_output IlluminatedSceneVS( _IlluminatedSceneVS_input IN )
|
---|
477 | {
|
---|
478 | _IlluminatedSceneVS_output OUT;
|
---|
479 | OUT.hPosition = mul( IN.Position, WorldViewProjection );
|
---|
480 |
|
---|
481 | // texel_size as uniform parameter
|
---|
482 | OUT.hPosition.x -= texel_size * OUT.hPosition.w;
|
---|
483 | OUT.hPosition.y += texel_size * OUT.hPosition.w;
|
---|
484 |
|
---|
485 | if (iShowCubeMap > 0)
|
---|
486 | {
|
---|
487 | // if one of the cube maps is displayed on the walls,
|
---|
488 | // position is simply forwarded
|
---|
489 | OUT.Position = IN.Position;
|
---|
490 | }
|
---|
491 | else
|
---|
492 | {
|
---|
493 | // also consider camera orientation
|
---|
494 | OUT.Position = mul( IN.Position, WorldView );
|
---|
495 | }
|
---|
496 |
|
---|
497 | OUT.TexCoord = IN.TexCoord;
|
---|
498 | return OUT;
|
---|
499 | }
|
---|
500 |
|
---|
501 | /// Displays the environment with a simple shading
|
---|
502 | float4 IlluminatedScenePS( _IlluminatedSceneVS_output IN ) : COLOR0
|
---|
503 | {
|
---|
504 | float3 color = objColor * tex2D(DecorationSampler, IN.TexCoord);
|
---|
505 |
|
---|
506 | if (iShowCubeMap > 0)
|
---|
507 | {
|
---|
508 | // if one of the cube maps should be displayed on the walls,
|
---|
509 | // display it
|
---|
510 | color = texCUBE(EnvironmentMapSampler, IN.Position) * intensity;
|
---|
511 | }
|
---|
512 | else if (brightness>0)
|
---|
513 | {
|
---|
514 | // create an exponential falloff for each face of the room
|
---|
515 | float3 L = float3(2*IN.TexCoord.x-1, 2*IN.TexCoord.y-1, -1);
|
---|
516 | L = normalize(L);
|
---|
517 | float3 N = float3(0,0,1);
|
---|
518 | color *= abs(pow(dot(L,N), 4)) * brightness;
|
---|
519 | }
|
---|
520 | else color *= 0.7;
|
---|
521 |
|
---|
522 | float dist = length( IN.Position );
|
---|
523 | return float4(color, dist);
|
---|
524 | }
|
---|
525 |
|
---|
526 |
|
---|
527 |
|
---|
528 |
|
---|
529 | //--------------------------------------------------------------------------------------
|
---|
530 | // Techniques
|
---|
531 | //--------------------------------------------------------------------------------------
|
---|
532 |
|
---|
533 |
|
---|
534 | /// a helpful macro to define techniques with a common vertex program
|
---|
535 | #define TechniqueUsingCommonVS(name); \
|
---|
536 | technique name \
|
---|
537 | { \
|
---|
538 | pass p0 \
|
---|
539 | { \
|
---|
540 | VertexShader = compile vs_3_0 EnvMapVS(); \
|
---|
541 | PixelShader = compile ps_3_0 name##PS(); \
|
---|
542 | } \
|
---|
543 | }
|
---|
544 |
|
---|
545 | TechniqueUsingCommonVS( EnvMapDiffuseClassic );
|
---|
546 | TechniqueUsingCommonVS( EnvMapDiffuseLocalized5Tex );
|
---|
547 |
|
---|
548 | //TechniqueUsingCommonVS( EnvMapDiffuseLocalized );
|
---|
549 | TechniqueUsingCommonVS( EnvMapDiffuseLocalized2 );
|
---|
550 | TechniqueUsingCommonVS( EnvMapDiffuseLocalized4 );
|
---|
551 | TechniqueUsingCommonVS( EnvMapDiffuseLocalized8 );
|
---|
552 | TechniqueUsingCommonVS( EnvMapDiffuseLocalized16 );
|
---|
553 |
|
---|
554 | #define ReduceTextureTechnique(M); \
|
---|
555 | technique ReduceTexture##M \
|
---|
556 | { \
|
---|
557 | pass p0 \
|
---|
558 | { \
|
---|
559 | VertexShader = compile vs_3_0 ReduceTextureVS(); \
|
---|
560 | PixelShader = compile ps_3_0 ReduceTexture##M##PS(); \
|
---|
561 | } \
|
---|
562 | }
|
---|
563 |
|
---|
564 | ReduceTextureTechnique( 2 );
|
---|
565 | ReduceTextureTechnique( 4 );
|
---|
566 | ReduceTextureTechnique( 8 );
|
---|
567 | ReduceTextureTechnique( 16 );
|
---|
568 |
|
---|
569 | #define ConvolutionTechnique(M); \
|
---|
570 | technique Convolution##M \
|
---|
571 | { \
|
---|
572 | pass p0 \
|
---|
573 | { \
|
---|
574 | VertexShader = compile vs_3_0 ConvolutionVS(); \
|
---|
575 | PixelShader = compile ps_3_0 Convolution##M##PS(); \
|
---|
576 | } \
|
---|
577 | }
|
---|
578 |
|
---|
579 | ConvolutionTechnique( 2 );
|
---|
580 | ConvolutionTechnique( 4 );
|
---|
581 | ConvolutionTechnique( 8 );
|
---|
582 | ConvolutionTechnique( 16 );
|
---|
583 |
|
---|
584 | /// a helpful macro to define techniques
|
---|
585 | /// where the name of EnvMapVS program is <TechniqueName>VS
|
---|
586 | /// and the name of PS program is <TechniqueName>PS
|
---|
587 | #define Technique(name); \
|
---|
588 | technique name \
|
---|
589 | { \
|
---|
590 | pass p0 \
|
---|
591 | { \
|
---|
592 | VertexShader = compile vs_3_0 name##VS(); \
|
---|
593 | PixelShader = compile ps_3_0 name##PS(); \
|
---|
594 | } \
|
---|
595 | }
|
---|
596 |
|
---|
597 | Technique( IlluminatedScene );
|
---|
598 | //Technique( Convolution );
|
---|
599 | //Technique( ReduceTexture ); |
---|