nux-1.14.0
IOpenGLIndexBuffer.cpp
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 (t_u32 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     t_u32 OffsetToLock,
00057     t_u32 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     _MemMap = (BYTE *) glMapBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); // todo: use Format instead of GL_WRITE_ONLY_ARB
00083     CHECKGL_MSG (glMapBufferARB);
00084     *ppbData = (void *) (_MemMap + OffsetToLock);
00085 
00086     _OffsetToLock   = OffsetToLock;
00087     _SizeToLock     = SizeToLock;
00088 
00089     CHECKGL ( glBindBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, 0) );
00090 
00091     return OGL_OK;
00092   }
00093 
00094   int IOpenGLIndexBuffer::Unlock()
00095   {
00096     nuxAssert (_MemMap != 0);
00097     nuxAssert (_SizeToLock != 0);
00098 
00099     // No need to bind
00100     CHECKGL ( glBindBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, _OpenGLID) );
00101 
00102     _MemMap         = 0;
00103     _OffsetToLock   = 0;
00104     _SizeToLock     = 0;
00105 
00106     CHECKGL ( glUnmapBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB) );
00107     CHECKGL ( glBindBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, 0) );
00108 
00109     return OGL_OK;
00110   }
00111 
00112   int IOpenGLIndexBuffer::GetStride()
00113   {
00114     if (_Format == INDEX_FORMAT_USHORT)
00115     {
00116       return 2;
00117     }
00118     else if (_Format == INDEX_FORMAT_UINT)
00119     {
00120       return 4;
00121     }
00122     else
00123       return 0;
00124   }
00125 
00126   void IOpenGLIndexBuffer::BindIndexBuffer()
00127   {
00128     CHECKGL (glBindBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, _OpenGLID) );
00129   }
00130 
00131   t_u32 IOpenGLIndexBuffer::GetSize()
00132   {
00133     return _Length;
00134   }
00135 
00136 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends