Ray.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. [StructLayout(LayoutKind.Sequential), SerializeObject]
  9. public struct Ray
  10. {
  11. public Vector3 origin;
  12. public Vector3 direction;
  13. public Ray(Vector3 origin, Vector3 direction)
  14. {
  15. this.origin = origin;
  16. this.direction = direction;
  17. }
  18. public static Vector3 operator*(Ray ray, float t)
  19. {
  20. return ray.origin + ray.direction * t;
  21. }
  22. public void Transform(Matrix4 matrix)
  23. {
  24. Vector3 end = this * 1.0f;
  25. origin = matrix.Multiply(origin);
  26. end = matrix.Multiply(end);
  27. direction = Vector3.Normalize(end - origin);
  28. }
  29. public void TransformAffine(Matrix4 matrix)
  30. {
  31. Vector3 end = this * 1.0f;
  32. origin = matrix.MultiplyAffine(origin);
  33. end = matrix.MultiplyAffine(end);
  34. direction = Vector3.Normalize(end - origin);
  35. }
  36. public override string ToString()
  37. {
  38. return String.Format("(origin: {0} direction: {1})", origin, direction);
  39. }
  40. };
  41. }