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.h |
---|
13 | * \author Pierre Terdiman |
---|
14 | * \date May, 18, 2003 |
---|
15 | */ |
---|
16 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
17 | |
---|
18 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
19 | // Include Guard |
---|
20 | #ifndef __OPC_BASEMODEL_H__ |
---|
21 | #define __OPC_BASEMODEL_H__ |
---|
22 | |
---|
23 | //! Model creation structure |
---|
24 | struct OPCODE_API OPCODECREATE |
---|
25 | { |
---|
26 | //! Constructor |
---|
27 | OPCODECREATE(); |
---|
28 | |
---|
29 | MeshInterface* mIMesh; //!< Mesh interface (access to triangles & vertices) (*) |
---|
30 | BuildSettings mSettings; //!< Builder's settings |
---|
31 | bool mNoLeaf; //!< true => discard leaf nodes (else use a normal tree) |
---|
32 | bool mQuantized; //!< true => quantize the tree (else use a normal tree) |
---|
33 | #ifdef __MESHMERIZER_H__ |
---|
34 | bool mCollisionHull; //!< true => use convex hull + GJK |
---|
35 | #endif // __MESHMERIZER_H__ |
---|
36 | bool mKeepOriginal; //!< true => keep a copy of the original tree (debug purpose) |
---|
37 | bool mCanRemap; //!< true => allows OPCODE to reorganize client arrays |
---|
38 | |
---|
39 | // (*) This pointer is saved internally and used by OPCODE until collision structures are released, |
---|
40 | // so beware of the object's lifetime. |
---|
41 | }; |
---|
42 | |
---|
43 | enum ModelFlag |
---|
44 | { |
---|
45 | OPC_QUANTIZED = (1<<0), //!< Compressed/uncompressed tree |
---|
46 | OPC_NO_LEAF = (1<<1), //!< Leaf/NoLeaf tree |
---|
47 | OPC_SINGLE_NODE = (1<<2) //!< Special case for 1-node models |
---|
48 | }; |
---|
49 | |
---|
50 | class OPCODE_API BaseModel |
---|
51 | { |
---|
52 | public: |
---|
53 | // Constructor/Destructor |
---|
54 | BaseModel(); |
---|
55 | virtual ~BaseModel(); |
---|
56 | |
---|
57 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
58 | /** |
---|
59 | * Builds a collision model. |
---|
60 | * \param create [in] model creation structure |
---|
61 | * \return true if success |
---|
62 | */ |
---|
63 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
64 | virtual bool Build(const OPCODECREATE& create) = 0; |
---|
65 | |
---|
66 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
67 | /** |
---|
68 | * Gets the number of bytes used by the tree. |
---|
69 | * \return amount of bytes used |
---|
70 | */ |
---|
71 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
72 | virtual udword GetUsedBytes() const = 0; |
---|
73 | |
---|
74 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
75 | /** |
---|
76 | * Refits the collision model. This can be used to handle dynamic meshes. Usage is: |
---|
77 | * 1. modify your mesh vertices (keep the topology constant!) |
---|
78 | * 2. refit the tree (call this method) |
---|
79 | * \return true if success |
---|
80 | */ |
---|
81 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
82 | virtual bool Refit(); |
---|
83 | |
---|
84 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
85 | /** |
---|
86 | * Gets the source tree. |
---|
87 | * \return generic tree |
---|
88 | */ |
---|
89 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
90 | inline_ const AABBTree* GetSourceTree() const { return mSource; } |
---|
91 | |
---|
92 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
93 | /** |
---|
94 | * Gets the tree. |
---|
95 | * \return the collision tree |
---|
96 | */ |
---|
97 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
98 | inline_ const AABBOptimizedTree* GetTree() const { return mTree; } |
---|
99 | |
---|
100 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
101 | /** |
---|
102 | * Gets the tree. |
---|
103 | * \return the collision tree |
---|
104 | */ |
---|
105 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
106 | inline_ AABBOptimizedTree* GetTree() { return mTree; } |
---|
107 | |
---|
108 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
109 | /** |
---|
110 | * Gets the number of nodes in the tree. |
---|
111 | * Should be 2*N-1 for normal trees and N-1 for optimized ones. |
---|
112 | * \return number of nodes |
---|
113 | */ |
---|
114 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
115 | inline_ udword GetNbNodes() const { return mTree->GetNbNodes(); } |
---|
116 | |
---|
117 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
118 | /** |
---|
119 | * Checks whether the tree has leaf nodes or not. |
---|
120 | * \return true if the tree has leaf nodes (normal tree), else false (optimized tree) |
---|
121 | */ |
---|
122 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
123 | inline_ BOOL HasLeafNodes() const { return !(mModelCode & OPC_NO_LEAF); } |
---|
124 | |
---|
125 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
126 | /** |
---|
127 | * Checks whether the tree is quantized or not. |
---|
128 | * \return true if the tree is quantized |
---|
129 | */ |
---|
130 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
131 | inline_ BOOL IsQuantized() const { return mModelCode & OPC_QUANTIZED; } |
---|
132 | |
---|
133 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
134 | /** |
---|
135 | * Checks whether the model has a single node or not. This special case must be handled separately. |
---|
136 | * \return true if the model has only 1 node |
---|
137 | */ |
---|
138 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
139 | inline_ BOOL HasSingleNode() const { return mModelCode & OPC_SINGLE_NODE; } |
---|
140 | |
---|
141 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
142 | /** |
---|
143 | * Gets the model's code. |
---|
144 | * \return model's code |
---|
145 | */ |
---|
146 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
147 | inline_ udword GetModelCode() const { return mModelCode; } |
---|
148 | |
---|
149 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
150 | /** |
---|
151 | * Gets the mesh interface. |
---|
152 | * \return mesh interface |
---|
153 | */ |
---|
154 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
155 | inline_ const MeshInterface* GetMeshInterface() const { return mIMesh; } |
---|
156 | |
---|
157 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
158 | /** |
---|
159 | * Sets the mesh interface. |
---|
160 | * \param imesh [in] mesh interface |
---|
161 | */ |
---|
162 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
163 | inline_ void SetMeshInterface(const MeshInterface* imesh) { mIMesh = imesh; } |
---|
164 | |
---|
165 | protected: |
---|
166 | const MeshInterface* mIMesh; //!< User-defined mesh interface |
---|
167 | udword mModelCode; //!< Model code = combination of ModelFlag(s) |
---|
168 | AABBTree* mSource; //!< Original source tree |
---|
169 | AABBOptimizedTree* mTree; //!< Optimized tree owned by the model |
---|
170 | // Internal methods |
---|
171 | void ReleaseBase(); |
---|
172 | bool CreateTree(bool no_leaf, bool quantized); |
---|
173 | }; |
---|
174 | |
---|
175 | #endif //__OPC_BASEMODEL_H__ |
---|