/src/ogre/OgreMain/include/OgreSceneNode.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #ifndef _SceneNode_H__ |
29 | | #define _SceneNode_H__ |
30 | | |
31 | | #include "OgrePrerequisites.h" |
32 | | #include "OgreCommon.h" |
33 | | |
34 | | #include "OgreNode.h" |
35 | | #include "OgreAxisAlignedBox.h" |
36 | | #include "OgreHeaderPrefix.h" |
37 | | |
38 | | namespace Ogre { |
39 | | |
40 | | // forward decl |
41 | | struct VisibleObjectsBoundsInfo; |
42 | | template <typename T> class ConstVectorIterator; |
43 | | template <typename T> class VectorIterator; |
44 | | |
45 | | /** \addtogroup Core |
46 | | * @{ |
47 | | */ |
48 | | /** \addtogroup Scene |
49 | | * @{ |
50 | | */ |
51 | | /** Class representing a node in the scene graph. |
52 | | |
53 | | A SceneNode is a type of Node which is used to organise objects in a scene. |
54 | | It has the same hierarchical transformation properties of the generic Node class, |
55 | | but also adds the ability to attach world objects to the node, and stores hierarchical |
56 | | bounding volumes of the nodes in the tree. |
57 | | Child nodes are contained within the bounds of the parent, and so on down the |
58 | | tree, allowing for fast culling. |
59 | | */ |
60 | | class _OgreExport SceneNode : public Node |
61 | | { |
62 | | friend class SceneManager; |
63 | | public: |
64 | | typedef std::vector<MovableObject*> ObjectMap; |
65 | | typedef VectorIterator<ObjectMap> ObjectIterator; |
66 | | typedef ConstVectorIterator<ObjectMap> ConstObjectIterator; |
67 | | |
68 | | protected: |
69 | | ObjectMap mObjectsByName; |
70 | | |
71 | | /// SceneManager which created this node |
72 | | SceneManager* mCreator; |
73 | | |
74 | | /// Auto tracking target |
75 | | SceneNode* mAutoTrackTarget; |
76 | | |
77 | | /** Index in the vector holding this node reference. Used for O(1) removals. |
78 | | |
79 | | It is the parent (or our creator) the one that sets this value, not ourselves. Do NOT modify |
80 | | it manually. |
81 | | */ |
82 | | size_t mGlobalIndex; |
83 | | |
84 | | /// World-Axis aligned bounding box, updated only through _update |
85 | | AxisAlignedBox mWorldAABB; |
86 | | |
87 | | void updateFromParentImpl(void) const override; |
88 | | |
89 | | /** See Node */ |
90 | | void setParent(Node* parent) override; |
91 | | |
92 | | /// Tracking offset for fine tuning |
93 | | Vector3 mAutoTrackOffset; |
94 | | /// Local 'normal' direction vector |
95 | | Vector3 mAutoTrackLocalDirection; |
96 | | /// Fixed axis to yaw around |
97 | | Vector3 mYawFixedAxis; |
98 | | |
99 | | /// Whether to yaw around a fixed axis. |
100 | | bool mYawFixed : 1; |
101 | | /// Is this node a current part of the scene graph? |
102 | | bool mIsInSceneGraph : 1; |
103 | | private: // private in 1.13 |
104 | | /// Flag that determines if the bounding box of the node should be displayed |
105 | | bool mShowBoundingBox : 1; |
106 | | |
107 | | /** Internal method for setting whether the node is in the scene |
108 | | graph. |
109 | | */ |
110 | | virtual void setInSceneGraph(bool inGraph); |
111 | | /** See Node. */ |
112 | | Node* createChildImpl(void) override; |
113 | | |
114 | | /** See Node. */ |
115 | | Node* createChildImpl(const String& name) override; |
116 | | public: |
117 | | /** Constructor, only to be called by the creator SceneManager. |
118 | | |
119 | | Creates a node with a specified name. |
120 | | */ |
121 | | SceneNode(SceneManager* creator, const String& name = ""); |
122 | | ~SceneNode(); |
123 | | |
124 | | /** Adds an instance of a scene object to this node. |
125 | | |
126 | | Scene objects can include Entity objects, Camera objects, Light objects, |
127 | | ParticleSystem objects etc. Anything that subclasses from MovableObject. |
128 | | */ |
129 | | virtual void attachObject(MovableObject* obj); |
130 | | |
131 | | /** Reports the number of objects attached to this node. |
132 | | */ |
133 | 0 | size_t numAttachedObjects(void) const { return mObjectsByName.size(); } |
134 | | |
135 | | /** Retrieves a pointer to an attached object by index |
136 | | @note The index of an object may change as other objects are added / removed. |
137 | | */ |
138 | 0 | MovableObject* getAttachedObject(size_t index) const { return mObjectsByName.at(index); } |
139 | | |
140 | | /** Retrieves a pointer to an attached object by name |
141 | | */ |
142 | | MovableObject* getAttachedObject(const String& name) const; |
143 | | |
144 | | /** Detaches the indexed object from this scene node. |
145 | | |
146 | | Detaches by index, see the alternate version to detach by name. Object indexes |
147 | | may change as other objects are added / removed. |
148 | | */ |
149 | | virtual MovableObject* detachObject(unsigned short index); |
150 | | /** Detaches an object by pointer. */ |
151 | | virtual void detachObject(MovableObject* obj); |
152 | | |
153 | | /** Detaches the named object from this node and returns a pointer to it. */ |
154 | | virtual MovableObject* detachObject(const String& name); |
155 | | |
156 | | /** Detaches all objects attached to this node. |
157 | | */ |
158 | | virtual void detachAllObjects(void); |
159 | | |
160 | | /** Detaches and destroys all objects attached to this node. |
161 | | * |
162 | | * Does not destroy objects attached to children of this node |
163 | | */ |
164 | | void destroyAllObjects(void); |
165 | | |
166 | | /** Determines whether this node is in the scene graph, i.e. |
167 | | whether it's ultimate ancestor is the root scene node. |
168 | | */ |
169 | 0 | bool isInSceneGraph(void) const { return mIsInSceneGraph; } |
170 | | |
171 | | /** Notifies this SceneNode that it is the root scene node. |
172 | | |
173 | | Only SceneManager should call this! |
174 | | */ |
175 | 0 | void _notifyRootNode(void) { mIsInSceneGraph = true; } |
176 | | |
177 | | |
178 | | /** Internal method to update the Node. |
179 | | @note |
180 | | Updates this scene node and any relevant children to incorporate transforms etc. |
181 | | Don't call this yourself unless you are writing a SceneManager implementation. |
182 | | @param |
183 | | updateChildren If true, the update cascades down to all children. Specify false if you wish to |
184 | | update children separately, e.g. because of a more selective SceneManager implementation. |
185 | | @param |
186 | | parentHasChanged This flag indicates that the parent transform has changed, |
187 | | so the child should retrieve the parent's transform and combine it with its own |
188 | | even if it hasn't changed itself. |
189 | | */ |
190 | | void _update(bool updateChildren, bool parentHasChanged) override; |
191 | | |
192 | | /** Tells the SceneNode to update the world bound info it stores. |
193 | | */ |
194 | | virtual void _updateBounds(void); |
195 | | |
196 | | /** Internal method which locates any visible objects attached to this node and adds them to the passed in queue. |
197 | | |
198 | | Should only be called by a SceneManager implementation, and only after the _updat method has been called to |
199 | | ensure transforms and world bounds are up to date. |
200 | | SceneManager implementations can choose to let the search cascade automatically, or choose to prevent this |
201 | | and select nodes themselves based on some other criteria. |
202 | | @param |
203 | | cam The active camera |
204 | | @param |
205 | | queue The SceneManager's rendering queue |
206 | | @param |
207 | | visibleBounds bounding information created on the fly containing all visible objects by the camera |
208 | | @param |
209 | | includeChildren If true, the call is cascaded down to all child nodes automatically. |
210 | | @param |
211 | | displayNodes If true, the nodes themselves are rendered as a set of 3 axes as well |
212 | | as the objects being rendered. For debugging purposes. |
213 | | @param onlyShadowCasters |
214 | | */ |
215 | | void _findVisibleObjects(Camera* cam, RenderQueue* queue, |
216 | | VisibleObjectsBoundsInfo* visibleBounds, |
217 | | bool includeChildren = true, bool displayNodes = false, bool onlyShadowCasters = false); |
218 | | |
219 | | /** Gets the axis-aligned bounding box of this node (and hence all subnodes). |
220 | | |
221 | | Recommended only if you are extending a SceneManager, because the bounding box returned |
222 | | from this method is only up to date after the SceneManager has called _update. |
223 | | */ |
224 | 0 | const AxisAlignedBox& _getWorldAABB(void) const { return mWorldAABB; } |
225 | | |
226 | | /// @deprecated use getAttachedObjects() |
227 | | OGRE_DEPRECATED ObjectIterator getAttachedObjectIterator(void); |
228 | | /// @deprecated use getAttachedObjects() |
229 | | OGRE_DEPRECATED ConstObjectIterator getAttachedObjectIterator(void) const; |
230 | | |
231 | | /** The MovableObjects attached to this node |
232 | | * |
233 | | * This is a much faster way to go through <B>all</B> the objects attached to the node than |
234 | | * using getAttachedObject. |
235 | | */ |
236 | 0 | const ObjectMap& getAttachedObjects() const { |
237 | 0 | return mObjectsByName; |
238 | 0 | } |
239 | | |
240 | | /** Gets the creator of this scene node. |
241 | | |
242 | | This method returns the SceneManager which created this node. |
243 | | This can be useful for destroying this node. |
244 | | */ |
245 | 0 | SceneManager* getCreator(void) const { return mCreator; } |
246 | | |
247 | | /** This method removes and destroys the named child and all of its children. |
248 | | |
249 | | Unlike removeChild, which removes a single named child from this |
250 | | node but does not destroy it, this method destroys the child |
251 | | and all of it's children. |
252 | | @par |
253 | | Use this if you wish to recursively destroy a node as well as |
254 | | detaching it from it's parent. Note that any objects attached to |
255 | | the nodes will be detached but will not themselves be destroyed. |
256 | | */ |
257 | | void removeAndDestroyChild(const String& name); |
258 | | |
259 | | /// @overload |
260 | | void removeAndDestroyChild(unsigned short index); |
261 | | |
262 | | /// @overload |
263 | | void removeAndDestroyChild(SceneNode* child); |
264 | | |
265 | | |
266 | | /** Removes and destroys all children of this node. |
267 | | |
268 | | Use this to destroy all child nodes of this node and remove |
269 | | them from the scene graph. Note that all objects attached to this |
270 | | node will be detached but will not be destroyed. |
271 | | */ |
272 | | void removeAndDestroyAllChildren(void); |
273 | | |
274 | | /** Removes and destroys the child and all movable objects attached to the child, |
275 | | * and does the same to any children of that child node. |
276 | | * |
277 | | * Does **not** destroy animation, textures, meshes associated with those movable objects |
278 | | * */ |
279 | | void destroyChildAndObjects(const String& name); |
280 | | ///@overload |
281 | | void destroyChildAndObjects(unsigned short index); |
282 | | ///@overload |
283 | | void destroyChildAndObjects(SceneNode * child); |
284 | | |
285 | | /** Destroys everything attatched to or decended from this node |
286 | | * @par |
287 | | * Detaches and destroys all objects attached to this node or |
288 | | * its children. |
289 | | * Removes and destroys all the children of this node |
290 | | * @par |
291 | | * Use this method to complete destroy a node, for example, |
292 | | * if you want to recreate its render tree from scratch. |
293 | | * @par |
294 | | * Does **not** destroy animations, textures, meshes associated with those movable objects |
295 | | * Does not destroy the node itself |
296 | | * */ |
297 | | void destroyAllChildrenAndObjects(); |
298 | | |
299 | | /** |
300 | | * Load a scene from a file as children of this node |
301 | | * |
302 | | * The file and any referenced resources will be searched in @ref ResourceGroupManager::getWorldResourceGroupName |
303 | | * Depending on the type of SceneManager you can load different scene file-formats. |
304 | | * @param filename source file containing the scene structure |
305 | | */ |
306 | | void loadChildren(const String& filename); |
307 | | |
308 | | /** |
309 | | * Save the scene hierarchy starting at this node to file |
310 | | * |
311 | | * @param filename destination file |
312 | | */ |
313 | | void saveChildren(const String& filename); |
314 | | |
315 | | /** Allows the showing of the node's bounding box. |
316 | | |
317 | | Use this to show or hide the bounding box of the node. |
318 | | */ |
319 | 0 | void showBoundingBox(bool bShow) { mShowBoundingBox = bShow; } |
320 | | |
321 | | /** This allows scene managers to determine if the node's bounding box |
322 | | should be added to the rendering queue. |
323 | | |
324 | | Scene Managers that implement their own _findVisibleObjects will have to |
325 | | check this flag and then use _addBoundingBoxToQueue to add the bounding box |
326 | | wireframe. |
327 | | */ |
328 | 0 | bool getShowBoundingBox() const { return mShowBoundingBox; } |
329 | | |
330 | | /** Creates an unnamed new SceneNode as a child of this node. |
331 | | @param |
332 | | translate Initial translation offset of child relative to parent |
333 | | @param |
334 | | rotate Initial rotation relative to parent |
335 | | */ |
336 | | virtual SceneNode* createChildSceneNode( |
337 | | const Vector3& translate = Vector3::ZERO, |
338 | | const Quaternion& rotate = Quaternion::IDENTITY ); |
339 | | |
340 | | /** Creates a new named SceneNode as a child of this node. |
341 | | |
342 | | This creates a child node with a given name, which allows you to look the node up from |
343 | | the parent which holds this collection of nodes. |
344 | | @param name name of the node |
345 | | @param |
346 | | translate Initial translation offset of child relative to parent |
347 | | @param |
348 | | rotate Initial rotation relative to parent |
349 | | */ |
350 | | virtual SceneNode* createChildSceneNode(const String& name, const Vector3& translate = Vector3::ZERO, const Quaternion& rotate = Quaternion::IDENTITY); |
351 | | |
352 | | /** Allows retrieval of the nearest lights to the centre of this SceneNode. |
353 | | |
354 | | This method allows a list of lights, ordered by proximity to the centre |
355 | | of this SceneNode, to be retrieved. Can be useful when implementing |
356 | | MovableObject::queryLights and Renderable::getLights. |
357 | | @par |
358 | | Note that only lights could be affecting the frustum will take into |
359 | | account, which cached in scene manager. |
360 | | @see SceneManager::_getLightsAffectingFrustum |
361 | | @see SceneManager::_populateLightList |
362 | | @param destList List to be populated with ordered set of lights; will be |
363 | | cleared by this method before population. |
364 | | @param radius Parameter to specify lights intersecting a given radius of |
365 | | this SceneNode's centre. |
366 | | @param lightMask The mask with which to include / exclude lights |
367 | | */ |
368 | | void findLights(LightList& destList, Real radius, uint32 lightMask = 0xFFFFFFFF) const; |
369 | | |
370 | | /** Tells the node whether to yaw around it's own local Y axis or a fixed axis of choice. |
371 | | |
372 | | This method allows you to change the yaw behaviour of the node - by default, it |
373 | | yaws around it's own local Y axis when told to yaw with TS_LOCAL, this makes it |
374 | | yaw around a fixed axis. |
375 | | You only really need this when you're using auto tracking (see setAutoTracking, |
376 | | because when you're manually rotating a node you can specify the TransformSpace |
377 | | in which you wish to work anyway. |
378 | | @param |
379 | | useFixed If true, the axis passed in the second parameter will always be the yaw axis no |
380 | | matter what the node orientation. If false, the node returns to it's default behaviour. |
381 | | @param |
382 | | fixedAxis The axis to use if the first parameter is true. |
383 | | */ |
384 | | void setFixedYawAxis( bool useFixed, const Vector3& fixedAxis = Vector3::UNIT_Y ); |
385 | | |
386 | | /** Rotate the node around the Y-axis. |
387 | | */ |
388 | | void yaw(const Radian& angle, TransformSpace relativeTo = TS_LOCAL) override; |
389 | | /** Sets the node's direction vector ie it's local -z. |
390 | | |
391 | | Note that the 'up' vector for the orientation will automatically be |
392 | | recalculated based on the current 'up' vector (i.e. the roll will |
393 | | remain the same). If you need more control, use setOrientation. |
394 | | @param x,y,z The components of the direction vector |
395 | | @param relativeTo The space in which this direction vector is expressed |
396 | | @param localDirectionVector The vector which normally describes the natural |
397 | | direction of the node, usually -Z |
398 | | */ |
399 | | void setDirection(Real x, Real y, Real z, |
400 | | TransformSpace relativeTo = TS_PARENT, |
401 | | const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); |
402 | | |
403 | | /// @overload |
404 | | void setDirection(const Vector3& vec, TransformSpace relativeTo = TS_PARENT, |
405 | | const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); |
406 | | /** Points the local -Z direction of this node at a point in space. |
407 | | @param targetPoint A vector specifying the look at point. |
408 | | @param relativeTo The space in which the point resides |
409 | | @param localDirectionVector The vector which normally describes the natural |
410 | | direction of the node, usually -Z |
411 | | */ |
412 | | void lookAt( const Vector3& targetPoint, TransformSpace relativeTo, |
413 | | const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); |
414 | | /** Enables / disables automatic tracking of another SceneNode. |
415 | | |
416 | | If you enable auto-tracking, this SceneNode will automatically rotate to |
417 | | point it's -Z at the target SceneNode every frame, no matter how |
418 | | it or the other SceneNode move. Note that by default the -Z points at the |
419 | | origin of the target SceneNode, if you want to tweak this, provide a |
420 | | vector in the 'offset' parameter and the target point will be adjusted. |
421 | | @param enabled If true, tracking will be enabled and the next |
422 | | parameter cannot be null. If false tracking will be disabled and the |
423 | | current orientation will be maintained. |
424 | | @param target Pointer to the SceneNode to track. Make sure you don't |
425 | | delete this SceneNode before turning off tracking (e.g. SceneManager::clearScene will |
426 | | delete it so be careful of this). Can be null if and only if the enabled param is false. |
427 | | @param localDirectionVector The local vector considered to be the usual 'direction' |
428 | | of the node; normally the local -Z but can be another direction. |
429 | | @param offset If supplied, this is the target point in local space of the target node |
430 | | instead of the origin of the target node. Good for fine tuning the look at point. |
431 | | */ |
432 | | void setAutoTracking(bool enabled, SceneNode* const target = 0, |
433 | | const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z, |
434 | | const Vector3& offset = Vector3::ZERO); |
435 | | /** Get the auto tracking target for this node, if any. */ |
436 | 0 | SceneNode* getAutoTrackTarget(void) const { return mAutoTrackTarget; } |
437 | | /** Get the auto tracking offset for this node, if the node is auto tracking. */ |
438 | 0 | const Vector3& getAutoTrackOffset(void) const { return mAutoTrackOffset; } |
439 | | /** Get the auto tracking local direction for this node, if it is auto tracking. */ |
440 | 0 | const Vector3& getAutoTrackLocalDirection(void) const { return mAutoTrackLocalDirection; } |
441 | | /** Internal method used by OGRE to update auto-tracking cameras. */ |
442 | | void _autoTrack(void); |
443 | | /** Gets the parent of this SceneNode. */ |
444 | | SceneNode* getParentSceneNode(void) const; |
445 | | /** Makes all objects attached to this node become visible / invisible. |
446 | | |
447 | | This is a shortcut to calling setVisible() on the objects attached |
448 | | to this node, and optionally to all objects attached to child |
449 | | nodes. |
450 | | @param visible Whether the objects are to be made visible or invisible |
451 | | @param cascade If true, this setting cascades into child nodes too. |
452 | | */ |
453 | | void setVisible(bool visible, bool cascade = true) const; |
454 | | /** Inverts the visibility of all objects attached to this node. |
455 | | |
456 | | This is a shortcut to calling setVisible(!isVisible()) on the objects attached |
457 | | to this node, and optionally to all objects attached to child |
458 | | nodes. |
459 | | @param cascade If true, this setting cascades into child nodes too. |
460 | | */ |
461 | | void flipVisibility(bool cascade = true) const; |
462 | | |
463 | | /** Tells all objects attached to this node whether to display their |
464 | | debug information or not. |
465 | | |
466 | | This is a shortcut to calling setDebugDisplayEnabled() on the objects attached |
467 | | to this node, and optionally to all objects attached to child |
468 | | nodes. |
469 | | @param enabled Whether the objects are to display debug info or not |
470 | | @param cascade If true, this setting cascades into child nodes too. |
471 | | */ |
472 | | void setDebugDisplayEnabled(bool enabled, bool cascade = true) const; |
473 | | }; |
474 | | /** @} */ |
475 | | /** @} */ |
476 | | |
477 | | |
478 | | }// namespace |
479 | | |
480 | | #include "OgreHeaderSuffix.h" |
481 | | |
482 | | #endif |