OgreHardwareVertexBuffer.h
Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2013 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef __HardwareVertexBuffer__
00029 #define __HardwareVertexBuffer__
00030 
00031 // Precompiler options
00032 #include "OgrePrerequisites.h"
00033 #include "OgreHardwareBuffer.h"
00034 #include "OgreSharedPtr.h"
00035 #include "OgreColourValue.h"
00036 #include "OgreHeaderPrefix.h"
00037 
00038 namespace Ogre {
00039     class HardwareBufferManagerBase;
00040 
00048     class _OgreExport HardwareVertexBuffer : public HardwareBuffer
00049     {
00050         protected:
00051 
00052             HardwareBufferManagerBase* mMgr;
00053             size_t mNumVertices;
00054             size_t mVertexSize;
00055             bool mIsInstanceData;
00056             size_t mInstanceDataStepRate;           
00058             virtual bool checkIfVertexInstanceDataIsSupported();
00059 
00060         public:
00062             HardwareVertexBuffer(HardwareBufferManagerBase* mgr, size_t vertexSize, size_t numVertices,
00063                 HardwareBuffer::Usage usage, bool useSystemMemory, bool useShadowBuffer);
00064             ~HardwareVertexBuffer();
00066             HardwareBufferManagerBase* getManager() const { return mMgr; }
00068             size_t getVertexSize(void) const { return mVertexSize; }
00070             size_t getNumVertices(void) const { return mNumVertices; }
00072             bool isInstanceData() const { return mIsInstanceData; }
00074             void setIsInstanceData(const bool val);
00076             size_t getInstanceDataStepRate() const;
00078             void setInstanceDataStepRate(const size_t val);
00079 
00080 
00081             // NB subclasses should override lock, unlock, readData, writeData
00082 
00083     };
00084 
00086     class _OgreExport HardwareVertexBufferSharedPtr : public SharedPtr<HardwareVertexBuffer>
00087     {
00088     public:
00089         HardwareVertexBufferSharedPtr() : SharedPtr<HardwareVertexBuffer>() {}
00090         explicit HardwareVertexBufferSharedPtr(HardwareVertexBuffer* buf);
00091 
00092 
00093     };
00094 
00096     typedef HardwareBufferLockGuard<HardwareVertexBufferSharedPtr> HardwareVertexBufferLockGuard;
00097 
00099     enum VertexElementSemantic {
00101         VES_POSITION = 1,
00103         VES_BLEND_WEIGHTS = 2,
00105         VES_BLEND_INDICES = 3,
00107         VES_NORMAL = 4,
00109         VES_DIFFUSE = 5,
00111         VES_SPECULAR = 6,
00113         VES_TEXTURE_COORDINATES = 7,
00115         VES_BINORMAL = 8,
00117         VES_TANGENT = 9,
00119         VES_COUNT = 9
00120     };
00121 
00123     enum VertexElementType
00124     {
00125         VET_FLOAT1 = 0,
00126         VET_FLOAT2 = 1,
00127         VET_FLOAT3 = 2,
00128         VET_FLOAT4 = 3,
00130         VET_COLOUR = 4,
00131         VET_SHORT1 = 5,
00132         VET_SHORT2 = 6,
00133         VET_SHORT3 = 7,
00134         VET_SHORT4 = 8,
00135         VET_UBYTE4 = 9,
00137         VET_COLOUR_ARGB = 10,
00139         VET_COLOUR_ABGR = 11,
00140         VET_DOUBLE1 = 12,
00141         VET_DOUBLE2 = 13,
00142         VET_DOUBLE3 = 14,
00143         VET_DOUBLE4 = 15,
00144         VET_USHORT1 = 16,
00145         VET_USHORT2 = 17,
00146         VET_USHORT3 = 18,
00147         VET_USHORT4 = 19,      
00148         VET_INT1 = 20,
00149         VET_INT2 = 21,
00150         VET_INT3 = 22,
00151         VET_INT4 = 23,
00152         VET_UINT1 = 24,
00153         VET_UINT2 = 25,
00154         VET_UINT3 = 26,
00155         VET_UINT4 = 27
00156     };
00157 
00167     class _OgreExport VertexElement : public VertexDataAlloc
00168     {
00169     protected:
00171         unsigned short mSource;
00173         size_t mOffset;
00175         VertexElementType mType;
00177         VertexElementSemantic mSemantic;
00179         unsigned short mIndex;
00180     public:
00182         VertexElement() {}
00184         VertexElement(unsigned short source, size_t offset, VertexElementType theType,
00185             VertexElementSemantic semantic, unsigned short index = 0);
00187         unsigned short getSource(void) const { return mSource; }
00189         size_t getOffset(void) const { return mOffset; }
00191         VertexElementType getType(void) const { return mType; }
00193         VertexElementSemantic getSemantic(void) const { return mSemantic; }
00195         unsigned short getIndex(void) const { return mIndex; }
00197         size_t getSize(void) const;
00199         static size_t getTypeSize(VertexElementType etype);
00201         static unsigned short getTypeCount(VertexElementType etype);
00205         static VertexElementType multiplyTypeCount(VertexElementType baseType, unsigned short count);
00209         static VertexElementType getBaseType(VertexElementType multiType);
00210 
00217         static void convertColourValue(VertexElementType srcType,
00218             VertexElementType dstType, uint32* ptr);
00219 
00225         static uint32 convertColourValue(const ColourValue& src,
00226             VertexElementType dst);
00227 
00229         static VertexElementType getBestColourVertexElementType(void);
00230 
00231         inline bool operator== (const VertexElement& rhs) const
00232         {
00233             if (mType != rhs.mType ||
00234                 mIndex != rhs.mIndex ||
00235                 mOffset != rhs.mOffset ||
00236                 mSemantic != rhs.mSemantic ||
00237                 mSource != rhs.mSource)
00238                 return false;
00239             else
00240                 return true;
00241 
00242         }
00250         inline void baseVertexPointerToElement(void* pBase, void** pElem) const
00251         {
00252             // The only way we can do this is to cast to char* in order to use byte offset
00253             // then cast back to void*.
00254             *pElem = static_cast<void*>(
00255                 static_cast<unsigned char*>(pBase) + mOffset);
00256         }
00264         inline void baseVertexPointerToElement(void* pBase, float** pElem) const
00265         {
00266             // The only way we can do this is to cast to char* in order to use byte offset
00267             // then cast back to float*. However we have to go via void* because casting
00268             // directly is not allowed
00269             *pElem = static_cast<float*>(
00270                 static_cast<void*>(
00271                     static_cast<unsigned char*>(pBase) + mOffset));
00272         }
00273 
00281         inline void baseVertexPointerToElement(void* pBase, RGBA** pElem) const
00282         {
00283             *pElem = static_cast<RGBA*>(
00284                 static_cast<void*>(
00285                     static_cast<unsigned char*>(pBase) + mOffset));
00286         }
00294         inline void baseVertexPointerToElement(void* pBase, unsigned char** pElem) const
00295         {
00296             *pElem = static_cast<unsigned char*>(pBase) + mOffset;
00297         }
00298 
00306         inline void baseVertexPointerToElement(void* pBase, unsigned short** pElem) const
00307         {
00308             *pElem = static_cast<unsigned short*>(
00309                 static_cast<void*>(
00310                     static_cast<unsigned char*>(pBase) + mOffset));
00311         }
00312 
00313 
00314     };
00337     class _OgreExport VertexDeclaration : public VertexDataAlloc
00338     {
00339     public:
00341         typedef list<VertexElement>::type VertexElementList;
00343         static bool vertexElementLess(const VertexElement& e1, const VertexElement& e2);
00344     protected:
00345         VertexElementList mElementList;
00346     public:
00348         VertexDeclaration();
00349         virtual ~VertexDeclaration();
00350 
00352         size_t getElementCount(void) const { return mElementList.size(); }
00354         const VertexElementList& getElements(void) const;
00356         const VertexElement* getElement(unsigned short index) const;
00357 
00366         void sort(void);
00367 
00378         void closeGapsInSource(void);
00379 
00391         VertexDeclaration* getAutoOrganisedDeclaration(bool skeletalAnimation,
00392             bool vertexAnimation, bool vertexAnimationNormals) const;
00393 
00395         unsigned short getMaxSource(void) const;
00396 
00397 
00398 
00412         virtual const VertexElement& addElement(unsigned short source, size_t offset, VertexElementType theType,
00413             VertexElementSemantic semantic, unsigned short index = 0);
00427         virtual const VertexElement& insertElement(unsigned short atPosition,
00428             unsigned short source, size_t offset, VertexElementType theType,
00429             VertexElementSemantic semantic, unsigned short index = 0);
00430 
00432         virtual void removeElement(unsigned short elem_index);
00433 
00440         virtual void removeElement(VertexElementSemantic semantic, unsigned short index = 0);
00441 
00443         virtual void removeAllElements(void);
00444 
00450         virtual void modifyElement(unsigned short elem_index, unsigned short source, size_t offset, VertexElementType theType,
00451             VertexElementSemantic semantic, unsigned short index = 0);
00452 
00458         virtual const VertexElement* findElementBySemantic(VertexElementSemantic sem, unsigned short index = 0) const;
00468         virtual VertexElementList findElementsBySource(unsigned short source) const;
00469 
00471         virtual size_t getVertexSize(unsigned short source) const;
00472         
00476         virtual unsigned short getNextFreeTextureCoordinate() const;
00477 
00482         virtual VertexDeclaration* clone(HardwareBufferManagerBase* mgr = 0) const;
00483 
00484         inline bool operator== (const VertexDeclaration& rhs) const
00485         {
00486             if (mElementList.size() != rhs.mElementList.size())
00487                 return false;
00488 
00489             VertexElementList::const_iterator i, iend, rhsi, rhsiend;
00490             iend = mElementList.end();
00491             rhsiend = rhs.mElementList.end();
00492             rhsi = rhs.mElementList.begin();
00493             for (i = mElementList.begin(); i != iend && rhsi != rhsiend; ++i, ++rhsi)
00494             {
00495                 if ( !(*i == *rhsi) )
00496                     return false;
00497             }
00498 
00499             return true;
00500         }
00501         inline bool operator!= (const VertexDeclaration& rhs) const
00502         {
00503             return !(*this == rhs);
00504         }
00505 
00506     };
00507 
00521     class _OgreExport VertexBufferBinding : public VertexDataAlloc
00522     {
00523     public:
00525         typedef map<unsigned short, HardwareVertexBufferSharedPtr>::type VertexBufferBindingMap;
00526     protected:
00527         VertexBufferBindingMap mBindingMap;
00528         mutable unsigned short mHighIndex;
00529     public:
00531         VertexBufferBinding();
00532         virtual ~VertexBufferBinding();
00541         virtual void setBinding(unsigned short index, const HardwareVertexBufferSharedPtr& buffer);
00543         virtual void unsetBinding(unsigned short index);
00544 
00546         virtual void unsetAllBindings(void);
00547 
00549         virtual const VertexBufferBindingMap& getBindings(void) const;
00550 
00552         virtual const HardwareVertexBufferSharedPtr& getBuffer(unsigned short index) const;
00554         virtual bool isBufferBound(unsigned short index) const;
00555 
00556         virtual size_t getBufferCount(void) const { return mBindingMap.size(); }
00557 
00563         virtual unsigned short getNextIndex(void) const { return mHighIndex++; }
00564 
00567         virtual unsigned short getLastBoundIndex(void) const;
00568 
00569         typedef map<ushort, ushort>::type BindingIndexMap;
00570 
00573         virtual bool hasGaps(void) const;
00574 
00587         virtual void closeGaps(BindingIndexMap& bindingIndexMap);
00588 
00590         virtual bool hasInstanceData() const;
00591 
00592 
00593     };
00599 }
00600 
00601 #include "OgreHeaderSuffix.h"
00602 
00603 #endif
00604 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:43