-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeom.cpp
70 lines (53 loc) · 1.88 KB
/
Geom.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
#include "Geom.h"
Geom::Geom(vector<float> verts, vector<unsigned int> elems, vector<float> uvs)
: vertexes(verts), elements(elems), uvs(uvs) {
init();
}
Geom::~Geom() {}
void Geom::init() {
D("Geom init")
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
}
void Geom::bufferElements() {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(unsigned int),
&elements.front(), drawType);
}
void Geom::bufferVertexData(shared_ptr<Shader> shader, vector<float> data) {
size_t dataSize = data.size() * sizeof(float);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, dataSize,
&data.front(), drawType);
GLint pos = glGetAttribLocation(shader->program, "in_Position");
glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
// glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(pos);
GLint uv = glGetAttribLocation(shader->program, "in_Texcoord0");
glVertexAttribPointer(uv, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)( 3 * sizeof(float) ));
glEnableVertexAttribArray(uv);
}
void Geom::combineVertexData(vector<float> * data) {
// what i know is i need to add 2 items for every 3
data->reserve(vertexes.size() + uvs.size());
for (int i = 0; i < vertexes.size() / 3; i++) {
for (int j = 0; j < 3; j++) {
data->push_back(vertexes[i*3+j]);
}
data->push_back(uvs[i*2]);
data->push_back(uvs[i*2+1]);
cout.flush();
}
}
void Geom::draw() {
glBindVertexArray(vao);
bufferElements();
auto shader = Shader::get(shaderName);
shader->use();
vector<float> combinedVertexData;
combineVertexData(&combinedVertexData);
bufferVertexData(shader, combinedVertexData);
callMaterial(shader);
glDrawElements(GL_TRIANGLES, elements.size(), GL_UNSIGNED_INT, 0);
}