Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreHardwareVertexBuffer.cpp

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 © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 #include "OgreHardwareVertexBuffer.h"
00027 #include "OgreColourValue.h"
00028 #include "OgreException.h"
00029 #include "OgreStringConverter.h"
00030 #include "OgreHardwareBufferManager.h"
00031 #include "OgreDefaultHardwareBufferManager.h"
00032 
00033 namespace Ogre {
00034 
00035     //-----------------------------------------------------------------------------
00036     HardwareVertexBuffer::HardwareVertexBuffer(size_t vertexSize,  
00037         size_t numVertices, HardwareBuffer::Usage usage, 
00038         bool useSystemMemory, bool useShadowBuffer) 
00039         : HardwareBuffer(usage, useSystemMemory, useShadowBuffer), 
00040           mNumVertices(numVertices),
00041           mVertexSize(vertexSize)
00042     {
00043         // Calculate the size of the vertices
00044         mSizeInBytes = mVertexSize * numVertices;
00045 
00046         // Create a shadow buffer if required
00047         if (mUseShadowBuffer)
00048         {
00049             mpShadowBuffer = new DefaultHardwareVertexBuffer(mVertexSize, 
00050                     mNumVertices, HardwareBuffer::HBU_DYNAMIC);
00051         }
00052 
00053     }
00054     //-----------------------------------------------------------------------------
00055     HardwareVertexBuffer::~HardwareVertexBuffer()
00056     {
00057         if (mpShadowBuffer)
00058         {
00059             delete mpShadowBuffer;
00060         }
00061     }
00062     //-----------------------------------------------------------------------------
00063     VertexElement::VertexElement(unsigned short source, size_t offset, 
00064         VertexElementType theType, VertexElementSemantic semantic, unsigned short index)
00065         : mSource(source), mOffset(offset), mType(theType), 
00066         mSemantic(semantic), mIndex(index)
00067     {
00068     }
00069     //-----------------------------------------------------------------------------
00070     size_t VertexElement::getSize(void) const
00071     {
00072         return getTypeSize(mType);
00073     }
00074     //-----------------------------------------------------------------------------
00075     size_t VertexElement::getTypeSize(VertexElementType etype)
00076     {
00077         switch(etype)
00078         {
00079         case VET_COLOUR:
00080             return sizeof(RGBA);
00081         case VET_FLOAT1:
00082             return sizeof(float);
00083         case VET_FLOAT2:
00084             return sizeof(float)*2;
00085         case VET_FLOAT3:
00086             return sizeof(float)*3;
00087         case VET_FLOAT4:
00088             return sizeof(float)*4;
00089         case VET_SHORT1:
00090             return sizeof(short);
00091         case VET_SHORT2:
00092             return sizeof(short)*2;
00093         case VET_SHORT3:
00094             return sizeof(short)*3;
00095         case VET_SHORT4:
00096             return sizeof(short)*4;
00097         }
00098         return 0;
00099     }
00100     //-----------------------------------------------------------------------------
00101     unsigned short VertexElement::getTypeCount(VertexElementType etype)
00102     {
00103         switch (etype)
00104         {
00105         case VET_COLOUR:
00106             return 1;
00107         case VET_FLOAT1:
00108             return 1;
00109         case VET_FLOAT2:
00110             return 2;
00111         case VET_FLOAT3:
00112             return 3;
00113         case VET_FLOAT4:
00114             return 4;
00115         case VET_SHORT1:
00116             return 1;
00117         case VET_SHORT2:
00118             return 2;
00119         case VET_SHORT3:
00120             return 3;
00121         case VET_SHORT4:
00122             return 4;
00123         }
00124         Except(Exception::ERR_INVALIDPARAMS, "Invalid type", 
00125             "VertexElement::getTypeCount");
00126     }
00127     //-----------------------------------------------------------------------------
00128     VertexElementType VertexElement::multiplyTypeCount(VertexElementType baseType, 
00129         unsigned short count)
00130     {
00131         switch (baseType)
00132         {
00133         case VET_FLOAT1:
00134             switch(count)
00135             {
00136             case 1:
00137                 return VET_FLOAT1;
00138             case 2:
00139                 return VET_FLOAT2;
00140             case 3:
00141                 return VET_FLOAT3;
00142             case 4:
00143                 return VET_FLOAT4;
00144             default:
00145                 break;
00146             }
00147             break;
00148         case VET_SHORT1:
00149             switch(count)
00150             {
00151             case 1:
00152                 return VET_SHORT1;
00153             case 2:
00154                 return VET_SHORT2;
00155             case 3:
00156                 return VET_SHORT3;
00157             case 4:
00158                 return VET_SHORT4;
00159             default:
00160                 break;
00161             }
00162             break;
00163         default:
00164             break;
00165         }
00166         Except(Exception::ERR_INVALIDPARAMS, "Invalid base type", 
00167             "VertexElement::multiplyTypeCount");
00168     }
00169     //-----------------------------------------------------------------------------
00170     VertexDeclaration::VertexDeclaration()
00171     {
00172     }
00173     //-----------------------------------------------------------------------------
00174     VertexDeclaration::~VertexDeclaration()
00175     {
00176     }
00177     //-----------------------------------------------------------------------------
00178     const VertexDeclaration::VertexElementList& VertexDeclaration::getElements(void) const
00179     {
00180         return mElementList;
00181     }
00182     //-----------------------------------------------------------------------------
00183     const VertexElement& VertexDeclaration::addElement(unsigned short source, 
00184         size_t offset, VertexElementType theType,
00185         VertexElementSemantic semantic, unsigned short index)
00186     {
00187         mElementList.push_back(
00188             VertexElement(source, offset, theType, semantic, index)
00189             );
00190         return mElementList.back();
00191     }
00192     //-----------------------------------------------------------------------------
00193     void VertexDeclaration::removeElement(unsigned short elem_index)
00194     {
00195         assert(elem_index < mElementList.size() && "Index out of bounds");
00196         VertexElementList::iterator i = mElementList.begin();
00197         i += elem_index;
00198         mElementList.erase(i);
00199     }
00200     //-----------------------------------------------------------------------------
00201     void VertexDeclaration::removeElement(VertexElementSemantic semantic, unsigned short index)
00202     {
00203         VertexElementList::iterator ei, eiend;
00204         eiend = mElementList.end();
00205         for (ei = mElementList.begin(); ei != eiend; ++ei)
00206         {
00207             if (ei->getSemantic() == semantic && ei->getIndex() == index)
00208             {
00209                 mElementList.erase(ei);
00210                 break;
00211             }
00212         }
00213     }
00214     //-----------------------------------------------------------------------------
00215     void VertexDeclaration::modifyElement(unsigned short elem_index, 
00216         unsigned short source, size_t offset, VertexElementType theType,
00217         VertexElementSemantic semantic, unsigned short index)
00218     {
00219         assert(elem_index < mElementList.size() && "Index out of bounds");
00220         VertexElementList::iterator i = mElementList.begin();
00221         i += elem_index;
00222         (*i) = VertexElement(source, offset, theType, semantic, index);
00223     }
00224     //-----------------------------------------------------------------------------
00225     const VertexElement* VertexDeclaration::findElementBySemantic(
00226         VertexElementSemantic sem, unsigned short index)
00227     {
00228         VertexElementList::const_iterator ei, eiend;
00229         eiend = mElementList.end();
00230         for (ei = mElementList.begin(); ei != eiend; ++ei)
00231         {
00232             if (ei->getSemantic() == sem && ei->getIndex() == index)
00233             {
00234                 return &(*ei);
00235             }
00236         }
00237 
00238         return NULL;
00239 
00240 
00241     }
00242     //-----------------------------------------------------------------------------
00243     VertexDeclaration::VertexElementList VertexDeclaration::findElementsBySource(
00244         unsigned short source)
00245     {
00246         VertexElementList retList;
00247         VertexElementList::const_iterator ei, eiend;
00248         eiend = mElementList.end();
00249         for (ei = mElementList.begin(); ei != eiend; ++ei)
00250         {
00251             if (ei->getSource() == source)
00252             {
00253                 retList.push_back(*ei);
00254             }
00255         }
00256         return retList;
00257 
00258     }
00259 
00260     //-----------------------------------------------------------------------------
00261     size_t VertexDeclaration::getVertexSize(unsigned short source)
00262     {
00263         VertexElementList::const_iterator i, iend;
00264         iend = mElementList.end();
00265         size_t sz = 0;
00266 
00267         for (i = mElementList.begin(); i != iend; ++i)
00268         {
00269             if (i->getSource() == source)
00270             {
00271                 sz += i->getSize();
00272 
00273             }
00274         }
00275         return sz;
00276     }
00277     //-----------------------------------------------------------------------------
00278     VertexDeclaration* VertexDeclaration::clone(void)
00279     {
00280         VertexDeclaration* ret = HardwareBufferManager::getSingleton().createVertexDeclaration();
00281 
00282         VertexElementList::const_iterator i, iend;
00283         iend = mElementList.end();
00284         for (i = mElementList.begin(); i != iend; ++i)
00285         {
00286             ret->addElement(i->getSource(), i->getOffset(), i->getType(), i->getSemantic(), i->getIndex());
00287         }
00288         return ret;
00289     }
00290     //-----------------------------------------------------------------------------
00291     VertexBufferBinding::VertexBufferBinding() : mHighIndex(0)
00292     {
00293     }
00294     //-----------------------------------------------------------------------------
00295     VertexBufferBinding::~VertexBufferBinding()
00296     {
00297         unsetAllBindings();
00298     }
00299     //-----------------------------------------------------------------------------
00300     void VertexBufferBinding::setBinding(unsigned short index, HardwareVertexBufferSharedPtr buffer)
00301     {
00302         // NB will replace any existing buffer ptr at this index, and will thus cause
00303         // reference count to decrement on that buffer (possibly destroying it)
00304         mBindingMap[index] = buffer;
00305         mHighIndex = std::max(mHighIndex, index);
00306     }
00307     //-----------------------------------------------------------------------------
00308     void VertexBufferBinding::unsetBinding(unsigned short index)
00309     {
00310         VertexBufferBindingMap::iterator i = mBindingMap.find(index);
00311         if (i == mBindingMap.end())
00312         {
00313             Except(Exception::ERR_ITEM_NOT_FOUND,
00314                 "Cannot find buffer binding for index " + StringConverter::toString(index),
00315                 "VertexBufferBinding::unsetBinding");
00316         }
00317         mBindingMap.erase(i);
00318     }
00319     //-----------------------------------------------------------------------------
00320     void VertexBufferBinding::unsetAllBindings(void)
00321     {
00322         mBindingMap.clear();
00323     }
00324     //-----------------------------------------------------------------------------
00325     const VertexBufferBinding::VertexBufferBindingMap& 
00326     VertexBufferBinding::getBindings(void) const
00327     {
00328         return mBindingMap;
00329     }
00330     //-----------------------------------------------------------------------------
00331     HardwareVertexBufferSharedPtr VertexBufferBinding::getBuffer(unsigned short index)
00332     {
00333         VertexBufferBindingMap::iterator i = mBindingMap.find(index);
00334         if (i == mBindingMap.end())
00335         {
00336             Except(Exception::ERR_ITEM_NOT_FOUND, "No buffer is bound to that index.",
00337                 "VertexBufferBinding::getBuffer");
00338         }
00339         return i->second;
00340     }
00341     //-----------------------------------------------------------------------------
00342     HardwareVertexBufferSharedPtr::HardwareVertexBufferSharedPtr(HardwareVertexBuffer* buf)
00343         : SharedPtr<HardwareVertexBuffer>(buf)
00344     {
00345 
00346     }
00347 
00348 
00349 
00350 
00351 }

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:13 2004