GeomUtil.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //-----------------------------------------------------------------------------
  2. // TriangleTest.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. namespace CollisionSample
  10. {
  11. /// <summary>
  12. /// Contains miscellaneous utilities augmenting the framework math library.
  13. /// </summary>
  14. public static class GeomUtil
  15. {
  16. /// <summary>
  17. /// Do a full perspective transform of the given vector by the given matrix,
  18. /// dividing out the w coordinate to return a Vector3 result.
  19. /// </summary>
  20. /// <param name="position">Vector3 of a point in space</param>
  21. /// <param name="matrix">4x4 matrix</param>
  22. /// <param name="result">Transformed vector after perspective divide</param>
  23. public static void PerspectiveTransform(ref Vector3 position, ref Matrix matrix, out Vector3 result)
  24. {
  25. float w = position.X * matrix.M14 + position.Y * matrix.M24 + position.Z * matrix.M34 + matrix.M44;
  26. float winv = 1.0f / w;
  27. float x = position.X * matrix.M11 + position.Y * matrix.M21 + position.Z * matrix.M31 + matrix.M41;
  28. float y = position.X * matrix.M12 + position.Y * matrix.M22 + position.Z * matrix.M32 + matrix.M42;
  29. float z = position.X * matrix.M13 + position.Y * matrix.M23 + position.Z * matrix.M33 + matrix.M43;
  30. result = new Vector3();
  31. result.X = x * winv;
  32. result.Y = y * winv;
  33. result.Z = z * winv;
  34. }
  35. }
  36. }