mirror of
https://github.com/recp/cglm.git
synced 2026-01-02 13:49:59 +00:00
Add ray-triangle intersection check
This commit is contained in:
@@ -31,6 +31,7 @@ extern "C" {
|
||||
#include "call/ease.h"
|
||||
#include "call/curve.h"
|
||||
#include "call/bezier.h"
|
||||
#include "call/ray.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
22
include/cglm/call/ray.h
Normal file
22
include/cglm/call/ray.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (c), Recep Aslantas.
|
||||
*
|
||||
* MIT License (MIT), http://opensource.org/licenses/MIT
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
#ifndef cglmc_ray_h
|
||||
#define cglmc_ray_h
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include "../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_ray_triangle(vec3 origin, vec3 direction, vec3 v0, vec3 v1, vec3 v2, float *d);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* cglmc_ray_h */
|
||||
@@ -30,5 +30,6 @@
|
||||
#include "ease.h"
|
||||
#include "curve.h"
|
||||
#include "bezier.h"
|
||||
#include "ray.h"
|
||||
|
||||
#endif /* cglm_h */
|
||||
|
||||
66
include/cglm/ray.h
Normal file
66
include/cglm/ray.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c), Recep Aslantas.
|
||||
*
|
||||
* MIT License (MIT), http://opensource.org/licenses/MIT
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
/*
|
||||
Functions:
|
||||
CGLM_INLINE bool glm_line_triangle_intersect(vec3 origin, vec3 direction, vec3 v0, vec3 v1, vec3 v2, float *d);
|
||||
*/
|
||||
|
||||
#ifndef cglm_ray_h
|
||||
#define cglm_ray_h
|
||||
|
||||
#include "vec3.h"
|
||||
|
||||
/*!
|
||||
* @brief Möller–Trumbore ray-triangle intersection algorithm
|
||||
*
|
||||
* @param[in] origin origin of ray
|
||||
* @param[in] direction direction of ray
|
||||
* @param[in] v0 first vertex of triangle
|
||||
* @param[in] v1 second vertex of triangle
|
||||
* @param[in] v2 third vertex of triangle
|
||||
* @param[in, out] d distance to intersection
|
||||
* @param[out] intersection whether there is intersection
|
||||
*/
|
||||
|
||||
CGLM_INLINE
|
||||
bool
|
||||
glm_ray_triangle(vec3 origin, vec3 direction, vec3 v0, vec3 v1, vec3 v2, float *d) {
|
||||
const float epsilon = 0.000001;
|
||||
vec3 edge1, edge2, p, t, q;
|
||||
float det, inv_det, u, v;
|
||||
|
||||
glm_vec3_sub(v1, v0, edge1);
|
||||
glm_vec3_sub(v2, v0, edge2);
|
||||
|
||||
glm_vec3_cross(direction, edge2, p);
|
||||
|
||||
det = glm_vec3_dot(edge1, p);
|
||||
|
||||
if (det > -epsilon && det < epsilon)
|
||||
return 0;
|
||||
inv_det = 1.0 / det;
|
||||
|
||||
glm_vec3_sub(origin, v0, t);
|
||||
|
||||
u = inv_det * glm_vec3_dot(t, p);
|
||||
|
||||
if (u < 0.0 || u > 1.0)
|
||||
return 0;
|
||||
|
||||
glm_vec3_cross(t, edge1, q);
|
||||
|
||||
v = inv_det * glm_vec3_dot(direction, q);
|
||||
|
||||
if (v < 0.0 || u + v > 1.0)
|
||||
return 0;
|
||||
|
||||
*d = inv_det * glm_vec3_dot(edge2, q);
|
||||
return *d > epsilon;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user