Back to index

nux  3.0.0
IOpenGLVertexBuffer.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "GpuDevice.h"
00024 #include "GLDeviceObjects.h"
00025 #include "IOpenGLVertexBuffer.h"
00026 
00027 namespace nux
00028 {
00029 
00030   NUX_IMPLEMENT_OBJECT_TYPE(IOpenGLVertexBuffer);
00031 
00032   IOpenGLVertexBuffer::IOpenGLVertexBuffer(unsigned int Length, VBO_USAGE Usage, NUX_FILE_LINE_DECL)
00033     :   IOpenGLResource(RTVERTEXBUFFER, NUX_FILE_LINE_PARAM)
00034     ,   _Length(Length)
00035     ,   _Usage(Usage)
00036     ,   _MemMap(0)
00037     ,   _OffsetToLock(0)
00038     ,   _SizeToLock(0)
00039   {
00040     CHECKGL(glGenBuffersARB(1, &_OpenGLID));
00041     CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, _OpenGLID));
00042     CHECKGL(glBufferDataARB(GL_ARRAY_BUFFER_ARB, _Length, NULL, Usage));
00043     CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
00044     GRunTimeStats.Register(this);
00045   }
00046 
00047   IOpenGLVertexBuffer::~IOpenGLVertexBuffer()
00048   {
00049     CHECKGL(glDeleteBuffersARB(1, &_OpenGLID));
00050     _OpenGLID = 0;
00051     GRunTimeStats.UnRegister(this);
00052   }
00053 
00054   int IOpenGLVertexBuffer::Lock(
00055     unsigned int OffsetToLock,
00056     unsigned int SizeToLock,
00057     void **ppbData)
00058   {
00059     nuxAssert(SizeToLock <= _Length);
00060     nuxAssert(OffsetToLock + SizeToLock <= _Length);
00061 
00062     if (SizeToLock == 0)
00063     {
00064       if (OffsetToLock == 0)
00065       {
00066         // lock the entire buffer
00067         SizeToLock = _Length;
00068       }
00069       else
00070       {
00071         nuxDebugMsg("[IOpenGLVertexBuffer::Lock] Invalid parameters.");
00072         return OGL_INVALID_CALL;
00073       }
00074     }
00075 
00076     // If _MemMap, _OffsetToLock and _SizeToLock are not equal to zero, then we have already mapped the buffer
00077     // Unlock it before locking again.
00078     nuxAssert(_MemMap == 0);
00079     nuxAssert(_OffsetToLock == 0);
00080     nuxAssert(_SizeToLock == 0);
00081 
00082     CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, _OpenGLID));
00083 #ifndef NUX_OPENGLES_20
00084     // Map the Entire buffer into system memory
00085     _MemMap = (BYTE *) glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
00086     CHECKGL_MSG(glMapBufferARB);
00087     *ppbData = (void *) (_MemMap + OffsetToLock);
00088 #else
00089     _MemMap = new BYTE[SizeToLock];
00090     *ppbData = _MemMap;
00091 #endif
00092 
00093     _OffsetToLock   = OffsetToLock;
00094     _SizeToLock     = SizeToLock;
00095 
00096     CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
00097 
00098     return OGL_OK;
00099   }
00100 
00101   int IOpenGLVertexBuffer::Unlock()
00102   {
00103     CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, _OpenGLID));
00104 
00105 #ifndef NUX_OPENGLES_20
00106     CHECKGL(glUnmapBufferARB(GL_ARRAY_BUFFER_ARB));
00107 #else
00108     CHECKGL(glBufferSubData(GL_ARRAY_BUFFER_ARB, _OffsetToLock, _SizeToLock, _MemMap));
00109     delete [] _MemMap;
00110 #endif
00111     CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0));
00112 
00113     _MemMap         = 0;
00114     _OffsetToLock   = 0;
00115     _SizeToLock     = 0;
00116     return OGL_OK;
00117   }
00118 
00119   void IOpenGLVertexBuffer::BindVertexBuffer()
00120   {
00121     CHECKGL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, _OpenGLID));
00122   }
00123 
00124   unsigned int IOpenGLVertexBuffer::GetSize()
00125   {
00126     return _Length;
00127   }
00128 
00129 }