1
0

ray.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. #ifndef cglms_ray_h
  8. #define cglms_ray_h
  9. #include "../common.h"
  10. #include "../types-struct.h"
  11. #include "../ray.h"
  12. /* api definition */
  13. #define glms_ray_(NAME) CGLM_STRUCTAPI(ray, NAME)
  14. /*!
  15. * @brief Möller–Trumbore ray-triangle intersection algorithm
  16. *
  17. * @param[in] origin origin of ray
  18. * @param[in] direction direction of ray
  19. * @param[in] v0 first vertex of triangle
  20. * @param[in] v1 second vertex of triangle
  21. * @param[in] v2 third vertex of triangle
  22. * @param[in, out] d distance to intersection
  23. * @return whether there is intersection
  24. */
  25. CGLM_INLINE
  26. bool
  27. glms_ray_(triangle)(vec3s origin,
  28. vec3s direction,
  29. vec3s v0,
  30. vec3s v1,
  31. vec3s v2,
  32. float *d) {
  33. return glm_ray_triangle(origin.raw, direction.raw, v0.raw, v1.raw, v2.raw, d);
  34. }
  35. /*!
  36. * @brief ray sphere intersection
  37. *
  38. * returns false if there is no intersection if true:
  39. *
  40. * - t1 > 0, t2 > 0: ray intersects the sphere at t1 and t2 both ahead of the origin
  41. * - t1 < 0, t2 > 0: ray starts inside the sphere, exits at t2
  42. * - t1 < 0, t2 < 0: no intersection ahead of the ray ( returns false )
  43. * - the caller can check if the intersection points (t1 and t2) fall within a
  44. * specific range (for example, tmin < t1, t2 < tmax) to determine if the
  45. * intersections are within a desired segment of the ray
  46. *
  47. * @param[in] origin ray origin
  48. * @param[out] dir normalized ray direction
  49. * @param[in] s sphere [center.x, center.y, center.z, radii]
  50. * @param[in] t1 near point1 (closer to origin)
  51. * @param[in] t2 far point2 (farther from origin)
  52. *
  53. * @returns whether there is intersection
  54. */
  55. CGLM_INLINE
  56. bool
  57. glms_ray_(sphere)(vec3s origin,
  58. vec3s dir,
  59. vec4s s,
  60. float * __restrict t1,
  61. float * __restrict t2) {
  62. return glm_ray_sphere(origin.raw, dir.raw, s.raw, t1, t2);
  63. }
  64. /*!
  65. * @brief point using t by 𝐏(𝑡)=𝐀+𝑡𝐛
  66. *
  67. * @param[in] orig origin of ray
  68. * @param[in] dir direction of ray
  69. * @param[in] t parameter
  70. * @returns point point at t
  71. */
  72. CGLM_INLINE
  73. vec3s
  74. glms_ray_(at)(vec3s orig, vec3s dir, float t) {
  75. vec3s r;
  76. glm_ray_at(orig.raw, dir.raw, t, r.raw);
  77. return r;
  78. }
  79. #endif /* cglms_ray_h */