From ee1148577080c68bc6c6734e945e139605835122 Mon Sep 17 00:00:00 2001 From: Jerome St-Louis Date: Wed, 6 Aug 2014 22:51:44 -0400 Subject: [PATCH] ecere/gfx3D/Vector3D: Using Fast Inverse Square Root for Normalize() - http://en.wikipedia.org/wiki/Fast_inverse_square_root --- ecere/src/gfx/3D/Vector3D.ec | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ecere/src/gfx/3D/Vector3D.ec b/ecere/src/gfx/3D/Vector3D.ec index d051507..2cf29f7 100644 --- a/ecere/src/gfx/3D/Vector3D.ec +++ b/ecere/src/gfx/3D/Vector3D.ec @@ -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) -- 1.8.3.1