source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/Standalone/EnvMap [DirectX]/EnvMap.fx @ 3255

Revision 3255, 29.3 KB checked in by szirmay, 15 years ago (diff)
Line 
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
13int LR_CUBEMAP_SIZE;
14#define PI 3.14159f
15
16
17//--------------------------------------------------------------------------------------
18// Global variables
19//--------------------------------------------------------------------------------------
20
21
22float4x4 World;                                 ///< World matrix for the current object
23float4x4 WorldIT;                               ///< World matrix IT (inverse transposed) to transform surface normals of the current object
24float4x4 WorldView;                             ///< World * View matrix
25//float4x4 WorldViewIT;                 ///< World * View IT (inverse transposed) to transform surface normals of the current object
26float4x4 WorldViewProjection;   ///< World * View * Projection matrix
27
28float texel_size;                               ///< upload this constant every time the viewport changes
29
30float4 eyePos;                                  ///< current eye (camera) position
31float4 reference_pos;                   ///< Reference point for the last cube map generation.
32
33int nFace;                                              ///<
34int iShowCubeMap;                               ///<
35float4 objColor;
36
37float intensity, shininess, brightness;
38
39float4 readCubeMap(samplerCUBE cm, float3 dir)
40{
41 return texCUBElod(cm, float4(dir, 0));
42}
43//--------------------------------------------------------------------------------------
44// Textures & texture samplers
45//--------------------------------------------------------------------------------------
46
47
48texture EnvironmentMap, SmallEnvironmentMap, PreconvolvedEnvironmentMap, Decoration;
49
50sampler EnvironmentMapSampler = sampler_state
51{
52    /*MinFilter = LINEAR;
53    MagFilter = LINEAR;
54    MipFilter = LINEAR;*/
55    Texture   = <EnvironmentMap>;
56    AddressU  = WRAP;
57    AddressV  = WRAP;
58};
59
60sampler PreconvolvedEnvironmentMapSampler = sampler_state
61{
62    MinFilter = LINEAR;
63    MagFilter = LINEAR;
64    //MipFilter = LINEAR;
65    Texture   = <PreconvolvedEnvironmentMap>;
66    AddressU  = WRAP;
67    AddressV  = WRAP;
68};
69
70sampler SmallEnvironmentMapSampler = sampler_state
71{
72//    MinFilter = Point;
73//    MagFilter = Point;
74
75    MinFilter = LINEAR;
76    MagFilter = LINEAR;
77
78    //MipFilter = Point;
79    Texture   = <SmallEnvironmentMap>;
80    AddressU  = WRAP;
81    AddressV  = WRAP;
82};
83
84sampler DecorationSampler = sampler_state
85{
86    Texture   = <Decoration>;
87    MinFilter = LINEAR;
88    MagFilter = LINEAR;
89    //MipFilter = LINEAR;
90    AddressU  = CLAMP; //WRAP;
91    AddressV  = CLAMP; //WRAP;
92};
93
94
95
96//--------------------------------------------------------------------------------------
97// Shader programs
98//--------------------------------------------------------------------------------------
99
100
101
102void ReduceTextureVS( float4 position : POSITION,
103                float4 color0 : COLOR0,
104                float3 Normal : NORMAL,
105                float2 Tex : TEXCOORD0,
106                out float4 hposition : POSITION,
107                out float4 color : COLOR0,
108                out float2 oTex : TEXCOORD0,
109                out float4 pos : TEXCOORD1 )
110{
111    pos = position;
112    hposition = pos;
113    color = color0;
114    oTex = Tex;
115}
116
117/**
118        \brief Downsamples a cube map face.
119*/
120#define _ReduceTexturePS( M )                                                                                                           \
121        float4 ReduceTexture##M##PS( float2 Tex : TEXCOORD0,                                                    \
122                                  float4 pos : TEXCOORD1,                                                                                       \
123                                  float4 color0 : COLOR0 ) : COLOR0                                                                     \
124{                                                                                                                                                                       \
125        /* offset to texel center */                                                                                                    \
126        pos.xy += float2(1/(float)CUBEMAP_SIZE, -1/(float)CUBEMAP_SIZE);                                \
127        /* transform position into texture coord */                                                                             \
128    float2 tpos = pos.xy/2+0.5;         /* rescale from -1..1 into range 0..1 */                \
129    tpos.y = 1-tpos.y;                                                                                                                          \
130                                                                                                                                                                        \
131    float2 t;                                                                                                                                           \
132    float4 color = 0;                                                                                                                           \
133        const int RATE = CUBEMAP_SIZE / M;                                                                                              \
134                                                                                                                                                                        \
135    for (int i = 0; i < RATE; i++)                                                                                                      \
136     for (int j = 0; j < RATE; j++)                                                                                                     \
137    {                                                                                                                                                           \
138                t.x = tpos.x + i/(float)CUBEMAP_SIZE;                                                                           \
139                t.y = tpos.y + j/(float)CUBEMAP_SIZE;                                                                           \
140                color += tex2D(DecorationSampler, t) / (RATE * RATE);                                           \
141    }                                                                                                                                                           \
142        return color;                                                                                                                                   \
143} // end of macro definition
144
145_ReduceTexturePS( 2 );
146_ReduceTexturePS( 4 );
147_ReduceTexturePS( 8 );
148_ReduceTexturePS( 16 );
149
150
151
152//--------------------------------------------------------------------------------------
153// Method #0: CLASSIC (pre-convolved)
154//--------------------------------------------------------------------------------------
155
156
157
158/// \brief Returns the precalculated contribution of a texel with regard to the specified query direction.
159///
160/// \param q <b>query direction</b> (i.e. surface normal in diffuse case, ideal reflection direction in specular case).
161/// \param L vector pointing to the texel center
162float4 GetContr(float3 q, float3 L)
163// Lin * a * ( dw )
164// -- actually, dw is calculated by the caller --
165{
166        //float shininess = 1;
167        float fcos = max(dot(L, q), 0);
168        // diffuse
169        if (shininess <= 0)     
170                return 0.2 * fcos * readCubeMap( SmallEnvironmentMapSampler, L);
171        else
172        {
173                // some ad-hoc formula to avoid darkening
174                float brightness = (pow(shininess,0.8)*0.2);
175                return brightness * pow(fcos, shininess) * readCubeMap( SmallEnvironmentMapSampler, L);
176        }
177}
178
179/// \brief Input for vertex shader ConvolutionVS().
180struct _ConvolutionVS_input {
181    float4 Position : POSITION;
182};
183
184/// \brief Input for pixel shader ::_ConvolutionPS().
185struct _ConvolutionVS_output {
186    float4 hPosition : POSITION;
187    float3 Position  : TEXCOORD0;
188};
189
190_ConvolutionVS_output ConvolutionVS(_ConvolutionVS_input IN) {
191    _ConvolutionVS_output OUT;
192    OUT.hPosition = IN.Position;
193   
194        float2 pos = IN.Position.xy;    // -1..1
195
196        pos.x += 0.5f / LR_CUBEMAP_SIZE;
197        pos.y -= 0.5f / LR_CUBEMAP_SIZE;       
198   
199        if (nFace == 0) OUT.Position = float3(1, pos.y, -pos.x);
200        if (nFace == 1) OUT.Position = float3(-1, pos.y, pos.x);
201        if (nFace == 2) OUT.Position = float3(pos.x, 1, -pos.y);
202        if (nFace == 3) OUT.Position = float3(pos.x,-1, pos.y);
203        if (nFace == 4) OUT.Position = float3(pos.xy, 1);
204        if (nFace == 5) OUT.Position = float3(-pos.x, pos.y,-1);
205       
206    return OUT;
207}
208
209/**
210        \brief Convolves the values of a cube map of resoultion MxM.
211
212        Calculates the diffuse/specular irradiance map of resolution #LR_CUBEMAP_SIZE by summing up the contributions of all cube map texels
213        with regard to the current query direction.
214*/
215
216#define _ConvolutionPS( M )                                                                                                     \
217        float4 Convolution##M##PS( _ConvolutionVS_output IN ) : COLOR                   \
218{                                                                                                                                                       \
219        /* input position = query direction for the result */                                   \
220    float3 q = normalize( IN.Position );                                                                        \
221    float4 color = 0;                                                                                                           \
222                                                                                                                                                        \
223    for (int i = 0; i < M; i++)                                                                                         \
224     for (int j = 0; j < M; j++)                                                                                        \
225    {                                                                                                                                           \
226        float u = (i+0.5) / (float)M;                                                                           \
227        float v = (j+0.5) / (float)M;                                                                           \
228        float3 pos = float3( 2*u-1, 1-2*v, 1 );                                                         \
229                                                                                                                                                        \
230                float r = length(pos);                                                                                          \
231                pos /= r;                                                                                                                       \
232                                                                                                                                                        \
233                float4 dcolor = 0;                                                                                                      \
234            float3 L;                                                                                                                   \
235                L = float3(pos.z, pos.y, -pos.x);       dcolor += GetContr( q, L );             \
236                L = float3(-pos.z, pos.y, pos.x);       dcolor += GetContr( q, L );             \
237                L = float3(pos.x, pos.z, -pos.y);       dcolor += GetContr( q, L );             \
238                L = float3(pos.x, -pos.z, pos.y);       dcolor += GetContr( q, L );             \
239                L = float3(pos.x, pos.y, pos.z);        dcolor += GetContr( q, L );             \
240                L = float3(-pos.x, pos.y, -pos.z);      dcolor += GetContr( q, L );             \
241                                                                                                                                                        \
242                float dw = 4 / (r*r*r);                                                                                         \
243                color += dcolor * dw;                                                                                           \
244    }                                                                                                                                           \
245                                                                                                                                                        \
246        return color / (M * M);                                                                                                 \
247} /* end of macro definition */
248
249_ConvolutionPS( 2 );
250_ConvolutionPS( 4 );
251_ConvolutionPS( 8 );
252_ConvolutionPS( 16 );
253
254/// \brief Input for vertex shader EnvMapVS().
255struct _EnvMapVS_input
256{
257    float4 Position                     : POSITION;
258    float3 Normal                       : NORMAL;
259    float2 TexCoord                     : TEXCOORD0;
260};
261
262/// \brief Input for pixel shaders EnvMapDiffuseClassicPS(), ::_EnvMapDiffuseLocalizedPS(), EnvMapDiffuseLocalized5TexPS().
263struct _EnvMapVS_output
264{
265    float4 hPosition            : POSITION;
266    float2 TexCoord                     : TEXCOORD0;
267    float3 Normal                       : TEXCOORD1;
268    float3 View                         : TEXCOORD2;
269    float3 Position                     : TEXCOORD3;
270};
271
272_EnvMapVS_output EnvMapVS( _EnvMapVS_input IN )
273{
274        _EnvMapVS_output OUT;
275 
276    OUT.Position = mul( IN.Position, World ).xyz;               // scale & offset
277    OUT.View = normalize( OUT.Position - eyePos );
278        //OUT.Normal = IN.Normal;                                                               
279    OUT.Normal = mul( IN.Normal, WorldIT ).xyz;                 // allow distortion/rotation
280           
281    OUT.TexCoord = IN.TexCoord;
282   
283    OUT.hPosition = mul( IN.Position, WorldViewProjection );
284    return OUT;
285}
286
287/// \brief Determines diffuse or specular illumination with a single lookup into #PreconvolvedEnvironmentMap.
288/// PreconvolvedEnvironmentMap is bound to EnvMap::pCubeTexturePreConvolved (cube map of resolution #LR_CUBEMAP_SIZE).
289float4 EnvMapDiffuseClassicPS( _EnvMapVS_output IN ) : COLOR
290{
291        IN.View = normalize( IN.View );
292        IN.Normal = normalize( IN.Normal );
293       
294        float3 R = reflect(IN.View, IN.Normal);
295       
296        if (shininess <= 0)     // diffuse
297                return intensity * readCubeMap(PreconvolvedEnvironmentMapSampler, IN.Normal) *2;               
298        else                            // specular
299                return intensity * readCubeMap(PreconvolvedEnvironmentMapSampler, R) *2;       
300}
301
302
303
304//--------------------------------------------------------------------------------------
305// Method #1-#2: OUR METHOD
306//--------------------------------------------------------------------------------------
307
308
309
310/// \brief Calculates the contribution of a single texel of #SmallEnvironmentMap to the illumination of the shaded point.
311/// To compute reflectivity, precalculated integral values are used.
312///
313/// \param L vector pointing to the center of the texel under examination. We assume that the largest coordinate component
314/// of L is equal to one, i.e. L points to the face of a cube of edge length of 2.
315/// \param pos is the position of the shaded point
316/// \param N is the surface normal at the shaded point
317/// \param V is the viewing direction at the shaded point
318
319
320float4 GetContr(int M, float3 L, float3 pos, float3 N, float3 V)        // Phong-Blinn
321// L is strictly non-normalized
322{
323        float l = length(L);
324        L = normalize(L);
325
326        //Lin
327        float4 Lin = readCubeMap(SmallEnvironmentMapSampler, L);
328
329        //dw
330        float dw = 4 / (M*M*l*l*l + 4/2/3.1416f);
331       
332        float dws = dw;
333
334        //r
335        float doy = readCubeMap(SmallEnvironmentMapSampler, L).a;
336        float dxy = length(pos - L * doy);
337
338        //dws
339        //dws = (doy*doy * dw) / (dxy*dxy*(1 - dw/3.1416f) + doy*doy*dw/3.1416f);       // localization:
340        //dws = (doy*doy * dw) / (dxy*dxy*(1 - dw/2/3.1416f) + doy*doy*dw/2/3.1416f);   // localization:
341       
342        float den = 1 + doy*doy / (dxy*dxy) * ( (2*3.1416f)*(2*3.1416f) / ((2*3.1416f-dw)*(2*3.1416f-dw)) - 1 );
343        dws = 2*3.1416f * (1 - 1/sqrt(den));
344
345        float3 LL = L * doy - pos;      // L should start from the object (and not from the reference point) !!!
346        LL = normalize(LL);     
347       
348        float3 H = normalize(L + V);    // halfway vector
349        float3 R = reflect(-V, N);              // reflection vector
350
351        // from texture
352
353        float4 color = 0;
354
355        float cos_value;
356        if (shininess <= 0)
357                 cos_value = dot(N,L);  // diffuse
358        else cos_value = dot(R,L);      // specular
359       
360        float2 tex;
361        tex.x = (cos_value + 1)/2;
362        tex.y = dws/2/PI;
363
364        // lookup into precalculated reflectivity values
365        cos_value = tex2D(DecorationSampler, tex).g * 3;
366        color = Lin * 0.5 * cos_value;
367       
368        return color;
369}
370
371// Method #1
372
373/// \brief Calculates diffuse or specular contributions of all texels in #SmallEnvironmentMap to the current point.
374/// For each texel of #SmallEnvironmentMap, function GetContr(int,float3,float3,float3,float3) is called.
375
376#define _EnvMapDiffuseLocalizedPS( M )                                                                  \
377        float4 EnvMapDiffuseLocalized##M##PS( _EnvMapVS_output IN ) : COLOR     \
378{                                                                                                                                                                       \
379        IN.View = -normalize( IN.View );                                                                                                \
380        IN.Normal = normalize( IN.Normal );                                                                                             \
381        IN.Position -= reference_pos.xyz;                               /* relative to the ref.point */ \
382                                                                                                                                                                        \
383        float3 R = -reflect( IN.View, IN.Normal );              /* reflection direction */              \
384                                                                                                                                                                        \
385    float4 I = 0;                                                                                                                                       \
386                                                                                                                                                                        \
387        for (int x = 0; x < M; x++)                     /* foreach texel */                                                     \
388         for (int y = 0; y < M; y++)                                                                                                    \
389         {                                                                                                                                                              \
390                /* compute intensity for 6 texels with equal solid angles */                            \
391                                                                                                                                                                        \
392                float2 tpos = float2( (x+0.5f)/M, (y+0.5f)/M ); /* texture coord (0..1) */      \
393                                                                                                                                                                        \
394            float2 p = float2(tpos.x, 1-tpos.y);                                                                                \
395            p.xy = 2*p.xy - 1;                                                          /* position (-1..1) */          \
396                                                                                                                                                                        \
397                I += GetContr( M, float3(p.x, p.y,  1), IN.Position, IN.Normal, IN.View );      \
398                I += GetContr( M, float3(p.x, p.y, -1), IN.Position, IN.Normal, IN.View );      \
399                I += GetContr( M, float3(p.x,  1, p.y), IN.Position, IN.Normal, IN.View );      \
400                I += GetContr( M, float3(p.x, -1, p.y), IN.Position, IN.Normal, IN.View );      \
401                I += GetContr( M, float3(1,  p.x, p.y), IN.Position, IN.Normal, IN.View );      \
402                I += GetContr( M, float3(-1, p.x, p.y), IN.Position, IN.Normal, IN.View );      \
403        }                                                                                                                                                               \
404                                                                                                                                                                        \
405        return intensity * I;                                                                                                                   \
406} // end of macro definition
407
408_EnvMapDiffuseLocalizedPS( 2 );
409_EnvMapDiffuseLocalizedPS( 4 );
410_EnvMapDiffuseLocalizedPS( 8 );
411_EnvMapDiffuseLocalizedPS( 16 );
412
413
414// Method #2
415
416/// \brief Calculates diffuse or specular contributions of the 5 "most important" texels of #SmallEnvironmentMap to the current point.
417/// For these texels, function GetContr(int,float3,float3,float3,float3) is called.
418/*
419float4 EnvMapDiffuseLocalized5TexPS( _EnvMapVS_output IN ) : COLOR
420{
421        IN.View = -normalize( IN.View );
422        IN.Normal = normalize( IN.Normal );
423        // translate reference point to the origin
424        IN.Position -= reference_pos.xyz;               
425       
426        float3 R = -reflect( IN.View, IN.Normal );              // reflection direction
427       
428    float4 I = 0;
429
430        float3 q;
431        if ( shininess <= 0 )
432                q = IN.Normal;                                  // diffuse
433        else
434                q = R;
435               
436    float rr = max( max(abs(q.x), abs(q.y)), abs(q.z) );        // select the largest component
437    q /= rr;    // scale the largest component to value +/-1
438   
439    float3 offset1 = float3(1,0,0);                                             // default: largest: z
440    float3 offset2 = float3(0,1,0);                                             // select: x,y
441   
442    if (abs(q.x) > abs(q.y) && abs(q.x) > abs(q.z)) {   // largest: x
443                offset1 = float3(0,0,1);                                                // select y,z
444        }
445    if (abs(q.y) > abs(q.x) && abs(q.y) > abs(q.z)) {   // largest: y
446                offset2 = float3(0,0,1);                                                // select x,z
447        }
448       
449
450        I += GetContr( LR_CUBEMAP_SIZE, q, IN.Position, IN.Normal, IN.View );
451        I += GetContr( LR_CUBEMAP_SIZE, q + offset1*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
452        I += GetContr( LR_CUBEMAP_SIZE, q - offset1*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
453        I += GetContr( LR_CUBEMAP_SIZE, q + offset2*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
454        I += GetContr( LR_CUBEMAP_SIZE, q - offset2*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
455       
456        // since only 5 texels are considered, the result gets darker.
457        // LR_CUBEMAP_SIZE is present to compensate this.       
458        return intensity * I * LR_CUBEMAP_SIZE / 2;             
459       
460}*/
461
462// Method #3
463
464/// \brief Calculates diffuse or specular contributions of the 5 "most important" texels of #SmallEnvironmentMap to the current point.
465/// For these texels, function GetContr(int,float3,float3,float3,float3) is called.
466float4 GetContibution(float3 L, float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, samplerCUBE cubemap)
467{
468        float d;
469        //d = readCubeMap(cubemap, L).a;       
470        d = readCubeMap(cubemap, L1).a;
471        L1 = d * normalize(L1);
472        d = readCubeMap(cubemap, L2).a;
473        L2 = d * normalize(L2);
474        d = readCubeMap(cubemap, L3).a;
475        L3 = d * normalize(L3);
476        d = readCubeMap(cubemap, L4).a;
477        L4 = d * normalize(L4);
478               
479       
480    float3 r1 = normalize(L1 - pos);   
481    float3 r2 = normalize(L2 - pos);
482    float3 r3 = normalize(L3 - pos);
483    float3 r4 = normalize(L4 - pos);
484  /*           
485        float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N);
486        float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N);
487        float tri3 = acos(dot(r3, r4)) * dot(cross(r3, r4), N);
488        float tri4 = acos(dot(r4, r1)) * dot(cross(r4, r1), N);
489  */
490  float3 crossP = cross(r1, r2);
491  float r = length(crossP);
492  float dd = dot(r1,r2);
493  float tri1 = acos(dd) * dot(crossP/r, N);
494 
495  crossP = cross(r2, r3);
496  r = length(crossP);
497  dd = dot(r1,r2);
498  float tri2 = acos(dd) * dot(crossP/r, N);
499 
500  crossP = cross(r3, r4);
501  r = length(crossP);
502  dd = dot(r1,r2);
503  float tri3 = acos(dd) * dot(crossP/r, N);
504 
505  crossP = cross(r4, r1);
506  r = length(crossP);
507  dd = dot(r1,r2);
508  float tri4= acos(dd) * dot(crossP/r, N);
509 
510 
511        return max(tri1 + tri2 + tri3 + tri4, 0);       
512        //return tri1 + tri2 + tri3 + tri4;     
513}
514
515
516
517
518float4 EnvMapDiffuseLocalizedNewPS( _EnvMapVS_output IN ) : COLOR                       
519{               
520        float M = 4.0;                                                                                                                                                 
521        IN.View = -normalize( IN.View );                                                                                               
522        IN.Normal = normalize( IN.Normal );                                                                                             
523        IN.Position -= reference_pos.xyz;                               
524        float3 pos = IN.Position.xyz;   
525       
526        //return        reference_pos;
527        //return  readCubeMap(SmallEnvironmentMapSampler, pos);
528                                                                                               
529        float3 N =IN.Normal;                                                                                                                                                           
530        float3 R = -reflect( IN.View, IN.Normal );             
531                                                                                                       
532    float4 I = 0;                                                                       
533        float3 L1, L2, L3, L4, L;                                               
534        float4 Le;                                                                             
535        float width = 1.0 / M;                                                 
536        float width2 = width * 2;
537        float d;
538       
539        for (float x = 0; x < M; x++)                   
540         for (float y = 0; y < M; y++)                                                                                 
541         {                                                                                                                             
542                float2 p, tpos;
543            tpos.x = x * width;
544            tpos.y = y * width;
545           
546            p = tpos.xy;   
547            p = 2.0 * p - 1.0; //-1..1
548                           
549                L1 = float3(p.x, p.y, 1);       
550                L2 = float3(p.x + width2, p.y, 1);     
551                L3 = float3(p.x + width2, p.y + width2, 1);     
552                L4 = float3(p.x, p.y + width2, 1);
553                L = float3(p.x + width, p.y + width, 1);
554                Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
555               
556                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
557                       
558        }                                                                                                                                                       
559       
560        for (float x = 0; x < M; x++)                   
561         for (float y = 0; y < M; y++)                                                                                 
562         {                                                                                                                             
563                float2 p, tpos;
564            tpos.x = x * width; // 0..1
565            tpos.y = y * width; // 0..1
566           
567            p = tpos.xy;   
568            p = 2.0 * p - 1.0; //-1..1
569                           
570                L4 = float3(p.x, p.y, -1);     
571                L3 = float3(p.x + width2, p.y, -1);     
572                L2 = float3(p.x + width2, p.y + width2, -1);   
573                L1 = float3(p.x, p.y + width2, -1);
574                L = float3(p.x + width, p.y + width, -1);
575                Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
576               
577                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
578         }     
579         
580        for (float x = 0; x < M; x++)                   
581         for (float y = 0; y < M; y++)                                                                                 
582         {                                                                                                                             
583                float2 p, tpos;
584            tpos.x = x * width; // 0..1
585            tpos.y = y * width; // 0..1
586           
587            p = tpos.xy;   
588            p = 2.0 * p - 1.0; //-1..1
589                           
590                L4 = float3(p.x, 1, p.y);
591                L3 = float3(p.x + width2, 1, p.y);     
592                L2 = float3(p.x + width2, 1, p.y + width2);     
593                L1 = float3(p.x, 1, p.y + width2);                     
594                L = float3(p.x + width, 1, p.y + width);
595                Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
596               
597                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
598         }             
599         
600        for (float x = 0; x < M; x++)                   
601         for (float y = 0; y < M; y++)                                                                                 
602         {                                                                                                                             
603                float2 p, tpos;
604            tpos.x = x * width; // 0..1
605            tpos.y = y * width; // 0..1
606           
607            p = tpos.xy;   
608            p = 2.0 * p - 1.0; //-1..1
609                           
610                L1 = float3(p.x, -1, p.y);
611                L2 = float3(p.x + width2, -1, p.y);     
612                L3 = float3(p.x + width2, -1, p.y + width2);   
613                L4 = float3(p.x, -1, p.y + width2);                     
614                L = float3(p.x + width, -1, p.y + width);
615                Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
616               
617                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
618         }
619         
620         for (float x = 0; x < M; x++)                 
621                for (float y = 0; y < M; y++)                                                                                   
622                {                                                                                                                               
623                float2 p, tpos;
624            tpos.x = x * width; // 0..1
625            tpos.y = y * width; // 0..1
626           
627            p = tpos.xy;   
628            p = 2.0 * p - 1.0; //-1..1
629                           
630                L1 = float3(1, p.x, p.y);
631                L2 = float3(1, p.x + width2, p.y);     
632                L3 = float3(1, p.x + width2, p.y + width2);     
633                L4 = float3(1, p.x, p.y + width2);     
634                L = float3(1, p.x + width, p.y + width);
635                Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
636               
637                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
638        }
639         
640        for (float x = 0; x < M; x++)                   
641         for (float y = 0; y < M; y++)                                                                                 
642         {                                                                                                                             
643                float2 p, tpos;
644            tpos.x = x * width; // 0..1
645            tpos.y = y * width; // 0..1
646           
647            p = tpos.xy;   
648            p = 2.0 * p - 1.0; //-1..1
649                           
650                L4 = float3(-1, p.x, p.y);
651                L3 = float3(-1, p.x + width2, p.y);     
652                L2 = float3(-1, p.x + width2, p.y + width2);   
653                L1 = float3(-1, p.x, p.y + width2);     
654                L = float3(-1, p.x + width, p.y + width);
655                Le = float4(readCubeMap(SmallEnvironmentMapSampler, L).rgb, 1);
656               
657                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                         
658         }                                                                                                                                                             
659        return intensity * I;                                                                                                                   
660}
661
662float4 P2PContr(float3 N, float3 Nl, float3 pos, float3 L, samplerCUBE cubemap)
663{
664        Nl = normalize(Nl);
665        L = normalize(L);
666        float4 Le = float4(readCubeMap(cubemap, L).rgb, 1);
667        float d = readCubeMap(cubemap, L).a;
668        float3 Lpos = L * d;
669        float3 Ldir = Lpos - pos;
670        float dist = dot(Ldir, Ldir);
671        Ldir = normalize(Ldir);
672       
673        return Le * max(dot(N, Ldir),0) * dot(Nl, -1 * Ldir) / dist;   
674}
675
676float4 EnvMapDiffuseP2PPS( _EnvMapVS_output IN ) : COLOR                       
677{               
678        float M = 4.0;                                                                                                                                                 
679        IN.View = -normalize( IN.View );                                                                                               
680        IN.Normal = normalize( IN.Normal );                                                                                             
681        IN.Position -= reference_pos.xyz;                               
682        float3 pos = IN.Position.xyz;   
683       
684        //return        reference_pos;
685        //return  readCubeMap(SmallEnvironmentMapSampler, pos);
686                                                                                               
687        float3 N =IN.Normal;                                                                                                                                                           
688                                                                                                       
689    float4 I = 0;                                                                       
690        float3 L;                                               
691        float4 Le;                                                                             
692        float width = 1.0 / M;
693        float d;                                                       
694       
695        for (float x = 0.5; x < M; x++)                 
696         for (float y = 0.5; y < M; y++)                                                                                       
697         {                                                                                                                             
698                float2 p, tpos;
699            tpos.x = x * width;
700            tpos.y = y * width;
701           
702            p = tpos.xy;   
703            p = 2.0 * p - 1.0; //-1..1
704                           
705                I += P2PContr(N, float3(0,0,-1), pos, float3(p.x, p.y, 1), SmallEnvironmentMapSampler);
706                I += P2PContr(N, float3(0,0,1), pos, float3(-p.x, p.y, -1), SmallEnvironmentMapSampler);
707                I += P2PContr(N, float3(-1,0,0), pos, float3(1, p.y, -p.x), SmallEnvironmentMapSampler);
708                I += P2PContr(N, float3(1,0,0), pos, float3(-1, p.y, p.x), SmallEnvironmentMapSampler);
709                I += P2PContr(N, float3(0,-1,0), pos, float3(p.x, 1, -p.y), SmallEnvironmentMapSampler);
710                I += P2PContr(N, float3(0,1,0), pos, float3(p.x, -1, p.y), SmallEnvironmentMapSampler);
711        }
712                                                                                                                                               
713        return intensity * I;                                                                                                                   
714}
715
716float4 EnvMapDiffuseLocalized5TexPS( _EnvMapVS_output IN ) : COLOR
717{
718        IN.View = -normalize( IN.View );
719        IN.Normal = normalize( IN.Normal );
720        // translate reference point to the origin
721        IN.Position -= reference_pos.xyz;               
722       
723        float3 R = -reflect( IN.View, IN.Normal );              // reflection direction
724       
725    float4 I = 0;
726
727        float3 q;
728        if ( shininess <= 0 )
729                q = IN.Normal;                                  // diffuse
730        else
731                q = R;
732               
733    float rr = max( max(abs(q.x), abs(q.y)), abs(q.z) );        // select the largest component
734    q /= rr;    // scale the largest component to value +/-1
735   
736    float3 offset1 = float3(1,0,0);                                             // default: largest: z
737    float3 offset2 = float3(0,1,0);                                             // select: x,y
738   
739    if (abs(q.x) > abs(q.y) && abs(q.x) > abs(q.z)) {   // largest: x
740                offset1 = float3(0,0,1);                                                // select y,z
741        }
742    if (abs(q.y) > abs(q.x) && abs(q.y) > abs(q.z)) {   // largest: y
743                offset2 = float3(0,0,1);                                                // select x,z
744        }
745       
746
747        I += GetContr( LR_CUBEMAP_SIZE, q, IN.Position, IN.Normal, IN.View );
748        I += GetContr( LR_CUBEMAP_SIZE, q + offset1*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
749        I += GetContr( LR_CUBEMAP_SIZE, q - offset1*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
750        I += GetContr( LR_CUBEMAP_SIZE, q + offset2*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
751        I += GetContr( LR_CUBEMAP_SIZE, q - offset2*(2.0/LR_CUBEMAP_SIZE), IN.Position, IN.Normal, IN.View );
752       
753        // since only 5 texels are considered, the result gets darker.
754        // LR_CUBEMAP_SIZE is present to compensate this.       
755        return intensity * I * LR_CUBEMAP_SIZE / 2;             
756       
757}
758
759//--------------------------------------------------------------------------------------
760// Shading the environment
761//--------------------------------------------------------------------------------------
762
763/// \brief Input for vertex shader IlluminatedSceneVS().
764struct _IlluminatedSceneVS_input {
765    float4 Position : POSITION;
766    float3 Normal : NORMAL;
767    float2 TexCoord : TEXCOORD0;
768};
769
770/// \brief Input for pixel shader IlluminatedScenePS().
771struct _IlluminatedSceneVS_output {
772    float4 hPosition : POSITION;
773    float2 TexCoord : TEXCOORD0;
774    float3 Position : TEXCOORD1;
775};
776
777_IlluminatedSceneVS_output IlluminatedSceneVS( _IlluminatedSceneVS_input IN )
778{
779        _IlluminatedSceneVS_output OUT;
780    OUT.hPosition = mul( IN.Position, WorldViewProjection );
781   
782    // texel_size as uniform parameter
783        OUT.hPosition.x -= texel_size * OUT.hPosition.w;
784        OUT.hPosition.y += texel_size * OUT.hPosition.w;
785
786    if (iShowCubeMap > 0)
787    {
788                // if one of the cube maps is displayed on the walls,
789                // position is simply forwarded
790                OUT.Position = IN.Position;
791        }
792        else
793        {
794                // also consider camera orientation
795                OUT.Position = mul( IN.Position, WorldView );
796        }
797   
798    OUT.TexCoord = IN.TexCoord;
799    return OUT;
800}
801
802/// Displays the environment with a simple shading
803float4 IlluminatedScenePS( _IlluminatedSceneVS_output IN ) : COLOR0
804{
805    float3 color = objColor * tex2D(DecorationSampler, IN.TexCoord);
806   
807    if (iShowCubeMap > 0)
808    {
809                // if one of the cube maps should be displayed on the walls,
810                // display it
811            color = readCubeMap(EnvironmentMapSampler, IN.Position) * intensity;
812    }
813    else if (brightness>0)
814    {
815                // create an exponential falloff for each face of the room
816                float3 L = float3(2*IN.TexCoord.x-1, 2*IN.TexCoord.y-1, -1);
817                L = normalize(L);
818                float3 N = float3(0,0,1);
819                color *= abs(pow(dot(L,N), 4)) * brightness;
820        }
821        else color *= 0.7;
822               
823        float dist = length( IN.Position );
824    return float4(color, dist);
825}
826
827
828
829
830//--------------------------------------------------------------------------------------
831// Techniques
832//--------------------------------------------------------------------------------------
833
834
835/// a helpful macro to define techniques with a common vertex program
836#define TechniqueUsingCommonVS(name);                                                           \
837        technique name                                                                                                  \
838        {                                                                                                                               \
839            pass p0                                                                                                             \
840            {                                                                                                                   \
841                    VertexShader = compile vs_3_0 EnvMapVS();                           \
842                    PixelShader  = compile ps_3_0 name##PS();                           \
843                }                                                                                                                       \
844        }
845       
846TechniqueUsingCommonVS( EnvMapDiffuseClassic );
847TechniqueUsingCommonVS( EnvMapDiffuseLocalized5Tex );
848
849//TechniqueUsingCommonVS( EnvMapDiffuseLocalized );
850TechniqueUsingCommonVS( EnvMapDiffuseLocalized2 );
851TechniqueUsingCommonVS( EnvMapDiffuseLocalized4 );
852TechniqueUsingCommonVS( EnvMapDiffuseLocalized8 );
853TechniqueUsingCommonVS( EnvMapDiffuseLocalized16 );
854TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew );
855TechniqueUsingCommonVS( EnvMapDiffuseP2P );
856
857#define ReduceTextureTechnique(M);                                                                      \
858        technique ReduceTexture##M                                                                              \
859        {                                                                                                                               \
860            pass p0                                                                                                             \
861            {                                                                                                                   \
862                    VertexShader = compile vs_3_0 ReduceTextureVS();            \
863                    PixelShader  = compile ps_3_0 ReduceTexture##M##PS();       \
864                }                                                                                                                       \
865        }
866
867ReduceTextureTechnique( 2 );
868ReduceTextureTechnique( 4 );
869ReduceTextureTechnique( 8 );
870ReduceTextureTechnique( 16 );
871
872#define ConvolutionTechnique(M);                                                                        \
873        technique Convolution##M                                                                                \
874        {                                                                                                                               \
875            pass p0                                                                                                             \
876            {                                                                                                                   \
877                    VertexShader = compile vs_3_0 ConvolutionVS();                      \
878                    PixelShader  = compile ps_3_0 Convolution##M##PS();         \
879                }                                                                                                                       \
880        }
881
882ConvolutionTechnique( 2 );
883ConvolutionTechnique( 4 );
884ConvolutionTechnique( 8 );
885ConvolutionTechnique( 16 );
886
887/// a helpful macro to define techniques
888/// where the name of EnvMapVS program is <TechniqueName>VS
889/// and the name of PS program is <TechniqueName>PS
890#define Technique(name);                                                                \
891        technique name                                                                          \
892        {                                                                                                       \
893            pass p0                                                                                     \
894            {                                                                                           \
895                    VertexShader = compile vs_3_0 name##VS();   \
896                    PixelShader  = compile ps_3_0 name##PS();   \
897                }                                                                                               \
898        }
899
900Technique( IlluminatedScene );
901//Technique( Convolution );
902//Technique( ReduceTexture );
Note: See TracBrowser for help on using the repository browser.