git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / GLEXT / GLVertexBufferObject.cpp
blob5e976c4f575d52e7bbb100437d16cb078f68ce8d
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <GLEXT/GLVertexBufferObject.h>
22 #include <GLEXT/GLStateExtension.h>
23 #include <common/DefinesAssert.h>
24 #include <common/Logger.h>
26 GLVertexBufferObject::GLVertexBufferObject(bool indexbuffer) :
27 id_(0), size_(0), mapped_(false),
28 target_(indexbuffer ? GL_ELEMENT_ARRAY_BUFFER_ARB : GL_ARRAY_BUFFER_ARB)
30 DIALOG_ASSERT(GLStateExtension::hasVBO());
32 glGenBuffersARB(1, &id_);
35 GLVertexBufferObject::~GLVertexBufferObject()
37 if (mapped_) unmap();
38 glDeleteBuffersARB(1, &id_);
41 void GLVertexBufferObject::init_data(unsigned size, const void* data, int usage)
43 size_ = size;
44 bind();
45 glBufferDataARB(target_, size_, data, usage);
46 unbind();
49 void GLVertexBufferObject::init_sub_data(unsigned offset, unsigned subsize, const void* data)
51 bind();
52 glBufferSubDataARB(target_, offset, subsize, data);
53 unbind();
56 void GLVertexBufferObject::bind() const
58 glBindBufferARB(target_, id_);
61 void GLVertexBufferObject::unbind() const
63 glBindBufferARB(target_, 0);
66 void* GLVertexBufferObject::map(int access)
68 DIALOG_ASSERT(!mapped_); //("vertex buffer object mapped twice");
69 bind();
70 void* addr = glMapBufferARB(target_, access);
71 DIALOG_ASSERT(addr); // ("vertex buffer object mapping failed");
72 mapped_ = true;
74 return addr;
77 void GLVertexBufferObject::unmap()
79 DIALOG_ASSERT(mapped_) //("vertex buffer object not mapped before unmap()");
80 mapped_ = false;
81 bind();
83 if (glUnmapBufferARB(target_) != GL_TRUE)
85 Logger::log("failed to unmap Vertex Buffer object, data invalid");