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 base model interface.
12  *	\file		OPC_BaseModel.cpp
13  *	\author		Pierre Terdiman
14  *	\date		May, 18, 2003
15  */
16 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
17 
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 /**
20  *	The base class for collision models.
21  *
22  *	\class		BaseModel
23  *	\author		Pierre Terdiman
24  *	\version	1.3
25  *	\date		May, 18, 2003
26 */
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 
29 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
30 // Precompiled Header
31 #include "Stdafx.h"
32 
33 using namespace Opcode;
34 
35 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
36 /**
37  *	Constructor.
38  */
39 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
OPCODECREATE()40 OPCODECREATE::OPCODECREATE()
41 {
42 	mIMesh				= null;
43 	mSettings.mRules	= SPLIT_SPLATTER_POINTS | SPLIT_GEOM_CENTER;
44 	mSettings.mLimit	= 1;	// Mandatory for complete trees
45 	mNoLeaf				= true;
46 	mQuantized			= true;
47 #ifdef __MESHMERIZER_H__
48 	mCollisionHull		= false;
49 #endif // __MESHMERIZER_H__
50 	mKeepOriginal		= false;
51 	mCanRemap			= false;
52 }
53 
54 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
55 /**
56  *	Constructor.
57  */
58 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BaseModel()59 BaseModel::BaseModel() : mIMesh(null), mModelCode(0), mSource(null), mTree(null)
60 {
61 }
62 
63 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64 /**
65  *	Destructor.
66  */
67 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~BaseModel()68 BaseModel::~BaseModel()
69 {
70 	ReleaseBase();
71 }
72 
73 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
74 /**
75  *	Releases everything.
76  */
77 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ReleaseBase()78 void BaseModel::ReleaseBase()
79 {
80 	DELETESINGLE(mSource);
81 	DELETESINGLE(mTree);
82 }
83 
84 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85 /**
86  *	Creates an optimized tree according to user-settings, and setups mModelCode.
87  *	\param		no_leaf		[in] true for "no leaf" tree
88  *	\param		quantized	[in] true for quantized tree
89  *	\return		true if success
90  */
91 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CreateTree(bool no_leaf,bool quantized)92 bool BaseModel::CreateTree(bool no_leaf, bool quantized)
93 {
94 	DELETESINGLE(mTree);
95 
96 	// Setup model code
97 	if(no_leaf)		mModelCode |= OPC_NO_LEAF;
98 	else			mModelCode &= ~OPC_NO_LEAF;
99 
100 	if(quantized)	mModelCode |= OPC_QUANTIZED;
101 	else			mModelCode &= ~OPC_QUANTIZED;
102 
103 	// Create the correct class
104 	if(mModelCode & OPC_NO_LEAF)
105 	{
106 		if(mModelCode & OPC_QUANTIZED)	mTree = new AABBQuantizedNoLeafTree;
107 		else							mTree = new AABBNoLeafTree;
108 	}
109 	else
110 	{
111 		if(mModelCode & OPC_QUANTIZED)	mTree = new AABBQuantizedTree;
112 		else							mTree = new AABBCollisionTree;
113 	}
114 	CHECKALLOC(mTree);
115 
116 	return true;
117 }
118 
119 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
120 /**
121  *	Refits the collision model. This can be used to handle dynamic meshes. Usage is:
122  *	1. modify your mesh vertices (keep the topology constant!)
123  *	2. refit the tree (call this method)
124  *	\return		true if success
125  */
126 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Refit()127 bool BaseModel::Refit()
128 {
129 	// Refit the optimized tree
130 	return mTree->Refit(mIMesh);
131 
132 // Old code kept for reference : refit the source tree then rebuild !
133 //	if(!mSource)	return false;
134 //	// Ouch...
135 //	mSource->Refit(&mTB);
136 //	// Ouch...
137 //	return mTree->Build(mSource);
138 }
139