source: OGRE/trunk/ogrenew/OgreMain/include/OgreMovableObject.h @ 692

Revision 692, 19.1 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

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-2005 The OGRE Team
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-----------------------------------------------------------------------------
24*/
25
26#ifndef __MovableObject_H__
27#define __MovableObject_H__
28
29// Precompiler options
30#include "OgrePrerequisites.h"
31#include "OgreRenderQueue.h"
32#include "OgreAxisAlignedBox.h"
33#include "OgreSphere.h"
34#include "OgreShadowCaster.h"
35#include "OgreFactoryObj.h"
36#include "OgreAnimable.h"
37#include "OgreAny.h"
38#include "OgreUserDefinedObject.h"
39
40namespace Ogre {
41
42        // Forward declaration
43        class MovableObjectFactory;
44
45    /** Abstract class definining a movable object in a scene.
46        @remarks
47            Instances of this class are discrete, relatively small, movable objects
48            which are attached to SceneNode objects to define their position.
49    */
50    class _OgreExport MovableObject : public ShadowCaster, public AnimableObject
51    {
52    protected:
53                /// Name of this object
54                String mName;
55                /// Creator of this object (if created by a factory)
56                MovableObjectFactory* mCreator;
57                /// SceneManager holding this object (if applicable)
58                SceneManager* mManager;
59        /// node to which this object is attached
60        Node* mParentNode;
61        bool mParentIsTagPoint;
62        /// Is this object visible?
63        bool mVisible;
64                /// Upper distance to still render
65                Real mUpperDistance;
66                Real mSquaredUpperDistance;
67                /// Hidden because of distance?
68                bool mBeyondFarDistance;
69                /// User defined link to another object / value / whatever
70                Any mUserAny;
71        /// The render queue to use when rendering this object
72        uint8 mRenderQueueID;
73                /// Flags whether the RenderQueue's default should be used.
74                bool mRenderQueueIDSet;
75        /// Flags determining whether this object is included / excluded from scene queries
76        uint32 mQueryFlags;
77        /// Flags determining whether this object is visible (compared to SceneManager mask)
78        uint32 mVisibilityFlags;
79        /// Cached world AABB of this object
80        mutable AxisAlignedBox mWorldAABB;
81                // Cached world bounding sphere
82                mutable Sphere mWorldBoundingSphere;
83        /// World space AABB of this object's dark cap
84        mutable AxisAlignedBox mWorldDarkCapBounds;
85        /// Does this object cast shadows?
86        bool mCastShadows;
87
88                // Static members
89                /// Default query flags
90                static uint32 msDefaultQueryFlags;
91                /// Default visibility flags
92                static uint32 msDefaultVisibilityFlags;
93
94
95
96    public:
97        /// Constructor
98        MovableObject();
99
100                /// Named constructor
101                MovableObject(const String& name);
102        /** Virtual destructor - read Scott Meyers if you don't know why this is needed.
103        */
104        virtual ~MovableObject();
105
106                /** Notify the object of it's creator (internal use only) */
107                virtual void _notifyCreator(MovableObjectFactory* fact) { mCreator = fact; }
108                /** Notify the object of it's manager (internal use only) */
109                virtual void _notifyManager(SceneManager* man) { mManager = man; }
110                /** Get the manager of this object, if any (internal use only) */
111                virtual SceneManager* _getManager(void) const { return mManager; }
112
113        /** Returns the name of this object. */
114                virtual const String& getName(void) const { return mName; }
115
116        /** Returns the type name of this object. */
117        virtual const String& getMovableType(void) const = 0;
118
119        /** Returns the node to which this object is attached.
120        @remarks
121            A MovableObject may be attached to either a SceneNode or to a TagPoint,
122            the latter case if it's attached to a bone on an animated entity.
123            Both are Node subclasses so this method will return either.
124        */
125        virtual Node* getParentNode(void) const;
126
127        /** Returns the scene node to which this object is attached.
128        @remarks
129            A MovableObject may be attached to either a SceneNode or to a TagPoint,
130            the latter case if it's attached to a bone on an animated entity.
131            This method will return the scene node of the parent entity
132            if the latter is true.
133        */
134        virtual SceneNode* getParentSceneNode(void) const;
135
136        /** Internal method called to notify the object that it has been attached to a node.
137        */
138        virtual void _notifyAttached(Node* parent, bool isTagPoint = false);
139
140        /** Returns true if this object is attached to a SceneNode or TagPoint. */
141        virtual bool isAttached(void) const;
142
143        /** Returns true if this object is attached to a SceneNode or TagPoint,
144                        and this SceneNode / TagPoint is currently in an active part of the
145                        scene graph. */
146        virtual bool isInScene(void) const;
147
148                /** Internal method to notify the object of the camera to be used for the next rendering operation.
149            @remarks
150                Certain objects may want to do specific processing based on the camera position. This method notifies
151                them incase they wish to do this.
152        */
153        virtual void _notifyCurrentCamera(Camera* cam);
154
155        /** Retrieves the local axis-aligned bounding box for this object.
156            @remarks
157                This bounding box is in local coordinates.
158        */
159        virtual const AxisAlignedBox& getBoundingBox(void) const = 0;
160
161                /** Retrieves the radius of the origin-centered bounding sphere
162                         for this object.
163                */
164                virtual Real getBoundingRadius(void) const = 0;
165
166        /** Retrieves the axis-aligned bounding box for this object in world coordinates. */
167        virtual const AxisAlignedBox& getWorldBoundingBox(bool derive = false) const;
168                /** Retrieves the worldspace bounding sphere for this object. */
169        virtual const Sphere& getWorldBoundingSphere(bool derive = false) const;
170        /** Internal method by which the movable object must add Renderable subclass instances to the rendering queue.
171            @remarks
172                The engine will call this method when this object is to be rendered. The object must then create one or more
173                Renderable subclass instances which it places on the passed in Queue for rendering.
174        */
175        virtual void _updateRenderQueue(RenderQueue* queue) = 0;
176
177        /** Tells this object whether to be visible or not, if it has a renderable component.
178                @note An alternative approach of making an object invisible is to detach it
179                        from it's SceneNode, or to remove the SceneNode entirely.
180                        Detaching a node means that structurally the scene graph changes.
181                        Once this change has taken place, the objects / nodes that have been
182                        removed have less overhead to the visbility detection pass than simply
183                        making the object invisible, so if you do this and leave the objects
184                        out of the tree for a long time, it's faster. However, the act of
185                        detaching / reattaching nodes is in itself more expensive than
186                        setting an object visibility flag, since in the latter case
187                        structural changes are not made. Therefore, small or frequent visbility
188                        changes are best done using this method; large or more longer term
189                        changes are best done by detaching.
190                */
191        virtual void setVisible(bool visible);
192
193        /** Returns whether or not this object is supposed to be visible or not.
194                @remarks
195                        Takes into account both upper rendering distance and visible flag.
196                */
197        virtual bool isVisible(void) const;
198                /** Sets the distance at which the object is no longer rendered.
199                @param dist Distance beyond which the object will not be rendered
200                        (the default is 0, which means objects are always rendered).
201                */
202                virtual void setRenderingDistance(Real dist) {
203                        mUpperDistance = dist;
204                        mSquaredUpperDistance = mUpperDistance * mUpperDistance;
205                }
206
207                /** Gets the distance at which batches are no longer rendered. */
208                virtual Real getRenderingDistance(void) const { return mUpperDistance; }
209
210        /** Call this to associate your own custom user object instance with this MovableObject.
211        @remarks
212            By simply making your game / application object a subclass of UserDefinedObject, you
213            can establish a link between an OGRE instance of MovableObject and your own application
214            classes. Call this method to establish the link.
215        */
216        virtual void setUserObject(UserDefinedObject* obj) { mUserAny = Any(obj); }
217        /** Retrieves a pointer to a custom application object associated with this movable by an earlier
218            call to setUserObject.
219        */
220        virtual UserDefinedObject* getUserObject(void)
221                {
222                        return mUserAny.isEmpty() ? 0 : any_cast<UserDefinedObject*>(mUserAny);
223                }
224
225                /** Sets any kind of user value on this object.
226                @remarks
227                        This method allows you to associate any user value you like with
228                        this MovableObject. This can be a pointer back to one of your own
229                        classes for instance.
230                @note This value is shared with setUserObject so don't use both!
231                */
232                virtual void setUserAny(const Any& anything) { mUserAny = anything; }
233
234                /** Retrieves the custom user value associated with this object.
235                */
236                virtual const Any& getUserAny(void) const { return mUserAny; }
237
238        /** Sets the render queue group this entity will be rendered through.
239        @remarks
240            Render queues are grouped to allow you to more tightly control the ordering
241            of rendered objects. If you do not call this method, all Entity objects default
242            to the default queue (RenderQueue::getDefaultQueueGroup), which is fine for most objects. You may want to alter this
243            if you want this entity to always appear in front of other objects, e.g. for
244            a 3D menu system or such.
245        @par
246            See RenderQueue for more details.
247        @param queueID Enumerated value of the queue group to use.
248        */
249        virtual void setRenderQueueGroup(uint8 queueID);
250
251        /** Gets the queue group for this entity, see setRenderQueueGroup for full details. */
252        virtual uint8 getRenderQueueGroup(void) const;
253
254                /// return the full transformation of the parent sceneNode or the attachingPoint node
255                virtual Matrix4 _getParentNodeFullTransform(void) const;
256
257        /** Sets the query flags for this object.
258        @remarks
259            When performing a scene query, this object will be included or excluded according
260            to flags on the object and flags on the query. This is a bitwise value, so only when
261            a bit on these flags is set, will it be included in a query asking for that flag. The
262            meaning of the bits is application-specific.
263        */
264        virtual void setQueryFlags(uint32 flags) { mQueryFlags = flags; }
265
266        /** As setQueryFlags, except the flags passed as parameters are appended to the
267        existing flags on this object. */
268        virtual void addQueryFlags(uint32 flags) { mQueryFlags |= flags; }
269           
270        /** As setQueryFlags, except the flags passed as parameters are removed from the
271        existing flags on this object. */
272        virtual void removeQueryFlags(unsigned long flags) { mQueryFlags &= ~flags; }
273       
274        /// Returns the query flags relevant for this object
275        virtual uint32 getQueryFlags(void) const { return mQueryFlags; }
276
277                /** Set the default query flags for all future MovableObject instances.
278                */
279                static void setDefaultQueryFlags(uint32 flags) { msDefaultQueryFlags = flags; }
280
281                /** Get the default query flags for all future MovableObject instances.
282                */
283                static uint32 getDefaultQueryFlags(uint32 flags) { return msDefaultQueryFlags; }
284
285               
286        /** Sets the visiblity flags for this object.
287        @remarks
288                        As well as a simple true/false value for visibility (as seen in setVisible),
289                        you can also set visiblity flags which when 'and'ed with the SceneManager's
290                        visibility mask can also make an object invisible.
291        */
292        virtual void setVisibilityFlags(uint32 flags) { mVisibilityFlags = flags; }
293
294        /** As setVisibilityFlags, except the flags passed as parameters are appended to the
295        existing flags on this object. */
296        virtual void addVisibilityFlags(uint32 flags) { mVisibilityFlags |= flags; }
297           
298        /** As setVisibilityFlags, except the flags passed as parameters are removed from the
299        existing flags on this object. */
300        virtual void removeVisibilityFlags(uint32 flags) { mVisibilityFlags &= ~flags; }
301       
302        /// Returns the visibility flags relevant for this object
303        virtual uint32 getVisibilityFlags(void) const { return mVisibilityFlags; }
304
305                /** Set the default visibility flags for all future MovableObject instances.
306                */
307                static void setDefaultVisibilityFlags(uint32 flags) { msDefaultVisibilityFlags = flags; }
308               
309                /** Get the default visibility flags for all future MovableObject instances.
310                */
311                static uint32 getDefaultVisibilityFlags(uint32 flags) { return msDefaultVisibilityFlags; }
312
313                /// Define a default implementation of method from ShadowCaster which implements no shadows
314        EdgeData* getEdgeList(void) { return NULL; }
315        /// Define a default implementation of method from ShadowCaster which implements no shadows
316        ShadowRenderableListIterator getShadowVolumeRenderableIterator(
317            ShadowTechnique shadowTechnique, const Light* light,
318            HardwareIndexBufferSharedPtr* indexBuffer,
319            bool extrudeVertices, Real extrusionDist, unsigned long flags = 0);
320               
321        /** Overridden member from ShadowCaster. */
322        const AxisAlignedBox& getLightCapBounds(void) const;
323        /** Overridden member from ShadowCaster. */
324        const AxisAlignedBox& getDarkCapBounds(const Light& light, Real dirLightExtrusionDist) const;
325        /** Sets whether or not this object will cast shadows.
326        @remarks
327        This setting simply allows you to turn on/off shadows for a given object.
328        An object will not cast shadows unless the scene supports it in any case
329        (see SceneManager::setShadowTechnique), and also the material which is
330        in use must also have shadow casting enabled. By default all entities cast
331        shadows. If, however, for some reason you wish to disable this for a single
332        object then you can do so using this method.
333        @note This method normally refers to objects which block the light, but
334        since Light is also a subclass of MovableObject, in that context it means
335        whether the light causes shadows itself.
336        */
337        void setCastShadows(bool enabled) { mCastShadows = enabled; }
338        /** Returns whether shadow casting is enabled for this object. */
339        bool getCastShadows(void) const { return mCastShadows; }
340        /** Get the distance to extrude for a point/spot light */
341        Real getPointExtrusionDistance(const Light* l) const;
342                /** Get the 'type flags' for this MovableObject.
343                @remarks
344                        A type flag identifies the type of the MovableObject as a bitpattern.
345                        This is used for categorical inclusion / exclusion in SceneQuery
346                        objects. By default, this method returns all ones for objects not
347                        created by a MovableObjectFactory (hence always including them);
348                        otherwise it returns the value assigned to the MovableObjectFactory.
349                        Custom objects which don't use MovableObjectFactory will need to
350                        override this if they want to be included in queries.
351                */
352                virtual uint32 getTypeFlags(void) const;
353
354
355
356
357
358    };
359
360        /** Interface definition for a factory class which produces a certain
361                kind of MovableObject, and can be registered with Root in order
362                to allow all clients to produce new instances of this object, integrated
363                with the standard Ogre processing.
364        */
365        class _OgreExport MovableObjectFactory
366        {
367        protected:
368                /// Type flag, allocated if requested
369                unsigned long mTypeFlag;
370
371                /// Internal implementation of create method - must be overridden
372                virtual MovableObject* createInstanceImpl(
373                        const String& name, const NameValuePairList* params = 0) = 0;
374        public:
375                MovableObjectFactory() : mTypeFlag(0xFFFFFFFF) {}
376                virtual ~MovableObjectFactory() {}
377                /// Get the type of the object to be created
378                virtual const String& getType(void) const = 0;
379
380                /** Create a new instance of the object.
381                @param name The name of the new object
382                @param manager The SceneManager instance that will be holding the
383                        instance once created.
384                @param params Name/value pair list of additional parameters required to
385                        construct the object (defined per subtype). Optional.
386                */
387                virtual MovableObject* createInstance(
388                        const String& name, SceneManager* manager,
389                        const NameValuePairList* params = 0);
390                /** Destroy an instance of the object */
391                virtual void destroyInstance(MovableObject* obj) = 0;
392
393                /** Does this factory require the allocation of a 'type flag', used to
394                        selectively include / exclude this type from scene queries?
395                @remarks
396                        The default implementation here is to return 'false', ie not to
397                        request a unique type mask from Root. For objects that
398                        never need to be excluded in SceneQuery results, that's fine, since
399                        the default implementation of MovableObject::getTypeFlags is to return
400                        all ones, hence matching any query type mask. However, if you want the
401                        objects created by this factory to be filterable by queries using a
402                        broad type, you have to give them a (preferably unique) type mask -
403                        and given that you don't know what other MovableObject types are
404                        registered, Root will allocate you one.
405                */
406                virtual bool requestTypeFlags(void) const { return false; }
407                /** Notify this factory of the type mask to apply.
408                @remarks
409                        This should normally only be called by Root in response to
410                        a 'true' result from requestTypeMask. However, you can actually use
411                        it yourself if you're careful; for example to assign the same mask
412                        to a number of different types of object, should you always wish them
413                        to be treated the same in queries.
414                */
415                void _notifyTypeFlags(unsigned long flag) { mTypeFlag = flag; }
416
417                /** Gets the type flag for this factory.
418                @remarks
419                        A type flag is like a query flag, except that it applies to all instances
420                        of a certain type of object.
421                */
422                unsigned long getTypeFlags(void) const { return mTypeFlag; }
423
424        };
425
426}
427#endif
Note: See TracBrowser for help on using the repository browser.