-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.cpp
104 lines (77 loc) · 2.4 KB
/
mesh.cpp
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
#include "mesh.h"
Mesh::Mesh(const std::string& lexical_name, std::uint32_t id, MeshCacheOption cache_option) : id_(id), vbo_name_(0), ibo_name_(0), indices_(nullptr),
vertices_(nullptr), cache_option_(cache_option),
initialized_(false), lexical_name_(lexical_name){
}
Mesh::~Mesh(){
if(vbo_name_ != 0){
glDeleteBuffers(1, &vbo_name_);
}
if(ibo_name_ != 0){
glDeleteBuffers(1, &ibo_name_);
}
}
GLuint Mesh::getVBO(){
if(vbo_name_ != 0){
return vbo_name_;
}
if(vertices_ == nullptr || indices_ == nullptr){
return 0;
}
initializeBuffers();
return vbo_name_;
}
GLuint Mesh::getIBO(){
if(ibo_name_ != 0){
return ibo_name_;
}
if(vertices_ == nullptr || indices_ == nullptr){
return 0;
}
initializeBuffers();
return ibo_name_;
}
std::uint32_t Mesh::getID(){
return id_;
}
std::vector<GLuint>* Mesh::getIndices(){
return indices_.get();
}
std::string Mesh::getLexicalName(){
return lexical_name_;
}
std::vector<VertexData>* Mesh::getVertices(){
return vertices_.get();
}
bool Mesh::setMeshData(std::unique_ptr<std::vector<VertexData> >&& vertices, std::unique_ptr<std::vector<GLuint> >&& indices){
if(initialized_){
return false;
}
vertices_ = std::move(vertices);
indices_ = std::move(indices);
num_indices_ = indices_->size();
num_vertices_ = vertices->size();
return true;
}
void Mesh::initializeBuffers(){
assert(vertices_ != nullptr && indices_ != nullptr);
glGenBuffers(1, &vbo_name_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_name_);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData) * vertices_->size(), &(*vertices_)[0], GL_STATIC_DRAW);
glGenBuffers(1, &ibo_name_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_name_);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices_->size(), &(*indices_)[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
if(cache_option_ == DELETE_ON_BUFFER_CREATION){
vertices_ = nullptr;
indices_ = nullptr;
}
initialized_ = true;
}
size_t Mesh::getNumIndices(){
return num_indices_;
}
size_t Mesh::getNumVertices(){
return num_vertices_;
}