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 case VET_UBYTE4: 00098 return sizeof(unsigned char)*4; 00099 } 00100 return 0; 00101 } 00102 //----------------------------------------------------------------------------- 00103 unsigned short VertexElement::getTypeCount(VertexElementType etype) 00104 { 00105 switch (etype) 00106 { 00107 case VET_COLOUR: 00108 return 1; 00109 case VET_FLOAT1: 00110 return 1; 00111 case VET_FLOAT2: 00112 return 2; 00113 case VET_FLOAT3: 00114 return 3; 00115 case VET_FLOAT4: 00116 return 4; 00117 case VET_SHORT1: 00118 return 1; 00119 case VET_SHORT2: 00120 return 2; 00121 case VET_SHORT3: 00122 return 3; 00123 case VET_SHORT4: 00124 return 4; 00125 case VET_UBYTE4: 00126 return 4; 00127 } 00128 Except(Exception::ERR_INVALIDPARAMS, "Invalid type", 00129 "VertexElement::getTypeCount"); 00130 } 00131 //----------------------------------------------------------------------------- 00132 VertexElementType VertexElement::multiplyTypeCount(VertexElementType baseType, 00133 unsigned short count) 00134 { 00135 switch (baseType) 00136 { 00137 case VET_FLOAT1: 00138 switch(count) 00139 { 00140 case 1: 00141 return VET_FLOAT1; 00142 case 2: 00143 return VET_FLOAT2; 00144 case 3: 00145 return VET_FLOAT3; 00146 case 4: 00147 return VET_FLOAT4; 00148 default: 00149 break; 00150 } 00151 break; 00152 case VET_SHORT1: 00153 switch(count) 00154 { 00155 case 1: 00156 return VET_SHORT1; 00157 case 2: 00158 return VET_SHORT2; 00159 case 3: 00160 return VET_SHORT3; 00161 case 4: 00162 return VET_SHORT4; 00163 default: 00164 break; 00165 } 00166 break; 00167 default: 00168 break; 00169 } 00170 Except(Exception::ERR_INVALIDPARAMS, "Invalid base type", 00171 "VertexElement::multiplyTypeCount"); 00172 } 00173 //----------------------------------------------------------------------------- 00174 VertexDeclaration::VertexDeclaration() 00175 { 00176 } 00177 //----------------------------------------------------------------------------- 00178 VertexDeclaration::~VertexDeclaration() 00179 { 00180 } 00181 //----------------------------------------------------------------------------- 00182 const VertexDeclaration::VertexElementList& VertexDeclaration::getElements(void) const 00183 { 00184 return mElementList; 00185 } 00186 //----------------------------------------------------------------------------- 00187 const VertexElement& VertexDeclaration::addElement(unsigned short source, 00188 size_t offset, VertexElementType theType, 00189 VertexElementSemantic semantic, unsigned short index) 00190 { 00191 mElementList.push_back( 00192 VertexElement(source, offset, theType, semantic, index) 00193 ); 00194 return mElementList.back(); 00195 } 00196 //----------------------------------------------------------------------------- 00197 const VertexElement& VertexDeclaration::insertElement(unsigned short atPosition, 00198 unsigned short source, size_t offset, VertexElementType theType, 00199 VertexElementSemantic semantic, unsigned short index) 00200 { 00201 if (atPosition >= mElementList.size()) 00202 { 00203 return addElement(source, offset, theType, semantic, index); 00204 } 00205 00206 VertexElementList::iterator i = mElementList.begin(); 00207 for (unsigned short n = 0; n < atPosition; ++n) 00208 ++i; 00209 00210 i = mElementList.insert(i, 00211 VertexElement(source, offset, theType, semantic, index)); 00212 return *i; 00213 00214 } 00215 //----------------------------------------------------------------------------- 00216 const VertexElement* VertexDeclaration::getElement(unsigned short index) 00217 { 00218 assert(index < mElementList.size() && "Index out of bounds"); 00219 00220 VertexElementList::iterator i = mElementList.begin(); 00221 for (unsigned short n = 0; n < index; ++n) 00222 ++i; 00223 00224 return &(*i); 00225 00226 } 00227 //----------------------------------------------------------------------------- 00228 void VertexDeclaration::removeElement(unsigned short elem_index) 00229 { 00230 assert(elem_index < mElementList.size() && "Index out of bounds"); 00231 VertexElementList::iterator i = mElementList.begin(); 00232 for (unsigned short n = 0; n < elem_index; ++n) 00233 ++i; 00234 mElementList.erase(i); 00235 } 00236 //----------------------------------------------------------------------------- 00237 void VertexDeclaration::removeElement(VertexElementSemantic semantic, unsigned short index) 00238 { 00239 VertexElementList::iterator ei, eiend; 00240 eiend = mElementList.end(); 00241 for (ei = mElementList.begin(); ei != eiend; ++ei) 00242 { 00243 if (ei->getSemantic() == semantic && ei->getIndex() == index) 00244 { 00245 mElementList.erase(ei); 00246 break; 00247 } 00248 } 00249 } 00250 //----------------------------------------------------------------------------- 00251 void VertexDeclaration::modifyElement(unsigned short elem_index, 00252 unsigned short source, size_t offset, VertexElementType theType, 00253 VertexElementSemantic semantic, unsigned short index) 00254 { 00255 assert(elem_index < mElementList.size() && "Index out of bounds"); 00256 VertexElementList::iterator i = mElementList.begin(); 00257 for (unsigned short n = 0; n < elem_index; ++n) 00258 ++i; 00259 (*i) = VertexElement(source, offset, theType, semantic, index); 00260 } 00261 //----------------------------------------------------------------------------- 00262 const VertexElement* VertexDeclaration::findElementBySemantic( 00263 VertexElementSemantic sem, unsigned short index) 00264 { 00265 VertexElementList::const_iterator ei, eiend; 00266 eiend = mElementList.end(); 00267 for (ei = mElementList.begin(); ei != eiend; ++ei) 00268 { 00269 if (ei->getSemantic() == sem && ei->getIndex() == index) 00270 { 00271 return &(*ei); 00272 } 00273 } 00274 00275 return NULL; 00276 00277 00278 } 00279 //----------------------------------------------------------------------------- 00280 VertexDeclaration::VertexElementList VertexDeclaration::findElementsBySource( 00281 unsigned short source) 00282 { 00283 VertexElementList retList; 00284 VertexElementList::const_iterator ei, eiend; 00285 eiend = mElementList.end(); 00286 for (ei = mElementList.begin(); ei != eiend; ++ei) 00287 { 00288 if (ei->getSource() == source) 00289 { 00290 retList.push_back(*ei); 00291 } 00292 } 00293 return retList; 00294 00295 } 00296 00297 //----------------------------------------------------------------------------- 00298 size_t VertexDeclaration::getVertexSize(unsigned short source) 00299 { 00300 VertexElementList::const_iterator i, iend; 00301 iend = mElementList.end(); 00302 size_t sz = 0; 00303 00304 for (i = mElementList.begin(); i != iend; ++i) 00305 { 00306 if (i->getSource() == source) 00307 { 00308 sz += i->getSize(); 00309 00310 } 00311 } 00312 return sz; 00313 } 00314 //----------------------------------------------------------------------------- 00315 VertexDeclaration* VertexDeclaration::clone(void) 00316 { 00317 VertexDeclaration* ret = HardwareBufferManager::getSingleton().createVertexDeclaration(); 00318 00319 VertexElementList::const_iterator i, iend; 00320 iend = mElementList.end(); 00321 for (i = mElementList.begin(); i != iend; ++i) 00322 { 00323 ret->addElement(i->getSource(), i->getOffset(), i->getType(), i->getSemantic(), i->getIndex()); 00324 } 00325 return ret; 00326 } 00327 //----------------------------------------------------------------------------- 00328 VertexBufferBinding::VertexBufferBinding() : mHighIndex(0) 00329 { 00330 } 00331 //----------------------------------------------------------------------------- 00332 VertexBufferBinding::~VertexBufferBinding() 00333 { 00334 unsetAllBindings(); 00335 } 00336 //----------------------------------------------------------------------------- 00337 void VertexBufferBinding::setBinding(unsigned short index, HardwareVertexBufferSharedPtr buffer) 00338 { 00339 // NB will replace any existing buffer ptr at this index, and will thus cause 00340 // reference count to decrement on that buffer (possibly destroying it) 00341 mBindingMap[index] = buffer; 00342 mHighIndex = std::max(mHighIndex, (unsigned short)(index+1)); 00343 } 00344 //----------------------------------------------------------------------------- 00345 void VertexBufferBinding::unsetBinding(unsigned short index) 00346 { 00347 VertexBufferBindingMap::iterator i = mBindingMap.find(index); 00348 if (i == mBindingMap.end()) 00349 { 00350 Except(Exception::ERR_ITEM_NOT_FOUND, 00351 "Cannot find buffer binding for index " + StringConverter::toString(index), 00352 "VertexBufferBinding::unsetBinding"); 00353 } 00354 mBindingMap.erase(i); 00355 } 00356 //----------------------------------------------------------------------------- 00357 void VertexBufferBinding::unsetAllBindings(void) 00358 { 00359 mBindingMap.clear(); 00360 } 00361 //----------------------------------------------------------------------------- 00362 const VertexBufferBinding::VertexBufferBindingMap& 00363 VertexBufferBinding::getBindings(void) const 00364 { 00365 return mBindingMap; 00366 } 00367 //----------------------------------------------------------------------------- 00368 HardwareVertexBufferSharedPtr VertexBufferBinding::getBuffer(unsigned short index) 00369 { 00370 VertexBufferBindingMap::iterator i = mBindingMap.find(index); 00371 if (i == mBindingMap.end()) 00372 { 00373 Except(Exception::ERR_ITEM_NOT_FOUND, "No buffer is bound to that index.", 00374 "VertexBufferBinding::getBuffer"); 00375 } 00376 return i->second; 00377 } 00378 //----------------------------------------------------------------------------- 00379 HardwareVertexBufferSharedPtr::HardwareVertexBufferSharedPtr(HardwareVertexBuffer* buf) 00380 : SharedPtr<HardwareVertexBuffer>(buf) 00381 { 00382 00383 } 00384 00385 00386 00387 00388 }
Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:17 2004