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

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

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