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

Revision 1573, 27.3 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 L, float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, samplerCUBE cubemap)
462{
463        float d;
464        d = texCUBE(cubemap, L).a;     
465        //d = texCUBE(cubemap, L1).a;   
466        L1 = d * normalize(L1);
467        //d = texCUBE(cubemap, L2).a;   
468        L2 = d * normalize(L2);
469        //d = texCUBE(cubemap, L3).a;   
470        L3 = d * normalize(L3);
471        //d = texCUBE(cubemap, L4).a;   
472        L4 = d * normalize(L4);
473               
474       
475    float3 r1 = normalize(L1 - pos);   
476    float3 r2 = normalize(L2 - pos);
477    float3 r3 = normalize(L3 - pos);
478    float3 r4 = normalize(L4 - pos);
479       
480        float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N);
481        float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N);
482        float tri3 = acos(dot(r3, r4)) * dot(cross(r3, r4), N);
483        float tri4 = acos(dot(r4, r1)) * dot(cross(r4, r1), N);
484       
485        return max(tri1 + tri2 + tri3 + tri4, 0);       
486        //return tri1 + tri2 + tri3 + tri4;     
487}
488
489
490
491
492float4 EnvMapDiffuseLocalizedNewPS( _EnvMapVS_output IN ) : COLOR                       
493{               
494        float M = 4.0;                                                                                                                                                 
495        IN.View = -normalize( IN.View );                                                                                               
496        IN.Normal = normalize( IN.Normal );                                                                                             
497        IN.Position -= reference_pos.xyz;                               
498        float3 pos = IN.Position.xyz;   
499       
500        //return        reference_pos;
501        //return  texCUBE(SmallEnvironmentMapSampler, pos);     
502                                                                                               
503        float3 N =IN.Normal;                                                                                                                                                           
504        float3 R = -reflect( IN.View, IN.Normal );             
505                                                                                                       
506    float4 I = 0;                                                                       
507        float3 L1, L2, L3, L4, L;                                               
508        float4 Le;                                                                             
509        float width = 1.0 / M;                                                 
510        float width2 = width * 2;
511        float d;
512       
513        for (float x = 0.5; x < M; x++)                 
514         for (float y = 0.5; y < M; y++)                                                                                       
515         {                                                                                                                             
516                float2 p, tpos;
517            tpos.x = x * width;
518            tpos.y = y * width;
519           
520            p = tpos.xy;   
521            p = 2.0 * p - 1.0; //-1..1
522                           
523                L1 = float3(p.x - width, p.y - width, 1);       
524                L2 = float3(p.x + width, p.y - width, 1);       
525                L3 = float3(p.x + width, p.y + width, 1);       
526                L4 = float3(p.x - width, p.y + width, 1);
527                L = float3(p.x, p.y, 1);
528                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
529               
530                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
531                       
532        }                                                                                                                                                       
533       
534        for (float x = 0; x < M; x++)                   
535         for (float y = 0; y < M; y++)                                                                                 
536         {                                                                                                                             
537                float2 p, tpos;
538            tpos.x = x * width; // 0..1
539            tpos.y = y * width; // 0..1
540           
541            p = tpos.xy;   
542            p = 2.0 * p - 1.0; //-1..1
543                           
544                L4 = float3(p.x - width, p.y - width, -1);     
545                L3 = float3(p.x + width, p.y - width, -1);     
546                L2 = float3(p.x + width, p.y + width, -1);     
547                L1 = float3(p.x - width, p.y + width, -1);
548                L = float3(p.x, p.y, -1);
549                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
550               
551                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
552         }     
553         
554        for (float x = 0; x < M; x++)                   
555         for (float y = 0; y < M; y++)                                                                                 
556         {                                                                                                                             
557                float2 p, tpos;
558            tpos.x = x * width; // 0..1
559            tpos.y = y * width; // 0..1
560           
561            p = tpos.xy;   
562            p = 2.0 * p - 1.0; //-1..1
563                           
564                L4 = float3(p.x - width, 1, p.y - width);
565                L3 = float3(p.x + width, 1, p.y - width);       
566                L2 = float3(p.x + width, 1, p.y + width);       
567                L1 = float3(p.x - width, 1, p.y + width);                       
568                L = float3(p.x, 1, p.y);
569                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
570               
571                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
572         }             
573         
574        for (float x = 0; x < M; x++)                   
575         for (float y = 0; y < M; y++)                                                                                 
576         {                                                                                                                             
577                float2 p, tpos;
578            tpos.x = x * width; // 0..1
579            tpos.y = y * width; // 0..1
580           
581            p = tpos.xy;   
582            p = 2.0 * p - 1.0; //-1..1
583                           
584                L1 = float3(p.x - width, -1, p.y - width);
585                L2 = float3(p.x + width, -1, p.y - width);     
586                L3 = float3(p.x + width, -1, p.y + width);     
587                L4 = float3(p.x - width, -1, p.y + width);                     
588                L = float3(p.x, -1, p.y);
589                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
590               
591                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
592         }
593         
594         for (float x = 0; x < M; x++)                 
595                for (float y = 0; y < M; y++)                                                                                   
596                {                                                                                                                               
597                float2 p, tpos;
598            tpos.x = x * width; // 0..1
599            tpos.y = y * width; // 0..1
600           
601            p = tpos.xy;   
602            p = 2.0 * p - 1.0; //-1..1
603                           
604                L1 = float3(1, p.x - width, p.y - width);
605                L2 = float3(1, p.x + width, p.y - width);       
606                L3 = float3(1, p.x + width, p.y + width);       
607                L4 = float3(1, p.x - width, p.y + width);       
608                L = float3(1, p.x, p.y);
609                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
610               
611                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                 
612        }
613         
614        for (float x = 0; x < M; x++)                   
615         for (float y = 0; y < M; y++)                                                                                 
616         {                                                                                                                             
617                float2 p, tpos;
618            tpos.x = x * width; // 0..1
619            tpos.y = y * width; // 0..1
620           
621            p = tpos.xy;   
622            p = 2.0 * p - 1.0; //-1..1
623                           
624                L4 = float3(-1, p.x - width, p.y - width);
625                L3 = float3(-1, p.x + width, p.y - width);     
626                L2 = float3(-1, p.x + width, p.y + width);     
627                L1 = float3(-1, p.x - width, p.y + width);     
628                L = float3(-1, p.x, p.y);
629                Le = float4(texCUBE(SmallEnvironmentMapSampler, L).rgb, 1);
630               
631                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, SmallEnvironmentMapSampler);                         
632         }                                                                                                                                                             
633        return intensity * I;                                                                                                                   
634}
635
636float4 P2PContr(float3 N, float3 Nl, float3 pos, float3 L, samplerCUBE cubemap)
637{
638        Nl = normalize(Nl);
639        L = normalize(L);
640        float4 Le = float4(texCUBE(cubemap, L).rgb, 1);
641        float d = texCUBE(cubemap, L).a;
642        float3 Lpos = L * d;
643        float3 Ldir = Lpos - pos;
644        float dist = dot(Ldir, Ldir);
645        Ldir = normalize(Ldir);
646       
647        return Le * max(dot(N, Ldir),0) * dot(Nl, -1 * Ldir) / dist;   
648}
649
650float4 EnvMapDiffuseP2PPS( _EnvMapVS_output IN ) : COLOR                       
651{               
652        float M = 4.0;                                                                                                                                                 
653        IN.View = -normalize( IN.View );                                                                                               
654        IN.Normal = normalize( IN.Normal );                                                                                             
655        IN.Position -= reference_pos.xyz;                               
656        float3 pos = IN.Position.xyz;   
657       
658        //return        reference_pos;
659        //return  texCUBE(SmallEnvironmentMapSampler, pos);     
660                                                                                               
661        float3 N =IN.Normal;                                                                                                                                                           
662                                                                                                       
663    float4 I = 0;                                                                       
664        float3 L;                                               
665        float4 Le;                                                                             
666        float width = 1.0 / M;
667        float d;                                                       
668       
669        for (float x = 0.5; x < M; x++)                 
670         for (float y = 0.5; y < M; y++)                                                                                       
671         {                                                                                                                             
672                float2 p, tpos;
673            tpos.x = x * width;
674            tpos.y = y * width;
675           
676            p = tpos.xy;   
677            p = 2.0 * p - 1.0; //-1..1
678                           
679                I += P2PContr(N, float3(0,0,-1), pos, float3(p.x, p.y, 1), SmallEnvironmentMapSampler);
680                I += P2PContr(N, float3(0,0,1), pos, float3(-p.x, p.y, -1), SmallEnvironmentMapSampler);
681                I += P2PContr(N, float3(-1,0,0), pos, float3(1, p.y, -p.x), SmallEnvironmentMapSampler);
682                I += P2PContr(N, float3(1,0,0), pos, float3(-1, p.y, p.x), SmallEnvironmentMapSampler);
683                I += P2PContr(N, float3(0,-1,0), pos, float3(p.x, 1, -p.y), SmallEnvironmentMapSampler);
684                I += P2PContr(N, float3(0,1,0), pos, float3(p.x, -1, p.y), SmallEnvironmentMapSampler);
685        }
686                                                                                                                                               
687        return intensity * I;                                                                                                                   
688}
689//--------------------------------------------------------------------------------------
690// Shading the environment
691//--------------------------------------------------------------------------------------
692
693/// \brief Input for vertex shader IlluminatedSceneVS().
694struct _IlluminatedSceneVS_input {
695    float4 Position : POSITION;
696    float3 Normal : NORMAL;
697    float2 TexCoord : TEXCOORD0;
698};
699
700/// \brief Input for pixel shader IlluminatedScenePS().
701struct _IlluminatedSceneVS_output {
702    float4 hPosition : POSITION;
703    float2 TexCoord : TEXCOORD0;
704    float3 Position : TEXCOORD1;
705};
706
707_IlluminatedSceneVS_output IlluminatedSceneVS( _IlluminatedSceneVS_input IN )
708{
709        _IlluminatedSceneVS_output OUT;
710    OUT.hPosition = mul( IN.Position, WorldViewProjection );
711   
712    // texel_size as uniform parameter
713        OUT.hPosition.x -= texel_size * OUT.hPosition.w;
714        OUT.hPosition.y += texel_size * OUT.hPosition.w;
715
716    if (iShowCubeMap > 0)
717    {
718                // if one of the cube maps is displayed on the walls,
719                // position is simply forwarded
720                OUT.Position = IN.Position;
721        }
722        else
723        {
724                // also consider camera orientation
725                OUT.Position = mul( IN.Position, WorldView );
726        }
727   
728    OUT.TexCoord = IN.TexCoord;
729    return OUT;
730}
731
732/// Displays the environment with a simple shading
733float4 IlluminatedScenePS( _IlluminatedSceneVS_output IN ) : COLOR0
734{
735    float3 color = objColor * tex2D(DecorationSampler, IN.TexCoord);
736   /*
737    if (iShowCubeMap > 0)
738    {
739                // if one of the cube maps should be displayed on the walls,
740                // display it
741            color = texCUBE(EnvironmentMapSampler, IN.Position) * intensity;
742    }
743    else if (brightness>0)
744    {
745                // create an exponential falloff for each face of the room
746                float3 L = float3(2*IN.TexCoord.x-1, 2*IN.TexCoord.y-1, -1);
747                L = normalize(L);
748                float3 N = float3(0,0,1);
749                color *= abs(pow(dot(L,N), 4)) * brightness;
750        }
751        else color *= 0.7;*/
752               
753        float dist = length( IN.Position );
754    return float4(color, dist);
755}
756
757
758
759
760//--------------------------------------------------------------------------------------
761// Techniques
762//--------------------------------------------------------------------------------------
763
764
765/// a helpful macro to define techniques with a common vertex program
766#define TechniqueUsingCommonVS(name);                                                           \
767        technique name                                                                                                  \
768        {                                                                                                                               \
769            pass p0                                                                                                             \
770            {                                                                                                                   \
771                    VertexShader = compile vs_3_0 EnvMapVS();                           \
772                    PixelShader  = compile ps_3_0 name##PS();                           \
773                }                                                                                                                       \
774        }
775       
776TechniqueUsingCommonVS( EnvMapDiffuseClassic );
777TechniqueUsingCommonVS( EnvMapDiffuseLocalized5Tex );
778
779//TechniqueUsingCommonVS( EnvMapDiffuseLocalized );
780TechniqueUsingCommonVS( EnvMapDiffuseLocalized2 );
781TechniqueUsingCommonVS( EnvMapDiffuseLocalized4 );
782TechniqueUsingCommonVS( EnvMapDiffuseLocalized8 );
783TechniqueUsingCommonVS( EnvMapDiffuseLocalized16 );
784
785TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew );
786TechniqueUsingCommonVS( EnvMapDiffuseP2P );
787//TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew4 );
788//TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew8 );
789//TechniqueUsingCommonVS( EnvMapDiffuseLocalizedNew16 );
790
791#define ReduceTextureTechnique(M);                                                                      \
792        technique ReduceTexture##M                                                                              \
793        {                                                                                                                               \
794            pass p0                                                                                                             \
795            {                                                                                                                   \
796                    VertexShader = compile vs_3_0 ReduceTextureVS();            \
797                    PixelShader  = compile ps_3_0 ReduceTexture##M##PS();       \
798                }                                                                                                                       \
799        }
800
801ReduceTextureTechnique( 2 );
802ReduceTextureTechnique( 4 );
803ReduceTextureTechnique( 8 );
804ReduceTextureTechnique( 16 );
805
806#define ConvolutionTechnique(M);                                                                        \
807        technique Convolution##M                                                                                \
808        {                                                                                                                               \
809            pass p0                                                                                                             \
810            {                                                                                                                   \
811                    VertexShader = compile vs_3_0 ConvolutionVS();                      \
812                    PixelShader  = compile ps_3_0 Convolution##M##PS();         \
813                }                                                                                                                       \
814        }
815
816ConvolutionTechnique( 2 );
817ConvolutionTechnique( 4 );
818ConvolutionTechnique( 8 );
819ConvolutionTechnique( 16 );
820
821/// a helpful macro to define techniques
822/// where the name of EnvMapVS program is <TechniqueName>VS
823/// and the name of PS program is <TechniqueName>PS
824#define Technique(name);                                                                \
825        technique name                                                                          \
826        {                                                                                                       \
827            pass p0                                                                                     \
828            {                                                                                           \
829                    VertexShader = compile vs_3_0 name##VS();   \
830                    PixelShader  = compile ps_3_0 name##PS();   \
831                }                                                                                               \
832        }
833
834Technique( IlluminatedScene );
835//Technique( Convolution );
836//Technique( ReduceTexture );
Note: See TracBrowser for help on using the repository browser.