xoreos  0.0.5
indexbuffer.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cstdlib>
26 #include <cstring>
27 
29 
30 namespace Graphics {
31 
32 IndexBuffer::IndexBuffer() : _count(0), _size(0), _type(GL_UNSIGNED_INT), _data(0), _ibo(0), _hint(GL_STATIC_DRAW) {
33 }
34 
35 IndexBuffer::IndexBuffer(const IndexBuffer &other) : _data(0), _ibo(0), _hint(GL_STATIC_DRAW) {
36  *this = other;
37 }
38 
40  destroyGL();
41  delete[] _data;
42 }
43 
45  if (this == &other)
46  return *this;
47 
48  setSize(other._count, other._size, other._type);
49 
50  /* _data, _count, _size and _type are always only ever set by the setSize()
51  * method which is also called above to replicate the situation in the right-
52  * hand side IndexBuffer of the assignment. _data is set to a new allocated
53  * block of memory of count * size size, but only if both count and size > 0.
54  *
55  * We can never run into a situation here where _data == 0 and other._data
56  * != 0 or vice versa. Either both are == 0 (in which case count * size == 0)
57  * or both are != 0. */
58 
59  if (_data && other._data)
60  memcpy(_data, other._data, other._count * other._size);
61 
62  return *this;
63 }
64 
65 void IndexBuffer::setSize(uint32 indexCount, uint32 indexSize, GLenum indexType) {
66  _count = indexCount;
67  _size = indexSize;
68  _type = indexType;
69 
70  delete[] _data;
71  _data = 0;
72 
73  if (_count && _size) {
74  _data = new byte[_count * _size];
75  }
76 }
77 
79  return static_cast<GLvoid *>(_data);
80 }
81 
82 const GLvoid *IndexBuffer::getData() const {
83  return static_cast<const GLvoid *>(_data);
84 }
85 
87  return _count;
88 }
89 
90 GLenum IndexBuffer::getType() const {
91  return _type;
92 }
93 
94 void IndexBuffer::initGL(GLuint hint) {
95  if (_ibo != 0) {
96  return; // Already initialised.
97  }
98 
99  _hint = hint;
100  if (_count) {
101  glGenBuffers(1, &_ibo);
102  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
103  glBufferData(GL_ELEMENT_ARRAY_BUFFER, _count * _size, _data, _hint);
104  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // Return to default buffer.
105  }
106 }
107 
109  if (_count) {
110  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
111  glBufferData(GL_ELEMENT_ARRAY_BUFFER, _count * _size, _data, _hint);
112  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // Return to default buffer. Maybe this isn't required.
113  }
114 }
115 
117  if (_ibo != 0) {
118  glDeleteBuffers(1, &_ibo);
119  _ibo = 0;
120  }
121 }
122 
123 GLuint IndexBuffer::getIBO() const {
124  return _ibo;
125 }
126 
127 } // End of namespace Graphics
GLvoid * getData()
Access buffer data.
Definition: indexbuffer.cpp:78
void destroyGL()
Clear (destroy) GL resources associated with the buffer.
A index buffer.
GLuint _ibo
"Index" Buffer Object.
Definition: indexbuffer.h:75
void updateGL()
Update existing GL buffer object.
GLuint _hint
GL hint for static or dynamic data.
Definition: indexbuffer.h:76
uint32 _count
Number of elements in buffer.
Definition: indexbuffer.h:70
void initGL(GLuint hint=GL_STATIC_DRAW)
Initialise internal buffer object for GL handling.
Definition: indexbuffer.cpp:94
byte * _data
Buffer data.
Definition: indexbuffer.h:73
GLenum getType() const
Get element type.
Definition: indexbuffer.cpp:90
uint32 getCount() const
Get element count.
Definition: indexbuffer.cpp:86
uint32 _size
Size of a buffer element in bytes.
Definition: indexbuffer.h:71
uint32_t uint32
Definition: types.h:204
Buffer containing indices data.
Definition: indexbuffer.h:33
GLuint getIBO() const
void setSize(uint32 indexCount, uint32 indexSize, GLenum indexType)
Change buffer size.
Definition: indexbuffer.cpp:65
uint8 byte
Definition: types.h:209
GLenum _type
Element type (GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, ...).
Definition: indexbuffer.h:72
IndexBuffer & operator=(const IndexBuffer &other)
Definition: indexbuffer.cpp:44