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_Model.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 
89 
92 // Precompiled Header
93 #include "Stdafx.h"
94 
95 
96 using namespace Opcode;
97 
99 
102 Model::Model()
104 {
105 #ifdef __MESHMERIZER_H__ // Collision hulls only supported within ICE !
106  mHull = null;
107 #endif // __MESHMERIZER_H__
108 }
109 
111 
116 {
117  Release();
118 }
119 
121 
124 void Model::Release()
126 {
127  ReleaseBase();
128 #ifdef __MESHMERIZER_H__ // Collision hulls only supported within ICE !
129  DELETESINGLE(mHull);
130 #endif // __MESHMERIZER_H__
131 }
132 
134 
139 bool Model::Build(const OPCODECREATE& create)
141 {
142  // 1) Checkings
143  if(!create.mIMesh || !create.mIMesh->IsValid()) return false;
144 
145  // For this model, we only support complete trees
146 // if(create.mSettings.mLimit!=1) return SetIceError ("OPCODE WARNING: supports complete trees only! Use mLimit = 1.", null);
147  if(create.mSettings.mLimit!=1) return(false);
148  // Look for degenerate faces.
149  udword NbDegenerate = create.mIMesh->CheckTopology();
150 // if(NbDegenerate) Log ("OPCODE WARNING: found %lu degenerate faces in model! Collision might report wrong results!", NbDegenerate);
151  // We continue nonetheless....
152 
153  Release(); // Make sure previous tree has been discarded [Opcode 1.3, thanks Adam]
154 
155  // 1-1) Setup mesh interface automatically [Opcode 1.3]
156  SetMeshInterface(create.mIMesh);
157 
158  // Special case for 1-triangle meshes [Opcode 1.3]
159  udword NbTris = create.mIMesh->GetNbTriangles();
160  if(NbTris==1)
161  {
162  // We don't need to actually create a tree here, since we'll only have a single triangle to deal with anyway.
163  // It's a waste to use a "model" for this but at least it will work.
165  return true;
166  }
167 
168  // 2) Build a generic AABB Tree.
169  mSource = new AABBTree;
171 
172  // 2-1) Setup a builder. Our primitives here are triangles from input mesh,
173  // so we use an AABBTreeOfTrianglesBuilder.....
174  {
176  TB.mIMesh = create.mIMesh;
177  TB.mSettings = create.mSettings;
178  TB.mNbPrimitives = NbTris;
179  if(!mSource->Build(&TB)) return false;
180  }
181 
182  // 3) Create an optimized tree according to user-settings
183  if(!CreateTree(create.mNoLeaf, create.mQuantized)) return false;
184 
185  // 3-2) Create optimized tree
186  if(!mTree->Build(mSource)) return false;
187 
188  // 3-3) Delete generic tree if needed
189  if(!create.mKeepOriginal) DELETESINGLE(mSource);
190 
191 #ifdef __MESHMERIZER_H__
192  // 4) Convex hull
193  if(create.mCollisionHull)
194  {
195  // Create hull
196  mHull = new CollisionHull;
197  CHECKALLOC(mHull);
198 
199  CONVEXHULLCREATE CHC;
200  // ### doesn't work with strides
201  CHC.NbVerts = create.mIMesh->GetNbVertices();
202  CHC.Vertices = create.mIMesh->GetVerts();
203  CHC.UnifyNormals = true;
204  CHC.ReduceVertices = true;
205  CHC.WordFaces = false;
206  mHull->Compute(CHC);
207  }
208 #endif // __MESHMERIZER_H__
209 
210  return true;
211 }
212 
214 
220 {
221  if(!mTree) return 0;
222  return mTree->GetUsedBytes();
223 }
224