GLVertexBufferObject.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <GLEXT/GLVertexBufferObject.h>
00022 #include <GLEXT/GLStateExtension.h>
00023 #include <common/DefinesAssert.h>
00024 #include <common/Logger.h>
00025 
00026 GLVertexBufferObject::GLVertexBufferObject(bool indexbuffer) :
00027         id_(0), size_(0), mapped_(false),
00028         target_(indexbuffer ? GL_ELEMENT_ARRAY_BUFFER_ARB : GL_ARRAY_BUFFER_ARB)
00029 {
00030         DIALOG_ASSERT(GLStateExtension::hasVBO());
00031 
00032         glGenBuffersARB(1, &id_);
00033 }
00034 
00035 GLVertexBufferObject::~GLVertexBufferObject()
00036 {
00037         if (mapped_) unmap();
00038         glDeleteBuffersARB(1, &id_);
00039 }
00040 
00041 void GLVertexBufferObject::init_data(unsigned size, const void* data, int usage)
00042 {
00043         size_ = size;
00044         bind();
00045         glBufferDataARB(target_, size_, data, usage);
00046         unbind();
00047 }
00048 
00049 void GLVertexBufferObject::init_sub_data(unsigned offset, unsigned subsize, const void* data)
00050 {
00051         bind();
00052         glBufferSubDataARB(target_, offset, subsize, data);
00053         unbind();
00054 }
00055 
00056 void GLVertexBufferObject::bind() const
00057 {
00058         glBindBufferARB(target_, id_);
00059 }
00060 
00061 void GLVertexBufferObject::unbind() const
00062 {
00063         glBindBufferARB(target_, 0);
00064 }
00065 
00066 void* GLVertexBufferObject::map(int access)
00067 {
00068         DIALOG_ASSERT(!mapped_); //("vertex buffer object mapped twice");
00069         bind();
00070         void* addr = glMapBufferARB(target_, access);
00071         DIALOG_ASSERT(addr); // ("vertex buffer object mapping failed");
00072         mapped_ = true;
00073 
00074         return addr;
00075 }
00076 
00077 void GLVertexBufferObject::unmap()
00078 {
00079         DIALOG_ASSERT(mapped_) //("vertex buffer object not mapped before unmap()");
00080         mapped_ = false;
00081         bind();
00082 
00083         if (glUnmapBufferARB(target_) != GL_TRUE) 
00084         {
00085                 Logger::log("failed to unmap Vertex Buffer object, data invalid");
00086         }
00087 }

Generated on Mon Feb 16 15:14:45 2009 for Scorched3D by  doxygen 1.5.3