-
Notifications
You must be signed in to change notification settings - Fork 47
/
IndexedVertexBuffer.h
240 lines (205 loc) · 6.35 KB
/
IndexedVertexBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#pragma once
#include "render.h"
#include <GL/glew.h>
#include "render/VBO.h"
#include "GLProgramAttributes.h"
namespace render
{
/**
* \brief
* Receiver of indexed vertex geometry for rendering
*
* An IndexedVertexBuffer stores a pool of vertex data, and receives batches of
* indices into the vertex buffer which are rendered with glDrawElements. It
* functions much like \a VertexBuffer except for indexed geometry rather than
* raw vertex geometry.
*/
template<typename Vertex_T>
class IndexedVertexBuffer final
{
public:
typedef std::vector<Vertex_T> Vertices;
typedef std::vector<RenderIndex> Indices;
private:
typedef VertexTraits<Vertex_T> Traits;
// OpenGL VBO
mutable GLuint _vertexVBO;
mutable GLuint _indexVBO;
// Vertex and index storage
Vertices _vertices;
Indices _indices;
// Batches of indices (start index and count)
struct Batch
{
std::size_t start;
std::size_t size;
};
// Batches
std::vector<Batch> _batches;
private:
void initialiseVBOs() const
{
_vertexVBO = makeVBOFromArray(GL_ARRAY_BUFFER, _vertices);
_indexVBO = makeVBOFromArray(GL_ELEMENT_ARRAY_BUFFER, _indices);
}
public:
/// Construct an empty IndexedVertexBuffer
IndexedVertexBuffer()
: _vertexVBO(0), _indexVBO(0)
{ }
/// Destroy resources
~IndexedVertexBuffer()
{
deleteVBO(_vertexVBO);
deleteVBO(_indexVBO);
}
/**
* \brief
* Add vertices to the IndexedVertexBuffer
*
* Unlike with VertexBuffer, adding vertices to an IndexedVertexBuffer does
* not create a batch. It just adds vertices to the common vertex pool.
*/
template<typename Iter_T>
void addVertices(Iter_T begin, Iter_T end)
{
std::copy(begin, end, std::back_inserter(_vertices));
}
typename Vertices::size_type getNumVertices() const
{
return _vertices.size();
}
/// Add a batch of indices
template<typename Iter_T>
void addIndexBatch(Iter_T begin, std::size_t count)
{
if (count < 1)
{
throw std::logic_error("Batch must contain at least one index");
}
// Create the new batch
Batch newBatch = { _indices.size(), count };
_batches.push_back(newBatch);
// Copy the indices
_indices.reserve(_indices.size() + count);
for (Iter_T i = begin; i < begin + count; ++i)
{
_indices.push_back(*i);
}
}
/// Add a batch of indices
template<typename Iter_T>
void addIndicesToLastBatch(Iter_T begin, std::size_t count, Indices::value_type offset = 0)
{
if (_batches.empty())
{
addIndexBatch(begin, count);
return;
}
if (count < 1)
{
throw std::logic_error("Batch must contain at least one index");
}
auto& batch = *_batches.rbegin();
batch.size += count;
// Append the indices
_indices.reserve(_indices.size() + count);
for (Iter_T i = begin; i < begin + count; ++i)
{
_indices.push_back(*i + offset);
}
// Invalidate the vertex and index buffers
deleteVBO(_vertexVBO);
deleteVBO(_indexVBO);
}
/**
* \brief
* Replace all data with that from another IndexedVertexBuffer
*
* \see VertexBuffer::replaceData
*/
void replaceData(const IndexedVertexBuffer& other)
{
replaceVBODataIfPossible(GL_ARRAY_BUFFER, _vertexVBO,
_vertices, other._vertices);
replaceVBODataIfPossible(GL_ELEMENT_ARRAY_BUFFER, _indexVBO,
_indices, other._indices);
_vertices = other._vertices;
_indices = other._indices;
_batches = other._batches;
}
/**
* \brief
* Render all batches with given primitive type
*
* \param renderBump
* True if tangent and bitangent vectors should be submitted for bump map
* rendering. If false, only regular normal vectors will be submitted. In
* all cases the pointers will only be set if carried by the particular
* vertex type.
*/
void renderAllBatches(GLenum primitiveType, bool renderBump = false) const
{
if (_vertexVBO == 0 || _indexVBO == 0)
{
initialiseVBOs();
}
glBindBuffer(GL_ARRAY_BUFFER, _vertexVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);
// Vertex pointer includes whole vertex buffer
const GLsizei STRIDE = sizeof(Vertex_T);
glVertexPointer(3, GL_DOUBLE, STRIDE, Traits::VERTEX_OFFSET());
// Set other pointers as necessary
if (Traits::hasTexCoord())
{
if (renderBump)
{
glVertexAttribPointer(
ATTR_TEXCOORD, 2, GL_DOUBLE, GL_FALSE,
STRIDE, Traits::TEXCOORD_OFFSET()
);
}
else
{
glTexCoordPointer(2, GL_DOUBLE, STRIDE,
Traits::TEXCOORD_OFFSET());
}
}
if (Traits::hasNormal())
{
if (renderBump)
{
glVertexAttribPointer(
ATTR_NORMAL, 3, GL_DOUBLE, GL_FALSE,
STRIDE, Traits::NORMAL_OFFSET()
);
}
else
{
glNormalPointer(GL_DOUBLE, STRIDE, Traits::NORMAL_OFFSET());
}
}
if (Traits::hasTangents() && renderBump)
{
glVertexAttribPointer(ATTR_TANGENT, 3, GL_DOUBLE, GL_FALSE,
STRIDE, Traits::TANGENT_OFFSET());
glVertexAttribPointer(ATTR_BITANGENT, 3, GL_DOUBLE, GL_FALSE,
STRIDE, Traits::BITANGENT_OFFSET());
}
// Render each batch of indices
for (typename std::vector<Batch>::const_iterator i = _batches.begin();
i != _batches.end();
++i)
{
glDrawElements(
primitiveType, GLint(i->size), RenderIndexTypeID,
reinterpret_cast<const GLvoid*>(
i->start * sizeof(Indices::value_type)
)
);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
};
}