Back to index

nux  3.0.0
IOpenGLIndexBuffer.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 "IOpenGLIndexBuffer.h"
00026 
00027 namespace nux
00028 {
00029 
00030   NUX_IMPLEMENT_OBJECT_TYPE(IOpenGLIndexBuffer);
00031 
00032   IOpenGLIndexBuffer::IOpenGLIndexBuffer(unsigned int Length, VBO_USAGE Usage, INDEX_FORMAT Format, NUX_FILE_LINE_DECL)
00033     :   IOpenGLResource(RTINDEXBUFFER, NUX_FILE_LINE_PARAM)
00034     ,   _Length(Length)
00035     ,   _Format(Format)
00036     ,   _Usage(Usage)
00037     ,   _MemMap(0)
00038     ,   _OffsetToLock(0)
00039     ,   _SizeToLock(0)
00040   {
00041     CHECKGL(glGenBuffersARB(1, &_OpenGLID));
00042     CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, _OpenGLID));
00043     CHECKGL(glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, _Length, NULL, Usage));
00044     CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
00045     GRunTimeStats.Register(this);
00046   }
00047 
00048   IOpenGLIndexBuffer::~IOpenGLIndexBuffer()
00049   {
00050     CHECKGL(glDeleteBuffersARB(1, &_OpenGLID));
00051     _OpenGLID = 0;
00052     GRunTimeStats.UnRegister(this);
00053   }
00054 
00055   int IOpenGLIndexBuffer::Lock(
00056     unsigned int OffsetToLock,
00057     unsigned int SizeToLock,
00058     void **ppbData)
00059   {
00060     nuxAssert(SizeToLock <= _Length);
00061     nuxAssert(OffsetToLock + SizeToLock <= _Length);
00062 
00063     if (SizeToLock == 0)
00064     {
00065       if (OffsetToLock == 0)
00066       {
00067         // lock the entire buffer
00068         SizeToLock = _Length;
00069       }
00070       else
00071         return OGL_INVALID_CALL;
00072     }
00073 
00074     // If _MemMap, _OffsetToLock and _SizeToLock are not equal to zero, then we have already mapped the buffer
00075     // Unlock it before locking again.
00076     nuxAssert(_MemMap == 0);
00077     nuxAssert(_OffsetToLock == 0);
00078     nuxAssert(_SizeToLock == 0);
00079 
00080     CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, _OpenGLID));
00081     // Map the Entire buffer into system memory
00082 #ifndef NUX_OPENGLES_20
00083     _MemMap = (BYTE *) glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); // todo: use Format instead of GL_WRITE_ONLY_ARB
00084     CHECKGL_MSG(glMapBufferARB);
00085     *ppbData = (void *) (_MemMap + OffsetToLock);
00086 #else
00087     _MemMap = new BYTE[SizeToLock];
00088     *ppbData = _MemMap;
00089 #endif
00090 
00091     _OffsetToLock   = OffsetToLock;
00092     _SizeToLock     = SizeToLock;
00093 
00094     CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
00095 
00096     return OGL_OK;
00097   }
00098 
00099   int IOpenGLIndexBuffer::Unlock()
00100   {
00101     nuxAssert(_MemMap != 0);
00102     nuxAssert(_SizeToLock != 0);
00103 
00104     // No need to bind
00105     CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, _OpenGLID));
00106 
00107 #ifndef NUX_OPENGLES_20
00108     CHECKGL(glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB));
00109 #else
00110     CHECKGL(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER_ARB, _OffsetToLock, _SizeToLock, _MemMap));
00111     delete [] _MemMap;
00112 #endif
00113 
00114     CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0));
00115 
00116     _MemMap         = 0;
00117     _OffsetToLock   = 0;
00118     _SizeToLock     = 0;
00119 
00120     return OGL_OK;
00121   }
00122 
00123   int IOpenGLIndexBuffer::GetStride()
00124   {
00125     if (_Format == INDEX_FORMAT_USHORT)
00126     {
00127       return 2;
00128     }
00129     else if (_Format == INDEX_FORMAT_UINT)
00130     {
00131       return 4;
00132     }
00133     else
00134       return 0;
00135   }
00136 
00137   void IOpenGLIndexBuffer::BindIndexBuffer()
00138   {
00139     CHECKGL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, _OpenGLID));
00140   }
00141 
00142   unsigned int IOpenGLIndexBuffer::GetSize()
00143   {
00144     return _Length;
00145   }
00146 
00147 }