1 | |
---|
2 | /* |
---|
3 | ----------------------------------------------------------------------------- |
---|
4 | This source file is part of GIMPACT Library. |
---|
5 | |
---|
6 | For the latest info, see http://gimpact.sourceforge.net/ |
---|
7 | |
---|
8 | Copyright (c) 2006 Francisco Leon. C.C. 80087371. |
---|
9 | email: projectileman@yahoo.com |
---|
10 | |
---|
11 | This library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of EITHER: |
---|
13 | (1) The GNU Lesser General Public License as published by the Free |
---|
14 | Software Foundation; either version 2.1 of the License, or (at |
---|
15 | your option) any later version. The text of the GNU Lesser |
---|
16 | General Public License is included with this library in the |
---|
17 | file GIMPACT-LICENSE-LGPL.TXT. |
---|
18 | (2) The BSD-style license that is included with this library in |
---|
19 | the file GIMPACT-LICENSE-BSD.TXT. |
---|
20 | |
---|
21 | This library is distributed in the hope that it will be useful, |
---|
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files |
---|
24 | GIMPACT-LICENSE-LGPL.TXT and GIMPACT-LICENSE-BSD.TXT for more details. |
---|
25 | |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | |
---|
29 | #include "GIMPACT/gim_trimesh.h" |
---|
30 | |
---|
31 | |
---|
32 | //! Trimesh Ray Collisions |
---|
33 | /*! |
---|
34 | |
---|
35 | \param trimesh |
---|
36 | \param contact |
---|
37 | \return 1 if the ray collides, else 0 |
---|
38 | */ |
---|
39 | int gim_trimesh_ray_collision(GIM_TRIMESH * trimesh,vec3f origin,vec3f dir, GREAL tmax, GIM_TRIANGLE_RAY_CONTACT_DATA * contact) |
---|
40 | { |
---|
41 | GDYNAMIC_ARRAY collision_result; |
---|
42 | GIM_CREATE_BOXQUERY_LIST(collision_result); |
---|
43 | |
---|
44 | gim_aabbset_ray_collision(origin,dir,tmax,&trimesh->m_aabbset,&collision_result); |
---|
45 | |
---|
46 | if(collision_result.m_size==0) |
---|
47 | { |
---|
48 | GIM_DYNARRAY_DESTROY(collision_result); |
---|
49 | return 0; |
---|
50 | } |
---|
51 | |
---|
52 | //collide triangles |
---|
53 | |
---|
54 | GUINT * boxesresult = GIM_DYNARRAY_POINTER(GUINT,collision_result); |
---|
55 | GIM_TRIANGLE_DATA tridata; |
---|
56 | vec3f pout; |
---|
57 | GREAL tparam,u,v; |
---|
58 | char does_intersect; |
---|
59 | |
---|
60 | gim_trimesh_locks_work_data(trimesh); |
---|
61 | |
---|
62 | for(unsigned int i=0;i<collision_result.m_size;i++) |
---|
63 | { |
---|
64 | gim_trimesh_get_triangle_data(trimesh,boxesresult[i],&tridata); |
---|
65 | |
---|
66 | RAY_TRIANGLE_INTERSECTION(origin,dir,tridata.m_vertices[0],tridata.m_vertices[1],tridata.m_vertices[2],tridata.m_planes.m_planes[0],pout,u,v,tparam,tmax,does_intersect); |
---|
67 | if(does_intersect) |
---|
68 | { |
---|
69 | contact->tparam = tparam; |
---|
70 | contact->u = u; |
---|
71 | contact->v = v; |
---|
72 | contact->m_face_id = boxesresult[i]; |
---|
73 | VEC_COPY(contact->m_point,pout); |
---|
74 | VEC_COPY(contact->m_normal,tridata.m_planes.m_planes[0]); |
---|
75 | |
---|
76 | gim_trimesh_unlocks_work_data(trimesh); |
---|
77 | GIM_DYNARRAY_DESTROY(collision_result); |
---|
78 | return 1; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | gim_trimesh_unlocks_work_data(trimesh); |
---|
83 | GIM_DYNARRAY_DESTROY(collision_result); |
---|
84 | return 0;//no collisiion |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | //! Trimesh Ray Collisions closest |
---|
89 | /*! |
---|
90 | Find the closest primitive collided by the ray |
---|
91 | \param trimesh |
---|
92 | \param contact |
---|
93 | \return 1 if the ray collides, else 0 |
---|
94 | */ |
---|
95 | int gim_trimesh_ray_closest_collision(GIM_TRIMESH * trimesh,vec3f origin,vec3f dir, GREAL tmax, GIM_TRIANGLE_RAY_CONTACT_DATA * contact) |
---|
96 | { |
---|
97 | GDYNAMIC_ARRAY collision_result; |
---|
98 | GIM_CREATE_BOXQUERY_LIST(collision_result); |
---|
99 | |
---|
100 | gim_aabbset_ray_collision(origin,dir,tmax,&trimesh->m_aabbset,&collision_result); |
---|
101 | |
---|
102 | if(collision_result.m_size==0) |
---|
103 | { |
---|
104 | GIM_DYNARRAY_DESTROY(collision_result); |
---|
105 | return 0; |
---|
106 | } |
---|
107 | |
---|
108 | //collide triangles |
---|
109 | |
---|
110 | GUINT * boxesresult = GIM_DYNARRAY_POINTER(GUINT,collision_result); |
---|
111 | GIM_TRIANGLE_DATA tridata; |
---|
112 | vec3f pout; |
---|
113 | GREAL tparam,u,v; |
---|
114 | char does_intersect; |
---|
115 | contact->tparam = tmax + 0.1f; |
---|
116 | |
---|
117 | |
---|
118 | gim_trimesh_locks_work_data(trimesh); |
---|
119 | |
---|
120 | for(unsigned int i=0;i<collision_result.m_size;i++) |
---|
121 | { |
---|
122 | gim_trimesh_get_triangle_data(trimesh,boxesresult[i],&tridata); |
---|
123 | |
---|
124 | RAY_TRIANGLE_INTERSECTION(origin,dir,tridata.m_vertices[0],tridata.m_vertices[1],tridata.m_vertices[2],tridata.m_planes.m_planes[0],pout,u,v,tparam,tmax,does_intersect); |
---|
125 | if(does_intersect && (tparam < contact->tparam)) |
---|
126 | { |
---|
127 | contact->tparam = tparam; |
---|
128 | contact->u = u; |
---|
129 | contact->v = v; |
---|
130 | contact->m_face_id = boxesresult[i]; |
---|
131 | VEC_COPY(contact->m_point,pout); |
---|
132 | VEC_COPY(contact->m_normal,tridata.m_planes.m_planes[0]); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | gim_trimesh_unlocks_work_data(trimesh); |
---|
137 | GIM_DYNARRAY_DESTROY(collision_result); |
---|
138 | if(contact->tparam > tmax) return 0; |
---|
139 | return 1; |
---|
140 | } |
---|