Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1 was 1, checked in by landauf, 17 years ago
File size: 12.4 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
30// Thanks to Vincent Cantin (karmaGfa) for the original implementation of this
31// class, although it has now been mostly rewritten
32
33#ifndef _BillboardChain_H__
34#define _BillboardChain_H__
35
36#include "OgrePrerequisites.h"
37
38#include "OgreMovableObject.h"
39#include "OgreRenderable.h"
40
41namespace Ogre {
42
43
44        /** Allows the rendering of a chain of connected billboards.
45        @remarks
46                A billboard chain operates much like a traditional billboard, ie its
47                segments always face the camera; the difference being that instead of
48                a set of disconnected quads, the elements in this class are connected
49                together in a chain which must always stay in a continuous strip. This
50                kind of effect is useful for creating effects such as trails, beams,
51                lightning effects, etc.
52        @par
53                A single instance of this class can actually render multiple separate
54                chain segments in a single render operation, provided they all use the
55                same material. To clarify the terminology: a 'segment' is a separate
56                sub-part of the chain with its own start and end (called the 'head'
57                and the 'tail'. An 'element' is a single position / colour / texcoord
58                entry in a segment. You can add items to the head of a chain, and
59                remove them from the tail, very efficiently. Each segment has a max
60                size, and if adding an element to the segment would exceed this size,
61                the tail element is automatically removed and re-used as the new item
62                on the head.
63        @par
64                This class has no auto-updating features to do things like alter the
65                colour of the elements or to automatically add / remove elements over
66                time - you have to do all this yourself as a user of the class.
67                Subclasses can however be used to provide this kind of behaviour
68                automatically. @see RibbonTrail
69        */
70        class _OgreExport BillboardChain : public MovableObject, public Renderable
71        {
72
73        public:
74
75                /** Contains the data of an element of the BillboardChain.
76                */
77                class _OgreExport Element
78                {
79
80                public:
81
82                        Element();
83
84                        Element(Vector3 position,
85                                Real width,
86                                Real texCoord,
87                                ColourValue colour);
88
89                        Vector3 position;
90                        Real width;
91                        /// U or V texture coord depending on options
92                        Real texCoord;
93                        ColourValue colour;
94
95                };
96                typedef std::vector<Element> ElementList;
97
98                /** Constructor (don't use directly, use factory)
99                @param name The name to give this object
100                @param maxElements The maximum number of elements per chain
101                @param numberOfChains The number of separate chain segments contained in this object
102                @param useTextureCoords If true, use texture coordinates from the chain elements
103                @param useVertexColours If true, use vertex colours from the chain elements
104                @param dynamic If true, buffers are created with the intention of being updated
105                */
106                BillboardChain(const String& name, size_t maxElements = 20, size_t numberOfChains = 1, 
107                        bool useTextureCoords = true, bool useColours = true, bool dynamic = true);
108                /// destructor
109                virtual ~BillboardChain();
110
111                /** Set the maximum number of chain elements per chain
112                */
113                virtual void setMaxChainElements(size_t maxElements);
114                /** Get the maximum number of chain elements per chain
115                */
116                virtual size_t getMaxChainElements(void) const { return mMaxElementsPerChain; }
117                /** Set the number of chain segments (this class can render multiple chains
118                        at once using the same material).
119                */
120                virtual void setNumberOfChains(size_t numChains);
121                /** Get the number of chain segments (this class can render multiple chains
122                at once using the same material).
123                */
124                virtual size_t getNumberOfChains(void) const { return mChainCount; }
125
126                /** Sets whether texture coordinate information should be included in the
127                        final buffers generated.
128                @note You must use either texture coordinates or vertex colour since the
129                        vertices have no normals and without one of these there is no source of
130                        colour for the vertices.
131                */
132                virtual void setUseTextureCoords(bool use);
133                /** Gets whether texture coordinate information should be included in the
134                        final buffers generated.
135                */
136                virtual bool getUseTextureCoords(void) const { return mUseTexCoords; }
137
138                /** The direction in which texture coordinates from elements of the
139                        chain are used.
140                */
141                enum TexCoordDirection
142                {
143                        /// Tex coord in elements is treated as the 'u' texture coordinate
144                        TCD_U,
145                        /// Tex coord in elements is treated as the 'v' texture coordinate
146                        TCD_V
147                };
148                /** Sets the direction in which texture coords specified on each element
149                        are deemed to run along the length of the chain.
150                @param dir The direction, default is TCD_U.
151                */
152                virtual void setTextureCoordDirection(TexCoordDirection dir);
153                /** Gets the direction in which texture coords specified on each element
154                        are deemed to run.
155                */
156                virtual TexCoordDirection getTextureCoordDirection(void) { return mTexCoordDir; }
157
158                /** Set the range of the texture coordinates generated across the width of
159                        the chain elements.
160                @param start Start coordinate, default 0.0
161                @param end End coordinate, default 1.0
162                */
163                virtual void setOtherTextureCoordRange(Real start, Real end);
164                /** Get the range of the texture coordinates generated across the width of
165                        the chain elements.
166                */
167                virtual const Real* getOtherTextureCoordRange(void) const { return mOtherTexCoordRange; }
168
169                /** Sets whether vertex colour information should be included in the
170                        final buffers generated.
171                @note You must use either texture coordinates or vertex colour since the
172                        vertices have no normals and without one of these there is no source of
173                        colour for the vertices.
174                */
175                virtual void setUseVertexColours(bool use);
176                /** Gets whether vertex colour information should be included in the
177                        final buffers generated.
178                */
179                virtual bool getUseVertexColours(void) const { return mUseVertexColour; }
180
181                /** Sets whether or not the buffers created for this object are suitable
182                        for dynamic alteration.
183                */
184                virtual void setDynamic(bool dyn);
185
186                /** Gets whether or not the buffers created for this object are suitable
187                        for dynamic alteration.
188                */
189                virtual bool getDynamic(void) const { return mDynamic; }
190               
191                /** Add an element to the 'head' of a chain.
192                @remarks
193                        If this causes the number of elements to exceed the maximum elements
194                        per chain, the last element in the chain (the 'tail') will be removed
195                        to allow the additional element to be added.
196                @param chainIndex The index of the chain
197                @param billboardChainElement The details to add
198                */
199                virtual void addChainElement(size_t chainIndex, 
200                        const Element& billboardChainElement);
201                /** Remove an element from the 'tail' of a chain.
202                @param chainIndex The index of the chain
203                */
204                virtual void removeChainElement(size_t chainIndex);
205                /** Update the details of an existing chain element.
206                @param chainIndex The index of the chain
207                @param elementIndex The element index within the chain, measured from
208                        the 'head' of the chain
209                @param billboardChainElement The details to set
210                */
211                virtual void updateChainElement(size_t chainIndex, size_t elementIndex, 
212                        const Element& billboardChainElement);
213                /** Get the detail of a chain element.
214                @param chainIndex The index of the chain
215                @param elementIndex The element index within the chain, measured from
216                        the 'head' of the chain
217                */
218                virtual const Element& getChainElement(size_t chainIndex, size_t elementIndex) const;
219
220                /** Remove all elements of a given chain (but leave the chain intact). */
221                virtual void clearChain(size_t chainIndex);
222                /** Remove all elements from all chains (but leave the chains themselves intact). */
223                virtual void clearAllChains(void);
224
225                /// Get the material name in use
226                virtual const String& getMaterialName(void) const { return mMaterialName; }
227                /// Set the material name to use for rendering
228                virtual void setMaterialName(const String& name);
229
230
231                // Overridden members follow
232                void _notifyCurrentCamera(Camera* cam);
233                Real getSquaredViewDepth(const Camera* cam) const;
234                Real getBoundingRadius(void) const;
235                const AxisAlignedBox& getBoundingBox(void) const;
236                const MaterialPtr& getMaterial(void) const;
237                const String& getMovableType(void) const;
238                void _updateRenderQueue(RenderQueue *);
239                void getRenderOperation(RenderOperation &);
240                void getWorldTransforms(Matrix4 *) const;
241                const Quaternion& getWorldOrientation(void) const;
242                const Vector3& getWorldPosition(void) const;
243                const LightList& getLights(void) const;
244
245
246
247        protected:
248
249                /// Maximum length of each chain
250                size_t mMaxElementsPerChain;
251                /// Number of chains
252                size_t mChainCount;
253                /// Use texture coords?
254                bool mUseTexCoords;
255                /// Use vertex colour?
256                bool mUseVertexColour;
257                /// Dynamic use?
258                bool mDynamic;
259                /// Vertex data
260                VertexData* mVertexData;
261                /// Index data (to allow multiple unconnected chains)
262                IndexData* mIndexData;
263                /// Is the vertex declaration dirty?
264                bool mVertexDeclDirty;
265                /// Do the buffers need recreating?
266                bool mBuffersNeedRecreating;
267                /// Do the bounds need redefining?
268                mutable bool mBoundsDirty;
269                /// Is the index buffer dirty?
270                bool mIndexContentDirty;
271                /// AABB
272                mutable AxisAlignedBox mAABB;
273                /// Bounding radius
274                mutable Real mRadius;
275                /// Material
276                String mMaterialName;
277                MaterialPtr mMaterial;
278                /// Tetxure coord direction
279                TexCoordDirection mTexCoordDir;
280                /// Other texture coord range
281                Real mOtherTexCoordRange[2];
282
283
284                /// The list holding the chain elements
285                ElementList mChainElementList;
286
287                /** Simple struct defining a chain segment by referencing a subset of
288                        the preallocated buffer (which will be mMaxElementsPerChain * mChainCount
289                        long), by it's chain index, and a head and tail value which describe
290                        the current chain. The buffer subset wraps at mMaxElementsPerChain
291                        so that head and tail can move freely. head and tail are inclusive,
292                        when the chain is empty head and tail are filled with high-values.
293                */
294                struct ChainSegment
295                {
296                        /// The start of this chains subset of the buffer
297                        size_t start;
298                        /// The 'head' of the chain, relative to start
299                        size_t head;
300                        /// The 'tail' of the chain, relative to start
301                        size_t tail;
302                };
303                typedef std::vector<ChainSegment> ChainSegmentList;
304                ChainSegmentList mChainSegmentList;
305
306                /// Setup the STL collections
307                virtual void setupChainContainers(void);
308                /// Setup vertex declaration
309                virtual void setupVertexDeclaration(void);
310                // Setup buffers
311                virtual void setupBuffers(void);
312                /// Update the contents of the vertex buffer
313                virtual void updateVertexBuffer(Camera* cam);
314                /// Update the contents of the index buffer
315                virtual void updateIndexBuffer(void);
316                virtual void updateBoundingBox(void) const;
317
318                /// Chain segment has no elements
319                static const size_t SEGMENT_EMPTY;
320        };
321
322
323        /** Factory object for creating BillboardChain instances */
324        class _OgreExport BillboardChainFactory : public MovableObjectFactory
325        {
326        protected:
327                MovableObject* createInstanceImpl( const String& name, const NameValuePairList* params);
328        public:
329                BillboardChainFactory() {}
330                ~BillboardChainFactory() {}
331
332                static String FACTORY_TYPE_NAME;
333
334                const String& getType(void) const;
335                void destroyInstance( MovableObject* obj); 
336
337        };
338
339
340} // namespace
341
342#endif
343
Note: See TracBrowser for help on using the repository browser.