source: GTP/trunk/App/Demos/Illum/EnvMap/EnvMap.fx @ 1556

Revision 1556, 26.0 KB checked in by szirmay, 18 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
39
40//--------------------------------------------------------------------------------------
41// Textures & texture samplers
42//--------------------------------------------------------------------------------------
43
44
45texture EnvironmentMap, SmallEnvironmentMap, PreconvolvedEnvironmentMap, Decoration;
46
47sampler EnvironmentMapSampler = sampler_state
48{
49    /*MinFilter = LINEAR;
50    MagFilter = LINEAR;
51    MipFilter = LINEAR;*/
52    Texture   = <EnvironmentMap>;
53    AddressU  = WRAP;
54    AddressV  = WRAP;
55};
56
57sampler PreconvolvedEnvironmentMapSampler = sampler_state
58{
59    MinFilter = LINEAR;
60    MagFilter = LINEAR;
61    //MipFilter = LINEAR;
62    Texture   = <PreconvolvedEnvironmentMap>;
63    AddressU  = WRAP;
64    AddressV  = WRAP;
65};
66
67sampler 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
81sampler 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
99void 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
159float4 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().
177struct _ConvolutionVS_input {
178    float4 Position : POSITION;
179};
180
181/// \brief Input for pixel shader ::_ConvolutionPS().
182struct _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().
252struct _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().
260struct _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).
286float4 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
317float4 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
416float4 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// Method #3
458
459/// \brief Calculates diffuse or specular contributions of the 5 "most important" texels of #SmallEnvironmentMap to the current point.
460/// For these texels, function GetContr(int,float3,float3,float3,float3) is called.
461float4 GetContibution(float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, float d)
462{
463        L1 = d * normalize(L1);
464        L2 = d * normalize(L2);
465        L3 = d *  normalize(L3);
466        L4 = d * normalize(L4);
467       
468    float3 r1 = normalize(L1 - pos);   
469    float3 r2 = normalize(L2 - pos);
470    float3 r3 = normalize(L3 - pos);
471    float3 r4 = normalize(L4 - pos);
472   
473   
474        float kd = 0.3; // 0.3
475        /*
476        float3 R;
477        R = cross(r1, r2);
478        float tri1 = asin(length(R)) * dot(R, N);
479        R = cross(r2, r3);
480        float tri2 = asin(length(R)) * dot(R, N);
481        R = cross(r3, r4);
482        float tri3 = asin(length(R)) * dot(R, N);
483        R = cross(r4, r1);
484        float tri4 = asin(length(R)) * dot(R, N);
485        */
486       
487        float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N);
488        float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N);
489        float tri3 = acos(dot(r3, r4)) * dot(cross(r3, r4), N);
490        float tri4 = acos(dot(r4, r1)) * dot(cross(r4, r1), N);
491       
492        return max(tri1 + tri2 + tri3 + tri4, 0);       
493        //return tri1 + tri2 + tri3 + tri4;     
494}
495
496float4 EnvMapDiffuseLocalizedNewPS( _EnvMapVS_output IN ) : COLOR                       
497{               
498        float M = 4.0;                                                                                                                                                 
499        IN.View = -normalize( IN.View );                                                                                               
500        IN.Normal = normalize( IN.Normal );                                                                                             
501        IN.Position -= reference_pos.xyz;                               
502        float3 pos = IN.Position.xyz;   
503       
504        //return        reference_pos;
505        //return  texCUBE(SmallEnvironmentMapSampler, pos);     
506                                                                                               
507        float3 N =IN.Normal;                                                                                                                                                           
508        float3 R = -reflect( IN.View, IN.Normal );             
509                                                                                                       
510    float4 I = 0;                                                                       
511        float3 L1, L2, L3, L4, L;                                               
512        float4 Le;                                                                             
513        float d;                                                                               
514        float width = 1.0 / M;                                                 
515       
516        for (int x = 0; x < M; x++)                     
517         for (int y = 0; y < M; y++)                                                                                   
518         {                                                                                                                             
519                float2 p, tpos;
520            tpos.x = (x + 0.5) * width; // 0..1
521            tpos.y = (y + 0.5) * width; // 0..1
522           
523            p = tpos.xy;   
524            p = 2.0 * p - 1.0; //-1..1
525                           
526                L1 = float3(p.x - width, p.y - width, 1);       
527                L2 = float3(p.x + width, p.y - width, 1);       
528                L3 = float3(p.x + width, p.y + width, 1);       
529                L4 = float3(p.x - width, p.y + width, 1);
530                L = float3(p.x, p.y, 1);
531                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
532                d = texCUBE(SmallEnvironmentMapSampler, L).a;   
533               
534                I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);                     
535                //I += Le / 16.0;
536         }                                                                                                                                                     
537                       
538        for (int x = 0; x < M; x++)                     
539         for (int y = 0; y < M; y++)                                                                                   
540         {                                                                                                                             
541                float2 p, tpos;
542            tpos.x = (x + 0.5) * width; // 0..1
543            tpos.y = (y + 0.5) * width; // 0..1
544           
545            p = tpos.xy;   
546            p = 2.0 * p - 1.0; //-1..1
547                           
548                L4 = float3(p.x - width, p.y - width, -1);     
549                L3 = float3(p.x + width, p.y - width, -1);     
550                L2 = float3(p.x + width, p.y + width, -1);     
551                L1 = float3(p.x - width, p.y + width, -1);
552                L = float3(p.x, p.y, -1);
553                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
554                d = texCUBE(SmallEnvironmentMapSampler, L).a;   
555               
556                I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);                     
557                //I += Le / 16.0;
558         }     
559         
560        for (int x = 0; x < M; x++)                     
561         for (int y = 0; y < M; y++)                                                                                   
562         {                                                                                                                             
563                float2 p, tpos;
564            tpos.x = (x + 0.5) * width; // 0..1
565            tpos.y = (y + 0.5) * width; // 0..1
566           
567            p = tpos.xy;   
568            p = 2.0 * p - 1.0; //-1..1
569                           
570                L4 = float3(p.x - width, 1, p.y - width);
571                L3 = float3(p.x + width, 1, p.y - width);       
572                L2 = float3(p.x + width, 1, p.y + width);       
573                L1 = float3(p.x - width, 1, p.y + width);                       
574                L = float3(p.x, 1, p.y);
575                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
576                d = texCUBE(SmallEnvironmentMapSampler, L).a;   
577               
578                I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);                     
579                //I += Le / 16.0;
580         }             
581         
582        for (int x = 0; x < M; x++)                     
583         for (int y = 0; y < M; y++)                                                                                   
584         {                                                                                                                             
585                float2 p, tpos;
586            tpos.x = (x + 0.5) * width; // 0..1
587            tpos.y = (y + 0.5) * width; // 0..1
588           
589            p = tpos.xy;   
590            p = 2.0 * p - 1.0; //-1..1
591                           
592                L1 = float3(p.x - width, -1, p.y - width);
593                L2 = float3(p.x + width, -1, p.y - width);     
594                L3 = float3(p.x + width, -1, p.y + width);     
595                L4 = float3(p.x - width, -1, p.y + width);                     
596                L = float3(p.x, -1, p.y);
597                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
598                d = texCUBE(SmallEnvironmentMapSampler, L).a;   
599               
600                I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);                     
601                //I += Le / 16.0;
602         }
603         for (int x = 0; x < M; x++)                   
604         for (int y = 0; y < M; y++)                                                                                   
605         {                                                                                                                             
606                float2 p, tpos;
607            tpos.x = (x + 0.5) * width; // 0..1
608            tpos.y = (y + 0.5) * width; // 0..1
609           
610            p = tpos.xy;   
611            p = 2.0 * p - 1.0; //-1..1
612                           
613                L1 = float3(1, p.x - width, p.y - width);
614                L2 = float3(1, p.x + width, p.y - width);       
615                L3 = float3(1, p.x + width, p.y + width);       
616                L4 = float3(1, p.x - width, p.y + width);       
617                L = float3(1, p.x, p.y);
618                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
619                d = texCUBE(SmallEnvironmentMapSampler, L).a;   
620               
621                I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);                     
622                //I += Le / 16.0;
623         }
624         
625         for (int x = 0; x < M; x++)                   
626         for (int y = 0; y < M; y++)                                                                                   
627         {                                                                                                                             
628                float2 p, tpos;
629            tpos.x = (x + 0.5) * width; // 0..1
630            tpos.y = (y + 0.5) * width; // 0..1
631           
632            p = tpos.xy;   
633            p = 2.0 * p - 1.0; //-1..1
634                           
635                L4 = float3(-1, p.x - width, p.y - width);
636                L3 = float3(-1, p.x + width, p.y - width);     
637                L2 = float3(-1, p.x + width, p.y + width);     
638                L1 = float3(-1, p.x - width, p.y + width);     
639                L = float3(-1, p.x, p.y);
640                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
641                d = texCUBE(SmallEnvironmentMapSampler, L).a;   
642               
643                I += 0.5 * Le * GetContibution( L1, L2, L3, L4, pos, N, d);                     
644                //I += Le / 16.0;
645         }                                                                                                                                                                     
646        return intensity * I;                                                                                                                   
647}
648
649
650//--------------------------------------------------------------------------------------
651// Shading the environment
652//--------------------------------------------------------------------------------------
653
654/// \brief Input for vertex shader IlluminatedSceneVS().
655struct _IlluminatedSceneVS_input {
656    float4 Position : POSITION;
657    float3 Normal : NORMAL;
658    float2 TexCoord : TEXCOORD0;
659};
660
661/// \brief Input for pixel shader IlluminatedScenePS().
662struct _IlluminatedSceneVS_output {
663    float4 hPosition : POSITION;
664    float2 TexCoord : TEXCOORD0;
665    float3 Position : TEXCOORD1;
666};
667
668_IlluminatedSceneVS_output IlluminatedSceneVS( _IlluminatedSceneVS_input IN )
669{
670        _IlluminatedSceneVS_output OUT;
671    OUT.hPosition = mul( IN.Position, WorldViewProjection );
672   
673    // texel_size as uniform parameter
674        OUT.hPosition.x -= texel_size * OUT.hPosition.w;
675        OUT.hPosition.y += texel_size * OUT.hPosition.w;
676
677    if (iShowCubeMap > 0)
678    {
679                // if one of the cube maps is displayed on the walls,
680                // position is simply forwarded
681                OUT.Position = IN.Position;
682        }
683        else
684        {
685                // also consider camera orientation
686                OUT.Position = mul( IN.Position, WorldView );
687        }
688   
689    OUT.TexCoord = IN.TexCoord;
690    return OUT;
691}
692
693/// Displays the environment with a simple shading
694float4 IlluminatedScenePS( _IlluminatedSceneVS_output IN ) : COLOR0
695{
696    float3 color = objColor;// * tex2D(DecorationSampler, IN.TexCoord);
697    /*
698    if (iShowCubeMap > 0)
699    {
700                // if one of the cube maps should be displayed on the walls,
701                // display it
702            color = texCUBE(EnvironmentMapSampler, IN.Position) * intensity;
703    }
704    else if (brightness>0)
705    {
706                // create an exponential falloff for each face of the room
707                float3 L = float3(2*IN.TexCoord.x-1, 2*IN.TexCoord.y-1, -1);
708                L = normalize(L);
709                float3 N = float3(0,0,1);
710                color *= abs(pow(dot(L,N), 4)) * brightness;
711        }
712        else color *= 0.7;
713        */     
714        float dist = length( IN.Position );
715    return float4(color, dist);
716}
717
718
719
720
721//--------------------------------------------------------------------------------------
722// Techniques
723//--------------------------------------------------------------------------------------
724
725
726/// a helpful macro to define techniques with a common vertex program
727#define TechniqueUsingCommonVS(name);                                                           \
728        technique name                                                                                                  \
729        {                                                                                                                               \
730            pass p0                                                                                                             \
731            {                                                                                                                   \
732                    VertexShader = compile vs_3_0 EnvMapVS();                           \
733                    PixelShader  = compile ps_3_0 name##PS();                           \
734                }                                                                                                                       \
735        }
736       
737TechniqueUsingCommonVS( EnvMapDiffuseClassic );
738TechniqueUsingCommonVS( EnvMapDiffuseLocalized5Tex );
739
740//TechniqueUsingCommonVS( EnvMapDiffuseLocalized );
741TechniqueUsingCommonVS( EnvMapDiffuseLocalized2 );
742TechniqueUsingCommonVS( EnvMapDiffuseLocalized4 );
743TechniqueUsingCommonVS( EnvMapDiffuseLocalized8 );
744TechniqueUsingCommonVS( EnvMapDiffuseLocalized16 );
745
746TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew );
747//TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew4 );
748//TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew8 );
749//TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew16 );
750
751#define ReduceTextureTechnique(M);                                                                      \
752        technique ReduceTexture##M                                                                              \
753        {                                                                                                                               \
754            pass p0                                                                                                             \
755            {                                                                                                                   \
756                    VertexShader = compile vs_3_0 ReduceTextureVS();            \
757                    PixelShader  = compile ps_3_0 ReduceTexture##M##PS();       \
758                }                                                                                                                       \
759        }
760
761ReduceTextureTechnique( 2 );
762ReduceTextureTechnique( 4 );
763ReduceTextureTechnique( 8 );
764ReduceTextureTechnique( 16 );
765
766#define ConvolutionTechnique(M);                                                                        \
767        technique Convolution##M                                                                                \
768        {                                                                                                                               \
769            pass p0                                                                                                             \
770            {                                                                                                                   \
771                    VertexShader = compile vs_3_0 ConvolutionVS();                      \
772                    PixelShader  = compile ps_3_0 Convolution##M##PS();         \
773                }                                                                                                                       \
774        }
775
776ConvolutionTechnique( 2 );
777ConvolutionTechnique( 4 );
778ConvolutionTechnique( 8 );
779ConvolutionTechnique( 16 );
780
781/// a helpful macro to define techniques
782/// where the name of EnvMapVS program is <TechniqueName>VS
783/// and the name of PS program is <TechniqueName>PS
784#define Technique(name);                                                                \
785        technique name                                                                          \
786        {                                                                                                       \
787            pass p0                                                                                     \
788            {                                                                                           \
789                    VertexShader = compile vs_3_0 name##VS();   \
790                    PixelShader  = compile ps_3_0 name##PS();   \
791                }                                                                                               \
792        }
793
794Technique( IlluminatedScene );
795//Technique( Convolution );
796//Technique( ReduceTexture );
Note: See TracBrowser for help on using the repository browser.