[1712] | 1 |
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | float3 Hit( float3 x, float3 R, samplerCUBE mp )
|
---|
| 5 | {
|
---|
| 6 |
|
---|
| 7 | float rl = readDistanceCubeMap(mp, R); // |r|
|
---|
| 8 |
|
---|
| 9 | float dp = rl - dot(x, R); // parallax
|
---|
| 10 |
|
---|
| 11 | float3 p = x + R * dp;
|
---|
| 12 | return p;
|
---|
| 13 | }
|
---|
| 14 | ---------------------------------------------------------------------------------------
|
---|
| 15 |
|
---|
| 16 | // This function is called several times.
|
---|
| 17 | float3 Hit( float3 x, float3 R, samplerCUBE mp )
|
---|
| 18 | {
|
---|
| 19 | float rl = readDistanceCubeMap( mp, R); // |r|
|
---|
| 20 |
|
---|
| 21 | float ppp = length( x ) / readDistanceCubeMap( mp, x); // |p|/|p|
|
---|
| 22 | float dun = 0, pun = ppp, dov = 0, pov = 0;
|
---|
| 23 | float dl = rl * ( 1 - ppp ); // eq. 2
|
---|
| 24 | float3 l = x + R * dl; // ray equation
|
---|
| 25 |
|
---|
| 26 | // iteration
|
---|
| 27 | for( int i = 0; i < 2; i++ ) // 2 !!!!!!!!!!!!!!!!!!!!!!!
|
---|
| 28 | {
|
---|
| 29 | float llp = length( l ) / readDistanceCubeMap( mp, l); // |l|/|l|
|
---|
| 30 | if ( llp < 0.999f ) // undershooting
|
---|
| 31 | {
|
---|
| 32 | dun = dl; pun = llp; // last undershooting
|
---|
| 33 | dl += ( dov == 0 ) ? rl * ( 1 - llp ) : // eq. 2
|
---|
| 34 | ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3
|
---|
| 35 | } else if ( llp > 1.001f ) // overshooting
|
---|
| 36 | {
|
---|
| 37 | dov = dl; pov = llp; // last overshooting
|
---|
| 38 | dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
|
---|
| 39 | }
|
---|
| 40 | l = x + R * dl; // ray equation
|
---|
| 41 | }
|
---|
| 42 | return l; // computed hit point
|
---|
| 43 | }
|
---|
| 44 | ----------------------------------------------------------------------------------------
|
---|
| 45 |
|
---|
| 46 | float3 Hit(float3 x, float3 R, samplerCUBE mp)
|
---|
| 47 | {
|
---|
| 48 | R = normalize(R);
|
---|
| 49 | float3 xNorm = normalize(x);
|
---|
| 50 | float3 dt = (R - xNorm) / (float) LIN_ITERATIONCOUNT;
|
---|
| 51 | float dx = length(cross(R,x));
|
---|
| 52 |
|
---|
| 53 | float3 pN = normalize(cross(R, xNorm));
|
---|
| 54 | float3 N = normalize(cross(pN,R));
|
---|
| 55 | float d = dot(x, N);
|
---|
| 56 |
|
---|
| 57 | float3 under = xNorm * readDistanceCubeMap(mp, x);
|
---|
| 58 | float3 over = R * readDistanceCubeMap(mp, R);
|
---|
| 59 |
|
---|
| 60 | //linear iteration
|
---|
| 61 | for(int i = 1; i < LIN_ITERATIONCOUNT; i++)
|
---|
| 62 | {
|
---|
| 63 | float3 dir = normalize(xNorm + dt * i);
|
---|
| 64 | float dist = readDistanceCubeMap( mp, dir);
|
---|
| 65 | float3 point = dir * dist;
|
---|
| 66 |
|
---|
| 67 | if( dot(N, point) > d)//undershooting
|
---|
| 68 | {
|
---|
| 69 | under = point;
|
---|
| 70 | }
|
---|
| 71 | else //overshooting
|
---|
| 72 | {
|
---|
| 73 | over = point;
|
---|
| 74 | i = LIN_ITERATIONCOUNT;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | float3 dirUn = normalize(under);
|
---|
| 79 | float3 dirOv = normalize(over);
|
---|
| 80 | float tun = d / dot(N, dirUn);
|
---|
| 81 | float pun = tun / length(under);
|
---|
| 82 | float dun = length(tun * dirUn - x);
|
---|
| 83 | float tov = d / dot(N, dirOv);
|
---|
| 84 | float pov = tov / length(over);
|
---|
| 85 | float dov = length(tov * dirOv - x);
|
---|
| 86 | float tl = (d - dot(N, over)) / dot(N, under - over);
|
---|
| 87 | float3 l = over + tl * (under - over);
|
---|
| 88 | float dl = length(l - x);
|
---|
| 89 |
|
---|
| 90 | //secant iteration
|
---|
| 91 | for( int i = 0; i < SECANT_ITERATIONCOUNT; i++ )
|
---|
| 92 | {
|
---|
| 93 | float llp = length( l ) / readDistanceCubeMap( mp, l); // |l|/|l|
|
---|
| 94 | if ( llp < 0.999f ) // undershooting
|
---|
| 95 | {
|
---|
| 96 | dun = dl; pun = llp; // last undershooting
|
---|
| 97 | dl += ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3
|
---|
| 98 | } else if ( llp > 1.001f ) // overshooting
|
---|
| 99 | {
|
---|
| 100 | dov = dl; pov = llp; // last overshooting
|
---|
| 101 | dl += ( dl - dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
|
---|
| 102 | }
|
---|
| 103 | l = x + R * dl; // ray equation
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | return l;
|
---|
| 107 | return over;
|
---|
| 108 | return (under + over)/2.0;
|
---|
| 109 | }
|
---|
| 110 | ----------------------------------------------------------------------------------------------
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | float3 Hit(float3 x, float3 R, samplerCUBE mp)
|
---|
| 114 | {
|
---|
| 115 | //return R + x * 0.0000000001;
|
---|
| 116 |
|
---|
| 117 | R = normalize(R);
|
---|
| 118 | float3 xNorm = normalize(x);
|
---|
| 119 | float3 dt = (R - xNorm) / (float) LIN_ITERATIONCOUNT;
|
---|
| 120 | float dx = length(cross(R,x));
|
---|
| 121 |
|
---|
| 122 | float3 pN = normalize(cross(R, xNorm));
|
---|
| 123 | float3 N = normalize(cross(pN,R));
|
---|
| 124 | float D = dot(x, N);
|
---|
| 125 |
|
---|
| 126 | float under = readDistanceCubeMap(mp, x);
|
---|
| 127 | float over = readDistanceCubeMap(mp, R);
|
---|
| 128 | float dun = 0;
|
---|
| 129 | float dov = 0;
|
---|
| 130 | float lun = length(x);
|
---|
| 131 | float lov;
|
---|
| 132 |
|
---|
| 133 | float3 l;
|
---|
| 134 |
|
---|
| 135 | //linear iteration
|
---|
| 136 | for(int i = 1; i < LIN_ITERATIONCOUNT; i++)
|
---|
| 137 | {
|
---|
| 138 |
|
---|
| 139 | l = normalize(xNorm + dt * i);
|
---|
| 140 | float lL = readDistanceCubeMap( mp, l);
|
---|
| 141 | float lambda = D / dot(N, l);
|
---|
| 142 | //float lambda = (dot(x,l) - dot(R,l) * dot(x,R)) / (lL * lL - dot(R,l) * dot(R,l));
|
---|
| 143 | float d = dot(l, R) * lambda - dot(x, R);
|
---|
| 144 |
|
---|
| 145 | if( lambda < lL)//undershooting
|
---|
| 146 | {
|
---|
| 147 | lun = lambda;
|
---|
| 148 | dun = d;
|
---|
| 149 | under = lL;
|
---|
| 150 | }
|
---|
| 151 | else //overshooting
|
---|
| 152 | {
|
---|
| 153 | lov = lambda;
|
---|
| 154 | dov = d;
|
---|
| 155 | over = lL;
|
---|
| 156 | i = LIN_ITERATIONCOUNT;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | if(dov == 0)l = R;
|
---|
| 161 | else l = x + R * dov;
|
---|
| 162 | return l;
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | float pun = lun / length(under);
|
---|
| 166 | float pov;
|
---|
| 167 | float dl;
|
---|
| 168 |
|
---|
| 169 | if(dov == 0)
|
---|
| 170 | {
|
---|
| 171 | dl = over * ( 1 - pun );
|
---|
| 172 | }
|
---|
| 173 | else
|
---|
| 174 | {
|
---|
| 175 | pov = lov / length(over);
|
---|
| 176 | dl = dun + (dun - dov) * (1 - pun)/(pun - pov);
|
---|
| 177 | }
|
---|
| 178 | l = x + R * dl;
|
---|
| 179 |
|
---|
| 180 | //secant iteration
|
---|
| 181 | for( int i = 0; i < SECANT_ITERATIONCOUNT; i++ )
|
---|
| 182 | {
|
---|
| 183 | float llp = length( l ) / readDistanceCubeMap( mp, l); // |l|/|l|
|
---|
| 184 | if ( llp < 0.999f ) // undershooting
|
---|
| 185 | {
|
---|
| 186 | dun = dl; pun = llp; // last undershooting
|
---|
| 187 | dl += ( dov == 0 ) ? over * ( 1 - llp ) : // eq. 2
|
---|
| 188 | ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3
|
---|
| 189 | } else if ( llp > 1.001f ) // overshooting
|
---|
| 190 | {
|
---|
| 191 | dov = dl; pov = llp; // last overshooting
|
---|
| 192 | dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
|
---|
| 193 | }
|
---|
| 194 | l = x + R * dl; // ray equation
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | return l;
|
---|
| 198 | return over;
|
---|
| 199 | return (under + over) / 2.0;
|
---|
| 200 | }
|
---|