Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgreHardwareVertexBuffer.h @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

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