Coverage Report

Created: 2026-02-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/OgreMain/include/OgreSceneNode.h
Line
Count
Source
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 axes of the node should be displayed
105
        bool mDisplaySceneNode : 1;
106
        /// Flag that determines if the bounding box of the node should be displayed
107
        bool mShowBoundingBox : 1;
108
109
        /** Internal method for setting whether the node is in the scene
110
            graph.
111
        */
112
        virtual void setInSceneGraph(bool inGraph);
113
        /** See Node. */
114
        Node* createChildImpl(void) override;
115
116
        /** See Node. */
117
        Node* createChildImpl(const String& name) override;
118
    public:
119
        /** Constructor, only to be called by the creator SceneManager.
120
121
            Creates a node with a specified name.
122
        */
123
        SceneNode(SceneManager* creator, const String& name = "");
124
        ~SceneNode();
125
126
        /** Adds an instance of a scene object to this node.
127
128
            Scene objects can include Entity objects, Camera objects, Light objects, 
129
            ParticleSystem objects etc. Anything that subclasses from MovableObject.
130
        */
131
        virtual void attachObject(MovableObject* obj);
132
133
        /** Reports the number of objects attached to this node.
134
        */
135
0
        size_t numAttachedObjects(void) const { return mObjectsByName.size(); }
136
137
        /** Retrieves a pointer to an attached object by index
138
        @note The index of an object may change as other objects are added / removed.
139
        */
140
0
        MovableObject* getAttachedObject(size_t index) const { return mObjectsByName.at(index); }
141
142
        /** Retrieves a pointer to an attached object by name
143
        */
144
        MovableObject* getAttachedObject(const String& name) const;
145
146
        /** Detaches the indexed object from this scene node.
147
148
            Detaches by index, see the alternate version to detach by name. Object indexes
149
            may change as other objects are added / removed.
150
        */
151
        virtual MovableObject* detachObject(unsigned short index);
152
        /** Detaches an object by pointer. */
153
        virtual void detachObject(MovableObject* obj);
154
155
        /** Detaches the named object from this node and returns a pointer to it. */
156
        virtual MovableObject* detachObject(const String& name);
157
158
        /** Detaches all objects attached to this node.
159
        */
160
        virtual void detachAllObjects(void);
161
162
        /** Detaches and destroys all objects attached to this node.
163
         *
164
         * Does not destroy objects attached to children of this node
165
        */
166
        void destroyAllObjects(void);
167
168
        /** Determines whether this node is in the scene graph, i.e.
169
            whether it's ultimate ancestor is the root scene node.
170
        */
171
0
        bool isInSceneGraph(void) const { return mIsInSceneGraph; }
172
173
        /** Notifies this SceneNode that it is the root scene node. 
174
175
            Only SceneManager should call this!
176
        */
177
0
        void _notifyRootNode(void) { mIsInSceneGraph = true; }
178
            
179
180
        /** Internal method to update the Node.
181
            @note
182
                Updates this scene node and any relevant children to incorporate transforms etc.
183
                Don't call this yourself unless you are writing a SceneManager implementation.
184
            @param
185
                updateChildren If true, the update cascades down to all children. Specify false if you wish to
186
                update children separately, e.g. because of a more selective SceneManager implementation.
187
            @param
188
                parentHasChanged This flag indicates that the parent transform has changed,
189
                    so the child should retrieve the parent's transform and combine it with its own
190
                    even if it hasn't changed itself.
191
        */
192
        void _update(bool updateChildren, bool parentHasChanged) override;
193
194
        /** Tells the SceneNode to update the world bound info it stores.
195
        */
196
        virtual void _updateBounds(void);
197
198
        /** Internal method which locates any visible objects attached to this node and adds them to the passed in queue.
199
200
            Should only be called by a SceneManager implementation, and only after the _updat method has been called to
201
            ensure transforms and world bounds are up to date.
202
            SceneManager implementations can choose to let the search cascade automatically, or choose to prevent this
203
            and select nodes themselves based on some other criteria.
204
            @param
205
                cam The active camera
206
            @param
207
                queue The SceneManager's rendering queue
208
            @param
209
                visibleBounds bounding information created on the fly containing all visible objects by the camera
210
            @param
211
                includeChildren If true, the call is cascaded down to all child nodes automatically.
212
            @param
213
                displayNodes If true, the nodes themselves are rendered as a set of 3 axes as well
214
                    as the objects being rendered. For debugging purposes.
215
            @param onlyShadowCasters
216
        */
