1 | /** |
---|
2 | * This source file is part of OgreColladaPlugin |
---|
3 | * an addon for OGRE (Object-oriented Graphics Rendering Engine) |
---|
4 | * For the latest info, see http://www.ogre3d.org/ |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify it under |
---|
7 | * the terms of the GNU Lesser General Public License as published by the Free Software |
---|
8 | * Foundation; either version 2 of the License, or (at your option) any later |
---|
9 | * version. |
---|
10 | |
---|
11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
---|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
13 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
14 | |
---|
15 | * You should have received a copy of the GNU Lesser General Public License along with |
---|
16 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
17 | * Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
18 | * http://www.gnu.org/copyleft/lesser.txt. |
---|
19 | * |
---|
20 | * @author Philipp Hartl |
---|
21 | * @see README |
---|
22 | */ |
---|
23 | |
---|
24 | #ifndef __COLLADA_SCENE_H__ |
---|
25 | #define __COLLADA_SCENE_H__ |
---|
26 | |
---|
27 | #include "OgreColladaPrerequisites.h" |
---|
28 | #include "OgreColladaEntity.h" |
---|
29 | #include "OgreMatrix4.h" |
---|
30 | |
---|
31 | namespace Ogre |
---|
32 | { |
---|
33 | // specific child nodes of <scene> or <node> |
---|
34 | class ColladaNodeElement; |
---|
35 | class ColladaBoundingBox; |
---|
36 | class ColladaTransform; |
---|
37 | class ColladaTLookAt; |
---|
38 | class ColladaTMatrix; |
---|
39 | class ColladaTPerspective; |
---|
40 | class ColladaTRotate; |
---|
41 | class ColladaTScale; |
---|
42 | class ColladaTSkew; |
---|
43 | class ColladaTTranslate; |
---|
44 | |
---|
45 | typedef std::vector<ColladaSceneNode *> ColladaSceneNodePtrVector; |
---|
46 | typedef std::vector<ColladaTransform *> ColladaTransformPtrVector; |
---|
47 | |
---|
48 | /** |
---|
49 | * ColladaSceneNode |
---|
50 | * traversing the DAG collada scene graph |
---|
51 | * start with <scene> element node |
---|
52 | * and build up corresponding OGRE scene graph |
---|
53 | */ |
---|
54 | class _OgreColladaExport ColladaSceneNode : public ColladaEntity |
---|
55 | { |
---|
56 | public: |
---|
57 | ColladaSceneNode(ColladaDocument *doc, xmlNode *node); |
---|
58 | virtual ~ColladaSceneNode(void); |
---|
59 | |
---|
60 | /** |
---|
61 | * attach entities of current collada scene node |
---|
62 | * to Ogre scene graph by iterative traversing |
---|
63 | * if ogrenode is NULL, then we start with root scene node |
---|
64 | * |
---|
65 | * @param ogrenode, where we start in Ogre scene graph |
---|
66 | * @return void |
---|
67 | */ |
---|
68 | void createOgreInstance(SceneNode *ogrenode) const; |
---|
69 | |
---|
70 | /** |
---|
71 | * import <scene> element |
---|
72 | * |
---|
73 | * @see base class ColladaEntity |
---|
74 | */ |
---|
75 | virtual bool doImport(void); |
---|
76 | |
---|
77 | ColladaBoundingBox *getBoundingBox(void) const { return mBB; } |
---|
78 | virtual EntityTypes getEntityType(void) const { return ColladaEntity::NODE; } // the <scene> (root) node will be handled like a <node> |
---|
79 | |
---|
80 | /** |
---|
81 | * find a scene node in the DAG graph by its unique id |
---|
82 | * start traversing always at first child |
---|
83 | * |
---|
84 | * @param the unique id of the node we are looking for |
---|
85 | * @return a pointer on the founded node, NULL otherwise |
---|
86 | */ |
---|
87 | ColladaSceneNode *getNode(const String &id); |
---|
88 | |
---|
89 | // an ogre instance of an scene node does not make sense |
---|
90 | virtual MovableObject *getOgreInstance(void) const { return NULL; } |
---|
91 | |
---|
92 | private: |
---|
93 | enum NodeType { JOINT, NODE }; // by default the <node> type is NODE |
---|
94 | NodeType mType; |
---|
95 | |
---|
96 | ColladaSceneNode *mParent; // for root it's empty ;) |
---|
97 | ColladaBoundingBox *mBB; // [0,1] |
---|
98 | ColladaEntity *mEntity; // a pointer at loaded instance |
---|
99 | ColladaTransformPtrVector mTransforms; // [*] |
---|
100 | ColladaSceneNodePtrVector mChilds; // a vector of <node>s [*] |
---|
101 | |
---|
102 | /** |
---|
103 | * create an ogre instance of mEntity and attach it |
---|
104 | * |
---|
105 | * @param ogrenode, where the instance will be attached to |
---|
106 | * @return void |
---|
107 | */ |
---|
108 | void attachEntityToOgreSceneNode(SceneNode *ogrenode) const; |
---|
109 | |
---|
110 | /** |
---|
111 | * set all transformations of a collada node to ogre scene node |
---|
112 | * traversing iterative corresponds to appearance of collada |
---|
113 | * |
---|
114 | * @param ogrenode, where the transforms should affect |
---|
115 | * @return void |
---|
116 | */ |
---|
117 | void attachTransformsToOgreSceneNode(SceneNode *ogrenode) const; |
---|
118 | |
---|
119 | /** |
---|
120 | * import <extra> information of current node |
---|
121 | * currently not implemented |
---|
122 | * |
---|
123 | * @param the current node |
---|
124 | * @return void |
---|
125 | */ |
---|
126 | void importExtra(xmlNode *node); |
---|
127 | |
---|
128 | /** |
---|
129 | * try to find the instance (entity) of current node |
---|
130 | * import the instance and save the pointer of the entity |
---|
131 | * |
---|
132 | * @param the current node |
---|
133 | * @return void |
---|
134 | */ |
---|
135 | void importInstance(xmlNode *node); |
---|
136 | }; |
---|
137 | |
---|
138 | /** |
---|
139 | * abstract class to import <scene> and <node> specific childs |
---|
140 | * @see inheritence for more information |
---|
141 | */ |
---|
142 | class ColladaNodeElement |
---|
143 | { |
---|
144 | public: |
---|
145 | ColladaNodeElement(ColladaSceneNode *parent) { mParent = parent; } |
---|
146 | virtual ~ColladaNodeElement(void) { mParent = NULL; } |
---|
147 | |
---|
148 | /** |
---|
149 | * abstract method to import node information |
---|
150 | * |
---|
151 | * @param node the node to import |
---|
152 | * @return void |
---|
153 | */ |
---|
154 | virtual void doImport(xmlNode *node) = 0; |
---|
155 | protected: |
---|
156 | ColladaSceneNode *mParent; // pointer back to parent node (should be a <scene> or <node>) |
---|
157 | }; |
---|
158 | |
---|
159 | /** |
---|
160 | * import a <boundingbox> node |
---|
161 | * fill up minimum and maximum vectors |
---|
162 | */ |
---|
163 | class ColladaBoundingBox : public ColladaNodeElement |
---|
164 | { |
---|
165 | public: |
---|
166 | ColladaBoundingBox(ColladaSceneNode *p); |
---|
167 | |
---|
168 | /** |
---|
169 | * special constructor |
---|
170 | * should only be used by ColladaGeometry |
---|
171 | * do not call doImport |
---|
172 | * |
---|
173 | * @param min, max bounds |
---|
174 | */ |
---|
175 | ColladaBoundingBox(const Vector3 &min, const Vector3 &max); |
---|
176 | virtual ~ColladaBoundingBox(void); |
---|
177 | |
---|
178 | virtual void doImport(xmlNode *node); |
---|
179 | |
---|
180 | AxisAlignedBox getAABB(void) const; |
---|
181 | |
---|
182 | /** |
---|
183 | * calculate the distance between center |
---|
184 | * of the box and a corner |
---|
185 | * in world units |
---|
186 | * |
---|
187 | * @param none |
---|
188 | * @return float, the outer radius of the AABB |
---|
189 | */ |
---|
190 | float getRadius(void) const; |
---|
191 | |
---|
192 | /** |
---|
193 | * return min,max vectors as string |
---|
194 | * |
---|
195 | * @param none |
---|
196 | * @return string |
---|
197 | */ |
---|
198 | String toString(void) const; |
---|
199 | |
---|
200 | private: |
---|
201 | Vector3 mMin, mMax; |
---|
202 | }; |
---|
203 | |
---|
204 | /** |
---|
205 | * abstract transform class |
---|
206 | * @see enum Type (e.g. translate, rotate, scale, ...) |
---|
207 | */ |
---|
208 | class ColladaTransform : public ColladaNodeElement |
---|
209 | { |
---|
210 | public: |
---|
211 | enum Type { LOOKAT, MATRIX, PERSPECTIVE, ROTATE, SCALE, SKEW, TRANSLATE }; |
---|
212 | |
---|
213 | ColladaTransform(ColladaSceneNode *p) : ColladaNodeElement(p) { } |
---|
214 | virtual ~ColladaTransform(void) { } |
---|
215 | |
---|
216 | // @see ColladaNodeElement |
---|
217 | virtual void doImport(xmlNode *node) = 0; |
---|
218 | |
---|
219 | /** |
---|
220 | * which type of transform is this element |
---|
221 | * |
---|
222 | * @param none |
---|
223 | * @return enum type |
---|
224 | */ |
---|
225 | virtual Type getType(void) const = 0; |
---|
226 | |
---|
227 | /** |
---|
228 | * return a transformation matrix |
---|
229 | * |
---|
230 | * @param none |
---|
231 | * @return a new Ogre Matrix |
---|
232 | */ |
---|
233 | virtual Ogre::Matrix4 getMatrix4(void) const = 0; |
---|
234 | }; |
---|
235 | |
---|
236 | // <lookat> |
---|
237 | class ColladaTLookAt : public ColladaTransform |
---|
238 | { |
---|
239 | public: |
---|
240 | ColladaTLookAt(ColladaSceneNode *p); |
---|
241 | virtual ~ColladaTLookAt(void); |
---|
242 | |
---|
243 | virtual void doImport(xmlNode *node); |
---|
244 | virtual Type getType(void) const { return LOOKAT; } |
---|
245 | virtual Matrix3 getMatrix3(void) const; |
---|
246 | virtual Matrix4 getMatrix4(void) const; |
---|
247 | |
---|
248 | Vector3 getEye(void) const; |
---|
249 | Vector3 getTarget(void) const; |
---|
250 | Vector3 getUp(void) const; |
---|
251 | private: |
---|
252 | Matrix3 mMatrix; |
---|
253 | }; |
---|
254 | |
---|
255 | // <matrix> |
---|
256 | class ColladaTMatrix : public ColladaTransform |
---|
257 | { |
---|
258 | public: |
---|
259 | ColladaTMatrix(ColladaSceneNode *p); |
---|
260 | virtual ~ColladaTMatrix(void); |
---|
261 | |
---|
262 | virtual void doImport(xmlNode *node); |
---|
263 | virtual Type getType(void) const { return MATRIX; } |
---|
264 | virtual Matrix4 getMatrix4(void) const; |
---|
265 | private: |
---|
266 | Matrix4 mMatrix; |
---|
267 | }; |
---|
268 | |
---|
269 | // <perspective> |
---|
270 | class ColladaTPerspective : public ColladaTransform |
---|
271 | { |
---|
272 | public: |
---|
273 | ColladaTPerspective(ColladaSceneNode *p); |
---|
274 | virtual ~ColladaTPerspective(void); |
---|
275 | |
---|
276 | virtual void doImport(xmlNode *node); |
---|
277 | virtual Type getType(void) const { return PERSPECTIVE; } |
---|
278 | virtual Matrix4 getMatrix4(void) const; // the identity matrix |
---|
279 | |
---|
280 | float getFieldOfView(void) const { return mFOV; } |
---|
281 | private: |
---|
282 | float mFOV; // horizontal field of view [°] |
---|
283 | }; |
---|
284 | |
---|
285 | // <rotate> |
---|
286 | class ColladaTRotate : public ColladaTransform |
---|
287 | { |
---|
288 | public: |
---|
289 | ColladaTRotate(ColladaSceneNode *p); |
---|
290 | virtual ~ColladaTRotate(void); |
---|
291 | |
---|
292 | virtual void doImport(xmlNode *node); |
---|
293 | virtual Type getType(void) const { return ROTATE; } |
---|
294 | virtual Matrix3 getMatrix3(void) const; |
---|
295 | virtual Matrix4 getMatrix4(void) const; |
---|
296 | |
---|
297 | Vector3 getAxis(void) const { return mAxis; } |
---|
298 | float getAngle(void) const { return mAngle; } |
---|
299 | private: |
---|
300 | Vector3 mAxis; |
---|
301 | float mAngle; // [°] |
---|
302 | }; |
---|
303 | |
---|
304 | // <scale> |
---|
305 | class ColladaTScale : public ColladaTransform |
---|
306 | { |
---|
307 | public: |
---|
308 | ColladaTScale(ColladaSceneNode *p); |
---|
309 | virtual ~ColladaTScale(void); |
---|
310 | |
---|
311 | virtual void doImport(xmlNode *node); |
---|
312 | virtual Type getType(void) const { return SCALE; } |
---|
313 | virtual Matrix3 getMatrix3(void) const; |
---|
314 | virtual Matrix4 getMatrix4(void) const; |
---|
315 | |
---|
316 | Vector3 getScale(void) const { return mScale; } |
---|
317 | private: |
---|
318 | Vector3 mScale; |
---|
319 | }; |
---|
320 | |
---|
321 | // <skew> |
---|
322 | class ColladaTSkew : public ColladaTransform |
---|
323 | { |
---|
324 | public: |
---|
325 | ColladaTSkew(ColladaSceneNode *p); |
---|
326 | virtual ~ColladaTSkew(void); |
---|
327 | |
---|
328 | virtual void doImport(xmlNode *node); |
---|
329 | virtual Type getType(void) const { return SKEW; } |
---|
330 | virtual Matrix3 getMatrix3(void) const; |
---|
331 | virtual Matrix4 getMatrix4(void) const; |
---|
332 | |
---|
333 | Vector3 getRotation(void) const { return mRotation; } |
---|
334 | Vector3 getTranslation(void) const { return mTranslation; } |
---|
335 | float getAngle(void) const { return mAngle; } |
---|
336 | private: |
---|
337 | Vector3 mRotation, mTranslation; |
---|
338 | float mAngle; // [°] |
---|
339 | }; |
---|
340 | |
---|
341 | // <translate> |
---|
342 | class ColladaTTranslate : public ColladaTransform |
---|
343 | { |
---|
344 | public: |
---|
345 | ColladaTTranslate(ColladaSceneNode *p); |
---|
346 | virtual ~ColladaTTranslate(void); |
---|
347 | |
---|
348 | virtual void doImport(xmlNode *node); |
---|
349 | virtual Type getType(void) const { return TRANSLATE; } |
---|
350 | virtual Matrix4 getMatrix4(void) const; |
---|
351 | |
---|
352 | Vector3 getTranslate(void) const { return mTranslate; } |
---|
353 | private: |
---|
354 | Vector3 mTranslate; |
---|
355 | }; |
---|
356 | } |
---|
357 | |
---|
358 | #endif // __COLLADA_SCENE_H__ |
---|