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

OgreGLHardwareIndexBuffer.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-2003 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 "OgreGLHardwareIndexBuffer.h"
00026 #include "OgreGLHardwareBufferManager.h"
00027 #include "OgreException.h"
00028 
00029 namespace Ogre {
00030 
00031     //---------------------------------------------------------------------
00032     GLHardwareIndexBuffer::GLHardwareIndexBuffer(IndexType idxType,
00033         size_t numIndexes, HardwareBuffer::Usage usage, bool useShadowBuffer)
00034         : HardwareIndexBuffer(idxType, numIndexes, usage, false, useShadowBuffer)
00035     {
00036         glGenBuffersARB_ptr( 1, &mBufferId );
00037 
00038         if (!mBufferId)
00039         {
00040             Except(Exception::ERR_INTERNAL_ERROR, 
00041                 "Cannot create GL index buffer", 
00042                 "GLHardwareIndexBuffer::GLHardwareIndexBuffer");
00043         }
00044 
00045         glBindBufferARB_ptr(GL_ARRAY_BUFFER_ARB, mBufferId);
00046 
00047         // Initialise buffer and set usage
00048         glBufferDataARB_ptr(GL_ARRAY_BUFFER_ARB, mSizeInBytes, NULL, 
00049             GLHardwareBufferManager::getGLUsage(usage));
00050 
00051         //std::cerr << "creating index buffer " << mBufferId << std::endl;
00052     }
00053     //---------------------------------------------------------------------
00054     GLHardwareIndexBuffer::~GLHardwareIndexBuffer()
00055     {
00056         glDeleteBuffersARB_ptr(1, &mBufferId);
00057     }
00058     //---------------------------------------------------------------------
00059     void* GLHardwareIndexBuffer::lockImpl(size_t offset, 
00060         size_t length, LockOptions options)
00061     {
00062         GLenum access = 0;
00063 
00064         if(mIsLocked)
00065         {
00066             Except(Exception::ERR_INTERNAL_ERROR, 
00067                 "Invalid attempt to lock an index buffer that has already been locked",
00068                     "GLHardwareIndexBuffer::lock");
00069         }
00070 
00071         glBindBufferARB_ptr( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
00072         
00073         if(options == HBL_DISCARD)
00074         {
00075 /*
00076             glBufferDataARB_ptr(GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, NULL, 
00077                 GLHardwareBufferManager::getGLUsage(mUsage));
00078 
00079 */
00080             access = (mUsage & HBU_DYNAMIC) ? GL_READ_WRITE_ARB : GL_WRITE_ONLY_ARB;
00081 
00082         }
00083         else if(options == HBL_READ_ONLY)
00084         {
00085             if(mUsage & HBU_WRITE_ONLY)
00086             {
00087                 Except(Exception::ERR_INTERNAL_ERROR, 
00088                     "Invalid attempt to lock a write-only index buffer as read-only",
00089                     "GLHardwareIndexBuffer::lock");
00090             }
00091             access = GL_READ_ONLY_ARB;
00092         }
00093         else if(options == HBL_NORMAL || options == HBL_NO_OVERWRITE)
00094         {
00095             access = (mUsage & HBU_DYNAMIC) ? GL_READ_WRITE_ARB : GL_WRITE_ONLY_ARB;
00096         }
00097         else
00098         {
00099             Except(Exception::ERR_INTERNAL_ERROR, 
00100                 "Invalid locking option set", "GLHardwareIndexBuffer::lock");
00101         }
00102 
00103         void* pBuffer = glMapBufferARB_ptr( GL_ELEMENT_ARRAY_BUFFER_ARB, access );
00104 
00105         if(pBuffer == 0)
00106         {
00107             Except(Exception::ERR_INTERNAL_ERROR, 
00108                 "Index Buffer: Out of memory", 
00109                 "GLHardwareIndexBuffer::lock");
00110         }
00111 
00112         mIsLocked = true;
00113         // return offsetted
00114         return static_cast<void*>(
00115             static_cast<unsigned char*>(pBuffer) + offset);
00116     }
00117     //---------------------------------------------------------------------
00118     void GLHardwareIndexBuffer::unlockImpl(void)
00119     {
00120         glBindBufferARB_ptr( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
00121 
00122         if(!glUnmapBufferARB_ptr( GL_ELEMENT_ARRAY_BUFFER_ARB ))
00123         {
00124             Except(Exception::ERR_INTERNAL_ERROR, 
00125                 "Buffer data corrupted, please reload", 
00126                 "GLHardwareIndexBuffer::unlock");
00127         }
00128 
00129         mIsLocked = false;
00130     }
00131     //---------------------------------------------------------------------
00132     void GLHardwareIndexBuffer::readData(size_t offset, size_t length, 
00133         void* pDest)
00134     {
00135         glBindBufferARB_ptr( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
00136         glGetBufferSubDataARB_ptr(GL_ELEMENT_ARRAY_BUFFER_ARB, offset, length, pDest);
00137     }
00138     //---------------------------------------------------------------------
00139     void GLHardwareIndexBuffer::writeData(size_t offset, size_t length, 
00140             const void* pSource, bool discardWholeBuffer)
00141     {
00142         glBindBufferARB_ptr( GL_ELEMENT_ARRAY_BUFFER_ARB, mBufferId );
00143 
00144         if(discardWholeBuffer)
00145         {
00146             glBufferDataARB_ptr( GL_ELEMENT_ARRAY_BUFFER_ARB, mSizeInBytes, NULL, 
00147                 GLHardwareBufferManager::getGLUsage(mUsage));
00148         }
00149 
00150         glBufferSubDataARB_ptr(GL_ELEMENT_ARRAY_BUFFER_ARB, offset, length, pSource);
00151     }
00152     //---------------------------------------------------------------------
00153 }

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