217
        void _findVisibleObjects(Camera* cam, RenderQueue* queue,
218
            VisibleObjectsBoundsInfo* visibleBounds, 
219
            bool includeChildren = true, bool displayNodes = false, bool onlyShadowCasters = false);
220
221
        /** Gets the axis-aligned bounding box of this node (and hence all subnodes).
222
223
            Recommended only if you are extending a SceneManager, because the bounding box returned
224
            from this method is only up to date after the SceneManager has called _update.
225
        */
226
0
        const AxisAlignedBox& _getWorldAABB(void) const { return mWorldAABB; }
227
228
        /// @deprecated use getAttachedObjects()
229
        OGRE_DEPRECATED ObjectIterator getAttachedObjectIterator(void);
230
        /// @deprecated use getAttachedObjects()
231
        OGRE_DEPRECATED ConstObjectIterator getAttachedObjectIterator(void) const;
232
233
        /** The MovableObjects attached to this node
234
         *
235
         * This is a much faster way to go through <B>all</B> the objects attached to the node than
236
         * using getAttachedObject.
237
         */
238
0
        const ObjectMap& getAttachedObjects() const {
239
0
            return mObjectsByName;
240
0
        }
241
242
        /** Gets the creator of this scene node. 
243
244
            This method returns the SceneManager which created this node.
245
            This can be useful for destroying this node.
246
        */
247
0
        SceneManager* getCreator(void) const { return mCreator; }
248
249
        /** This method removes and destroys the named child and all of its children.
250
251
            Unlike removeChild, which removes a single named child from this
252
            node but does not destroy it, this method destroys the child
253
            and all of it's children. 
254
        @par
255
            Use this if you wish to recursively destroy a node as well as
256
            detaching it from it's parent. Note that any objects attached to
257
            the nodes will be detached but will not themselves be destroyed.
258
        */
259
        void removeAndDestroyChild(const String& name);
260
261
        /// @overload
262
        void removeAndDestroyChild(unsigned short index);
263
264
        /// @overload
265
        void removeAndDestroyChild(SceneNode* child);
266
267
268
        /** Removes and destroys all children of this node.
269
270
            Use this to destroy all child nodes of this node and remove
271
            them from the scene graph. Note that all objects attached to this
272
            node will be detached but will not be destroyed.
273
        */
274
        void removeAndDestroyAllChildren(void);
275
276
        /** Removes and destroys the child and all movable objects attached to the child,
277
         * and does the same to any children of that child node.
278
         *
279
         * Does **not** destroy animation, textures, meshes associated with those movable objects
280
         * */
281
        void destroyChildAndObjects(const String& name);
282
        ///@overload
283
        void destroyChildAndObjects(unsigned short index);
284
        ///@overload
285
        void destroyChildAndObjects(SceneNode * child);
286
287
        /** Destroys everything attatched to or decended from this node
288
         * @par
289
         * Detaches and destroys all objects attached to this node or
290
         * its children.
291
         * Removes and destroys all the children of this node
292
         * @par
293
         * Use this method to complete destroy a node, for example,
294
         * if you want to recreate its render tree from scratch.
295
         * @par
296
         * Does **not** destroy animations, textures, meshes associated with those movable objects
297
         * Does not destroy the node itself
298
         * */
299
        void destroyAllChildrenAndObjects();
300
301
        /**
302
         * Load a scene from a file as children of this node
303
         *
304
         * The file and any referenced resources will be searched in @ref ResourceGroupManager::getWorldResourceGroupName
305
         * Depending on the type of SceneManager you can load different scene file-formats.
306
         * @param filename source file containing the scene structure
307
         */
308
        void loadChildren(const String& filename);
309
310
        /**
311
         * Save the scene hierarchy starting at this node to file
312
         *
313
         * @param filename destination file
314
         */
