Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgreSceneNode.h @ 1

Last change on this file since 1 was 1, checked in by landauf, 17 years ago
File size: 21.1 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#ifndef _SceneNode_H__
30#define _SceneNode_H__
31
32#include "OgrePrerequisites.h"
33
34#include "OgreNode.h"
35#include "OgreIteratorWrappers.h"
36#include "OgreAxisAlignedBox.h"
37
38namespace Ogre {
39
40        // forward decl
41        struct VisibleObjectsBoundsInfo;
42
43    /** Class representing a node in the scene graph.
44        @remarks
45            A SceneNode is a type of Node which is used to organise objects in a scene.
46            It has the same hierarchical transformation properties of the generic Node class,
47            but also adds the ability to attach world objects to the node, and stores hierarchical
48            bounding volumes of the nodes in the tree.
49            Child nodes are contained within the bounds of the parent, and so on down the
50            tree, allowing for fast culling.
51    */
52    class _OgreExport SceneNode : public Node
53    {
54    public:
55        typedef HashMap<String, MovableObject*> ObjectMap;
56        typedef MapIterator<ObjectMap> ObjectIterator;
57                typedef ConstMapIterator<ObjectMap> ConstObjectIterator;
58
59    protected:
60        ObjectMap mObjectsByName;
61
62                /// Pointer to a Wire Bounding Box for this Node
63                WireBoundingBox *mWireBoundingBox;
64                /// Flag that determines if the bounding box of the node should be displayed
65                bool mShowBoundingBox;
66
67        /// SceneManager which created this node
68        SceneManager* mCreator;
69
70        /// World-Axis aligned bounding box, updated only through _update
71        AxisAlignedBox mWorldAABB;
72
73        /** @copydoc Node::updateFromParentImpl. */
74        void updateFromParentImpl(void) const;
75
76        /** See Node. */
77        Node* createChildImpl(void);
78
79        /** See Node. */
80        Node* createChildImpl(const String& name);
81
82                /** See Node */
83                void setParent(Node* parent);
84
85                /** Internal method for setting whether the node is in the scene
86                        graph.
87                */
88                virtual void setInSceneGraph(bool inGraph);
89
90        /// Whether to yaw around a fixed axis.
91        bool mYawFixed;
92        /// Fixed axis to yaw around
93        Vector3 mYawFixedAxis;
94
95        /// Auto tracking target
96        SceneNode* mAutoTrackTarget;
97        /// Tracking offset for fine tuning
98        Vector3 mAutoTrackOffset;
99        /// Local 'normal' direction vector
100        Vector3 mAutoTrackLocalDirection;
101                /// Is this node a current part of the scene graph?
102                bool mIsInSceneGraph;
103    public:
104        /** Constructor, only to be called by the creator SceneManager.
105        @remarks
106            Creates a node with a generated name.
107        */
108        SceneNode(SceneManager* creator);
109        /** Constructor, only to be called by the creator SceneManager.
110        @remarks
111            Creates a node with a specified name.
112        */
113        SceneNode(SceneManager* creator, const String& name);
114        ~SceneNode();
115
116        /** Adds an instance of a scene object to this node.
117        @remarks
118            Scene objects can include Entity objects, Camera objects, Light objects,
119            ParticleSystem objects etc. Anything that subclasses from MovableObject.
120        */
121        virtual void attachObject(MovableObject* obj);
122
123        /** Reports the number of objects attached to this node.
124        */
125        virtual unsigned short numAttachedObjects(void) const;
126
127        /** Retrieves a pointer to an attached object.
128        @remarks Retrieves by index, see alternate version to retrieve by name. The index
129        of an object may change as other objects are added / removed.
130        */
131        virtual MovableObject* getAttachedObject(unsigned short index);
132
133        /** Retrieves a pointer to an attached object.
134        @remarks Retrieves by object name, see alternate version to retrieve by index.
135        */
136        virtual MovableObject* getAttachedObject(const String& name);
137
138        /** Detaches the indexed object from this scene node.
139        @remarks
140            Detaches by index, see the alternate version to detach by name. Object indexes
141            may change as other objects are added / removed.
142        */
143        virtual MovableObject* detachObject(unsigned short index);
144        /** Detaches an object by pointer. */
145        virtual void detachObject(MovableObject* obj);
146
147        /** Detaches the named object from this node and returns a pointer to it. */
148        virtual MovableObject* detachObject(const String& name);
149
150        /** Detaches all objects attached to this node.
151        */
152        virtual void detachAllObjects(void);
153
154                /** Determines whether this node is in the scene graph, ie
155                        whether it's ulitimate ancestor is the root scene node.
156                */
157                virtual bool isInSceneGraph(void) const { return mIsInSceneGraph; }
158
159                /** Notifies this SceneNode that it is the root scene node.
160                @remarks
161                        Only SceneManager should call this!
162                */
163                virtual void _notifyRootNode(void) { mIsInSceneGraph = true; }
164                       
165
166        /** Internal method to update the Node.
167            @note
168                Updates this scene node and any relevant children to incorporate transforms etc.
169                Don't call this yourself unless you are writing a SceneManager implementation.
170            @param
171                updateChildren If true, the update cascades down to all children. Specify false if you wish to
172                update children separately, e.g. because of a more selective SceneManager implementation.
173            @param
174                parentHasChanged This flag indicates that the parent xform has changed,
175                    so the child should retrieve the parent's xform and combine it with its own
176                    even if it hasn't changed itself.
177        */
178        virtual void _update(bool updateChildren, bool parentHasChanged);
179
180                /** Tells the SceneNode to update the world bound info it stores.
181                */
182                virtual void _updateBounds(void);
183
184        /** Internal method which locates any visible objects attached to this node and adds them to the passed in queue.
185            @remarks
186                Should only be called by a SceneManager implementation, and only after the _updat method has been called to
187                ensure transforms and world bounds are up to date.
188                SceneManager implementations can choose to let the search cascade automatically, or choose to prevent this
189                and select nodes themselves based on some other criteria.
190            @param
191                cam The active camera
192            @param
193                queue The SceneManager's rendering queue
194                        @param
195                                visibleBounds bounding information created on the fly containing all visible objects by the camera
196            @param
197                includeChildren If true, the call is cascaded down to all child nodes automatically.
198            @param
199                displayNodes If true, the nodes themselves are rendered as a set of 3 axes as well
200                    as the objects being rendered. For debugging purposes.
201        */
202                virtual void _findVisibleObjects(Camera* cam, RenderQueue* queue, 
203                        VisibleObjectsBoundsInfo* visibleBounds, 
204            bool includeChildren = true, bool displayNodes = false, bool onlyShadowCasters = false);
205
206        /** Gets the axis-aligned bounding box of this node (and hence all subnodes).
207        @remarks
208            Recommended only if you are extending a SceneManager, because the bounding box returned
209            from this method is only up to date after the SceneManager has called _update.
210        */
211        virtual const AxisAlignedBox& _getWorldAABB(void) const;
212
213        /** Retrieves an iterator which can be used to efficiently step through the objects
214            attached to this node.
215        @remarks
216            This is a much faster way to go through <B>all</B> the objects attached to the node
217            than using getAttachedObject. But the iterator returned is only valid until a change
218            is made to the collection (ie an addition or removal) so treat the returned iterator
219            as transient, and don't add / remove items as you go through the iterator, save changes
220            until the end, or retrieve a new iterator after making the change. Making changes to
221            the object returned through the iterator is OK though.
222        */
223        virtual ObjectIterator getAttachedObjectIterator(void);
224        /** Retrieves an iterator which can be used to efficiently step through the objects
225            attached to this node.
226        @remarks
227            This is a much faster way to go through <B>all</B> the objects attached to the node
228            than using getAttachedObject. But the iterator returned is only valid until a change
229            is made to the collection (ie an addition or removal) so treat the returned iterator
230            as transient, and don't add / remove items as you go through the iterator, save changes
231            until the end, or retrieve a new iterator after making the change. Making changes to
232            the object returned through the iterator is OK though.
233        */
234                virtual ConstObjectIterator getAttachedObjectIterator(void) const;
235
236        /** Gets the creator of this scene node.
237        @remarks
238            This method returns the SceneManager which created this node.
239            This can be useful for destroying this node.
240        */
241        SceneManager* getCreator(void) const { return mCreator; }
242
243        /** This method removes and destroys the named child and all of its children.
244        @remarks
245            Unlike removeChild, which removes a single named child from this
246            node but does not destroy it, this method destroys the child
247            and all of it's children.
248        @par
249            Use this if you wish to recursively destroy a node as well as
250            detaching it from it's parent. Note that any objects attached to
251            the nodes will be detached but will not themselves be destroyed.
252        */
253        virtual void removeAndDestroyChild(const String& name);
254
255        /** This method removes and destroys the child and all of its children.
256        @remarks
257            Unlike removeChild, which removes a single named child from this
258            node but does not destroy it, this method destroys the child
259            and all of it's children.
260        @par
261            Use this if you wish to recursively destroy a node as well as
262            detaching it from it's parent. Note that any objects attached to
263            the nodes will be detached but will not themselves be destroyed.
264        */
265        virtual void removeAndDestroyChild(unsigned short index);
266
267        /** Removes and destroys all children of this node.
268        @remarks
269            Use this to destroy all child nodes of this node and remove
270            them from the scene graph. Note that all objects attached to this
271            node will be detached but will not be destroyed.
272        */
273        virtual void removeAndDestroyAllChildren(void);
274
275        /** Allows the showing of the node's bounding box.
276        @remarks
277            Use this to show or hide the bounding box of the node.
278        */
279                virtual void showBoundingBox(bool bShow);
280
281        /** Add the bounding box to the rendering queue.
282        */
283                virtual void _addBoundingBoxToQueue(RenderQueue* queue);
284
285        /** This allows scene managers to determine if the node's bounding box
286                        should be added to the rendering queue.
287        @remarks
288            Scene Managers that implement their own _findVisibleObjects will have to
289                        check this flag and then use _addBoundingBoxToQueue to add the bounding box
290                        wireframe.
291        */
292                virtual bool getShowBoundingBox() const;
293
294        /** Creates an unnamed new SceneNode as a child of this node.
295        @param
296            translate Initial translation offset of child relative to parent
297        @param
298            rotate Initial rotation relative to parent
299        */
300        virtual SceneNode* createChildSceneNode(
301            const Vector3& translate = Vector3::ZERO, 
302            const Quaternion& rotate = Quaternion::IDENTITY );
303
304        /** Creates a new named SceneNode as a child of this node.
305        @remarks
306            This creates a child node with a given name, which allows you to look the node up from
307            the parent which holds this collection of nodes.
308            @param
309                translate Initial translation offset of child relative to parent
310            @param
311                rotate Initial rotation relative to parent
312        */
313        virtual SceneNode* createChildSceneNode(const String& name, const Vector3& translate = Vector3::ZERO, const Quaternion& rotate = Quaternion::IDENTITY);
314
315        /** Allows retrieval of the nearest lights to the centre of this SceneNode.
316        @remarks
317            This method allows a list of lights, ordered by proximity to the centre
318            of this SceneNode, to be retrieved. Can be useful when implementing
319            MovableObject::queryLights and Renderable::getLights.
320        @par
321            Note that only lights could be affecting the frustum will take into
322            account, which cached in scene manager.
323        @see SceneManager::_getLightsAffectingFrustum
324        @see SceneManager::_populateLightList
325        @param destList List to be populated with ordered set of lights; will be
326            cleared by this method before population.
327        @param radius Parameter to specify lights intersecting a given radius of
328            this SceneNode's centre.
329        */
330        virtual void findLights(LightList& destList, Real radius) const;
331
332        /** Tells the node whether to yaw around it's own local Y axis or a fixed axis of choice.
333        @remarks
334        This method allows you to change the yaw behaviour of the node - by default, it
335        yaws around it's own local Y axis when told to yaw with TS_LOCAL, this makes it
336        yaw around a fixed axis.
337        You only really need this when you're using auto tracking (see setAutoTracking,
338        because when you're manually rotating a node you can specify the TransformSpace
339        in which you wish to work anyway.
340        @param
341        useFixed If true, the axis passed in the second parameter will always be the yaw axis no
342        matter what the node orientation. If false, the node returns to it's default behaviour.
343        @param
344        fixedAxis The axis to use if the first parameter is true.
345        */
346        virtual void setFixedYawAxis( bool useFixed, const Vector3& fixedAxis = Vector3::UNIT_Y );
347
348                /** Rotate the node around the Y-axis.
349                */
350                virtual void yaw(const Radian& angle, TransformSpace relativeTo = TS_LOCAL);
351#ifndef OGRE_FORCE_ANGLE_TYPES
352                inline void yaw(Real degrees, TransformSpace relativeTo = TS_LOCAL) {
353                        yaw ( Angle(degrees), relativeTo );
354                }
355#endif//OGRE_FORCE_ANGLE_TYPES
356        /** Sets the node's direction vector ie it's local -z.
357        @remarks
358        Note that the 'up' vector for the orientation will automatically be
359        recalculated based on the current 'up' vector (i.e. the roll will
360        remain the same). If you need more control, use setOrientation.
361        @param x,y,z The components of the direction vector
362        @param relativeTo The space in which this direction vector is expressed
363        @param localDirectionVector The vector which normally describes the natural
364        direction of the node, usually -Z
365        */
366        virtual void setDirection(Real x, Real y, Real z, 
367            TransformSpace relativeTo = TS_LOCAL, 
368            const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
369
370        /** Sets the node's direction vector ie it's local -z.
371        @remarks
372        Note that the 'up' vector for the orientation will automatically be
373        recalculated based on the current 'up' vector (i.e. the roll will
374        remain the same). If you need more control, use setOrientation.
375        @param vec The direction vector
376        @param relativeTo The space in which this direction vector is expressed
377        @param localDirectionVector The vector which normally describes the natural
378        direction of the node, usually -Z
379        */
380        virtual void setDirection(const Vector3& vec, TransformSpace relativeTo = TS_LOCAL, 
381            const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
382        /** Points the local -Z direction of this node at a point in space.
383        @param targetPoint A vector specifying the look at point.
384        @param relativeTo The space in which the point resides
385        @param localDirectionVector The vector which normally describes the natural
386        direction of the node, usually -Z
387        */
388        virtual void lookAt( const Vector3& targetPoint, TransformSpace relativeTo,
389            const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
390        /** Enables / disables automatic tracking of another SceneNode.
391        @remarks
392        If you enable auto-tracking, this SceneNode will automatically rotate to
393        point it's -Z at the target SceneNode every frame, no matter how
394        it or the other SceneNode move. Note that by default the -Z points at the
395        origin of the target SceneNode, if you want to tweak this, provide a
396        vector in the 'offset' parameter and the target point will be adjusted.
397        @param enabled If true, tracking will be enabled and the next
398        parameter cannot be null. If false tracking will be disabled and the
399        current orientation will be maintained.
400        @param target Pointer to the SceneNode to track. Make sure you don't
401        delete this SceneNode before turning off tracking (e.g. SceneManager::clearScene will
402        delete it so be careful of this). Can be null if and only if the enabled param is false.
403        @param localDirectionVector The local vector considered to be the usual 'direction'
404        of the node; normally the local -Z but can be another direction.
405        @param offset If supplied, this is the target point in local space of the target node
406        instead of the origin of the target node. Good for fine tuning the look at point.
407        */
408        virtual void setAutoTracking(bool enabled, SceneNode* target = 0, 
409            const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z,
410            const Vector3& offset = Vector3::ZERO);
411                /** Get the auto tracking target for this node, if any. */
412        virtual SceneNode* getAutoTrackTarget(void) { return mAutoTrackTarget; }
413                /** Get the auto tracking offset for this node, if the node is auto tracking. */
414                virtual const Vector3& getAutoTrackOffset(void) { return mAutoTrackOffset; }
415                /** Get the auto tracking local direction for this node, if it is auto tracking. */
416                virtual const Vector3& getAutoTrackLocalDirection(void) { return mAutoTrackLocalDirection; }
417                /** Internal method used by OGRE to update auto-tracking cameras. */
418        void _autoTrack(void);
419        /** Gets the parent of this SceneNode. */
420        SceneNode* getParentSceneNode(void) const;
421        /** Makes all objects attached to this node become visible / invisble.
422        @remarks   
423            This is a shortcut to calling setVisible() on the objects attached
424            to this node, and optionally to all objects attached to child
425            nodes.
426        @param visible Whether the objects are to be made visible or invisible
427        @param cascade If true, this setting cascades into child nodes too.
428        */
429        virtual void setVisible(bool visible, bool cascade = true);
430        /** Inverts the visibility of all objects attached to this node.
431        @remarks   
432        This is a shortcut to calling setVisible(!isVisible()) on the objects attached
433        to this node, and optionally to all objects attached to child
434        nodes.
435        @param cascade If true, this setting cascades into child nodes too.
436        */
437        virtual void flipVisibility(bool cascade = true);
438
439
440
441
442
443    };
444
445
446}// namespace
447
448#endif
Note: See TracBrowser for help on using the repository browser.