source: GTP/trunk/App/Demos/Geom/OgreStuff/include/OgreHardwareVertexBuffer.h @ 1092

Revision 1092, 21.3 KB checked in by gumbau, 18 years ago (diff)

LodStrips? and LODTrees demos

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#ifndef __HardwareVertexBuffer__
26#define __HardwareVertexBuffer__
27
28// Precompiler options
29#include "OgrePrerequisites.h"
30#include "OgreHardwareBuffer.h"
31#include "OgreSharedPtr.h"
32#include "OgreColourValue.h"
33
34namespace Ogre {
35    /** Specialisation of HardwareBuffer for a vertex buffer. */
36    class _OgreExport HardwareVertexBuffer : public HardwareBuffer
37    {
38            protected:
39                   
40                    size_t mNumVertices;
41            size_t mVertexSize;
42
43            public:
44                    /// Should be called by HardwareBufferManager
45                    HardwareVertexBuffer(size_t vertexSize, size_t numVertices,
46                HardwareBuffer::Usage usage, bool useSystemMemory, bool useShadowBuffer);
47            ~HardwareVertexBuffer();
48            /// Gets the size in bytes of a single vertex in this buffer
49            size_t getVertexSize(void) const { return mVertexSize; }
50            /// Get the number of vertices in this buffer
51            size_t getNumVertices(void) const { return mNumVertices; }
52               
53
54
55                    // NB subclasses should override lock, unlock, readData, writeData
56       
57    };
58
59    /** Shared pointer implementation used to share index buffers. */
60    class _OgreExport HardwareVertexBufferSharedPtr : public SharedPtr<HardwareVertexBuffer>
61    {
62    public:
63        HardwareVertexBufferSharedPtr() : SharedPtr<HardwareVertexBuffer>() {}
64        explicit HardwareVertexBufferSharedPtr(HardwareVertexBuffer* buf);
65
66
67    };
68
69    /// Vertex element semantics, used to identify the meaning of vertex buffer contents
70        enum VertexElementSemantic {
71                /// Position, 3 reals per vertex
72                VES_POSITION = 1,
73                /// Blending weights
74                VES_BLEND_WEIGHTS = 2,
75        /// Blending indices
76        VES_BLEND_INDICES = 3,
77                /// Normal, 3 reals per vertex
78                VES_NORMAL = 4,
79                /// Diffuse colours
80                VES_DIFFUSE = 5,
81                /// Specular colours
82                VES_SPECULAR = 6,
83                /// Texture coordinates
84                VES_TEXTURE_COORDINATES = 7,
85        /// Binormal (Y axis if normal is Z)
86        VES_BINORMAL = 8,
87        /// Tangent (X axis if normal is Z)
88        VES_TANGENT = 9
89
90        };
91
92    /// Vertex element type, used to identify the base types of the vertex contents
93    enum VertexElementType
94    {
95        VET_FLOAT1,
96        VET_FLOAT2,
97        VET_FLOAT3,
98        VET_FLOAT4,
99        VET_COLOUR,
100                VET_SHORT1,
101                VET_SHORT2,
102                VET_SHORT3,
103                VET_SHORT4,
104        VET_UBYTE4
105    };
106
107    /** This class declares the usage of a single vertex buffer as a component
108        of a complete VertexDeclaration.
109        @remarks
110        Several vertex buffers can be used to supply the input geometry for a
111        rendering operation, and in each case a vertex buffer can be used in
112        different ways for different operations; the buffer itself does not
113        define the semantics (position, normal etc), the VertexElement
114        class does.
115    */
116    class _OgreExport VertexElement
117    {
118    protected:
119        /// The source vertex buffer, as bound to an index using VertexBufferBinding
120        unsigned short mSource;
121        /// The offset in the buffer that this element starts at
122        size_t mOffset;
123        /// The type of element
124        VertexElementType mType;
125        /// The meaning of the element
126        VertexElementSemantic mSemantic;
127        /// Index of the item, only applicable for some elements like texture coords
128        unsigned short mIndex;
129    public:
130        /// Constructor, should not be called directly, call VertexDeclaration::addElement
131        VertexElement(unsigned short source, size_t offset, VertexElementType theType,
132            VertexElementSemantic semantic, unsigned short index = 0);
133        /// Gets the vertex buffer index from where this element draws it's values
134        unsigned short getSource(void) const { return mSource; }
135        /// Gets the offset into the buffer where this element starts
136        size_t getOffset(void) const { return mOffset; }
137        /// Gets the data format of this element
138        VertexElementType getType(void) const { return mType; }
139        /// Gets the meaning of this element
140        VertexElementSemantic getSemantic(void) const { return mSemantic; }
141        /// Gets the index of this element, only applicable for repeating elements
142        unsigned short getIndex(void) const { return mIndex; }
143                /// Gets the size of this element in bytes
144                size_t getSize(void) const;
145                /// Utility method for helping to calculate offsets
146                static size_t getTypeSize(VertexElementType etype);
147                /// Utility method which returns the count of values in a given type
148                static unsigned short getTypeCount(VertexElementType etype);
149                /** Simple converter function which will turn a single-value type into a
150                        multi-value type based on a parameter.
151                */
152                static VertexElementType multiplyTypeCount(VertexElementType baseType, unsigned short count);
153                /** Simple converter function which will a type into it's single-value
154                        equivalent - makes switches on type easier.
155                */
156                static VertexElementType getBaseType(VertexElementType multiType);
157
158        inline bool operator== (const VertexElement& rhs) const
159        {
160            if (mType != rhs.mType ||
161                mIndex != rhs.mIndex ||
162                mOffset != rhs.mOffset ||
163                mSemantic != rhs.mSemantic ||
164                mSource != rhs.mSource)
165                return false;
166            else
167                return true;
168
169        }
170        /** Adjusts a pointer to the base of a vertex to point at this element.
171        @remarks
172            This variant is for void pointers, passed as a parameter because we can't
173            rely on covariant return types.
174        @param pBase Pointer to the start of a vertex in this buffer.
175        @param pElem Pointer to a pointer which will be set to the start of this element.
176        */
177        inline void baseVertexPointerToElement(void* pBase, void** pElem) const
178        {
179            // The only way we can do this is to cast to char* in order to use byte offset
180            // then cast back to void*.
181            *pElem = static_cast<void*>(
182                static_cast<unsigned char*>(pBase) + mOffset);
183        }
184        /** Adjusts a pointer to the base of a vertex to point at this element.
185        @remarks
186            This variant is for float pointers, passed as a parameter because we can't
187            rely on covariant return types.
188        @param pBase Pointer to the start of a vertex in this buffer.
189        @param pElem Pointer to a pointer which will be set to the start of this element.
190        */
191        inline void baseVertexPointerToElement(void* pBase, float** pElem) const
192        {
193            // The only way we can do this is to cast to char* in order to use byte offset
194            // then cast back to float*. However we have to go via void* because casting 
195            // directly is not allowed
196            *pElem = static_cast<float*>(
197                static_cast<void*>(
198                    static_cast<unsigned char*>(pBase) + mOffset));
199        }
200
201        /** Adjusts a pointer to the base of a vertex to point at this element.
202        @remarks
203            This variant is for RGBA pointers, passed as a parameter because we can't
204            rely on covariant return types.
205        @param pBase Pointer to the start of a vertex in this buffer.
206        @param pElem Pointer to a pointer which will be set to the start of this element.
207        */
208        inline void baseVertexPointerToElement(void* pBase, RGBA** pElem) const
209        {
210            *pElem = static_cast<RGBA*>(
211                static_cast<void*>(
212                    static_cast<unsigned char*>(pBase) + mOffset));
213        }
214        /** Adjusts a pointer to the base of a vertex to point at this element.
215        @remarks
216            This variant is for char pointers, passed as a parameter because we can't
217            rely on covariant return types.
218        @param pBase Pointer to the start of a vertex in this buffer.
219        @param pElem Pointer to a pointer which will be set to the start of this element.
220        */
221        inline void baseVertexPointerToElement(void* pBase, unsigned char** pElem) const
222        {
223            *pElem = static_cast<unsigned char*>(pBase) + mOffset;
224        }
225
226        /** Adjusts a pointer to the base of a vertex to point at this element.
227        @remarks
228        This variant is for ushort pointers, passed as a parameter because we can't
229        rely on covariant return types.
230        @param pBase Pointer to the start of a vertex in this buffer.
231        @param pElem Pointer to a pointer which will be set to the start of this element.
232        */
233        inline void baseVertexPointerToElement(void* pBase, unsigned short** pElem) const
234        {
235            *pElem = static_cast<unsigned short*>(pBase) + mOffset;
236        }
237
238
239    };
240    /** This class declares the format of a set of vertex inputs, which
241        can be issued to the rendering API through a RenderOperation.
242        @remarks
243        You should be aware that the ordering and structure of the
244        VertexDeclaration can be very important on DirectX with older
245        cards,so if you want to maintain maximum compatibility with
246        all render systems and all cards you should be careful to follow these
247        rules:<ol>
248        <li>VertexElements should be added in the following order, and the order of the
249        elements within a shared buffer should be as follows:
250        position, blending weights, normals, diffuse colours, specular colours,
251            texture coordinates (in order, with no gaps)</li>
252        <li>You must not have unused gaps in your buffers which are not referenced
253        by any VertexElement</li>
254        <li>You must not cause the buffer & offset settings of 2 VertexElements to overlap</li>
255        </ol>
256        Whilst GL and more modern graphics cards in D3D will allow you to defy these rules,
257        sticking to them will ensure that your buffers have the maximum compatibility.
258        @par
259        Like the other classes in this functional area, these declarations should be created and
260        destroyed using the HardwareBufferManager.
261    */
262    class _OgreExport VertexDeclaration
263    {
264    public:
265                /// Defines the list of vertex elements that makes up this declaration
266        typedef std::list<VertexElement> VertexElementList;
267        /// Sort routine for vertex elements
268        static bool vertexElementLess(const VertexElement& e1, const VertexElement& e2);
269    protected:
270        VertexElementList mElementList;
271    public:
272        /// Standard constructor, not you should use HardwareBufferManager::createVertexDeclaration
273        VertexDeclaration();
274        virtual ~VertexDeclaration();
275       
276        /** Get the number of elements in the declaration. */
277        size_t getElementCount(void) { return mElementList.size(); }
278        /** Gets read-only access to the list of vertex elements. */
279        const VertexElementList& getElements(void) const;
280        /** Get a single element. */
281        const VertexElement* getElement(unsigned short index);
282
283        /** Sorts the elements in this list to be compatible with the maximum
284            number of rendering APIs / graphics cards.
285        @remarks
286            Older graphics cards require vertex data to be presented in a more
287            rigid way, as defined in the main documentation for this class. As well
288            as the ordering being important, where shared source buffers are used, the
289            declaration must list all the elements for each source in turn.
290        */
291        void sort(void);
292
293        /** Remove any gaps in the source buffer list used by this declaration.
294        @remarks
295            This is useful if you've modified a declaration and want to remove
296            any gaps in the list of buffers being used. Note, however, that if this
297            declaration is already being used with a VertexBufferBinding, you will
298            need to alter that too. This method is mainly useful when reorganising
299            buffers based on an altered declaration.
300        @note
301            This will cause the vertex declaration to be re-sorted.
302        */
303        void closeGapsInSource(void);
304
305        /** Generates a new VertexDeclaration for optimal usage based on the current
306            vertex declaration, which can be used with VertexData::reorganiseBuffers later
307            if you wish, or simply used as a template.
308        @param animated Whether this vertex data is going to be animated; this
309            affects the choice of both usage and buffer splits.
310        */
311        VertexDeclaration* getAutoOrganisedDeclaration(bool animated);
312
313        /** Gets the indeex of the highest source value referenced by this declaration. */
314        unsigned short getMaxSource(void) const;
315
316
317
318        /** Adds a new VertexElement to this declaration.
319        @remarks
320            This method adds a single element (positions, normals etc) to the end of the
321            vertex declaration. <b>Please read the information in VertexDeclaration about
322            the importance of ordering and structure for compatibility with older D3D drivers</b>.
323            @param source The binding index of HardwareVertexBuffer which will provide the source for this element.
324                        See VertexBufferBindingState for full information.
325        @param offset The offset in bytes where this element is located in the buffer
326        @param theType The data format of the element (3 floats, a colour etc)
327        @param semantic The meaning of the data (position, normal, diffuse colour etc)
328        @param index Optional index for multi-input elements like texture coordinates
329                @returns A reference to the VertexElement added.
330        */
331        virtual const VertexElement& addElement(unsigned short source, size_t offset, VertexElementType theType,
332            VertexElementSemantic semantic, unsigned short index = 0);
333        /** Inserts a new VertexElement at a given position in this declaration.
334        @remarks
335        This method adds a single element (positions, normals etc) at a given position in this
336        vertex declaration. <b>Please read the information in VertexDeclaration about
337        the importance of ordering and structure for compatibility with older D3D drivers</b>.
338        @param source The binding index of HardwareVertexBuffer which will provide the source for this element.
339        See VertexBufferBindingState for full information.
340        @param offset The offset in bytes where this element is located in the buffer
341        @param theType The data format of the element (3 floats, a colour etc)
342        @param semantic The meaning of the data (position, normal, diffuse colour etc)
343        @param index Optional index for multi-input elements like texture coordinates
344        @returns A reference to the VertexElement added.
345        */
346        virtual const VertexElement& insertElement(unsigned short atPosition,
347            unsigned short source, size_t offset, VertexElementType theType,
348            VertexElementSemantic semantic, unsigned short index = 0);
349
350        /** Remove the element at the given index from this declaration. */
351        virtual void removeElement(unsigned short elem_index);
352
353        /** Remove the element with the given semantic and usage index.
354        @remarks
355            In this case 'index' means the usage index for repeating elements such
356            as texture coordinates. For other elements this will always be 0 and does
357            not refer to the index in the vector.
358        */
359        virtual void removeElement(VertexElementSemantic semantic, unsigned short index = 0);
360
361        /** Modify an element in-place, params as addElement.
362           @remarks
363           <b>Please read the information in VertexDeclaration about
364            the importance of ordering and structure for compatibility with older D3D drivers</b>.
365         */
366        virtual void modifyElement(unsigned short elem_index, unsigned short source, size_t offset, VertexElementType theType,
367            VertexElementSemantic semantic, unsigned short index = 0);
368
369                /** Finds a VertexElement with the given semantic, and index if there is more than
370                        one element with the same semantic.
371        @remarks
372            If the element is not found, this method returns null.
373                */
374                virtual const VertexElement* findElementBySemantic(VertexElementSemantic sem, unsigned short index = 0);
375                /** Based on the current elements, gets the size of the vertex for a given buffer source.
376                @param source The buffer binding index for which to get the vertex size.
377                */
378
379                /** Gets a list of elements which use a given source.
380                @remarks
381                        Note that the list of elements is returned by value therefore is separate from
382                        the declaration as soon as this method returns.
383                */
384                virtual VertexElementList findElementsBySource(unsigned short source);
385               
386                /** Gets the vertex size defined by this declaration for a given source. */
387        virtual size_t getVertexSize(unsigned short source);
388
389        /** Clones this declaration. */
390        virtual VertexDeclaration* clone(void);
391
392        inline bool operator== (const VertexDeclaration& rhs) const
393        {
394            if (mElementList.size() != rhs.mElementList.size())
395                return false;
396
397            VertexElementList::const_iterator i, iend, rhsi, rhsiend;
398            iend = mElementList.end();
399            rhsiend = rhs.mElementList.end();
400            rhsi = rhs.mElementList.begin();
401            for (i = mElementList.begin(); i != iend && rhsi != rhsiend; ++i, ++rhsi)
402            {
403                if ( !(*i == *rhsi) )
404                    return false;
405            }
406
407            return true;
408        }
409        inline bool operator!= (const VertexDeclaration& rhs) const
410        {
411            return !(*this == rhs);
412        }
413
414    };
415
416        /** Records the state of all the vertex buffer bindings required to provide a vertex declaration
417                with the input data it needs for the vertex elements.
418        @remarks
419                Why do we have this binding list rather than just have VertexElement referring to the
420                vertex buffers direct? Well, in the underlying APIs, binding the vertex buffers to an
421                index (or 'stream') is the way that vertex data is linked, so this structure better
422                reflects the realities of that. In addition, by separating the vertex declaration from
423                the list of vertex buffer bindings, it becomes possible to reuse bindings between declarations
424                and vice versa, giving opportunities to reduce the state changes required to perform rendering.
425        @par
426                Like the other classes in this functional area, these binding maps should be created and
427                destroyed using the HardwareBufferManager.
428        */
429        class _OgreExport VertexBufferBinding
430        {
431        public:
432                /// Defines the vertex buffer bindings used as source for vertex declarations
433                typedef std::map<unsigned short, HardwareVertexBufferSharedPtr> VertexBufferBindingMap;
434        protected:
435                VertexBufferBindingMap mBindingMap;
436                mutable unsigned short mHighIndex;
437        public:
438                /// Constructor, should not be called direct, use HardwareBufferManager::createVertexBufferBinding
439                VertexBufferBinding();
440                virtual ~VertexBufferBinding();
441                /** Set a binding, associating a vertex buffer with a given index.
442                @remarks
443                        If the index is already associated with a vertex buffer,
444            the association will be replaced. This may cause the old buffer
445            to be destroyed if nothing else is referring to it.
446                        You should assign bindings from 0 and not leave gaps, although you can
447                        bind them in any order.
448                */
449                virtual void setBinding(unsigned short index, HardwareVertexBufferSharedPtr buffer);
450                /** Removes an existing binding. */
451                virtual void unsetBinding(unsigned short index);
452
453        /** Removes all the bindings. */
454        virtual void unsetAllBindings(void);
455
456                /// Gets a read-only version of the buffer bindings
457                virtual const VertexBufferBindingMap& getBindings(void) const;
458
459                /// Gets the buffer bound to the given source index
460                virtual HardwareVertexBufferSharedPtr getBuffer(unsigned short index);
461
462        virtual size_t getBufferCount(void) const { return mBindingMap.size(); }
463
464                /** Gets the highest index which has already been set, plus 1.
465                @remarks
466                        This is to assist in binding the vertex buffers such that there are
467                        not gaps in the list.
468                */
469                virtual unsigned short getNextIndex(void) const { return mHighIndex++; }
470
471
472
473
474        };
475
476
477
478}
479#endif
480
Note: See TracBrowser for help on using the repository browser.