[1378] | 1 | //----------------------------------------------
|
---|
| 2 | // GLOBAL VARIABLES
|
---|
| 3 | //---------------------------------------------
|
---|
| 4 |
|
---|
| 5 | int g_iNumberOfIteration; // Number of iteration
|
---|
| 6 | int face;
|
---|
| 7 |
|
---|
| 8 | float g_fFresnelFactor; // Fresnel factor
|
---|
| 9 | float g_fPower; // Power of light source
|
---|
| 10 | float g_fRefractionIndex; // Refraction index
|
---|
| 11 |
|
---|
| 12 | float3 g_vCameraPos3f; // Camera position
|
---|
| 13 | float3 g_vLightPos3f; // Light source position
|
---|
| 14 |
|
---|
| 15 | //Gauss Stuff
|
---|
| 16 | const float4 horSamples[7] = {
|
---|
| 17 | -3.0, 0.0, 0, 1.0/64.0,
|
---|
| 18 | -2.0, 0.0, 0, 6.0/64.0,
|
---|
| 19 | -1.0, 0.0, 0, 15.0/64.0,
|
---|
| 20 | 0.0, 0.0, 0, 20.0/64.0,
|
---|
| 21 | 1.0, 0.0, 0, 15.0/64.0,
|
---|
| 22 | 2.0, 0.0, 0, 6.0/64.0,
|
---|
| 23 | 3.0, 0.0, 0, 1.0/64.0
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | const float4 verSamples[7] = {
|
---|
| 27 | 0.0, -3.0, 0, 1.0/64.0,
|
---|
| 28 | 0.0, -2.0, 0, 6.0/64.0,
|
---|
| 29 | 0.0, -1.0, 0, 15.0/64.0,
|
---|
| 30 | 0.0, 0.0, 0, 20.0/64.0,
|
---|
| 31 | 0.0, 1.0, 0, 15.0/64.0,
|
---|
| 32 | 0.0, 2.0, 0, 6.0/64.0,
|
---|
| 33 | 0.0, 3.0, 0, 1.0/64.0
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | float4x4 g_mWorldView; // WorldView transformation matrix
|
---|
| 38 | float4x4 g_mProj; // ProjectionMatrix
|
---|
| 39 | float4x4 g_mView; // ViewMatrix
|
---|
| 40 | float4x4 g_mWorldViewProjection; // WorldViewProjection transformation matrix
|
---|
| 41 | float4x4 g_mWorldCenterObject; // World transformation matrix of CenterObject
|
---|
| 42 | float4x4 g_mCenterObjectRot; // Rotation Matrix of CenterObject
|
---|
| 43 | float4x4 g_mWorldCenterObjectRotation; // Contains only the rotation of the CenterObject
|
---|
| 44 | float4x4 g_mLightViewTexBias; // Light space transformation matrix
|
---|
| 45 |
|
---|
| 46 | texture g_txRoomColorDistanceCubeMap; // Contain the color and distance information in a CubeMap
|
---|
| 47 | texture g_txTempRoomColorDistanceCubeMap; // Contain the color and distance information in a CubeMap
|
---|
| 48 | texture g_txCurrentTexture; //Aktuelle Textur mit der Gerade gerendert wird.
|
---|
| 49 |
|
---|
| 50 | //-----------------------------------------------------------------------------
|
---|
| 51 | // SAMPLERS
|
---|
| 52 | // Each texture must have its own sampler
|
---|
| 53 | //-----------------------------------------------------------------------------
|
---|
| 54 |
|
---|
| 55 | //-----------------------------------------------------------------------------
|
---|
| 56 | // macro definition for filtered samplers
|
---|
| 57 | //-----------------------------------------------------------------------------
|
---|
| 58 |
|
---|
| 59 | #define SAMPLER_LINEAR(g_samplerMap, g_txMap); \
|
---|
| 60 | sampler2D g_samplerMap = sampler_state { \
|
---|
| 61 | Texture = <g_txMap>; \
|
---|
| 62 | MinFilter = Linear; \
|
---|
| 63 | MagFilter = Linear; \
|
---|
| 64 | MipFilter = Linear; \
|
---|
| 65 | AddressU = BORDER; \
|
---|
| 66 | AddressV = BORDER; \
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 | SAMPLER_LINEAR(g_samplerCurrentTexture, g_txCurrentTexture);
|
---|
| 70 |
|
---|
| 71 | //-----------------------------------------------------------------------------
|
---|
| 72 | // macro definition for non-filtered samplers
|
---|
| 73 | //-----------------------------------------------------------------------------
|
---|
| 74 |
|
---|
| 75 | #define SAMPLER_POINT(g_samplerMap, g_txMap); \
|
---|
| 76 | sampler2D g_samplerMap = sampler_state { \
|
---|
| 77 | Texture = <g_txMap>; \
|
---|
| 78 | MinFilter = Point; \
|
---|
| 79 | MagFilter = Point; \
|
---|
| 80 | MipFilter = Point; \
|
---|
| 81 | AddressU = BORDER; \
|
---|
| 82 | AddressV = BORDER; \
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | samplerCUBE g_samplerRoomColorDistanceCubeMap = sampler_state
|
---|
| 86 | {
|
---|
| 87 | Texture = <g_txRoomColorDistanceCubeMap>;
|
---|
| 88 | MinFilter = Linear;
|
---|
| 89 | MagFilter = Linear;
|
---|
| 90 | MipFilter = Linear;
|
---|
| 91 | AddressU = BORDER;
|
---|
| 92 | AddressV = BORDER;
|
---|
| 93 | };
|
---|
| 94 |
|
---|
| 95 | samplerCUBE g_samplerTempRoomColorDistanceCubeMap = sampler_state
|
---|
| 96 | {
|
---|
| 97 | Texture = <g_txRoomColorDistanceCubeMap>;
|
---|
| 98 | MinFilter = Linear;
|
---|
| 99 | MagFilter = Linear;
|
---|
| 100 | MipFilter = Linear;
|
---|
| 101 | AddressU = BORDER;
|
---|
| 102 | AddressV = BORDER;
|
---|
| 103 | };
|
---|
| 104 |
|
---|
| 105 | //-----------------------------------------------------------------------------
|
---|
| 106 | // FUNCTIONS
|
---|
| 107 | //-----------------------------------------------------------------------------
|
---|
| 108 |
|
---|
| 109 | // This function is called several times.
|
---|
| 110 | float3 Hit( float3 x, float3 R, samplerCUBE mp )
|
---|
| 111 | {
|
---|
| 112 | float rl = texCUBE( mp, R ).a; // |r|
|
---|
| 113 | float ppp = length( x ) / texCUBE( mp, x ).a; // |p|/|p|
|
---|
| 114 | float dun = 0, pun = ppp, dov = 0, pov;
|
---|
| 115 | float dl = rl * ( 1 - ppp ); // eq. 2
|
---|
| 116 | float3 l = x + R * dl; // ray equation
|
---|
| 117 |
|
---|
| 118 | // iteration
|
---|
| 119 | for( int i = 0; i < g_iNumberOfIteration; i++ )
|
---|
| 120 | {
|
---|
| 121 | float llp = length( l ) / texCUBE( mp,l ).a; // |l|/|l|
|
---|
| 122 | if ( llp < 0.999f ) // undershooting
|
---|
| 123 | {
|
---|
| 124 | dun = dl; pun = llp; // last undershooting
|
---|
| 125 | dl += ( dov == 0 ) ? rl * ( 1 - llp ) : // eq. 2
|
---|
| 126 | ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3
|
---|
| 127 | } else if ( llp > 1.001f ) // overshooting
|
---|
| 128 | {
|
---|
| 129 | dov = dl; pov = llp; // last overshooting
|
---|
| 130 | dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
|
---|
| 131 | }
|
---|
| 132 | l = x + R * dl; // ray equation
|
---|
| 133 | }
|
---|
| 134 | return l; // computed hit point
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | //-----------------------------------------------------------------------------
|
---|
| 138 | // SHADERS
|
---|
| 139 | // Here are the different shaders.
|
---|
| 140 | //-----------------------------------------------------------------------------
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | //*****************************************************************************
|
---|
| 144 | //-----------------------------------------------------------------------------
|
---|
| 145 | // RenderRoomColorDistance
|
---|
| 146 | // This shader stores the viewable object's color and its distance from the CenterObject's position.
|
---|
| 147 | // The result is stored in a CubeMap.
|
---|
| 148 | //-----------------------------------------------------------------------------
|
---|
| 149 | void ColorDistanceVS(
|
---|
| 150 | float4 Pos : POSITION,
|
---|
| 151 | float2 Tex : TEXCOORD0,
|
---|
| 152 | out float4 outhPos : POSITION,
|
---|
| 153 | out float2 outTex : TEXCOORD0,
|
---|
| 154 | out float4 outPos : TEXCOORD1 )
|
---|
| 155 | {
|
---|
| 156 | outhPos = mul( Pos, g_mWorldViewProjection );
|
---|
| 157 | outTex = Tex;
|
---|
| 158 | // Calculate the world position
|
---|
| 159 | outPos = mul( Pos, g_mWorldView );
|
---|
| 160 | }
|
---|
| 161 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
---|
| 162 |
|
---|
| 163 | float4 ColorDistancePS(
|
---|
| 164 | float2 Tex : TEXCOORD0,
|
---|
| 165 | float4 Pos : TEXCOORD1 ) : COLOR
|
---|
| 166 | {
|
---|
| 167 | // The color of the viewable object in the current pixel.
|
---|
| 168 | // In the scene there are 2 different textured object. (the room, and the Columns)
|
---|
| 169 | // Every column has the same brdf texture.
|
---|
| 170 | float3 retColor = float4(0,0,0,0);
|
---|
| 171 |
|
---|
| 172 | retColor = float4(tex2D(g_samplerCurrentTexture, Tex).xyz, 1);
|
---|
| 173 | // The distance from the CenterObject
|
---|
| 174 | float Distance;
|
---|
| 175 | Distance = (float) ( length( Pos.xyz ) );
|
---|
| 176 | return float4(retColor, Distance); // Color(x,y,z) + Distance(w)
|
---|
| 177 | }
|
---|
| 178 | //*****************************************************************************
|
---|
| 179 |
|
---|
| 180 | //-----------------------------------------------------------------------------
|
---|
| 181 | // RenderRefractObjectScreen
|
---|
| 182 | // This shader calculates the color of the CenterObject with Hit function.
|
---|
| 183 | // It has reflection and refraction effect.
|
---|
| 184 | // The result is displayed on the screen.
|
---|
| 185 | //-----------------------------------------------------------------------------
|
---|
| 186 | void RefractObjectVS(
|
---|
| 187 | float4 Pos : POSITION,
|
---|
| 188 | float3 Normal : NORMAL,
|
---|
| 189 | float2 Tex : TEXCOORD0,
|
---|
| 190 | out float4 outhPos : POSITION,
|
---|
| 191 | out float2 outTex : TEXCOORD0,
|
---|
| 192 | out float3 outPos : TEXCOORD1,
|
---|
| 193 | out float3 outN : TEXCOORD2,
|
---|
| 194 | out float3 outV : TEXCOORD3 )
|
---|
| 195 | {
|
---|
| 196 | outTex = Tex;
|
---|
| 197 | outhPos = mul( Pos, g_mWorldViewProjection );
|
---|
| 198 | outPos = mul( Pos, g_mCenterObjectRot );
|
---|
| 199 | outN = normalize(mul(Normal, g_mCenterObjectRot));
|
---|
| 200 | float4 tempPos = mul(Pos, g_mWorldCenterObject);
|
---|
| 201 | outV = normalize( tempPos - g_vCameraPos3f );
|
---|
| 202 | }
|
---|
| 203 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
---|
| 204 |
|
---|
| 205 | float4 RefractObjectPS(
|
---|
| 206 | float2 Tex : TEXCOORD0,
|
---|
| 207 | float3 p0 : TEXCOORD1,
|
---|
| 208 | float3 N : TEXCOORD2,
|
---|
| 209 | float3 V : TEXCOORD3 ) : COLOR
|
---|
| 210 | {
|
---|
| 211 | V = normalize( V );
|
---|
| 212 | N = normalize( N );
|
---|
| 213 |
|
---|
| 214 | // Calculate reflection
|
---|
| 215 | float3 R = reflect( V, N );
|
---|
| 216 | float3 RR = Hit(p0, R, g_samplerRoomColorDistanceCubeMap);
|
---|
| 217 |
|
---|
| 218 | // Calculate refraction
|
---|
| 219 | float3 T = refract(V, N, g_fRefractionIndex);
|
---|
| 220 | float3 TT = Hit(p0, T, g_samplerRoomColorDistanceCubeMap);
|
---|
| 221 |
|
---|
| 222 | // Calculate reflected color
|
---|
| 223 | float4 reflectColor = texCUBE( g_samplerRoomColorDistanceCubeMap, RR );
|
---|
| 224 |
|
---|
| 225 | // Calculate refracted color
|
---|
| 226 | float4 refractColor = texCUBE( g_samplerRoomColorDistanceCubeMap, TT );
|
---|
| 227 |
|
---|
| 228 | // Calculate return color modified by Fresnel function.
|
---|
| 229 | float F = g_fFresnelFactor + ( 1 - g_fFresnelFactor ) * pow( 1 - dot( N, -V ), 5 );
|
---|
| 230 | return float4( F * reflectColor + ( 1 - F ) * refractColor );
|
---|
| 231 | //return refractColor;
|
---|
| 232 | }
|
---|
| 233 | //*****************************************************************************
|
---|
| 234 |
|
---|
| 235 |
|
---|
| 236 | //-----------------------------------------------------------------------------
|
---|
| 237 | // RenderRefractObjectScreenClassic
|
---|
| 238 | // This shader calculates the color of the CenterObject with classical method.
|
---|
| 239 | // It has reflection and refraction effect.
|
---|
| 240 | // The result is displayed on the screen.
|
---|
| 241 | //-----------------------------------------------------------------------------
|
---|
| 242 | void RefractObjectClassicVS(
|
---|
| 243 | float4 Pos : POSITION,
|
---|
| 244 | float3 Normal : NORMAL,
|
---|
| 245 | float2 Tex : TEXCOORD0,
|
---|
| 246 | out float4 outhPos : POSITION,
|
---|
| 247 | out float3 outPos : TEXCOORD1,
|
---|
| 248 | out float3 outN : TEXCOORD2,
|
---|
| 249 | out float3 outV : TEXCOORD3 )
|
---|
| 250 | {
|
---|
| 251 | //outTex = Tex;
|
---|
| 252 | outhPos = mul( Pos, g_mWorldViewProjection );
|
---|
| 253 | outPos = mul( Pos, g_mCenterObjectRot );
|
---|
| 254 | outN = normalize(mul(Normal, g_mCenterObjectRot));
|
---|
| 255 | float4 tempPos = mul(Pos, g_mWorldCenterObject);
|
---|
| 256 | outV = normalize( tempPos - g_vCameraPos3f );
|
---|
| 257 | }
|
---|
| 258 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
---|
| 259 |
|
---|
| 260 | float4 RefractObjectClassicPS(
|
---|
| 261 | float3 p0 : TEXCOORD1,
|
---|
| 262 | float3 N : TEXCOORD2,
|
---|
| 263 | float3 V : TEXCOORD3 ) : COLOR
|
---|
| 264 | {
|
---|
| 265 | V = normalize( V );
|
---|
| 266 | N = normalize( N );
|
---|
| 267 |
|
---|
| 268 | // Calculate reflection
|
---|
| 269 | float3 RR = reflect( V, N );
|
---|
| 270 |
|
---|
| 271 | // Calculate refraction
|
---|
| 272 | float3 TT = refract(V, N, g_fRefractionIndex);
|
---|
| 273 |
|
---|
| 274 |
|
---|
| 275 | // Calculate reflected color
|
---|
| 276 | float4 reflectColor = texCUBE( g_samplerRoomColorDistanceCubeMap, RR ); // UV position where the Atlas must be read
|
---|
| 277 |
|
---|
| 278 | // Calculate refracted color
|
---|
| 279 | float4 refractColor = texCUBE( g_samplerRoomColorDistanceCubeMap, TT );
|
---|
| 280 |
|
---|
| 281 | // Calculate return color modified by Fresnel function.
|
---|
| 282 | float F = g_fFresnelFactor + ( 1 - g_fFresnelFactor ) * pow( 1 - dot( N, -V ), 5 );
|
---|
| 283 | return float4( F * reflectColor + ( 1 - F ) * refractColor);
|
---|
| 284 | }
|
---|
| 285 | //*****************************************************************************
|
---|
| 286 |
|
---|
| 287 | void gaussHorizontalVS(
|
---|
| 288 | float4 Pos : POSITION,
|
---|
| 289 | float2 Tex : TEXCOORD0,
|
---|
| 290 | out float4 outhPos : POSITION,
|
---|
| 291 | out float2 outTex : TEXCOORD0 )
|
---|
| 292 | {
|
---|
| 293 | outhPos = Pos;
|
---|
| 294 | outTex = Tex;
|
---|
| 295 | }
|
---|
| 296 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
---|
| 297 |
|
---|
| 298 | float4 gaussHorizontalPS(
|
---|
| 299 | float2 Tex : TEXCOORD0 ) : COLOR
|
---|
| 300 | {
|
---|
| 301 | float4 retColor = float4(0,0,0,0);
|
---|
| 302 | for(int i=0;i<7;i++) {
|
---|
| 303 | retColor+=horSamples[i].w*tex2D(g_samplerCurrentTexture, Tex + horSamples[i].x);
|
---|
| 304 | }
|
---|
| 305 | retColor = texCUBE(g_samplerRoomColorDistanceCubeMap, float3(Tex , 1));
|
---|
| 306 | return retColor;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | void gaussVertikalVS(
|
---|
| 310 | float4 Pos : POSITION,
|
---|
| 311 | float2 Tex : TEXCOORD0,
|
---|
| 312 | out float4 outhPos : POSITION,
|
---|
| 313 | out float2 outTex : TEXCOORD0 )
|
---|
| 314 | {
|
---|
| 315 | outhPos = Pos;
|
---|
| 316 | outTex = Tex;
|
---|
| 317 | }
|
---|
| 318 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
---|
| 319 |
|
---|
| 320 | float4 gaussVertikalPS(
|
---|
| 321 | float2 Tex : TEXCOORD0 ) : COLOR
|
---|
| 322 | {
|
---|
| 323 | float4 retColor = float4(0,0,0,0);
|
---|
| 324 | for(int i=0;i<7;i++) {
|
---|
| 325 | retColor+=verSamples[i].w*tex2D(g_samplerCurrentTexture, Tex + verSamples[i].y);
|
---|
| 326 | }
|
---|
| 327 | retColor = texCUBE(g_samplerTempRoomColorDistanceCubeMap, float3(Tex , 1));
|
---|
| 328 | return retColor;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 |
|
---|
| 332 |
|
---|
| 333 |
|
---|
| 334 | //-----------------------------------------------------------------------------
|
---|
| 335 | // TECHNIQUES
|
---|
| 336 | // Here are the different Techniques.
|
---|
| 337 | //-----------------------------------------------------------------------------
|
---|
| 338 |
|
---|
| 339 | // Technique(AnyRenderPass):
|
---|
| 340 | // VertexShader = compile vs_3_0 AnyRenderPassVS()
|
---|
| 341 | // PixelShader = compile vs_3_0 AnyRenderPassPS()
|
---|
| 342 |
|
---|
| 343 | // if using the name convention above to name vertex/pixel shaders,
|
---|
| 344 | // you can use the following macro definition to quickly define a technique:
|
---|
| 345 |
|
---|
| 346 | // note: ## stands for concatenation
|
---|
| 347 |
|
---|
| 348 | #define Technique(name); \
|
---|
| 349 | technique name \
|
---|
| 350 | { \
|
---|
| 351 | pass p0 \
|
---|
| 352 | { \
|
---|
| 353 | VertexShader = compile vs_3_0 name##VS(); \
|
---|
| 354 | PixelShader = compile ps_3_0 name##PS(); \
|
---|
| 355 | } \
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | Technique( ColorDistance );
|
---|
| 359 | Technique( RefractObject );
|
---|
| 360 | Technique( RefractObjectClassic );
|
---|
| 361 | Technique( gaussHorizontal );
|
---|
| 362 | Technique( gaussVertikal );
|
---|