Debian Codegraph @

source/unix/odesrc/OPCODE/OPC_MeshInterface.cpp

raw
1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 /*
3 * OPCODE - Optimized Collision Detection
4 * Copyright (C) 2001 Pierre Terdiman
5 * Homepage: http://www.codercorner.com/Opcode.htm
6 */
7 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8
9 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10 /**
11 * Contains a mesh interface.
12 * \file OPC_MeshInterface.cpp
13 * \author Pierre Terdiman
14 * \date November, 27, 2002
15 */
16 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
17
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 /**
20 * This structure holds 3 vertex-pointers. It's mainly used by collision callbacks so that the app doesn't have
21 * to return 3 vertices to OPCODE (36 bytes) but only 3 pointers (12 bytes). It seems better but I never profiled
22 * the alternative.
23 *
24 * \class VertexPointers
25 * \author Pierre Terdiman
26 * \version 1.3
27 * \date March, 20, 2001
28 */
29 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
30
31 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
32 /**
33 * This class is an interface between us and user-defined meshes. Meshes can be defined in a lot of ways, and here we
34 * try to support most of them.
35 *
36 * Basically you have two options:
37 * - callbacks, if OPC_USE_CALLBACKS is defined in OPC_Settings.h.
38 * - else pointers.
39 *
40 * If using pointers, you can also use strides or not. Strides are used when OPC_USE_STRIDE is defined.
41 *
42 *
43 * CALLBACKS:
44 *
45 * Using callbacks is the most generic way to feed OPCODE with your meshes. Indeed, you just have to give
46 * access to three vertices at the end of the day. It's up to you to fetch them from your database, using
47 * whatever method you want. Hence your meshes can lie in system memory or AGP, be indexed or not, use 16
48 * or 32-bits indices, you can decompress them on-the-fly if needed, etc. On the other hand, a callback is
49 * called each time OPCODE needs access to a particular triangle, so there might be a slight overhead.
50 *
51 * To make things clear: geometry & topology are NOT stored in the collision system,
52 * in order to save some ram. So, when the system needs them to perform accurate intersection
53 * tests, you're requested to provide the triangle-vertices corresponding to a given face index.
54 *
55 * Ex:
56 *
57 * \code
58 * static void ColCallback(udword triangle_index, VertexPointers& triangle, udword user_data)
59 * {
60 * // Get back Mesh0 or Mesh1 (you also can use 2 different callbacks)
61 * Mesh* MyMesh = (Mesh*)user_data;
62 * // Get correct triangle in the app-controlled database
63 * const Triangle* Tri = MyMesh->GetTriangle(triangle_index);
64 * // Setup pointers to vertices for the collision system
65 * triangle.Vertex[0] = MyMesh->GetVertex(Tri->mVRef[0]);
66 * triangle.Vertex[1] = MyMesh->GetVertex(Tri->mVRef[1]);
67 * triangle.Vertex[2] = MyMesh->GetVertex(Tri->mVRef[2]);
68 * }
69 *
70 * // Setup callbacks
71 * MeshInterface0->SetCallback(ColCallback, udword(Mesh0));
72 * MeshInterface1->SetCallback(ColCallback, udword(Mesh1));
73 * \endcode
74 *
75 * Of course, you should make this callback as fast as possible. And you're also not supposed
76 * to modify the geometry *after* the collision trees have been built. The alternative was to
77 * store the geometry & topology in the collision system as well (as in RAPID) but we have found
78 * this approach to waste a lot of ram in many cases.
79 *
80 *
81 * POINTERS:
82 *
83 * If you're internally using the following canonical structures:
84 * - a vertex made of three 32-bits floating point values
85 * - a triangle made of three 32-bits integer vertex references
86 * ...then you may want to use pointers instead of callbacks. This is the same, except OPCODE will directly
87 * use provided pointers to access the topology and geometry, without using a callback. It might be faster,
88 * but probably not as safe. Pointers have been introduced in OPCODE 1.2.
89 *
90 * Ex:
91 *
92 * \code
93 * // Setup pointers
94 * MeshInterface0->SetPointers(Mesh0->GetFaces(), Mesh0->GetVerts());
95 * MeshInterface1->SetPointers(Mesh1->GetFaces(), Mesh1->GetVerts());
96 * \endcode
97 *
98 *
99 * STRIDES:
100 *
101 * If your vertices are D3D-like entities interleaving a position, a normal and/or texture coordinates
102 * (i.e. if your vertices are FVFs), you might want to use a vertex stride to skip extra data OPCODE
103 * doesn't need. Using a stride shouldn't be notably slower than not using it, but it might increase
104 * cache misses. Please also note that you *shouldn't* read from AGP or video-memory buffers !
105 *
106 *
107 * In any case, compilation flags are here to select callbacks/pointers/strides at compile time, so
108 * choose what's best for your application. All of this has been wrapped into this MeshInterface.
109 *
110 * \class MeshInterface
111 * \author Pierre Terdiman
112 * \version 1.3
113 * \date November, 27, 2002
114 */
115 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
116
117 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
118 // Precompiled Header
119 #include "Stdafx.h"
120
121 using namespace Opcode;
122
123 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
124 /**
125 * Constructor.
126 */
127 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
128 MeshInterface::MeshInterface() :
129 mNbTris (0),
130 mNbVerts (0),
131 #ifdef OPC_USE_CALLBACKS
132 mUserData (null),
133 mObjCallback (null),
134 mExUserData (null),
135 mObjExCallback (null),
136 #else
137 #ifdef OPC_USE_STRIDE
138 mTriStride (sizeof(IndexedTriangle)),
139 mVertexStride (sizeof(Point)),
140 mFetchTriangle (&MeshInterface::FetchTriangleFromSingles),
141 mFetchExTriangle (&MeshInterface::FetchExTriangleFromSingles),
142 #endif
143 mTris (null),
144 mVerts (null)
145 #endif
146 {
147 }
148
149 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
150 /**
151 * Destructor.
152 */
153 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
154 MeshInterface::~MeshInterface()
155 {
156 }
157
158 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
159 /**
160 * Checks the mesh interface is valid, i.e. things have been setup correctly.
161 * \return true if valid
162 */
163 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
164 bool MeshInterface::IsValid() const
165 {
166 if(!mNbTris || !mNbVerts) return false;
167 #ifdef OPC_USE_CALLBACKS
168 if(!mObjCallback) return false;
169 #else
170 if(!mTris || !mVerts) return false;
171 #endif
172 return true;
173 }
174
175 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
176 /**
177 * Checks the mesh itself is valid.
178 * Currently we only look for degenerate faces.
179 * \return number of degenerate faces
180 */
181 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
182 udword MeshInterface::CheckTopology() const
183 {
184 // Check topology. If the model contains degenerate faces, collision report can be wrong in some cases.
185 // e.g. it happens with the standard MAX teapot. So clean your meshes first... If you don't have a mesh cleaner
186 // you can try this: www.codercorner.com/Consolidation.zip
187
188 udword NbDegenerate = 0;
189
190 VertexPointers VP;
191 ConversionArea VC;
192
193 // Using callbacks, we don't have access to vertex indices. Nevertheless we still can check for
194 // redundant vertex pointers, which cover all possibilities (callbacks/pointers/strides).
195 for(udword i=0;i<mNbTris;i++)
196 {
197 GetTriangle(VP, i, VC);
198
199 if( (VP.Vertex[0]==VP.Vertex[1])
200 || (VP.Vertex[1]==VP.Vertex[2])
201 || (VP.Vertex[2]==VP.Vertex[0])) NbDegenerate++;
202 }
203
204 return NbDegenerate;
205 }
206
207 #ifdef OPC_USE_CALLBACKS
208 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
209 /**
210 * Callback control: setups object callback. Must provide triangle-vertices for a given triangle index.
211 * \param callback [in] user-defined callback
212 * \param user_data [in] user-defined data
213 * \return true if success
214 */
215 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
216 bool MeshInterface::SetCallback(RequestCallback callback, void* user_data)
217 {
218 if(!callback) return SetIceError("MeshInterface::SetCallback: callback pointer is null");
219
220 mObjCallback = callback;
221 mUserData = user_data;
222 return true;
223 }
224
225 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
226 /**
227 * Callback control: setups object ex-callback. Must provide triangle-vertices and vertex indice for a given triangle index.
228 * \param callback [in] user-defined callback
229 * \param user_data [in] user-defined data
230 * \return true if success
231 */
232 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
233 bool MeshInterface::SetExCallback(RequestExCallback callback, void* user_data)
234 {
235 // if(!callback) -- allow nulls
236
237 mObjExCallback = callback;
238 mExUserData = user_data;
239 return true;
240 }
241 #else
242 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
243 /**
244 * Pointers control: setups object pointers. Must provide access to faces and vertices for a given object.
245 * \param tris [in] pointer to triangles
246 * \param verts [in] pointer to vertices
247 * \return true if success
248 */
249 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
250 bool MeshInterface::SetPointers(const IndexedTriangle* tris, const Point* verts)
251 {
252 if(!tris || !verts) return SetIceError("MeshInterface::SetPointers: pointer is null", null);
253
254 mTris = tris;
255 mVerts = verts;
256 return true;
257 }
258 #ifdef OPC_USE_STRIDE
259 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
260 /**
261 * Strides control
262 * \param tri_stride [in] size of a triangle in bytes. The first sizeof(IndexedTriangle) bytes are used to get vertex indices.
263 * \param vertex_stride [in] size of a vertex in bytes. The first sizeof(Point) bytes are used to get vertex position.
264 * \return true if success
265 */
266 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
267 bool MeshInterface::SetStrides(udword tri_stride, udword vertex_stride)
268 {
269 if(tri_stride<sizeof(IndexedTriangle)) return SetIceError("MeshInterface::SetStrides: invalid triangle stride", null);
270 if(vertex_stride<sizeof(Point)) return SetIceError("MeshInterface::SetStrides: invalid vertex stride", null);
271
272 mTriStride = tri_stride;
273 mVertexStride = vertex_stride;
274 return true;
275 }
276 #endif
277 #endif
278
279 #ifndef OPC_USE_CALLBACKS
280 #ifdef OPC_USE_STRIDE
281 void MeshInterface::FetchTriangleFromSingles(VertexPointers& vp, udword index, ConversionArea vc) const
282 {
283 const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + index * mTriStride);
284
285 const Point* Verts = GetVerts();
286 udword VertexStride = GetVertexStride();
287 vp.Vertex[0] = (const Point*)(((ubyte*)Verts) + T->mVRef[0] * VertexStride);
288 vp.Vertex[1] = (const Point*)(((ubyte*)Verts) + T->mVRef[1] * VertexStride);
289 vp.Vertex[2] = (const Point*)(((ubyte*)Verts) + T->mVRef[2] * VertexStride);
290 }
291
292 void MeshInterface::FetchTriangleFromDoubles(VertexPointers& vp, udword index, ConversionArea vc) const
293 {
294 const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + index * mTriStride);
295
296 const Point* Verts = GetVerts();
297 udword VertexStride = GetVertexStride();
298
299 for (int i = 0; i < 3; i++){
300 const double* v = (const double*)(((ubyte*)Verts) + T->mVRef[i] * VertexStride);
301
302 vc[i].x = (float)v[0];
303 vc[i].y = (float)v[1];
304 vc[i].z = (float)v[2];
305 vp.Vertex[i] = &vc[i];
306 }
307 }
308
309 void MeshInterface::FetchExTriangleFromSingles(VertexPointersEx& vpe, udword index, ConversionArea vc) const
310 {
311 const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + index * mTriStride);
312
313 const Point* Verts = GetVerts();
314 udword VertexStride = GetVertexStride();
315
316 dTriIndex VertIndex0 = T->mVRef[0];
317 vpe.Index[0] = VertIndex0;
318 vpe.vp.Vertex[0] = (const Point*)(((ubyte*)Verts) + VertIndex0 * VertexStride);
319
320 dTriIndex VertIndex1 = T->mVRef[1];
321 vpe.Index[1] = VertIndex1;
322 vpe.vp.Vertex[1] = (const Point*)(((ubyte*)Verts) + VertIndex1 * VertexStride);
323
324 dTriIndex VertIndex2 = T->mVRef[2];
325 vpe.Index[2] = VertIndex2;
326 vpe.vp.Vertex[2] = (const Point*)(((ubyte*)Verts) + VertIndex2 * VertexStride);
327 }
328
329 void MeshInterface::FetchExTriangleFromDoubles(VertexPointersEx& vpe, udword index, ConversionArea vc) const
330 {
331 const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + index * mTriStride);
332
333 const Point* Verts = GetVerts();
334 udword VertexStride = GetVertexStride();
335
336 for (int i = 0; i < 3; i++){
337 dTriIndex VertIndex = T->mVRef[i];
338 vpe.Index[i] = VertIndex;
339
340 const double* v = (const double*)(((ubyte*)Verts) + VertIndex * VertexStride);
341 vc[i].x = (float)v[0];
342 vc[i].y = (float)v[1];
343 vc[i].z = (float)v[2];
344 vpe.vp.Vertex[i] = &vc[i];
345 }
346 }
347 #endif
348 #endif
349
350
351 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
352 /**
353 * Remaps client's mesh according to a permutation.
354 * \param nb_indices [in] number of indices in the permutation (will be checked against number of triangles)
355 * \param permutation [in] list of triangle indices
356 * \return true if success
357 */
358 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
359 bool MeshInterface::RemapClient(udword nb_indices, const dTriIndex* permutation) const
360 {
361 // Checkings
362 if(!nb_indices || !permutation) return false;
363 if(nb_indices!=mNbTris) return false;
364
365 #ifdef OPC_USE_CALLBACKS
366 // We can't really do that using callbacks
367 return false;
368 #else
369 IndexedTriangle* Tmp = new IndexedTriangle[mNbTris];
370 CHECKALLOC(Tmp);
371
372 #ifdef OPC_USE_STRIDE
373 udword Stride = mTriStride;
374 #else
375 udword Stride = sizeof(IndexedTriangle);
376 #endif
377
378 for(udword i=0;i<mNbTris;i++)
379 {
380 const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + i * Stride);
381 Tmp[i] = *T;
382 }
383
384 for(udword i=0;i<mNbTris;i++)
385 {
386 IndexedTriangle* T = (IndexedTriangle*)(((ubyte*)mTris) + i * Stride);
387 *T = Tmp[permutation[i]];
388 }
389
390 DELETEARRAY(Tmp);
391 #endif
392 return true;
393 }