mirror of
https://github.com/recp/cglm.git
synced 2025-10-03 16:51:35 +00:00
Add ray-triangle intersection check
This commit is contained in:
@@ -63,7 +63,8 @@ cglm_HEADERS = include/cglm/version.h \
|
||||
include/cglm/ease.h \
|
||||
include/cglm/curve.h \
|
||||
include/cglm/bezier.h \
|
||||
include/cglm/applesimd.h
|
||||
include/cglm/applesimd.h \
|
||||
include/cglm/ray.h
|
||||
|
||||
cglm_calldir=$(includedir)/cglm/call
|
||||
cglm_call_HEADERS = include/cglm/call/mat4.h \
|
||||
@@ -84,7 +85,8 @@ cglm_call_HEADERS = include/cglm/call/mat4.h \
|
||||
include/cglm/call/sphere.h \
|
||||
include/cglm/call/ease.h \
|
||||
include/cglm/call/curve.h \
|
||||
include/cglm/call/bezier.h
|
||||
include/cglm/call/bezier.h \
|
||||
include/cglm/call/ray.h
|
||||
|
||||
cglm_simddir=$(includedir)/cglm/simd
|
||||
cglm_simd_HEADERS = include/cglm/simd/intrin.h \
|
||||
@@ -147,7 +149,8 @@ libcglm_la_SOURCES=\
|
||||
src/sphere.c \
|
||||
src/ease.c \
|
||||
src/curve.c \
|
||||
src/bezier.c
|
||||
src/bezier.c \
|
||||
src/ray.c
|
||||
|
||||
test_tests_SOURCES=\
|
||||
test/runner.c \
|
||||
|
@@ -52,3 +52,4 @@ Follow the :doc:`build` documentation for this
|
||||
curve
|
||||
bezier
|
||||
version
|
||||
ray
|
||||
|
31
docs/source/ray.rst
Normal file
31
docs/source/ray.rst
Normal file
@@ -0,0 +1,31 @@
|
||||
.. default-domain:: C
|
||||
|
||||
ray
|
||||
====
|
||||
|
||||
Header: cglm/ray.h
|
||||
|
||||
This is for collision-checks used by ray-tracers and the like.
|
||||
|
||||
Table of contents (click to go):
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Functions:
|
||||
|
||||
1. :c:func:`glm_ray_triangle`
|
||||
|
||||
Functions documentation
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. c:function:: bool glm_ray_triangle(vec3 origin, vec3 direction, vec3 v0, vec3 v1, vec3 v2, float *d)
|
||||
|
||||
Möller–Trumbore ray-triangle intersection algorithm
|
||||
|
||||
Parameters:
|
||||
| *[in]* **origin** origin of ray
|
||||
| *[in]* **direction** direction of ray
|
||||
| *[in]* **v0** first vertex of triangle
|
||||
| *[in]* **v1** second vertex of triangle
|
||||
| *[in]* **v2** third vertex of triangle
|
||||
| *[in, out]* **d** float pointer to save distance to intersection
|
||||
| *[out]* **intersection** whether there is intersection
|
@@ -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
|
8
src/ray.c
Normal file
8
src/ray.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "../include/cglm/cglm.h"
|
||||
#include "../include/cglm/call.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
bool
|
||||
glmc_ray_triangle(vec3 origin, vec3 direction, vec3 v0, vec3 v1, vec3 v2, float *d) {
|
||||
return glm_ray_triangle(origin, direction, v0, v1, v2, d);
|
||||
}
|
@@ -34,6 +34,7 @@
|
||||
<ClCompile Include="..\src\plane.c" />
|
||||
<ClCompile Include="..\src\project.c" />
|
||||
<ClCompile Include="..\src\quat.c" />
|
||||
<ClCompile Include="..\src\ray.c" />
|
||||
<ClCompile Include="..\src\sphere.c" />
|
||||
<ClCompile Include="..\src\vec2.c" />
|
||||
<ClCompile Include="..\src\vec3.c" />
|
||||
@@ -61,6 +62,7 @@
|
||||
<ClInclude Include="..\include\cglm\call\plane.h" />
|
||||
<ClInclude Include="..\include\cglm\call\project.h" />
|
||||
<ClInclude Include="..\include\cglm\call\quat.h" />
|
||||
<ClInclude Include="..\include\cglm\call\ray.h" />
|
||||
<ClInclude Include="..\include\cglm\call\sphere.h" />
|
||||
<ClInclude Include="..\include\cglm\call\vec2.h" />
|
||||
<ClInclude Include="..\include\cglm\call\vec3.h" />
|
||||
@@ -80,6 +82,7 @@
|
||||
<ClInclude Include="..\include\cglm\plane.h" />
|
||||
<ClInclude Include="..\include\cglm\project.h" />
|
||||
<ClInclude Include="..\include\cglm\quat.h" />
|
||||
<ClInclude Include="..\include\cglm\ray.h" />
|
||||
<ClInclude Include="..\include\cglm\simd\arm.h" />
|
||||
<ClInclude Include="..\include\cglm\simd\avx\affine.h" />
|
||||
<ClInclude Include="..\include\cglm\simd\avx\mat4.h" />
|
||||
|
@@ -92,6 +92,9 @@
|
||||
<ClCompile Include="..\src\vec2.c">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\ray.c">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\src\config.h">
|
||||
@@ -124,6 +127,9 @@
|
||||
<ClInclude Include="..\include\cglm\call\vec4.h">
|
||||
<Filter>include\cglm\call</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\cglm\call\ray.h">
|
||||
<Filter>include\cglm\call</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\cglm\simd\avx\affine.h">
|
||||
<Filter>include\cglm\simd\avx</Filter>
|
||||
</ClInclude>
|
||||
@@ -241,6 +247,9 @@
|
||||
<ClInclude Include="..\include\cglm\ease.h">
|
||||
<Filter>include\cglm</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\cglm\ray.h">
|
||||
<Filter>include\cglm</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\cglm\simd\arm.h">
|
||||
<Filter>include\cglm\simd</Filter>
|
||||
</ClInclude>
|
||||
|
Reference in New Issue
Block a user