-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathVertexBuffer.cpp
More file actions
337 lines (260 loc) · 7.98 KB
/
Copy pathVertexBuffer.cpp
File metadata and controls
337 lines (260 loc) · 7.98 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <cpp3ds/Graphics/VertexBuffer.hpp>
#include <cpp3ds/Graphics/RenderTarget.hpp>
#include <cpp3ds/OpenGL.hpp>
#include <cpp3ds/System/Mutex.hpp>
#include <cpp3ds/System/Lock.hpp>
#include <cpp3ds/System/Err.hpp>
namespace
{
// Thread-safe unique identifier generator,
// is used for states cache (see RenderTarget)
cpp3ds::Uint64 getUniqueId()
{
static cpp3ds::Uint64 id = 1; // start at 1, zero is "no buffer"
static cpp3ds::Mutex mutex;
cpp3ds::Lock lock(mutex);
return id++;
}
}
namespace cpp3ds
{
////////////////////////////////////////////////////////////
VertexBuffer::VertexBuffer() :
VertexContainer(0),
m_vertices (),
m_primitiveType(Triangles),
m_bufferObject (0),
m_cacheId (getUniqueId()),
m_needUpload (true)
{
create();
}
////////////////////////////////////////////////////////////
VertexBuffer::VertexBuffer(PrimitiveType type, unsigned int vertexCount) :
VertexContainer(0),
m_vertices (vertexCount),
m_primitiveType(type),
m_bufferObject (0),
m_cacheId (getUniqueId()),
m_needUpload (true)
{
create();
}
////////////////////////////////////////////////////////////
VertexBuffer::VertexBuffer(const VertexBuffer& copy) :
VertexContainer(0),
m_vertices (copy.m_vertices),
m_primitiveType(copy.m_primitiveType),
m_bufferObject (0),
m_cacheId (getUniqueId()),
m_needUpload (true)
{
create();
}
////////////////////////////////////////////////////////////
VertexBuffer::~VertexBuffer()
{
// Destroy buffer object
if (m_bufferObject)
{
ensureGlContext();
GLuint bufferObject = static_cast<GLuint>(m_bufferObject);
glCheck(glDeleteBuffers(1, &bufferObject));
}
}
////////////////////////////////////////////////////////////
bool VertexBuffer::create()
{
// First make sure that we can use vertex buffers
if (!isAvailable())
{
err() << "Failed to create a vertex buffer: your system doesn't support vertex buffers "
<< "(you should test VertexBuffer::isAvailable() before trying to create a VertexBuffer object)" << std::endl;
return false;
}
// Create the OpenGL buffer object if it doesn't exist yet
if (!m_bufferObject)
{
GLuint bufferObject;
glCheck(glGenBuffers(1, &bufferObject));
m_bufferObject = static_cast<unsigned int>(bufferObject);
}
m_needUpload = true;
return true;
}
////////////////////////////////////////////////////////////
unsigned int VertexBuffer::getVertexCount() const
{
return static_cast<unsigned int>(m_vertices.size());
}
////////////////////////////////////////////////////////////
Vertex& VertexBuffer::operator [](unsigned int index)
{
m_needUpload = true;
return m_vertices[index];
}
////////////////////////////////////////////////////////////
const Vertex& VertexBuffer::operator [](unsigned int index) const
{
return m_vertices[index];
}
////////////////////////////////////////////////////////////
void VertexBuffer::clear()
{
if (!m_vertices.empty())
m_needUpload = true;
m_vertices.clear();
}
////////////////////////////////////////////////////////////
void VertexBuffer::resize(unsigned int vertexCount)
{
if (m_vertices.size() != vertexCount)
m_needUpload = true;
m_vertices.resize(vertexCount);
}
////////////////////////////////////////////////////////////
void VertexBuffer::append(const Vertex& vertex)
{
m_needUpload = true;
m_vertices.push_back(vertex);
}
////////////////////////////////////////////////////////////
void VertexBuffer::setPrimitiveType(PrimitiveType type)
{
m_primitiveType = type;
}
////////////////////////////////////////////////////////////
PrimitiveType VertexBuffer::getPrimitiveType() const
{
return m_primitiveType;
}
////////////////////////////////////////////////////////////
FloatBox VertexBuffer::getBounds() const
{
if (!m_vertices.empty())
{
float left = m_vertices[0].position.x;
float top = m_vertices[0].position.y;
float front = m_vertices[0].position.z;
float right = m_vertices[0].position.x;
float bottom = m_vertices[0].position.y;
float back = m_vertices[0].position.z;
for (std::size_t i = 1; i < m_vertices.size(); ++i)
{
Vector3f position = m_vertices[i].position;
// Update left and right
if (position.x < left)
left = position.x;
else if (position.x > right)
right = position.x;
// Update top and bottom
if (position.y < top)
top = position.y;
else if (position.y > bottom)
bottom = position.y;
// Update front and back
if (position.z < front)
front = position.z;
else if (position.z > back)
back = position.z;
}
return FloatBox(left, top, front, right - left, bottom - top, back - front);
}
else
{
// Buffer is empty
return FloatBox();
}
}
////////////////////////////////////////////////////////////
void* VertexBuffer::getPointer()
{
m_needUpload = true;
return &m_vertices[0];
}
////////////////////////////////////////////////////////////
const void* VertexBuffer::getPointer() const
{
return &m_vertices[0];
}
////////////////////////////////////////////////////////////
unsigned int VertexBuffer::getBufferObjectName() const
{
return m_bufferObject;
}
////////////////////////////////////////////////////////////
VertexBuffer& VertexBuffer::operator =(const VertexBuffer& right)
{
VertexBuffer temp(right);
std::swap(m_vertices, temp.m_vertices);
std::swap(m_primitiveType, temp.m_primitiveType);
m_cacheId = getUniqueId();
m_needUpload = true;
return *this;
}
////////////////////////////////////////////////////////////
void VertexBuffer::draw(RenderTarget& target, RenderStates states) const
{
target.draw(*this, states);
}
////////////////////////////////////////////////////////////
void VertexBuffer::bind(const VertexBuffer* buffer)
{
bind(buffer, GL_ARRAY_BUFFER);
}
////////////////////////////////////////////////////////////
void VertexBuffer::bind(const VertexBuffer* buffer, unsigned int target)
{
ensureGlContext();
if (buffer && buffer->m_bufferObject)
{
// Bind the buffer
glCheck(glBindBuffer(target, buffer->m_bufferObject));
if (buffer->m_needUpload)
{
// Orphan the buffer first to give the driver an opportunity to optimize streaming
glCheck(glBufferData(target, buffer->m_vertices.size() * sizeof(Vertex), NULL, GL_DYNAMIC_DRAW));
glCheck(glBufferData(target, buffer->m_vertices.size() * sizeof(Vertex), &(buffer->m_vertices[0]), GL_DYNAMIC_DRAW));
buffer->m_needUpload = false;
}
}
else
{
// Bind no buffer
glCheck(glBindBuffer(target, 0));
}
}
////////////////////////////////////////////////////////////
bool VertexBuffer::isAvailable()
{
static bool checked = false;
static bool bufferObjectsSupported = false;
if (!checked)
{
checked = true;
ensureGlContext();
// Make sure that GLEW is initialized
// priv::ensureGlewInit();
bufferObjectsSupported = true; //(GLEW_ARB_vertex_buffer_object != 0);
}
return bufferObjectsSupported;
}
////////////////////////////////////////////////////////////
bool VertexBuffer::hasVertexArrayObjects()
{
static bool checked = false;
static bool vertexArrayObjectsSupported = false;
if (!checked)
{
checked = true;
ensureGlContext();
// Make sure that GLEW is initialized
// priv::ensureGlewInit();
vertexArrayObjectsSupported = true; //(GLEW_ARB_vertex_array_object != 0);
}
return vertexArrayObjectsSupported;
}
} // namespace cpp3ds