Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfVertexBuffer.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <cstring>
26 #include "glfVertexBuffer.hh"
27 #include "glfException.hh"
28 
29 using namespace BIAS;
30 
32 {
33  useBuffer_ = false;
34  numVertices_ = 0;
35  bufferID_ = 0;
36  mapped_ = false;
37  array_ = NULL;
38 }
39 
41 {
42  if (bufferID_ != 0) {
43  glDeleteBuffers(1, &bufferID_);
44  }
45  delete [] array_;
46 }
47 
48 
50 glfVertexBuffer::operator=(const glfVertexBuffer& b)
51 {
52  return (*this);
53 }
54 
56 {}
57 
58 
59 void glfVertexBuffer::Create(int numVertices,
60  const glfVertexFormat& format,
61  const void* data,
62  bool useBuffer)
63 {
64  BIASASSERT(!mapped_);
65 
66  format_ = format;
67  numVertices_ = numVertices;
68 
69  // check whether to use OpenGL buffers.
70  useBuffer_ = false;
71 #ifdef BIAS_HAVE_GLEW
72  if (GLEW_VERSION_1_5) {
73  useBuffer_ = true & useBuffer;
74  }
75 #endif
76 
77  if (useBuffer_) {
78 
79  // upload to OpenGL buffer
80  if (bufferID_ == 0) {
81  glGenBuffers(1, &bufferID_);
82  }
83  glBindBuffer(GL_ARRAY_BUFFER, bufferID_);
84  glBufferData(GL_ARRAY_BUFFER,
85  numVertices * format.GetVertexSize(),
86  data, GL_STATIC_DRAW);
87  } else {
88 
89  // create a copy of data in system memory
90  // array_.resize(numVertices * format.GetVertexSize());
91  delete[] array_;
92  array_ = new GLubyte[numVertices * format.GetVertexSize()];
93  memcpy(array_, data, numVertices * format.GetVertexSize());
94  }
95 
96  GLF_THROW_ON_OPENGL_ERROR;
97 }
98 
100 {
101  BIASASSERT(!mapped_);
102  mapped_ = true;
103 
104  if (useBuffer_) {
105  BIASASSERT(bufferID_ != 0);
106  glBindBuffer(GL_ARRAY_BUFFER, bufferID_);
107  return glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
108  } else {
109  return array_;
110  }
111 }
112 
114 {
115  BIASASSERT(mapped_);
116  mapped_ = false;
117 
118  if (useBuffer_) {
119  BIASASSERT(bufferID_ != 0);
120  glBindBuffer(GL_ARRAY_BUFFER, bufferID_);
121  GLboolean result = glUnmapBuffer(GL_ARRAY_BUFFER);
122  if (result == GL_FALSE) {
123  GLF_THROW_EXCEPTION("Vertex buffer was corrupted while mapped");
124  }
125  }
126 }
127 
129 {
130  return numVertices_;
131 }
132 
134 {
135  if (useBuffer_) {
136  BIASASSERT(bufferID_ != 0);
137  glBindBuffer(GL_ARRAY_BUFFER, bufferID_);
138  format_.Bind(NULL);
139  } else {
140  glBindBuffer(GL_ARRAY_BUFFER, 0);
141  format_.Bind(array_);
142  }
143 }
int GetNumVertices() const
Returns the number of vertices in the vertex buffer.
A vertex buffer contains an array of vertices that can be used for rendering.
void Unmap()
Unmaps the vertex buffer.
void * Map()
Maps the vertex buffer to system memory, so it can be modified after creation.
void Create(int numVertices, const glfVertexFormat &format, const void *data=NULL, bool useBufferIfPossible=true)
Creates the vertex buffer for the given number of vertices and vertex format.
void Bind() const
Binds the vertex buffer in OpenGL.
int GetVertexSize() const
Returns the size in bytes of a single vertex.
A vertex format describes the attributes of a vertex.
void Bind(const void *data) const
Binds a vertex buffer or vertex array in OpenGL using this vertex format.