source: GTP/trunk/App/Demos/Illum/RayTraceEffects/RayTraceEffects.fx @ 754

Revision 754, 32.5 KB checked in by szirmay, 19 years ago (diff)

Added a folder remotely

Line 
1#define SHADOWMAP_SIZE  512.0f                          // The size of ShadowMap
2
3// Offsets in the ShadowMap
4#define HALF    0.5f  / SHADOWMAP_SIZE
5#define ONE             1.0f  / SHADOWMAP_SIZE
6#define TWO             2.0f  / SHADOWMAP_SIZE
7#define THREE   3.0f  / SHADOWMAP_SIZE
8#define FIVE    5.0f  / SHADOWMAP_SIZE
9#define TEN             10.0f / SHADOWMAP_SIZE
10
11#define AMB float4(0.5,0.5,0.5,1)                       // Power of the ambient light
12#define FI 1000                                                         // Summarized power of the light source
13
14//----------------------------------------------
15// GLOBAL VARIABLES
16//---------------------------------------------
17float3   g_vCameraPos3f;                                        // Camera position
18float3   g_vLightPos3f;                                         // Light source position
19int              g_iNumberOfIteration;                          // Number of iteration
20int      g_iObjectID;                                           // ID of the current object
21int              g_iPhotonMapSize;                                      // PhotonMap size
22float    g_fFresnelFactor;                                      // Fresnel factor
23float    g_fPower;                                                      // Power of light source
24float    g_fRefractionIndex;                            // Refraction index     
25float    g_fShadowIntensity;                            // Shadow intensity
26float    g_fCausticsIntensity;                          // The intensity of a caustics snippet                         
27bool     g_bShadowON;                                           // Shadow is used?
28bool     g_bShowHelp;                                           // Darken the scene if help is on
29
30float4x4 g_mWorldView;                                          // WorldView transformation matrix
31float4x4 g_mWorldViewProjection;                        // WorldViewProjection transformation matrix
32float4x4 g_mWorldCenterObject;                          // World transformation matrix of CenterObject
33float4x4 g_mLightViewTexBias;                           // Light space transformation matrix
34
35texture  g_txRoomMap;                                           // Contains the original texture of the Room
36texture  g_txRoomCubeMapColorDistanceMap;       // Contains the color and distance information in a CubeMap
37texture  g_txRoomCubeMapUVMap;                          // Contains the UV and ID of the object in a CubeMap
38texture  g_txPhotonUVMap;                                       // Contains the photon hits UV and ID information
39texture  g_txPowerOfSnippetMap;                         // Contains the Gaussian filter
40texture  g_txRoomModifyMap;                                     // Contains the texture of the Room with shadow and Caustics effect and black edges
41texture  g_txRoomLastMap;                                       // Contains the texture of the Room
42
43// Contain the textures of the Columns with shadow and Caustics effect
44texture g_txColumnModifyTexture0, g_txColumnLastTexture0;
45texture g_txColumnModifyTexture1, g_txColumnLastTexture1;
46texture g_txColumnModifyTexture2, g_txColumnLastTexture2;
47texture g_txColumnModifyTexture3, g_txColumnLastTexture3;
48
49texture g_txColumnOriginalTexture;                      // Contains the original texture of a Column
50texture  g_txShadowMap;                                         // Contains the shadowMap
51
52
53//-----------------------------------------------------------------------------
54// SAMPLERS
55// Each texture must have its own sampler
56//-----------------------------------------------------------------------------
57
58//-----------------------------------------------------------------------------
59// macro definition for filtered samplers
60//-----------------------------------------------------------------------------
61
62#define SAMPLER_LINEAR(g_samplerMap, g_txMap);  \
63        sampler2D g_samplerMap = sampler_state {        \
64    Texture = <g_txMap>;                                                \
65    MinFilter = Linear;                                                 \
66    MagFilter = Linear;                                                 \
67    MipFilter = Linear;                                                 \
68    AddressU  = BORDER;                                                 \
69    AddressV  = BORDER;                                                 \
70};
71
72SAMPLER_LINEAR(g_samplerRoomMap,                        g_txRoomMap);
73SAMPLER_LINEAR(g_samplerRoomLastMap,            g_txRoomLastMap);
74SAMPLER_LINEAR(g_samplerPhotonUVMap,            g_txPhotonUVMap);
75SAMPLER_LINEAR(g_samplerPowerOfSnippetMap,      g_txPowerOfSnippetMap);
76SAMPLER_LINEAR(g_samplerColumnLast0Map,         g_txColumnLastTexture0);
77SAMPLER_LINEAR(g_samplerColumnLast1Map,         g_txColumnLastTexture1);
78SAMPLER_LINEAR(g_samplerColumnLast2Map,         g_txColumnLastTexture2);
79SAMPLER_LINEAR(g_samplerColumnLast3Map,         g_txColumnLastTexture3);
80SAMPLER_LINEAR(g_samplerColumnOriginalMap,      g_txColumnOriginalTexture);
81
82//-----------------------------------------------------------------------------
83// macro definition for non-filtered samplers
84//-----------------------------------------------------------------------------
85
86#define SAMPLER_POINT(g_samplerMap, g_txMap);   \
87        sampler2D g_samplerMap = sampler_state {        \
88    Texture = <g_txMap>;                                                \
89    MinFilter = Point;                                                  \
90    MagFilter = Point;                                                  \
91    MipFilter = Point;                                                  \
92    AddressU  = BORDER;                                                 \
93    AddressV  = BORDER;                                                 \
94};
95
96SAMPLER_POINT(g_samplerRoomModifyMap,           g_txRoomModifyMap);     // must be point, it will be used for filtering
97SAMPLER_POINT(g_samplerColumnModify0Map,        g_txColumnModifyTexture0);     
98SAMPLER_POINT(g_samplerColumnModify1Map,        g_txColumnModifyTexture1);     
99SAMPLER_POINT(g_samplerColumnModify2Map,        g_txColumnModifyTexture2);     
100SAMPLER_POINT(g_samplerColumnModify3Map,        g_txColumnModifyTexture3);     
101
102sampler2D g_samplerShadowMap = sampler_state
103{
104    Texture = <g_txShadowMap>;
105    MinFilter = Linear;
106    MagFilter = Linear;
107    MipFilter = Linear;
108    AddressU  = CLAMP;
109    AddressV  = CLAMP;
110};
111
112samplerCUBE g_samplerRoomCubeMapColorDistanceMap = sampler_state
113{
114    Texture = <g_txRoomCubeMapColorDistanceMap>;
115    MinFilter = Linear;
116    MagFilter = Linear;
117    MipFilter = Linear;
118    AddressU  = BORDER;
119    AddressV  = BORDER;
120};
121
122samplerCUBE g_samplerRoomCubeMapUVMap = sampler_state
123{
124    Texture = <g_txRoomCubeMapUVMap>;
125    MinFilter = Linear;
126    MagFilter = Linear;
127    MipFilter = Linear;
128    AddressU  = BORDER;
129    AddressV  = BORDER;
130};
131
132samplerCUBE g_samplerRoomCubeMapUVMap_NO_FILTER = sampler_state
133{
134    Texture = <g_txRoomCubeMapUVMap>;
135    MinFilter = Point;
136    MagFilter = Point;
137    MipFilter = Point;
138    AddressU  = BORDER;
139    AddressV  = BORDER;
140};
141
142//-----------------------------------------------------------------------------
143// FUNCTIONS
144//-----------------------------------------------------------------------------
145
146// This function is called several times.
147float3 Hit( float3 x, float3 R, samplerCUBE mp )
148{
149        float rl = texCUBE( mp, R ).a;                                                  // |r|
150        float ppp = length( x ) / texCUBE( mp, x ).a;                   // |p|/|p’|
151        float dun = 0, pun = ppp, dov = 0, pov;
152        float dl = rl * ( 1 - ppp );                                                    // eq. 2
153        float3 l = x + R * dl;                                                                  // ray equation
154
155        // iteration
156        for( int i = 0; i < g_iNumberOfIteration; i++ )
157        {
158                float llp = length( l ) / texCUBE( mp,l ).a;            // |l|/|l’|
159                if ( llp < 0.999f )                                                                     // undershooting
160                {
161                        dun = dl; pun = llp;                                                    // last undershooting
162                        dl += ( dov == 0 ) ? rl * ( 1 - llp ) :                 // eq. 2
163                                ( dl - dov ) * ( 1 - llp ) / ( llp - pov );     // eq. 3
164                } else if ( llp > 1.001f )                                                      // overshooting
165                {
166                        dov = dl; pov = llp;                                                    // last overshooting
167                        dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
168                }
169                l = x + R * dl;                                                                         // ray equation
170        }
171        return l;                                                                                               // computed hit point
172}
173
174//*****************************************************************************
175
176// This function enlarges the usefull areas of the atlases.
177float4 FlowLight( sampler2D mp, float2 Tex )
178{
179        // Reads the current pixel
180        float4 preVal = tex2D( mp, Tex );
181        // If it is an unused pixel (.a=0)
182        if ( preVal.a == 0 )
183        {
184                float dist = ONE;
185                if( g_iObjectID != -1 ) dist *= 3.0f;
186       
187                // Reads a neighbor
188                float2 modTex = Tex + float2( dist, 0.0 );
189                float4 candid = tex2D( mp, modTex );
190               
191                // Calculate its intensity.
192                // If it is more intensive than the previous (original) one, it is stored.
193                if( dot( candid.xyz, candid.xyz ) > dot( preVal.xyz, preVal.xyz ) )
194                        preVal = candid;
195               
196                // Reads another neighbor
197                modTex += float2( -dist, dist );
198                candid = tex2D( mp, modTex );
199               
200                // Calculate its intensity.
201                // If it is more intensive than the previous one, it is stored.
202                if( dot( candid.xyz, candid.xyz ) > dot( preVal.xyz, preVal.xyz ) )
203                        preVal = candid;
204       
205                // Reads another neighbor. If it is more intensive than the previous, it is stored.
206                modTex += float2( -dist, -dist );
207                candid = tex2D( mp, modTex );
208                if( dot( candid.xyz, candid.xyz ) > dot( preVal.xyz, preVal.xyz ) )
209                        preVal = candid;
210       
211                // Reads another neighbor. If it is more intensive than the previous, it is stored.
212                modTex += float2( dist, -dist );
213                candid = tex2D( mp, modTex );
214                if( dot( candid.xyz, candid.xyz ) > dot( preVal.xyz, preVal.xyz ) )
215                        preVal = candid;
216        }       
217        return preVal;
218}
219
220
221//-----------------------------------------------------------------------------
222// SHADERS
223// Here are the different shaders.
224//-----------------------------------------------------------------------------
225
226
227//*****************************************************************************
228//-----------------------------------------------------------------------------
229// RenderRoomColorDistance
230// This shader stores the viewable object's color and its distance from the CenterObject's position.
231// The result is stored in a CubeMap.
232//-----------------------------------------------------------------------------
233void RenderRoomColorDistanceVS(
234        float4 Pos     : POSITION,
235        float2 Tex     : TEXCOORD0,
236    out float4 outhPos : POSITION,
237    out float2 outTex  : TEXCOORD0,
238    out float4 outPos  : TEXCOORD1 )
239{
240    outhPos = mul( Pos, g_mWorldViewProjection );
241    outTex  = Tex;
242    // Calculates the world position
243    outPos  = mul( Pos, g_mWorldView );
244}
245// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
246
247float4 RenderRoomColorDistancePS(
248        float2 Tex : TEXCOORD0,
249        float4 Pos : TEXCOORD1 ) : COLOR
250{
251    // The color of the viewable object in the current pixel.
252    // In the scene there are 2 different textured object. (the room, and the Columns)
253    // Every column has the same brdf texture.
254    float3 retColor = float4(0,0,0,0);
255    if ( g_iObjectID == -1 ) retColor = float4( tex2D(           g_samplerRoomMap, Tex ).xyz, 1);
256    else                                         retColor = float4( tex2D( g_samplerColumnOriginalMap, Tex ).xyz, 1);
257   
258    // The distance from the Center object
259    float Distance = (float) ( length( Pos.xyz ) );
260    return float4( retColor, Distance );                // Color(x,y,z) + Distance(w)
261}
262//*****************************************************************************
263
264
265//-----------------------------------------------------------------------------
266// RenderRoomUV
267// This shader stores the viewable object's UV and unique ObjectID.
268// The ObjectID identify the object, because the UV pair is not enough.
269// The result is stored in a CubeMap.
270//-----------------------------------------------------------------------------
271void RenderRoomUVVS(
272        float4 Pos     : POSITION,
273        float2 Tex     : TEXCOORD0,
274    out float4 outhPos : POSITION,
275    out float2 outTex  : TEXCOORD0 )
276{
277    outhPos = mul( Pos, g_mWorldViewProjection );
278    outTex  = Tex;
279}
280// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
281
282float4 RenderRoomUVPS(
283       float2 Tex : TEXCOORD0) : COLOR
284{       // The UV value and the ID of the viewable object in the current pixel
285                                                                                                // Here the "1" value is unnecessary
286    return float4( Tex, g_iObjectID, 1 );               // Tex(x,y) + g_iObjectID(z) + "1"(w)
287}
288//*****************************************************************************
289
290
291//-----------------------------------------------------------------------------
292// RenderUmbra
293// This shader copies the brdf color from the original texture Atlas of the Object to the
294// actual texture Atlas of the Object.
295// It is needed, because the brdf value is modified with shadow.
296// If the pixel is in shadow, the brdf value is decreased.
297// The distance from the light source is also a modifying factor.
298// The result is stored in a 2D map (Atlas).
299//-----------------------------------------------------------------------------
300void RenderUmbraVS(
301        float4 Pos     : POSITION,
302        float2 Tex     : TEXCOORD0,
303        float3 Normal  : NORMAL,
304    out float4 outhPos : POSITION,
305    out float2 outTex  : TEXCOORD0,
306    out float3 outPos  : TEXCOORD1,
307    out float  outCosT : TEXCOORD2,
308    out float4 outLPos : TEXCOORD3)
309{
310        // The Room has no offset.
311        // The Columns have offset from the world's center position.
312        float4 PosWorld = Pos;
313        if ( g_iObjectID > -0.1f ) PosWorld = mul( Pos, g_mWorldView );
314        // Light vector
315        float3 L = normalize( g_vLightPos3f - PosWorld.xyz );
316
317        // Transformation between texture and screen space.
318        // HALF is an offset between the two space.
319    outhPos.x = Tex.x * 2.0f - 1.0f - HALF;
320    outhPos.y = 1.0f - Tex.y * 2.0f + HALF;
321    outhPos.z = 0;
322    outhPos.w = 1;
323   
324    outTex  = Tex;
325    outPos  = PosWorld.xyz;
326    // The direction of the normal of the Room and the Columns are different.
327    if (g_iObjectID < -0.5f)
328                Normal = -Normal;
329    outCosT = dot( Normal, L );
330    outLPos = mul( PosWorld, g_mLightViewTexBias );
331}
332// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
333
334float4 RenderUmbraPS(
335       float2 Tex  : TEXCOORD0,
336       float3 Pos  : TEXCOORD1,
337       float  CosT : TEXCOORD2,
338       float4 LPos : TEXCOORD3 ) : COLOR
339{
340        // return Color
341        float4 retColor = float4(0,0,0,0);
342        // ShadowMap distance values
343       
344        // Read the brdf from the atlas of the object
345        // The Room and the Columns have 2 different brdf Atlas.
346        if ( g_iObjectID == -1 )
347                retColor = float4( tex2D(           g_samplerRoomMap, Tex ).xyz, 1);
348        else
349                retColor = float4( tex2D( g_samplerColumnOriginalMap, Tex ).xyz, 1);
350
351        // Use shadow?
352        if (g_bShadowON)
353        {
354                float visibility = 0;
355
356                for (float y = -1.5f; y < 2.0f; y += 1.0f){
357                        for (float x = -1.5f; x < 2.0f; x += 1.0f) {
358                                float4 coord = float4( LPos.xyz / LPos.w, 0 );
359                                coord.xy   += (float2(x,y)/(float)ONE);
360                                if( LPos.z - tex2Dlod( g_samplerShadowMap, coord ).x < 0.2 )
361                                {
362                                        visibility++;
363                                }
364                        }
365                }
366                visibility = visibility / 16.0f;
367                if( visibility < 16 )
368                {
369                        visibility = visibility + ( 0.01f + 1 - g_fShadowIntensity );
370                }
371                if( visibility > 1.0f )
372                {
373                        visibility = 1.0f;
374                }
375                retColor *= visibility;
376        }
377       
378        // The intensity of the brdf depends on the position of the light source.
379       
380        float3 L = g_vLightPos3f;
381        float pll2 = dot( ( Pos - L ), ( Pos - L ) );
382        retColor *= AMB + ( FI / pll2 ) * CosT * g_fCausticsIntensity;
383
384        if (g_bShowHelp)        // light's effect is decreased to improve the visibility of help text
385                retColor *= 0.5f;
386       
387    return retColor;
388}
389//*****************************************************************************
390
391
392//-----------------------------------------------------------------------------
393// RenderRoomAndColumnsScreen
394// This shader render the scene's objects to the screen with their own atlas.
395//-----------------------------------------------------------------------------
396void RenderRoomAndColumnsScreenVS(
397
398        float4 Pos         : POSITION,
399        float2 Tex         : TEXCOORD0,
400    out float4 outhPos : POSITION,
401    out float2 outTex  : TEXCOORD0 )
402{
403    outhPos = mul( Pos, g_mWorldViewProjection );
404    outTex  = Tex;
405}
406// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
407
408float4 RenderRoomAndColumnsScreenPS(
409       float2 Tex : TEXCOORD0) : COLOR
410{
411        float4 retColor = float4(1,1,1,1);
412       
413        // Five different atlas for the five object.
414        if (g_iObjectID == -1 ) retColor = float4( tex2D(    g_samplerRoomLastMap, Tex ).xyz, 1 );
415        if (g_iObjectID ==  0 ) retColor = float4( tex2D( g_samplerColumnLast0Map, Tex ).xyz, 1 );
416        if (g_iObjectID ==  1 ) retColor = float4( tex2D( g_samplerColumnLast1Map, Tex ).xyz, 1 );
417        if (g_iObjectID ==  2 ) retColor = float4( tex2D( g_samplerColumnLast2Map, Tex ).xyz, 1 );
418        if (g_iObjectID ==  3 ) retColor = float4( tex2D( g_samplerColumnLast3Map, Tex ).xyz, 1 );
419       
420        return retColor;
421}
422//*****************************************************************************
423
424
425//-----------------------------------------------------------------------------
426// RenderPhotonUVMap
427// This shader creates the PhotonUVTexture with Hit function.
428// It is a 2D map with the photon hits. It is created from the position of the light source.
429// Every pixel stores the UV, and ObjectID information.
430// The result is stored in a 2D map.
431//-----------------------------------------------------------------------------
432void RenderPhotonUVMapVS(
433        float4 Pos     : POSITION,
434        float3 Normal  : NORMAL,
435        float3 Tex         : TEXCOORD0,
436    out float4 outhPos : POSITION,
437    out float3 outTex  : TEXCOORD0,
438    out float3 outPos  : TEXCOORD1,
439    out float3 outN    : TEXCOORD2,
440    out float3 outL    : TEXCOORD3)
441{
442        // The offset (+float4...) is needed to move the Center Object into the center of the PhotonMap.
443    outhPos = mul( Pos, g_mWorldViewProjection ) + float4( -4.0f / (float)g_iPhotonMapSize , 4.0f / (float)g_iPhotonMapSize , 0, 0 );
444   
445    outTex  = Tex;
446    outPos  = mul( Pos, g_mWorldCenterObject );
447    outN    = Normal;   
448    outL    = normalize( outPos - g_vLightPos3f );
449}
450// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
451
452float4 RenderPhotonUVMapPS(
453        float3 Tex : TEXCOORD0,
454        float3 Pos : TEXCOORD1,
455        float3 N   : TEXCOORD2,
456        float3 L   : TEXCOORD3 ) : COLOR
457{
458        // By default, there is no Hit.
459        float a = -1;
460        float4 hitSurf = float4(0,0,0,0);
461       
462        if ( dot( L, N ) < -0.1 ){
463                L = normalize( L );
464                N = normalize( N );
465                float3 T = normalize( refract( L, N, g_fRefractionIndex ) );
466               
467                // Look up from the CubeMap
468                // The return value is a vector. The CubeUVMap is read at that position.
469                float3 TT = Hit( Pos, T, g_samplerRoomCubeMapColorDistanceMap );
470                hitSurf = texCUBE( g_samplerRoomCubeMapUVMap, TT );
471                // There is a Hit.
472                a = 1;
473        }
474        return float4( hitSurf.xyz, a );
475}
476//*****************************************************************************
477
478
479//-----------------------------------------------------------------------------
480// RenderPhotonUVMapClassic
481// This shader creates the PhotonUVTexture with classical method.
482// It is a 2D map with the photon hits. It is created from the position of the light source.
483// Every pixel stores the UV, and ObjectID information.
484// The result is stored in a 2D map.
485//-----------------------------------------------------------------------------
486void RenderPhotonUVMapClassicVS(
487        float4 Pos     : POSITION,
488        float3 Normal  : NORMAL,
489        float3 Tex         : TEXCOORD0,
490    out float4 outhPos : POSITION,
491    out float3 outTex  : TEXCOORD0,
492    out float3 outPos  : TEXCOORD1,
493    out float3 outN    : TEXCOORD2,
494    out float3 outL    : TEXCOORD3)
495{
496        // The offset (+float4...) is needed to move the Center Object into the center of the PhotonMap.
497    outhPos = mul( Pos, g_mWorldViewProjection ) + float4( -4.0f / (float)g_iPhotonMapSize , 4.0f / (float)g_iPhotonMapSize , 0, 0 );
498   
499    outTex  = Tex;
500    outPos  = mul( Pos, g_mWorldCenterObject );
501    outN    = Normal;   
502    outL    = normalize( outPos - g_vLightPos3f );
503}
504// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
505
506float4 RenderPhotonUVMapClassicPS(
507        float3 Tex : TEXCOORD0,
508        float3 Pos : TEXCOORD1,
509        float3 N   : TEXCOORD2,
510        float3 L   : TEXCOORD3 ) : COLOR
511{
512        // By default, there is no Hit.
513        float a = -1;
514        float4 hitSurf = float4(0,0,0,0);
515       
516        if ( dot( L, N ) < -0.1 ){
517                L = normalize( L );
518                N = normalize( N );
519                float3 TT = normalize( refract( L, N, g_fRefractionIndex ) );
520               
521                hitSurf = texCUBE( g_samplerRoomCubeMapUVMap, TT );
522                // There is a Hit.
523                a = 1;
524        }
525        return float4( hitSurf.xyz, a );
526}
527//*****************************************************************************
528
529 
530       
531//-----------------------------------------------------------------------------
532// RenderPhotonHit
533// This shader uses the PhotonUVTexture as an input.
534// It renders as many snippets to the Object's atlas as many pixels the PhotonUVTexture has.
535// If a pixel in the PhotonUVTexture is not connected to the actual Object (not the same ObjectID),
536// it will not be rendered.
537// The result is blended into the Object's Atlas.
538//-----------------------------------------------------------------------------
539void RenderPhotonHitVS(
540        float4 Pos          : POSITION,
541        float4 Color        : COLOR0,
542        float2 Tex                  : TEXCOORD0,
543    out float4 outhPos      : POSITION,
544    //out float2 outTex     : TEXCOORD0,
545    out float2 outTexFilter : TEXCOORD1,
546    out float  outPower     : TEXCOORD2,
547    out float4 outPos       : TEXCOORD3,
548    out float4 outColor     : TEXCOORD4 )
549{
550        // Reads back the PhotonHit's UV position and the ObjectID
551        float4 ph = tex2Dlod(g_samplerPhotonUVMap, float4( Tex, 0, 0));
552
553        // This enlarges the snippet's size if it is rendered into a Column's atlas.
554        // It is needed because the Column's atlas contains more elements,
555        // and the same distance (in world space) is bigger in the Column's atlas.
556        if ( g_iObjectID > -0.5 ) Pos *= 3.3f;
557
558        // Adds the snippet's offset to the UV coordinates
559        outPos.x  = ph.x + Pos.x / 2.0;
560        outPos.y  = ph.y - Pos.y / 2.0;
561        outPos.z = 0;
562        outPos.w = 1;
563
564        // Transforms the texture coordinates to screen coordinates
565        // and adds the snippet's offset to it
566    outhPos.x = ( ph.x * 2 ) - 1 + Pos.x + HALF;
567    outhPos.y = 1 - ph.y * 2     + Pos.y - HALF;
568   
569    // Is it a real Hit?
570    // and are we render the right Object?
571    if ( ( ph.a > 0 ) && ( ph.b == g_iObjectID ) )
572    {
573                // If YES
574                outhPos.z = 0;
575        }
576        // If NO
577        else outhPos.z = -100;
578       
579        outhPos.w = 1;
580               
581        //outTex = outhPos;
582        outTexFilter = Tex;
583        outPower = ph.w;
584        outColor = Color;
585}
586// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
587
588float4 RenderPhotonHitPS(
589                //float2 Tex       : TEXCOORD0,
590        float2 TexFilter : TEXCOORD1,
591        float  Power     : TEXCOORD2,
592        float4 Pos       : TEXCOORD3,
593        float4 Color     : TEXCOORD4 ) : COLOR
594{
595        float4 retColor;
596        float4 brdf = float4(0,0,0,0);
597       
598        // Room or Column?
599        if ( g_iObjectID == -1 ) brdf = tex2D(           g_samplerRoomMap, Pos);
600        else                                     brdf = tex2D( g_samplerColumnOriginalMap, Pos);
601       
602        // The Gausian weight of a snippet.
603        float4 w = tex2D( g_samplerPowerOfSnippetMap, Color.yz );
604        // The color of the snippet's pixel.
605        retColor = g_fPower * brdf * w * g_fCausticsIntensity;
606        return float4( retColor.xyz, 0 );
607}
608//*****************************************************************************
609
610   
611//-----------------------------------------------------------------------------
612// RenderRefractObjectScreen
613// This shader calculates the color of the CenterObject with Hit function.
614// It has reflection and refraction effect.
615// The result is displayed on the screen.
616//-----------------------------------------------------------------------------
617void RenderRefractObjectScreenVS(
618                float4 Pos     : POSITION,
619        float3 Normal  : NORMAL,
620        float2 Tex         : TEXCOORD0,
621    out float4 outhPos : POSITION,
622    out float3 outPos  : TEXCOORD1,
623    out float3 outN    : TEXCOORD2,
624    out float3 outV    : TEXCOORD3 )
625{
626    outhPos = mul( Pos, g_mWorldViewProjection );
627    outPos  = mul( Pos, g_mWorldCenterObject );
628    outN    = normalize( Normal );
629    outV    = normalize( outPos - g_vCameraPos3f );
630}
631// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
632
633float4 RenderRefractObjectScreenPS(
634        float3 p0 : TEXCOORD1,
635        float3 N  : TEXCOORD2,
636        float3 V  : TEXCOORD3 ) : COLOR
637{       
638        V = normalize( V );
639        N = normalize( N );
640
641          // Calculates reflection
642        float3 R = reflect( V, N );
643        float3 RR = Hit(p0, R, g_samplerRoomCubeMapColorDistanceMap);
644       
645          // Calculates refraction
646        float3 T = refract(V, N, g_fRefractionIndex);
647        float3 TT = Hit(p0, T, g_samplerRoomCubeMapColorDistanceMap);
648
649          // Calculates reflected color
650        float4 hitSurf = texCUBE( g_samplerRoomCubeMapUVMap_NO_FILTER, RR );    // UV position where the Atlas must be read
651        float4 reflectColor = float4( 1,0,0,0 );
652        if (hitSurf.b == -1) reflectColor = tex2D(    g_samplerRoomLastMap, hitSurf.xy );
653        if (hitSurf.b ==  0) reflectColor = tex2D( g_samplerColumnLast0Map, hitSurf.xy );
654        if (hitSurf.b ==  1) reflectColor = tex2D( g_samplerColumnLast1Map, hitSurf.xy );
655        if (hitSurf.b ==  2) reflectColor = tex2D( g_samplerColumnLast2Map, hitSurf.xy );
656        if (hitSurf.b ==  3) reflectColor = tex2D( g_samplerColumnLast3Map, hitSurf.xy );
657       
658       
659          // Calculates refracted color
660        hitSurf = texCUBE( g_samplerRoomCubeMapUVMap_NO_FILTER, TT );
661        float4 refractColor = float4( 1,0,0,0 );
662        if (hitSurf.b == -1) refractColor = tex2D(    g_samplerRoomLastMap, hitSurf.xy );
663        if (hitSurf.b ==  0) refractColor = tex2D( g_samplerColumnLast0Map, hitSurf.xy );
664        if (hitSurf.b ==  1) refractColor = tex2D( g_samplerColumnLast1Map, hitSurf.xy );
665        if (hitSurf.b ==  2) refractColor = tex2D( g_samplerColumnLast2Map, hitSurf.xy );
666        if (hitSurf.b ==  3) refractColor = tex2D( g_samplerColumnLast3Map, hitSurf.xy );
667       
668          // Calculates return color modified by Fresnel function.
669        float F = g_fFresnelFactor + ( 1 - g_fFresnelFactor ) * pow( 1 - dot( N, -V ), 5 );
670        return float4( F * reflectColor + ( 1 - F ) * refractColor );
671}
672
673//*****************************************************************************
674
675
676//-----------------------------------------------------------------------------
677// RenderRefractObjectScreenClassic
678// This shader calculates the color of the CenterObject with classical method.
679// It has reflection and refraction effect.
680// The result is displayed on the screen.
681//-----------------------------------------------------------------------------
682void RenderRefractObjectScreenClassicVS(
683                float4 Pos     : POSITION,
684        float3 Normal  : NORMAL,
685        float2 Tex         : TEXCOORD0,
686    out float4 outhPos : POSITION,
687    out float3 outPos  : TEXCOORD1,
688    out float3 outN    : TEXCOORD2,
689    out float3 outV    : TEXCOORD3 )
690{
691    outhPos = mul( Pos, g_mWorldViewProjection );
692    outPos  = mul( Pos, g_mWorldCenterObject );
693    outN    = normalize( Normal );
694    outV    = normalize( outPos - g_vCameraPos3f );
695}
696// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
697
698float4 RenderRefractObjectScreenClassicPS(
699        float3 p0 : TEXCOORD1,
700        float3 N  : TEXCOORD2,
701        float3 V  : TEXCOORD3 ) : COLOR
702{       
703        V = normalize( V );
704        N = normalize( N );
705
706          // Calculates reflection
707        float3 RR = reflect( V, N );
708       
709          // Calculates refraction
710        float3 TT = refract(V, N, g_fRefractionIndex);
711
712
713          // Calculates reflected color
714        float4 hitSurf = texCUBE( g_samplerRoomCubeMapUVMap_NO_FILTER, RR );    // UV position where the Atlas must be read
715        float3 reflectColor = float4(0,0,0,0);
716        if (hitSurf.b == -1) reflectColor = tex2D(    g_samplerRoomLastMap, hitSurf.xy );
717        if (hitSurf.b ==  0) reflectColor = tex2D( g_samplerColumnLast0Map, hitSurf.xy );
718        if (hitSurf.b ==  1) reflectColor = tex2D( g_samplerColumnLast1Map, hitSurf.xy );
719        if (hitSurf.b ==  2) reflectColor = tex2D( g_samplerColumnLast2Map, hitSurf.xy );
720        if (hitSurf.b ==  3) reflectColor = tex2D( g_samplerColumnLast3Map, hitSurf.xy );
721       
722          // Calculates refracted color
723        hitSurf = texCUBE( g_samplerRoomCubeMapUVMap_NO_FILTER, TT );
724        float3 refractColor = float4(0,0,0,0);
725        if (hitSurf.b == -1) refractColor = tex2D(    g_samplerRoomLastMap, hitSurf.xy );
726        if (hitSurf.b ==  0) refractColor = tex2D( g_samplerColumnLast0Map, hitSurf.xy );
727        if (hitSurf.b ==  1) refractColor = tex2D( g_samplerColumnLast1Map, hitSurf.xy );
728        if (hitSurf.b ==  2) refractColor = tex2D( g_samplerColumnLast2Map, hitSurf.xy );
729        if (hitSurf.b ==  3) refractColor = tex2D( g_samplerColumnLast3Map, hitSurf.xy );
730       
731          // Calculates return color modified by Fresnel function.
732        float F = g_fFresnelFactor + ( 1 - g_fFresnelFactor ) * pow( 1 - dot( N, -V ), 5 );
733        return float4( F * reflectColor + ( 1 - F ) * refractColor, 1 );
734}
735
736//*****************************************************************************
737
738
739
740//-----------------------------------------------------------------------------
741// RenderLightScreen
742// This shader renders the light source's object to the screen with a diffuse color.
743//-----------------------------------------------------------------------------
744void RenderLightScreenVS(
745        float4 Pos    : POSITION,
746    out float4 outPos : POSITION )
747{
748    outPos = mul( Pos, g_mWorldViewProjection );
749}
750
751// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
752
753float4 RenderLightScreenPS() : COLOR
754{
755    return float4( 1, 1, 0.7f, 1 );
756}
757//*****************************************************************************
758
759
760//-----------------------------------------------------------------------------
761// RenderShadow
762// This shader calculates the ShadowMapTexture's depth values.
763// The result is stored in g_pShadowMapTexture.
764//-----------------------------------------------------------------------------
765void RenderShadowVS(
766                float4 Pos     : POSITION,
767    out float4 outhPos : POSITION,
768    out float2 outPos  : TEXCOORD1 )
769{
770    outhPos = mul( Pos, g_mWorldViewProjection );
771    outPos  = mul( Pos, g_mLightViewTexBias ).zw;
772}
773// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
774
775float4 RenderShadowPS(
776                float2 Pos : TEXCOORD1 ) : COLOR
777{
778    return float4(Pos.x, 0, 0, 1);
779}
780//*****************************************************************************
781
782
783//-----------------------------------------------------------------------------
784// FullScreenQuad
785// This shader modifies the atlases of Room and Columns.
786// It removes the black edges from the atlases.
787// The result is stored in the Object's atlas.
788//-----------------------------------------------------------------------------
789void FullScreenQuadVS(
790                float4 Pos     : POSITION,
791    out float4 outhPos : POSITION,
792    out float2 outTex  : TEXCOORD0 )
793{
794        outhPos = float4( Pos.xy, 0, 1 );
795    outTex.x = Pos.x * 0.5f + 0.5f;
796    outTex.y = -Pos.y * 0.5f + 0.5f;
797}
798// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
799
800float4 FullScreenQuadPS(
801                float2 Tex : TEXCOORD0 ) : COLOR
802{
803    float4 preVal = float4(1,1,1,1);
804   
805    if( g_iObjectID ==-1 ) preVal = FlowLight(    g_samplerRoomModifyMap, Tex );
806    if( g_iObjectID == 0 ) preVal = FlowLight( g_samplerColumnModify0Map, Tex );
807    if( g_iObjectID == 1 ) preVal = FlowLight( g_samplerColumnModify1Map, Tex );
808    if( g_iObjectID == 2 ) preVal = FlowLight( g_samplerColumnModify2Map, Tex );
809    if( g_iObjectID == 3 ) preVal = FlowLight( g_samplerColumnModify3Map, Tex );
810       
811        return preVal; 
812}
813//*****************************************************************************
814
815
816//-----------------------------------------------------------------------------
817// PhotonMapScreen
818// This shader renders the PhotonMap texture (g_txPhotonUVMap) into the screen lower left corner.
819// This texture contains the PhotonHits positions and ObjectIDs.
820// The result is displayed on the screen.
821//-----------------------------------------------------------------------------
822
823void PhotonMapScreenVS(
824                float4 Pos     : POSITION,
825    out float4 outhPos : POSITION,
826    out float2 outTex  : TEXCOORD0 )
827{
828        outhPos = float4( Pos.xy * 0.2f + float2( -0.7f, -0.7f ), 0, 1 );
829    outTex.x = Pos.x * 0.5f + 0.5f;
830    outTex.y = -Pos.y * 0.5f + 0.5f;
831}
832// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
833
834float4 PhotonMapScreenPS(
835                float2 Tex : TEXCOORD0 ) : COLOR
836{
837        return tex2D( g_samplerPhotonUVMap, Tex );     
838}
839//*****************************************************************************
840
841
842
843
844
845//-----------------------------------------------------------------------------
846// TECHNIQUES
847// Here are the different Techniques.
848//-----------------------------------------------------------------------------
849
850// Technique(AnyRenderPass):
851//              VertexShader = compile vs_3_0 AnyRenderPassVS()
852//              PixelShader  = compile vs_3_0 AnyRenderPassPS()
853
854// if using the name convention above to name vertex/pixel shaders,
855// you can use the following macro definition to quickly define a technique:
856
857// note: ## stands for concatenation
858
859#define Technique(name);                                                                \
860        technique name                                                                          \
861        {                                                                                                       \
862            pass p0                                                                                     \
863            {                                                                                           \
864                    VertexShader = compile vs_3_0 name##VS();   \
865                    PixelShader  = compile ps_3_0 name##PS();   \
866                }                                                                                               \
867        }
868       
869Technique( RenderRoomColorDistance );
870Technique( RenderRoomUV );
871Technique( RenderUmbra );
872Technique( RenderRoomAndColumnsScreen );
873Technique( RenderLightScreen );
874Technique( RenderPhotonUVMap );
875Technique( RenderPhotonUVMapClassic );
876Technique( RenderPhotonHit );
877Technique( RenderRefractObjectScreen );
878Technique( RenderRefractObjectScreenClassic );
879Technique( RenderShadow );
880Technique( FullScreenQuad );
881Technique( PhotonMapScreen );
Note: See TracBrowser for help on using the repository browser.