315
        void saveChildren(const String& filename);
316
317
        /** Allows the showing of the node's axes.
318
319
            Use this to show or hide the axes of the node.
320
        */
321
0
        void setDisplaySceneNode(bool bDisplay) { mDisplaySceneNode = bDisplay; }
322
323
        /** This allows debug drawers to determine if the node's axes
324
            should be added to the rendering queue.
325
        */
326
0
        bool getDisplaySceneNode() const { return mDisplaySceneNode; }
327
328
        /** Allows the showing of the node's bounding box.
329
330
            Use this to show or hide the bounding box of the node.
331
        */
332
0
        void showBoundingBox(bool bShow) { mShowBoundingBox = bShow; }
333
334
        /** This allows debug drawers to determine if the node's bounding box
335
            should be added to the rendering queue.
336
        */
337
0
        bool getShowBoundingBox() const { return mShowBoundingBox; }
338
339
        /** Creates an unnamed new SceneNode as a child of this node.
340
        @param
341
            translate Initial translation offset of child relative to parent
342
        @param
343
            rotate Initial rotation relative to parent
344
        */
345
        virtual SceneNode* createChildSceneNode(
346
            const Vector3& translate = Vector3::ZERO, 
347
            const Quaternion& rotate = Quaternion::IDENTITY );
348
349
        /** Creates a new named SceneNode as a child of this node.
350
351
            This creates a child node with a given name, which allows you to look the node up from 
352
            the parent which holds this collection of nodes.
353
            @param name name of the node
354
            @param
355
                translate Initial translation offset of child relative to parent
356
            @param
357
                rotate Initial rotation relative to parent
358
        */
359
        virtual SceneNode* createChildSceneNode(const String& name, const Vector3& translate = Vector3::ZERO, const Quaternion& rotate = Quaternion::IDENTITY);
360
361
        /** Allows retrieval of the nearest lights to the centre of this SceneNode.
362
363
            This method allows a list of lights, ordered by proximity to the centre
364
            of this SceneNode, to be retrieved. Can be useful when implementing
365
            MovableObject::queryLights and Renderable::getLights.
366
        @par
367
            Note that only lights could be affecting the frustum will take into
368
            account, which cached in scene manager.
369
        @see SceneManager::_getLightsAffectingFrustum
370
        @see SceneManager::_populateLightList
371
        @param destList List to be populated with ordered set of lights; will be
372
            cleared by this method before population.
373
        @param radius Parameter to specify lights intersecting a given radius of
374
            this SceneNode's centre.
375
        @param lightMask The mask with which to include / exclude lights
376
        */
377
        void findLights(LightList& destList, Real radius, uint32 lightMask = 0xFFFFFFFF) const;
378
379
        /** Tells the node whether to yaw around it's own local Y axis or a fixed axis of choice.
380
381
        This method allows you to change the yaw behaviour of the node - by default, it
382
        yaws around it's own local Y axis when told to yaw with TS_LOCAL, this makes it
383
        yaw around a fixed axis. 
384
        You only really need this when you're using auto tracking (see setAutoTracking,
385
        because when you're manually rotating a node you can specify the TransformSpace
386
        in which you wish to work anyway.
387
        @param
388
        useFixed If true, the axis passed in the second parameter will always be the yaw axis no
389
        matter what the node orientation. If false, the node returns to it's default behaviour.
390
        @param
391
        fixedAxis The axis to use if the first parameter is true.
392
        */
393
        void setFixedYawAxis( bool useFixed, const Vector3& fixedAxis = Vector3::UNIT_Y );
394
395
        /** Rotate the node around the Y-axis.
396
        */
397
        void yaw(const Radian& angle, TransformSpace relativeTo = TS_LOCAL) override;
398
        /** Sets the node's direction vector ie it's local -z.
399
400
        Note that the 'up' vector for the orientation will automatically be 
401
        recalculated based on the current 'up' vector (i.e. the roll will 
402
        remain the same). If you need more control, use setOrientation.
403
        @param x,y,z The components of the direction vector
404
        @param relativeTo The space in which this direction vector is expressed
405
        @param localDirectionVector The vector which normally describes the natural
406
        direction of the node, usually -Z
407
        */
