//---------------------------------------------- // GLOBAL VARIABLES //--------------------------------------------- int g_iNumberOfIteration; // Number of iteration int face; float g_fFresnelFactor; // Fresnel factor float g_fPower; // Power of light source float g_fRefractionIndex; // Refraction index float3 g_vCameraPos3f; // Camera position float3 g_vLightPos3f; // Light source position //Gauss Stuff const float4 horSamples[7] = { -3.0, 0.0, 0, 1.0/64.0, -2.0, 0.0, 0, 6.0/64.0, -1.0, 0.0, 0, 15.0/64.0, 0.0, 0.0, 0, 20.0/64.0, 1.0, 0.0, 0, 15.0/64.0, 2.0, 0.0, 0, 6.0/64.0, 3.0, 0.0, 0, 1.0/64.0 }; const float4 verSamples[7] = { 0.0, -3.0, 0, 1.0/64.0, 0.0, -2.0, 0, 6.0/64.0, 0.0, -1.0, 0, 15.0/64.0, 0.0, 0.0, 0, 20.0/64.0, 0.0, 1.0, 0, 15.0/64.0, 0.0, 2.0, 0, 6.0/64.0, 0.0, 3.0, 0, 1.0/64.0 }; float4x4 g_mWorldView; // WorldView transformation matrix float4x4 g_mProj; // ProjectionMatrix float4x4 g_mView; // ViewMatrix float4x4 g_mWorldViewProjection; // WorldViewProjection transformation matrix float4x4 g_mWorldCenterObject; // World transformation matrix of CenterObject float4x4 g_mCenterObjectRot; // Rotation Matrix of CenterObject float4x4 g_mWorldCenterObjectRotation; // Contains only the rotation of the CenterObject float4x4 g_mLightViewTexBias; // Light space transformation matrix texture g_txRoomColorDistanceCubeMap; // Contain the color and distance information in a CubeMap texture g_txTempRoomColorDistanceCubeMap; // Contain the color and distance information in a CubeMap texture g_txCurrentTexture; //Aktuelle Textur mit der Gerade gerendert wird. //----------------------------------------------------------------------------- // SAMPLERS // Each texture must have its own sampler //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // macro definition for filtered samplers //----------------------------------------------------------------------------- #define SAMPLER_LINEAR(g_samplerMap, g_txMap); \ sampler2D g_samplerMap = sampler_state { \ Texture = ; \ MinFilter = Linear; \ MagFilter = Linear; \ MipFilter = Linear; \ AddressU = BORDER; \ AddressV = BORDER; \ }; SAMPLER_LINEAR(g_samplerCurrentTexture, g_txCurrentTexture); //----------------------------------------------------------------------------- // macro definition for non-filtered samplers //----------------------------------------------------------------------------- #define SAMPLER_POINT(g_samplerMap, g_txMap); \ sampler2D g_samplerMap = sampler_state { \ Texture = ; \ MinFilter = Point; \ MagFilter = Point; \ MipFilter = Point; \ AddressU = BORDER; \ AddressV = BORDER; \ }; samplerCUBE g_samplerRoomColorDistanceCubeMap = sampler_state { Texture = ; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = BORDER; AddressV = BORDER; }; samplerCUBE g_samplerTempRoomColorDistanceCubeMap = sampler_state { Texture = ; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = BORDER; AddressV = BORDER; }; //----------------------------------------------------------------------------- // FUNCTIONS //----------------------------------------------------------------------------- // This function is called several times. float3 Hit( float3 x, float3 R, samplerCUBE mp ) { float rl = texCUBE( mp, R ).a; // |r| float ppp = length( x ) / texCUBE( mp, x ).a; // |p|/|p’| float dun = 0, pun = ppp, dov = 0, pov; float dl = rl * ( 1 - ppp ); // eq. 2 float3 l = x + R * dl; // ray equation // iteration for( int i = 0; i < g_iNumberOfIteration; i++ ) { float llp = length( l ) / texCUBE( mp,l ).a; // |l|/|l’| if ( llp < 0.999f ) // undershooting { dun = dl; pun = llp; // last undershooting dl += ( dov == 0 ) ? rl * ( 1 - llp ) : // eq. 2 ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3 } else if ( llp > 1.001f ) // overshooting { dov = dl; pov = llp; // last overshooting dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3 } l = x + R * dl; // ray equation } return l; // computed hit point } //----------------------------------------------------------------------------- // SHADERS // Here are the different shaders. //----------------------------------------------------------------------------- //***************************************************************************** //----------------------------------------------------------------------------- // RenderRoomColorDistance // This shader stores the viewable object's color and its distance from the CenterObject's position. // The result is stored in a CubeMap. //----------------------------------------------------------------------------- void ColorDistanceVS( float4 Pos : POSITION, float2 Tex : TEXCOORD0, out float4 outhPos : POSITION, out float2 outTex : TEXCOORD0, out float4 outPos : TEXCOORD1 ) { outhPos = mul( Pos, g_mWorldViewProjection ); outTex = Tex; // Calculate the world position outPos = mul( Pos, g_mWorldView ); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - float4 ColorDistancePS( float2 Tex : TEXCOORD0, float4 Pos : TEXCOORD1 ) : COLOR { // The color of the viewable object in the current pixel. // In the scene there are 2 different textured object. (the room, and the Columns) // Every column has the same brdf texture. float3 retColor = float4(0,0,0,0); retColor = float4(tex2D(g_samplerCurrentTexture, Tex).xyz, 1); // The distance from the CenterObject float Distance; Distance = (float) ( length( Pos.xyz ) ); return float4(retColor, Distance); // Color(x,y,z) + Distance(w) } //***************************************************************************** //----------------------------------------------------------------------------- // RenderRefractObjectScreen // This shader calculates the color of the CenterObject with Hit function. // It has reflection and refraction effect. // The result is displayed on the screen. //----------------------------------------------------------------------------- void RefractObjectVS( float4 Pos : POSITION, float3 Normal : NORMAL, float2 Tex : TEXCOORD0, out float4 outhPos : POSITION, out float2 outTex : TEXCOORD0, out float3 outPos : TEXCOORD1, out float3 outN : TEXCOORD2, out float3 outV : TEXCOORD3 ) { outTex = Tex; outhPos = mul( Pos, g_mWorldViewProjection ); outPos = mul( Pos, g_mCenterObjectRot ); outN = normalize(mul(Normal, g_mCenterObjectRot)); float4 tempPos = mul(Pos, g_mWorldCenterObject); outV = normalize( tempPos - g_vCameraPos3f ); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - float4 RefractObjectPS( float2 Tex : TEXCOORD0, float3 p0 : TEXCOORD1, float3 N : TEXCOORD2, float3 V : TEXCOORD3 ) : COLOR { V = normalize( V ); N = normalize( N ); // Calculate reflection float3 R = reflect( V, N ); float3 RR = Hit(p0, R, g_samplerRoomColorDistanceCubeMap); // Calculate refraction float3 T = refract(V, N, g_fRefractionIndex); float3 TT = Hit(p0, T, g_samplerRoomColorDistanceCubeMap); // Calculate reflected color float4 reflectColor = texCUBE( g_samplerRoomColorDistanceCubeMap, RR ); // Calculate refracted color float4 refractColor = texCUBE( g_samplerRoomColorDistanceCubeMap, TT ); // Calculate return color modified by Fresnel function. float F = g_fFresnelFactor + ( 1 - g_fFresnelFactor ) * pow( 1 - dot( N, -V ), 5 ); return float4( F * reflectColor + ( 1 - F ) * refractColor ); //return refractColor; } //***************************************************************************** //----------------------------------------------------------------------------- // RenderRefractObjectScreenClassic // This shader calculates the color of the CenterObject with classical method. // It has reflection and refraction effect. // The result is displayed on the screen. //----------------------------------------------------------------------------- void RefractObjectClassicVS( float4 Pos : POSITION, float3 Normal : NORMAL, float2 Tex : TEXCOORD0, out float4 outhPos : POSITION, out float3 outPos : TEXCOORD1, out float3 outN : TEXCOORD2, out float3 outV : TEXCOORD3 ) { //outTex = Tex; outhPos = mul( Pos, g_mWorldViewProjection ); outPos = mul( Pos, g_mCenterObjectRot ); outN = normalize(mul(Normal, g_mCenterObjectRot)); float4 tempPos = mul(Pos, g_mWorldCenterObject); outV = normalize( tempPos - g_vCameraPos3f ); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - float4 RefractObjectClassicPS( float3 p0 : TEXCOORD1, float3 N : TEXCOORD2, float3 V : TEXCOORD3 ) : COLOR { V = normalize( V ); N = normalize( N ); // Calculate reflection float3 RR = reflect( V, N ); // Calculate refraction float3 TT = refract(V, N, g_fRefractionIndex); // Calculate reflected color float4 reflectColor = texCUBE( g_samplerRoomColorDistanceCubeMap, RR ); // UV position where the Atlas must be read // Calculate refracted color float4 refractColor = texCUBE( g_samplerRoomColorDistanceCubeMap, TT ); // Calculate return color modified by Fresnel function. float F = g_fFresnelFactor + ( 1 - g_fFresnelFactor ) * pow( 1 - dot( N, -V ), 5 ); return float4( F * reflectColor + ( 1 - F ) * refractColor); } //***************************************************************************** void gaussHorizontalVS( float4 Pos : POSITION, float2 Tex : TEXCOORD0, out float4 outhPos : POSITION, out float2 outTex : TEXCOORD0 ) { outhPos = Pos; outTex = Tex; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - float4 gaussHorizontalPS( float2 Tex : TEXCOORD0 ) : COLOR { float4 retColor = float4(0,0,0,0); for(int i=0;i<7;i++) { retColor+=horSamples[i].w*tex2D(g_samplerCurrentTexture, Tex + horSamples[i].x); } retColor = texCUBE(g_samplerRoomColorDistanceCubeMap, float3(Tex , 1)); return retColor; } void gaussVertikalVS( float4 Pos : POSITION, float2 Tex : TEXCOORD0, out float4 outhPos : POSITION, out float2 outTex : TEXCOORD0 ) { outhPos = Pos; outTex = Tex; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - float4 gaussVertikalPS( float2 Tex : TEXCOORD0 ) : COLOR { float4 retColor = float4(0,0,0,0); for(int i=0;i<7;i++) { retColor+=verSamples[i].w*tex2D(g_samplerCurrentTexture, Tex + verSamples[i].y); } retColor = texCUBE(g_samplerTempRoomColorDistanceCubeMap, float3(Tex , 1)); return retColor; } //----------------------------------------------------------------------------- // TECHNIQUES // Here are the different Techniques. //----------------------------------------------------------------------------- // Technique(AnyRenderPass): // VertexShader = compile vs_3_0 AnyRenderPassVS() // PixelShader = compile vs_3_0 AnyRenderPassPS() // if using the name convention above to name vertex/pixel shaders, // you can use the following macro definition to quickly define a technique: // note: ## stands for concatenation #define Technique(name); \ technique name \ { \ pass p0 \ { \ VertexShader = compile vs_3_0 name##VS(); \ PixelShader = compile ps_3_0 name##PS(); \ } \ } Technique( ColorDistance ); Technique( RefractObject ); Technique( RefractObjectClassic ); Technique( gaussHorizontal ); Technique( gaussVertikal );