@@ -16,49 +16,55 @@ namespace math
1616{
1717
1818// Octahedral Transform, maps 3D direction vectors to 2D square and vice versa
19- template<typename T = float64_t >
19+ template<typename T = float32_t >
2020struct OctahedralTransform
2121{
2222 using scalar_type = T;
2323 using vector2_type = vector <T, 2 >;
2424 using vector3_type = vector <T, 3 >;
2525
26- // F : [-1 , 1]^2 -> S^2
27- static vector3_type eval (const vector2_type ndc )
26+ // F : [0 , 1]^2 -> S^2
27+ static vector3_type uvToDir (const vector2_type uv )
2828 {
29- vector3_type p = vector3_type (ndc.xy , scalar_type (0 ));
30- const vector2_type a = abs (p.xy );
29+ vector3_type p = vector3_type ((uv * scalar_type ( 2 ) - scalar_type ( 1 )) , scalar_type (0 ));
30+ const scalar_type a_x = abs (p.x); const scalar_type a_y = abs (p.y );
3131
32- p.z = scalar_type (1 ) - a.x - a.y ;
32+ p.z = scalar_type (1 ) - a_x - a_y ;
3333
34- if (p.z < scalar_type (0 ))
35- p.xy = hlsl::sign (p.xy) * (scalar_type (1 ) - abs (p.yx));
34+ if (p.z < scalar_type (0 ))
35+ {
36+ p.x = (p.x < scalar_type (0 ) ? scalar_type (-1 ) : scalar_type (1 )) * (scalar_type (1 ) - a_y);
37+ p.y = (p.y < scalar_type (0 ) ? scalar_type (-1 ) : scalar_type (1 )) * (scalar_type (1 ) - a_x);
38+ }
3639
3740 return hlsl::normalize (p);
3841 }
3942
4043 // F^-1 : S^2 -> [-1, 1]^2
41- static vector2_type inverse (vector3_type dir)
44+ static vector2_type dirToNDC (vector3_type dir)
4245 {
4346 dir = hlsl::normalize (dir);
4447 const scalar_type sum = hlsl::dot (vector3_type (scalar_type (1 ), scalar_type (1 ), scalar_type (1 )), abs (dir));
4548 vector3_type s = dir / sum;
4649
4750 if (s.z < scalar_type (0 ))
48- s.xy = hlsl::sign (s.xy) * (scalar_type (1 ) - abs (s.yx));
51+ {
52+ s.x = (s.x < scalar_type (0 ) ? scalar_type (-1 ) : scalar_type (1 )) * (scalar_type (1 ) - abs (s.y));
53+ s.y = (s.y < scalar_type (0 ) ? scalar_type (-1 ) : scalar_type (1 )) * (scalar_type (1 ) - abs (s.x));
54+ }
4955
5056 return s.xy;
5157 }
5258
53- // transforms direction vector into UV ( for corner sampling)
59+ // transforms direction vector into UV for corner sampling
5460 // dir in S^2, halfMinusHalfPixel in [0, 0.5)^2,
5561 // where halfMinusHalfPixel = 0.5-0.5/texSize
5662 // and texSize.x >= 1, texSize.y >= 1
57- // NOTE/TODO: not best place to keep it here imo
63+ // NOTE/TODO: not best place to keep it here
5864 static vector2_type toCornerSampledUV (vector3_type dir, vector2_type halfMinusHalfPixel)
5965 {
6066 // note: cornerSampled(NDC*0.5+0.5) = NDC*0.5*(1-1/texSize)+0.5
61- return inverse (dir) * halfMinusHalfPixel + scalar_type (0.5 );
67+ return dirToNDC (dir) * halfMinusHalfPixel + scalar_type (0.5 );
6268 }
6369};
6470
0 commit comments