408
        void setDirection(Real x, Real y, Real z,
409
            TransformSpace relativeTo = TS_PARENT,
410
            const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
411
412
        /// @overload
413
        void setDirection(const Vector3& vec, TransformSpace relativeTo = TS_PARENT,
414
            const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
415
        /** Points the local -Z direction of this node at a point in space.
416
        @param targetPoint A vector specifying the look at point.
417
        @param relativeTo The space in which the point resides
418
        @param localDirectionVector The vector which normally describes the natural
419
        direction of the node, usually -Z
420
        */
421
        void lookAt( const Vector3& targetPoint, TransformSpace relativeTo,
422
            const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
423
        /** Enables / disables automatic tracking of another SceneNode.
424
425
        If you enable auto-tracking, this SceneNode will automatically rotate to
426
        point it's -Z at the target SceneNode every frame, no matter how 
427
        it or the other SceneNode move. Note that by default the -Z points at the 
428
        origin of the target SceneNode, if you want to tweak this, provide a 
429
        vector in the 'offset' parameter and the target point will be adjusted.
430
        @param enabled If true, tracking will be enabled and the next 
431
        parameter cannot be null. If false tracking will be disabled and the 
432
        current orientation will be maintained.
433
        @param target Pointer to the SceneNode to track. Make sure you don't
434
        delete this SceneNode before turning off tracking (e.g. SceneManager::clearScene will
435
        delete it so be careful of this). Can be null if and only if the enabled param is false.
436
        @param localDirectionVector The local vector considered to be the usual 'direction'
437
        of the node; normally the local -Z but can be another direction.
438
        @param offset If supplied, this is the target point in local space of the target node
439
        instead of the origin of the target node. Good for fine tuning the look at point.
440
        */
441
        void setAutoTracking(bool enabled, SceneNode* const target = 0,
442
            const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z,
443
            const Vector3& offset = Vector3::ZERO);
444
        /** Get the auto tracking target for this node, if any. */
445
0
        SceneNode* getAutoTrackTarget(void) const { return mAutoTrackTarget; }
446
        /** Get the auto tracking offset for this node, if the node is auto tracking. */
447
0
        const Vector3& getAutoTrackOffset(void) const { return mAutoTrackOffset; }
448
        /** Get the auto tracking local direction for this node, if it is auto tracking. */
449
0
        const Vector3& getAutoTrackLocalDirection(void) const { return mAutoTrackLocalDirection; }
450
        /** Internal method used by OGRE to update auto-tracking cameras. */
451
        void _autoTrack(void);
452
        /** Gets the parent of this SceneNode. */
453
        SceneNode* getParentSceneNode(void) const;
454
        /** Makes all objects attached to this node become visible / invisible.
455
456
            This is a shortcut to calling setVisible() on the objects attached
457
            to this node, and optionally to all objects attached to child
458
            nodes. 
459
        @param visible Whether the objects are to be made visible or invisible
460
        @param cascade If true, this setting cascades into child nodes too.
461
        */
462
        void setVisible(bool visible, bool cascade = true) const;
463
        /** Inverts the visibility of all objects attached to this node.
464
465
        This is a shortcut to calling setVisible(!isVisible()) on the objects attached
466
        to this node, and optionally to all objects attached to child
467
        nodes. 
468
        @param cascade If true, this setting cascades into child nodes too.
469
        */
470
        void flipVisibility(bool cascade = true) const;
471
472
        /** Tells all objects attached to this node whether to display their
473
            debug information or not.
474
475
            This is a shortcut to calling setDebugDisplayEnabled() on the objects attached
476
            to this node, and optionally to all objects attached to child
477
            nodes. 
478
        @param enabled Whether the objects are to display debug info or not
479
        @param cascade If true, this setting cascades into child nodes too.
480
        */
481
        void setDebugDisplayEnabled(bool enabled, bool cascade = true) const;
482
    };
483
    /** @} */
484
    /** @} */
485
486
487
}// namespace
488
489
#include "OgreHeaderSuffix.h"
490
491
#endif