# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/GLBufferObject.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #ifndef GLBufferObject_INCLUDE_ONCE
    00033 #define GLBufferObject_INCLUDE_ONCE
    00034
    00035 #include <vlCore/Vector2.hpp>
    00036 #include <vlCore/Vector3.hpp>
    00037 #include <vlCore/Vector4.hpp>
    00038 #include <vlCore/Buffer.hpp>
    00039 #include <vlGraphics/OpenGL.hpp>
    00040 #include <vlCore/vlnamespace.hpp>
    00041 #include <vlCore/Vector4.hpp>
    00042 #include <vlCore/Sphere.hpp>
    00043 #include <vlCore/AABB.hpp>
    00044
    00045 namespace vl
    00046 {
    00047 //-----------------------------------------------------------------------------
    00048 // GLBufferObject
    00049 //-----------------------------------------------------------------------------
    00055   class GLBufferObject: public Buffer
    00056   {
    00057   public:
    00058     virtual const char* className() { return "vl::GLBufferObject"; }
    00059     GLBufferObject()
    00060     {
    00061       VL_DEBUG_SET_OBJECT_NAME()
    00062       mHandle = 0;
    00063       mUsage = BU_STATIC_DRAW;
    00064       mByteCountGPU = 0;
    00065     }
    00066
    00067     GLBufferObject(const GLBufferObject& other): Buffer(other)
    00068     {
    00069       VL_DEBUG_SET_OBJECT_NAME()
    00070       mHandle = 0;
    00071       mUsage = BU_STATIC_DRAW;
    00072       mByteCountGPU = 0;
    00073       // copy local data
    00074       *this = other;
    00075     }
    00076
    00077     // deletes the GPU data and copyes only the local data
    00078     GLBufferObject& operator=(const GLBufferObject& other)
    00079     {
    00080       deleteGLBufferObject();
    00081       Buffer::operator=(other);
    00082       return *this;
    00083     }
    00084
    00085     // swaps the gpu data and update state, as well as local data
    00086     void swap(GLBufferObject& other)
    00087     {
    00088       // swap local data
    00089       Buffer::swap(other);
    00090       // tmp
    00091       unsigned int tmp_handle = mHandle;
    00092       EGLBufferUsage tmp_usage = mUsage;
    00093       GLsizeiptr tmp_bytes = mByteCountGPU;
    00094       // this <- other
    00095       mHandle = other.mHandle;
    00096       mUsage = tmp_usage;
    00097       mByteCountGPU = other.mByteCountGPU;
    00098       // other <- this
    00099       other.mHandle = tmp_handle;
    00100       other.mUsage = tmp_usage;
    00101       other.mByteCountGPU = tmp_bytes;
    00102     }
    00103
    00104     ~GLBufferObject()
    00105     {
    00106       deleteGLBufferObject();
    00107     }
    00108
    00109     void setHandle(unsigned int handle) { mHandle = handle; }
    00110
    00111     unsigned int handle() const { return mHandle; }
    00112
    00113     GLsizeiptr byteCountGPU() const { return mByteCountGPU; }
    00114
    00115     void createGLBufferObject()
    00116     {
    00117       VL_CHECK(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0)
    00118       if(!(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0))
    00119         return;
    00120       if (handle() == 0)
    00121       {
    00122         VL_CHECK(mByteCountGPU == 0)
    00123         VL_glGenBuffers( 1, &mHandle );
    00124         mByteCountGPU = 0;
    00125       }
    00126       VL_CHECK(handle())
    00127     }
    00128
    00129     void deleteGLBufferObject()
    00130     {
    00131       if (handle() != 0)
    00132       {
    00133         VL_glDeleteBuffers( 1, &mHandle );
    00134         mHandle = 0;
    00135         mByteCountGPU = 0;
    00136       }
    00137     }
    00138
    00139     void downloadGLBufferObject()
    00140     {
    00141       if ( handle() )
    00142       {
    00143         resize( byteCountGPU() );
    00144         void* gpu_ptr = mapGPUBuffer(GBA_READ_ONLY);
    00145         memcpy( ptr(), gpu_ptr, byteCountGPU() );
    00146         unmapGPUBuffer();
    00147       }
    00148     }
    00149
    00150     // initializes the GPU Buffer from the local storage
    00151     // attention: discarding the local storage might delete data used by other interfaces
    00152     void setBufferData( EGLBufferUsage usage, bool discard_local_storage=false )
    00153     {
    00154       setBufferData( (int)bytesUsed(), ptr(), usage );
    00155       mUsage = usage;
    00156       if (discard_local_storage)
    00157         clear();
    00158     }
    00159
    00160     // modifies the GPU Buffer from the local storage
    00161     // attention: discarding the local storage might delete data used by other interfaces
    00162     void setBufferSubData( GLintptr offset=0, GLsizeiptr byte_count=-1, bool discard_local_storage=false )
    00163     {
    00164       byte_count = byte_count < 0 ? byteCountGPU() : byte_count;
    00165       setBufferSubData( offset, byte_count, ptr() );
    00166       if (discard_local_storage)
    00167         clear();
    00168     }
    00169
    00170     // if data == NULL the buffer will be allocated but no data will be writte into
    00171     // if data != must point to a buffer of at least 'size' bytes
    00172     void setBufferData( GLsizeiptr byte_count, const GLvoid* data, EGLBufferUsage usage )
    00173     {
    00174       VL_CHECK_OGL();
    00175       VL_CHECK(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0)
    00176       if(!(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0))
    00177         return;
    00178       //if (!data || !byte_count)
    00179       //  return;
    00180       createGLBufferObject();
    00181       // we use the GL_ARRAY_BUFFER slot to send the data for no special reason
    00182       VL_glBindBuffer( GL_ARRAY_BUFFER, handle() );
    00183       VL_glBufferData( GL_ARRAY_BUFFER, byte_count, data, usage );
    00184       VL_glBindBuffer( GL_ARRAY_BUFFER, 0 );
    00185       mByteCountGPU = byte_count;
    00186       mUsage = usage;
    00187       VL_CHECK_OGL();
    00188     }
    00189
    00190     void setBufferSubData( GLintptr offset, GLsizeiptr byte_count, const GLvoid* data )
    00191     {
    00192       VL_CHECK(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0)
    00193       if(!(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0))
    00194         return;
    00195       createGLBufferObject();
    00196       // we use the GL_ARRAY_BUFFER slot to send the data for no special reason
    00197       VL_glBindBuffer( GL_ARRAY_BUFFER, handle() );
    00198       VL_glBufferSubData( GL_ARRAY_BUFFER, offset, byte_count, data );
    00199       VL_glBindBuffer( GL_ARRAY_BUFFER, 0 );
    00200     }
    00201
    00202     // there can be only one GLBufferObject mapped at a time?
    00203     // you must unmapGPUBuffer before using the GPU Buffer again
    00204     void* mapGPUBuffer(EGLBufferAccess access)
    00205     {
    00206       VL_CHECK(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0)
    00207       if(!(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0))
    00208         return NULL;
    00209       createGLBufferObject(); VL_CHECK_OGL();
    00210       VL_glBindBuffer( GL_ARRAY_BUFFER, handle() ); VL_CHECK_OGL();
    00211       void* ptr = VL_glMapBuffer( GL_ARRAY_BUFFER, access ); VL_CHECK_OGL();
    00212       VL_glBindBuffer( GL_ARRAY_BUFFER, 0 );
    00213       return ptr;
    00214     }
    00215
    00216     // From OpenGL 3.0 specs:
    00217     // "UnmapBuffer returns TRUE unless data values in the buffer’s data store have
    00218     // become corrupted during the period that the buffer was mapped. Such corruption
    00219     // can be the result of a screen resolution change or other window system-dependent
    00220     // event that causes system heaps such as those for high-performance graphics memory
    00221     // to be discarded. GL implementations must guarantee that such corruption can
    00222     // occur only during the periods that a buffer’s data store is mapped. If such corruption
    00223     // has occurred, UnmapBuffer returns FALSE, and the contents of the buffer’s
    00224     // data store become undefined."
    00225     bool unmapGPUBuffer()
    00226     {
    00227       VL_CHECK(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0)
    00228       if(!(GLEW_ARB_vertex_buffer_object||GLEW_VERSION_1_5||GLEW_VERSION_3_0))
    00229         return false;
    00230       VL_CHECK_OGL();
    00231       createGLBufferObject();
    00232       VL_glBindBuffer( GL_ARRAY_BUFFER, handle() );
    00233       bool ok = VL_glUnmapBuffer( GL_ARRAY_BUFFER ) == GL_TRUE;
    00234       VL_CHECK(ok);
    00235       VL_glBindBuffer( GL_ARRAY_BUFFER, 0 );
    00236       VL_CHECK_OGL();
    00237       return ok;
    00238     }
    00239
    00240     EGLBufferUsage usage() const { return mUsage; }
    00241
    00242   protected:
    00243     unsigned int mHandle;
    00244     GLsizeiptr mByteCountGPU;
    00245     EGLBufferUsage mUsage;
    00246   };
    00247 }
    00248
    00249 #endif

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:23.
Permission is granted to use this page to write and publish articles regarding Visualization Library.