IceRay.h 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains code for rays.
  4. * \file IceRay.h
  5. * \author Pierre Terdiman
  6. * \date April, 4, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Include Guard
  11. #ifndef __ICERAY_H__
  12. #define __ICERAY_H__
  13. class ICEMATHS_API Ray
  14. {
  15. public:
  16. //! Constructor
  17. inline_ Ray() {}
  18. //! Constructor
  19. inline_ Ray(const Point& orig, const Point& dir) : mOrig(orig), mDir(dir) {}
  20. //! Copy constructor
  21. inline_ Ray(const Ray& ray) : mOrig(ray.mOrig), mDir(ray.mDir) {}
  22. //! Destructor
  23. inline_ ~Ray() {}
  24. float SquareDistance(const Point& point, float* t=null) const;
  25. inline_ float Distance(const Point& point, float* t=null) const { return sqrtf(SquareDistance(point, t)); }
  26. Point mOrig; //!< Ray origin
  27. Point mDir; //!< Normalized direction
  28. };
  29. inline_ void ComputeReflexionVector(Point& reflected, const Point& incoming_dir, const Point& outward_normal)
  30. {
  31. reflected = incoming_dir - outward_normal * 2.0f * (incoming_dir|outward_normal);
  32. }
  33. inline_ void ComputeReflexionVector(Point& reflected, const Point& source, const Point& impact, const Point& normal)
  34. {
  35. Point V = impact - source;
  36. reflected = V - normal * 2.0f * (V|normal);
  37. }
  38. inline_ void DecomposeVector(Point& normal_compo, Point& tangent_compo, const Point& outward_dir, const Point& outward_normal)
  39. {
  40. normal_compo = outward_normal * (outward_dir|outward_normal);
  41. tangent_compo = outward_dir - normal_compo;
  42. }
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. /**
  45. * Transforms a direction vector from world space to local space
  46. * \param local_dir [out] direction vector in local space
  47. * \param world_dir [in] direction vector in world space
  48. * \param world [in] world transform
  49. */
  50. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  51. inline_ void ComputeLocalDirection(Point& local_dir, const Point& world_dir, const Matrix4x4& world)
  52. {
  53. // Get world direction back in local space
  54. // Matrix3x3 InvWorld = world;
  55. // local_dir = InvWorld * world_dir;
  56. local_dir = Matrix3x3(world) * world_dir;
  57. }
  58. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. /**
  60. * Transforms a position vector from world space to local space
  61. * \param local_pt [out] position vector in local space
  62. * \param world_pt [in] position vector in world space
  63. * \param world [in] world transform
  64. */
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. inline_ void ComputeLocalPoint(Point& local_pt, const Point& world_pt, const Matrix4x4& world)
  67. {
  68. // Get world vertex back in local space
  69. Matrix4x4 InvWorld = world;
  70. InvWorld.Invert();
  71. local_pt = world_pt * InvWorld;
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. /**
  75. * Transforms a ray from world space to local space
  76. * \param local_ray [out] ray in local space
  77. * \param world_ray [in] ray in world space
  78. * \param world [in] world transform
  79. */
  80. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. inline_ void ComputeLocalRay(Ray& local_ray, const Ray& world_ray, const Matrix4x4& world)
  82. {
  83. // Get world ray back in local space
  84. ComputeLocalDirection(local_ray.mDir, world_ray.mDir, world);
  85. ComputeLocalPoint(local_ray.mOrig, world_ray.mOrig, world);
  86. }
  87. #endif // __ICERAY_H__