Ignore:
Timestamp:
03/20/07 19:10:47 (17 years ago)
Author:
szirmay
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Illum/Ogre/Media/materials/GTPAdvancedEnvMap/multibounce/GTPMultipleReflection.hlsl

    r2241 r2271  
    1111} 
    1212 
    13  
    14  
    15 #define MAX_LIN_ITERATIONCOUNT 50  //80 
    16 #define MIN_LIN_ITERATIONCOUNT 30  //60 
     13#define MAX_LIN_ITERATIONCOUNT 100  //80 
     14#define MIN_LIN_ITERATIONCOUNT 80  //60 
    1715#define SECANT_ITERATIONCOUNT 1 
    18 #define MAX_RAY_DEPTH 4 
    19  
    20 uniform samplerCUBE mp3Color : register(s0); 
    21 uniform samplerCUBE mp3Dist : register(s1); 
    22 uniform samplerCUBE mp1 : register(s2); 
    23 uniform samplerCUBE mp2 : register(s3); 
    24                                                          
    25  
    26 void linearSearch(  float3 x, float3 R, samplerCUBE mp, 
     16#define MAX_RAY_DEPTH 2 
     17 
     18void linearSearch(  float3 x, float3 R, float3 N, samplerCUBE mp, 
    2719                    out float3 p, 
    2820                    out float dl, 
     
    3022                    out float llp, 
    3123                    out float ppp) 
    32 {        
    33         p = 1; 
    34          
    35         float3 Ra = abs(R), xa = abs(x); 
    36         float  a = max(max(xa.x, xa.y), xa.z) / max(max(Ra.x, Ra.y), Ra.z);  
    37         bool   undershoot = false, overshoot = false;    
    38          
    39         float dt =  length(x / max(max(xa.x, xa.y), xa.z) - R / max(max(Ra.x, Ra.y), Ra.z)) * MAX_LIN_ITERATIONCOUNT; 
    40     dt = max(dt, MIN_LIN_ITERATIONCOUNT); 
    41     dt = 1.0 / dt; 
    42   
    43         float t = 0.01; 
    44         while( t < 1 && !(overshoot && undershoot) ) {  // iteration  
    45                 float dr = a * t / (1 - t);     // ray parameter corresponding to t 
    46                 float3 r = x + R * dr;          // point on the ray 
    47                 float ra =  readDistanceCubeMap(mp, r);         // |p'|: distance direction of p 
    48                  
    49                  
    50                 if (ra > 0) {           // valid texel, i.e. anything is visible 
    51                 float rrp = length(r)/ra; //|r|/|r'| 
    52                         if (rrp < 1) {          // undershooting 
    53                         dl = dr;        // store last undershooting as l 
    54                         llp = rrp; 
    55                         undershoot = true; 
    56                 } else { 
    57                         dp = dr;        // store last overshooting as p 
    58                         ppp = rrp; 
    59                         overshoot = true;} 
    60                 } else {                        // nothing is visible: restart search 
    61                 undershoot = false;      
    62                 overshoot = false; 
    63                 }        
    64                 t += dt;                        // next texel 
    65         } 
    66  
    67  
    68         if(!(overshoot && undershoot)) 
    69           p = float3(0,0,0);  
     24{ 
     25 float3 Ra = abs(R), xa = abs(x); 
     26 float xm =  max(max(xa.x,xa.y),xa.z); 
     27 float Rm = max(max(Ra.x,Ra.y),Ra.z); 
     28 float a = xm / Rm; 
     29  
     30 int shootL = 0, shootP = 0;           
     31 bool found = false;  
     32  
     33 float dt =  length(x / xm - R / Rm) * MAX_LIN_ITERATIONCOUNT; 
     34 dt = max(dt, MIN_LIN_ITERATIONCOUNT); 
     35 dt = 1.0 / dt; 
     36     
     37 float t = 0.01;//dt; 
     38 float pa; 
     39  
     40 //Linear iteration 
     41 while(t <= 1.0 && !found) 
     42 { 
     43   dp = a * t / (1 - t); 
     44   p = x + R * dp; 
     45   pa = readDistanceCubeMap(mp, p); 
     46       
     47   if(pa > 0) 
     48   { 
     49    ppp = length(p) / pa; 
     50    if(ppp < 1) 
     51      shootP = -1; 
     52    else  
     53      shootP = 1;       
     54    if(shootL * shootP == -1) 
     55      found = true; 
     56    else 
     57    { 
     58      shootL = shootP; 
     59      dl = dp; 
     60      llp = ppp; 
     61    } 
     62   } 
     63   else 
     64     shootL = 0; 
     65       
     66   t += dt;   
     67 } 
     68 
     69 if(!found) 
     70  p = float3(0,0,0); 
    7071} 
    7172 
     
    7677                       float llp, 
    7778                       float ppp, 
    78                        inout float3 p) 
     79                       out float3 p) 
    7980{ 
    8081  for(int i= 0; i < SECANT_ITERATIONCOUNT; i++) 
     
    9798} 
    9899 
    99 float3 Hit(float3 x, float3 R, out float4 Il, out float3 Nl) 
     100float3 Hit(float3 x, float3 R, float3 N,  
     101           samplerCUBE mp1, 
     102           samplerCUBE mp2, 
     103           samplerCUBE mp3Color, 
     104           samplerCUBE mp3Dist, 
     105           out float4 Il, out float3 Nl) 
    100106{ 
    101107 float dl1 = 0, dp1, llp1, ppp1; 
    102108 float3 p1; 
    103  linearSearch(x, R, mp1, p1, dl1, dp1, llp1, ppp1); 
     109 linearSearch(x, R, N, mp1, p1, dl1, dp1, llp1, ppp1); 
    104110 float dl2 = 0, dp2, llp2, ppp2; 
    105111 float3 p2; 
    106  linearSearch(x, R, mp2, p2, dl2, dp2, llp2, ppp2); 
     112 linearSearch(x, R, N, mp2, p2, dl2, dp2, llp2, ppp2); 
    107113  
    108114 bool valid1 = dot(p1,p1) != 0; 
     
    114120 if(!valid1 && ! valid2) 
    115121 { 
    116     linearSearch(x, R, mp3Dist, p, dl, dp, llp, ppp); 
     122    linearSearch(x, R, N, mp3Dist, p, dl, dp, llp, ppp); 
    117123    Il.a = 1;  
    118124    secantSearch(x, R, mp3Dist, dl, dp, llp, ppp, p); 
     
    139145} 
    140146 
    141  
    142  
    143 void SpecularReflectionVS(       
    144         in  float4 Pos  : POSITION,     // modeling space 
    145         in  float4 Norm : NORMAL,               // normal vector 
    146         out float4 hPos : POSITION,     // clipping space 
    147         out float3 x    : TEXCOORD1,    // cube map space 
    148         out float3 N    : TEXCOORD2,    // normal 
    149         out float3 V    : TEXCOORD3,    // view 
    150    uniform float4x4 WorldViewProj,   
    151    uniform float4x4 World, 
    152    uniform float4x4 WorldIT, 
    153    uniform float3   eyePos    // eye position 
    154 ) { 
    155         hPos = mul(Pos, WorldViewProj); 
    156         x    = mul(Pos, World).xyz; 
    157         N    = mul(Norm, WorldIT).xyz; 
    158         V    = x - eyePos; 
    159 } 
    160  
    161 float4 SingleReflectionPS(       
    162         float3 x : TEXCOORD1,           // cube map space 
    163         float3 N : TEXCOORD2,           // normal 
    164         float3 V : TEXCOORD3,           // view 
    165         uniform float Fp0, 
    166         uniform float3 lastCenter // cube map center position     
    167 ) : COLOR 
    168 { 
    169         x -= lastCenter; 
    170         V = normalize(V); N = normalize(N); 
    171         float3 R = reflect(V, N);       // reflection dir. 
    172         float3 Nl;                      // normal vector at the hit point 
    173         float3 Il;                      // radiance at the hit point 
    174         // ray hit l, radiance Il, normal Nl  
    175         float3 l = Hit(x, R, Il, Nl); 
    176  
    177         // Fresnel reflection 
    178         float3 F = Fp0 + pow(1-dot(N, -V), 5) * (1 - Fp0); 
    179         return float4(F * Il, 1); 
    180 } 
    181  
    182 float4 MultipleReflectionPS(     
    183                                                         float3 x : TEXCOORD1,           // shaded point in Cube map space 
    184                                                         float3 N : TEXCOORD2,           // normal vector 
    185                                                         float3 V : TEXCOORD3,           // view direction 
    186                                                         uniform float3 Fp0,     // Fresnel at perpendicular direction 
    187                                                         uniform float3 refIndex,        // index of refraction 
    188                                                         uniform float3 lastCenter 
    189 ) : COLOR 
    190 { 
    191         x-= lastCenter; 
    192         V = normalize(V); N = normalize(N); 
    193  
    194         float3 I = float3(1, 1, 1);// radiance of the path 
    195         float3 Fp = Fp0;           // Fresnel at 90 degrees at first hit 
    196         float  n = refIndex;             // index of refraction of the first hit 
    197         int depth = 0;          // length of the path 
    198         while (depth < MAX_RAY_DEPTH) { 
    199         float3 R;       // reflection or refraction dir 
    200         float3 F = Fp + pow(1-abs(dot(N, -V)), 5) * (1-Fp);  // Fresnel 
    201         if (n <= 0) { 
    202                 R = reflect(V, N);  // reflection 
    203                 I *= F;             // Fresnel reflection 
    204         } 
    205         else{                   // refraction 
    206                 if (dot(V,N) > 0) { // coming from inside 
    207                                 n = 1.0 / n; 
    208                                 N = -N; 
    209                 } 
    210                 R = refract(V, N, n); 
    211                 if (dot(R, R) == 0)     // no refraction direction exits 
    212                         R = reflect(V, N); // total reflection                           
    213                         else 
    214                         I *= (1-F);        // Fresnel refraction 
    215         } 
    216          
    217         float3 Nl;              // normal vector at the hit point 
    218         float4 Il;              // radiance at the hit point 
    219                 // Trace ray x+R*d and obtain hit l, radiance Il, normal Nl  
    220             float3 l = Hit(x, R, Il, Nl); 
    221             if (Il.a == 0) {            // hit point is on specular surface 
    222                 depth += 1; 
    223         } else {                // hit point is on diffuse surface 
    224                 I *= Il.rgb;    // multiply with the radiance  
    225                 depth = MAX_RAY_DEPTH;   // terminate 
    226         } 
    227         N = Nl; V = R; x = l; // hit point is the next shaded point 
    228         } 
    229         return float4(I, 1); 
    230 } 
    231  
    232  
    233147struct Shaded_OUT 
    234148{ 
     
    238152}; 
    239153 
    240 float4 MultipleReflectionPS_o(Shaded_OUT IN, 
     154float4 MultipleReflectionPS(Shaded_OUT IN, 
     155                                                        uniform samplerCUBE CubeMap : register(s0), 
     156                                                        uniform samplerCUBE DistanceMap : register(s1), 
     157                                                        uniform samplerCUBE NormDistMap1 : register(s2), 
     158                                                        uniform samplerCUBE NormDistMap2 : register(s3), 
    241159                                                        uniform float3 cameraPos, 
    242160                                                        uniform float3 lastCenter) : COLOR0 
    243161{        
    244         return 1; 
    245162         float4 I = float4(0,0,0,0); 
    246163                         
     
    264181           float3 Nl; 
    265182           float4 Il = 0; 
    266            l = Hit(x, R, Il, Nl); 
     183           l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, Il, Nl); 
    267184           if(Il.a == 0) 
    268185           {        
     
    280197        } 
    281198        if(I.a == 0) 
    282            I = readCubeMap(mp3Color, l); 
     199           I = readCubeMap(CubeMap, l); 
    283200         
    284201        return I; 
     
    286203 
    287204float4 MultipleRefractionPS(Shaded_OUT IN, 
     205                                                        uniform samplerCUBE CubeMap : register(s0), 
     206                                                        uniform samplerCUBE DistanceMap : register(s1), 
     207                                                        uniform samplerCUBE NormDistMap1 : register(s2), 
     208                                                        uniform samplerCUBE NormDistMap2 : register(s3), 
    288209                                                        uniform float3 cameraPos, 
    289                                                         uniform float3 lastCenter,                                                       
     210                                                        uniform float3 lastCenter, 
    290211                                                        uniform float sFresnel, 
    291212                                                        uniform float refIndex ) : COLOR0 
     
    298219          V = normalize(V); 
    299220          
    300          float F; 
     221         float F = sFresnel + pow(1 - dot(N, -V), 5) * (1 - sFresnel);    
     222         
     223         float3 l; 
     224         float4 Irefl; 
    301225         int depth = 0; 
    302         F = sFresnel + pow(1 - dot(N, -V), 5) * (1 - sFresnel);   
     226         
     227         while(depth < MAX_RAY_DEPTH) 
     228         { 
     229           float3 R; 
     230           R = normalize(reflect( V, N)); 
     231                 
     232           float3 Nl; 
     233           float4 Il; 
     234           l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, Il, Nl); 
     235           if(Il.a == 0) 
     236           { 
     237                   depth += 1;            
     238           } 
     239           else 
     240           { 
     241                   I = Il; 
     242                   depth = MAX_RAY_DEPTH; 
     243           } 
     244           x = l;           
     245           N = Nl; 
     246           V = R; 
     247        } 
     248        if(I.a == 0) 
     249           I = readCubeMap(CubeMap, l); 
     250        Irefl = I; 
     251         
     252        { 
     253        float4 Irefr; 
     254        N = normalize(IN.wNormal.xyz); 
     255        x = IN.wPos.xyz - lastCenter; 
     256        V  = (IN.wPos.xyz - cameraPos); 
     257        depth = 0; 
    303258         
    304259        while(depth < MAX_RAY_DEPTH) 
    305260        { 
    306261          float3 R;   
    307                   
    308           float ri = refIndex;    
     262           
     263          float ri = refIndex; 
     264          
    309265          if(dot(V,N) > 0) 
    310266          { 
     
    313269          } 
    314270          R = refract( V, N, ri); 
    315           if(dot(R,R) == 0) R = reflect( V, N); 
    316                                                  
     271          if(dot(R,R) == 0) R = reflect( V, N);  
     272                                         
    317273          float3 Nl; 
    318274          float4 Il; 
    319           float3 l = Hit(x, R, Il, Nl); 
     275          l = Hit(x, R, N, NormDistMap1, NormDistMap2, CubeMap, DistanceMap, Il, Nl); 
    320276          if(Il.a == 0) 
    321           {                
     277          { 
    322278                   depth += 1;            
    323279          } 
     
    330286          N = normalize(Nl); 
    331287          V = R;                    
    332         }  
    333         /*       
     288        } 
     289         
    334290        if(I.a == 0) 
    335         { 
    336          float ri = refIndex;     
    337          if(dot(V,N) > 0) 
    338          { 
    339                 ri = 1.0 / ri; 
    340                 N = -N;      
    341          } 
    342          float3 R = refract( V, N, ri); 
    343          if(dot(R,R) == 0) R = reflect( V, N); 
    344          I = readCubeMap(mp3Color, R); 
    345         } 
    346         */ 
    347         I *= (1.0 - F);  
    348      
    349         return I; 
     291           I = readCubeMap(CubeMap, l); 
     292        Irefr = I; 
     293             
     294        I = Irefl * F + (1.0 - F) * Irefr;       
     295    } 
     296        return I; 
    350297} 
    351298 
    352299float4 MultipleRefractionPhotonMap_PS(Shaded_OUT IN, 
     300                                                                        uniform samplerCUBE DistanceMap : register(s0), 
     301                                                                        uniform samplerCUBE NormDistMap1 : register(s1), 
     302                                                                        uniform samplerCUBE NormDistMap2 : register(s2), 
    353303                                                                        uniform float3 cameraPos, 
    354304                                                                        uniform float3 lastCenter, 
    355305                                                                        uniform float refIndex) : COLOR0 
    356306{ 
    357          float4 I = 0;           
    358          float3 N = normalize(IN.wNormal.xyz); 
    359          float3 x = IN.wPos.xyz - lastCenter; 
    360          float3 V  = (IN.wPos.xyz - cameraPos); 
    361          V = normalize(V); 
    362           
     307        float4 I = float4(0,0,0,0); 
     308                  
     309        float3 N = normalize(IN.wNormal.xyz); 
     310        float3 x = IN.wPos.xyz - lastCenter; 
     311        float3 V  = (IN.wPos.xyz - cameraPos); 
     312        V = normalize(V); 
     313        float3 l; 
    363314        int depth = 0; 
    364         float3 l; 
    365          
     315                 
    366316        while(depth < MAX_RAY_DEPTH) 
    367317        { 
    368318          float3 R;   
    369                   
    370           float ri = refIndex;    
     319           
     320          float ri = refIndex; 
     321          
    371322          if(dot(V,N) > 0) 
    372323          { 
     
    375326          } 
    376327          R = refract( V, N, ri); 
    377           if(dot(R,R) == 0) R = reflect( V, N); 
    378                                                  
     328          if(dot(R,R) == 0) R = reflect( V, N);  
     329                                         
    379330          float3 Nl; 
    380331          float4 Il; 
    381           l = Hit(x, R, Il, Nl); 
     332          l = Hit(x, R, N, NormDistMap1, NormDistMap2, DistanceMap, DistanceMap, Il, Nl); 
    382333          if(Il.a == 0) 
    383334          { 
    384                   depth += 1;             
     335                   depth += 1;            
    385336          } 
    386337          else 
    387338          { 
    388                   I = Il; 
    389                   depth = MAX_RAY_DEPTH; 
     339                   I = Il; 
     340                   depth = MAX_RAY_DEPTH; 
    390341          } 
    391342          x = l;            
    392343          N = normalize(Nl); 
    393344          V = R;                    
    394         }   
    395         if(I.a == 0) 
    396                 I = float4(V,1); 
    397         else 
    398                 I = float4(l,1); 
    399          
    400         return I; 
    401 } 
     345        } 
     346         
     347        return float4(l,1); 
     348} 
Note: See TracChangeset for help on using the changeset viewer.