Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OPC_MeshInterface.cpp
Go to the documentation of this file.
1 /*
3  * OPCODE - Optimized Collision Detection
4  * Copyright (C) 2001 Pierre Terdiman
5  * Homepage: http://www.codercorner.com/Opcode.htm
6  */
8 
10 
16 
19 
29 
32 
115 
118 // Precompiled Header
119 #include "Stdafx.h"
120 
121 
122 using namespace Opcode;
123 
125 
130 #ifdef OPC_USE_CALLBACKS
131  mUserData (null),
132  mObjCallback (null),
133 #else
134  mTris (null),
135  mVerts (null),
136  #ifdef OPC_USE_STRIDE
137  mTriStride (sizeof(IndexedTriangle)),
138  mVertexStride (sizeof(Point)),
139  #endif
140 #endif
141  mNbTris (0),
142  mNbVerts (0)
143 {
144 }
145 
147 
152 {
153 }
154 
156 
160 bool MeshInterface::IsValid() const
162 {
163  if(!mNbTris || !mNbVerts) return false;
164 #ifdef OPC_USE_CALLBACKS
165  if(!mObjCallback) return false;
166 #else
167  if(!mTris || !mVerts) return false;
168 #endif
169  return true;
170 }
171 
173 
180 {
181  // Check topology. If the model contains degenerate faces, collision report can be wrong in some cases.
182  // e.g. it happens with the standard MAX teapot. So clean your meshes first... If you don't have a mesh cleaner
183  // you can try this: www.codercorner.com/Consolidation.zip
184 
185  udword NbDegenerate = 0;
186 
187  VertexPointers VP;
188 
189  // Using callbacks, we don't have access to vertex indices. Nevertheless we still can check for
190  // redundant vertex pointers, which cover all possibilities (callbacks/pointers/strides).
191  for(udword i=0;i<mNbTris;i++)
192  {
193  GetTriangle(VP, i);
194 
195  if( (VP.Vertex[0]==VP.Vertex[1])
196  || (VP.Vertex[1]==VP.Vertex[2])
197  || (VP.Vertex[2]==VP.Vertex[0])) NbDegenerate++;
198  }
199 
200  return NbDegenerate;
201 }
202 
203 #ifdef OPC_USE_CALLBACKS
204 
211 bool MeshInterface::SetCallback(RequestCallback callback, void* user_data)
213 {
214 // if(!callback) return SetIceError("MeshInterface::SetCallback: callback pointer is null");
215  if(!callback) return(false);
216  mObjCallback = callback;
217  mUserData = user_data;
218  return true;
219 }
220 #else
221 
228 bool MeshInterface::SetPointers(const IndexedTriangle* tris, const Point* verts)
230 {
231 // if(!tris || !verts) return SetIceError("MeshInterface::SetPointers: pointer is null", null);
232  if(!tris || !verts) return(false);
233 
234  mTris = tris;
235  mVerts = verts;
236  return true;
237 }
238 #ifdef OPC_USE_STRIDE
239 
246 bool MeshInterface::SetStrides(udword tri_stride, udword vertex_stride)
248 {
249  if(tri_stride<sizeof(IndexedTriangle)) return SetIceError("MeshInterface::SetStrides: invalid triangle stride", null);
250  if(vertex_stride<sizeof(Point)) return SetIceError("MeshInterface::SetStrides: invalid vertex stride", null);
251 
252  mTriStride = tri_stride;
253  mVertexStride = vertex_stride;
254  return true;
255 }
256 #endif
257 #endif
258 
260 
266 bool MeshInterface::RemapClient(udword nb_indices, const udword* permutation) const
268 {
269  // Checkings
270  if(!nb_indices || !permutation) return false;
271  if(nb_indices!=mNbTris) return false;
272 
273 #ifdef OPC_USE_CALLBACKS
274  // We can't really do that using callbacks
275  return false;
276 #else
277  IndexedTriangle* Tmp = new IndexedTriangle[mNbTris];
278  CHECKALLOC(Tmp);
279 
280  #ifdef OPC_USE_STRIDE
281  udword Stride = mTriStride;
282  #else
283  udword Stride = sizeof(IndexedTriangle);
284  #endif
285 
286  for(udword i=0;i<mNbTris;i++)
287  {
288  const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + i * Stride);
289  Tmp[i] = *T;
290  }
291 
292  for(udword i=0;i<mNbTris;i++)
293  {
294  IndexedTriangle* T = (IndexedTriangle*)(((ubyte*)mTris) + i * Stride);
295  *T = Tmp[permutation[i]];
296  }
297 
298  DELETEARRAY(Tmp);
299 #endif
300  return true;
301 }
302