-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexBuffer.h
executable file
·94 lines (88 loc) · 2.9 KB
/
IndexBuffer.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
#ifndef _585_INDEXBUFFER_H_
#define _585_INDEXBUFFER_H_
#include "GLee.h"
#include <vector>
#include <cassert>
/// The C type of the indices in the index buffer. Smaller types save GPU and main memory.
typedef unsigned short IndexType;
/// The IndexType as OpenGL understands it.
#define GL_INDEX_TYPE GL_UNSIGNED_SHORT
class IndexBuffer {
public:
/// Constant index operator, for fetching a single index
IndexType operator[](size_t index) const {
assert(index >= 0 && index < this->size);
return this->rawStorage[index];
}
/// Non-constant operator, for writing or reading a single index
IndexType& operator[](size_t index) {
assert(index >= 0 && index < this->size);
return this->rawStorage[index];
}
/// Write the index buffer to the GPU, allocating the space we need
void Commit() const {
this->Bind();
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this->size * sizeof(IndexType), rawStorage, GL_STATIC_DRAW_ARB);
}
/// Bind the index buffer to the GPU state, preparing it for rendering
void Bind() const {
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this->handle);
}
/**
\brief Draw all elements of the bound vertex buffer using this (bound) index buffer
\param primitiveType The GL primitive type to draw
*/
void DrawAll(GLenum primitiveType = GL_TRIANGLES) const {
glDrawElements(primitiveType, this->size, GL_INDEX_TYPE, NULL);
}
/**
\brief Draw a selected set of elements on the bound vertex buffer using the (bound) index buffer
\param primitiveType The GL primitive type to draw
\param startIndex The index in the index buffer to start drawing at
\param vertexCount The number of indices in the index buffer to use (i.e. the number of vertices drawn)
*/
void DrawRange(GLenum primitiveType, unsigned int startIndex, unsigned int vertexCount) const {
glDrawRangeElements(primitiveType, startIndex, startIndex + vertexCount, vertexCount, GL_INDEX_TYPE, NULL);
}
/**
\brief Instantiate the index buffer.
\param size The number of indices to be stored in the buffer.
*/
IndexBuffer(unsigned int size) {
assert(size > 0);
this->handle = 0;
this->size = size;
// Allocate shadow storage
this->rawStorage = new IndexType[this->size];
for(size_t i = 0; i < this->size; i++) {
this->rawStorage[i] = 0;
}
glGenBuffersARB(1, &this->handle);
}
/// Destroy the index buffer, its shadow array, its handle and its storage on GPU
~IndexBuffer() {
if(this->handle != 0) {
glDeleteBuffersARB(1, &this->handle);
}
delete[] this->rawStorage;
}
public:
/**
\brief Set the index buffer from a vector of indices. Will not commit.
\param data The vector of indices.
*/
void SetData(const std::vector<IndexType>& data) {
assert(data.size() <= this->size);
for(unsigned int i = 0; i < data.size(); i++) {
this->rawStorage[i] = data[i];
}
}
unsigned int getSize() {
return size;
}
private:
GLuint handle;
unsigned int size;
IndexType* rawStorage;
};
#endif