ecere/gfx3D/Vector3D: Using Fast Inverse Square Root for Normalize()
authorJerome St-Louis <jerome@ecere.com>
Thu, 7 Aug 2014 02:51:44 +0000 (22:51 -0400)
committerJerome St-Louis <jerome@ecere.com>
Thu, 7 Aug 2014 02:53:07 +0000 (22:53 -0400)
 - http://en.wikipedia.org/wiki/Fast_inverse_square_root

ecere/src/gfx/3D/Vector3D.ec

index d051507..2cf29f7 100644 (file)
@@ -151,6 +151,16 @@ public struct Vector3D
    }
 };
 
+inline float FastInvSqrt(float x)
+{
+  union { float f; uint u; } i;
+  float halfX = x / 2;
+  i.f = x;
+  i.u = 0x5f375a86 - (i.u >> 1);
+  x = i.f;
+  return x * (1.5f - (halfX * x * x));
+}
+
 public struct Vector3Df
 {
    float x, y, z;
@@ -190,6 +200,7 @@ public struct Vector3Df
 
    void Normalize(Vector3Df source)
    {
+      /*
       float m = source.length;
       if(m)
       {
@@ -199,6 +210,11 @@ public struct Vector3Df
       }
       else
          x = y = z = 0;
+      */
+      float i = FastInvSqrt(source.x * source.x + source.y * source.y + source.z * source.z);
+      x = source.x * i;
+      y = source.y * i;
+      z = source.z * i;
    }
 
    void MultMatrix(Vector3Df source, Matrix matrix)