Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfElementBuffer.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-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 "glfElementBuffer.hh"
27 #include "glfException.hh"
28 
29 using namespace BIAS;
30 
32 {
33  numIndices_ = 0;
34  typeSize_ = 0;
35  useBuffer_ = false;
36  bufferID_ = 0;
37  mapped_ = false;
38 }
39 
41 {
42  if (bufferID_ != 0) {
43  glDeleteBuffers(1, &bufferID_);
44  }
45 }
46 
47 
49 glfElementBuffer::operator=(const glfElementBuffer& b)
50 {
51  return (*this);
52 }
53 
55 {}
56 
58 Create(int numIndices,
59  GLenum type,
60  GLenum mode, const void* indices)
61 {
62  BIASASSERT(!mapped_);
63 
64  mode_ = mode;
65  type_ = type;
66  numIndices_ = numIndices;
67 
68  switch (type) {
69  case GL_UNSIGNED_BYTE:
70  typeSize_ = sizeof(GLubyte);
71  break;
72  case GL_UNSIGNED_SHORT:
73  typeSize_ = sizeof(GLushort);
74  break;
75  case GL_UNSIGNED_INT:
76  typeSize_ = sizeof(GLuint);
77  break;
78  }
79 
80  // check whether to use OpenGL buffers.
81  useBuffer_ = false;
82 #ifdef BIAS_HAVE_GLEW
83  if (GLEW_VERSION_1_5) {
84  useBuffer_ = true;
85  }
86 #endif
87 
88  if (useBuffer_) {
89 
90  // upload to OpenGL buffer
91  if (bufferID_ == 0) {
92  glGenBuffers(1, &bufferID_);
93  }
94  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferID_);
95  glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * typeSize_,
96  indices, GL_STATIC_DRAW);
97 
98  } else {
99 
100  // create a copy of data in system memory
101  array_.resize(numIndices * typeSize_);
102  memcpy(&array_[0], indices, numIndices * typeSize_);
103 
104  }
105 
106  GLF_THROW_ON_OPENGL_ERROR;
107 }
108 
110 {
111  BIASASSERT(!mapped_);
112  mapped_ = true;
113 
114  if (useBuffer_) {
115  BIASASSERT(bufferID_ != 0);
116  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferID_);
117  return glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
118  } else {
119  return &array_[0];
120  }
121 }
122 
124 {
125  BIASASSERT(mapped_);
126  mapped_ = false;
127 
128  if (useBuffer_) {
129  BIASASSERT(bufferID_ != 0);
130  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferID_);
131  GLboolean result = glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
132  if (result == GL_FALSE) {
133  GLF_THROW_EXCEPTION("Element buffer was corrupted while mapped");
134  }
135  }
136 }
137 
138 void glfElementBuffer::Draw(int first, int count) const
139 {
140  BIASASSERT(!mapped_);
141  BIASASSERT(first >= 0 && first < numIndices_);
142  BIASASSERT(count == -1 || (count >= 0 && (first+count) <= numIndices_));
143 
144  if (count == -1) {
145  count = numIndices_;
146  first = 0;
147  }
148 
149  const void* indices = NULL;
150  if (useBuffer_) {
151  BIASASSERT(bufferID_ != 0);
152  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferID_);
153  } else {
154  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
155  indices = &array_[0];
156  }
157 
158  indices = (GLubyte*)indices + (first*typeSize_);
159 
160  glDrawElements(mode_, count, type_, indices);
161 
162  GLF_THROW_ON_OPENGL_ERROR;
163 }
An element buffer contains vertex indices that form primitives.
void Unmap()
Unmaps the element buffer.
void * Map()
Maps the element buffer to system memory, so it can be modified after creation.
void Create(int numIndices, GLenum type, GLenum mode, const void *indices)
Creates the element buffer for the given number of indices.
void Draw(int first=0, int count=-1) const
Draw elements from the buffer.