-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh_data.cpp
89 lines (74 loc) · 2.88 KB
/
mesh_data.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
#include "mesh_data.h"
namespace graphics
{
model_data load_model(std::string path, glm::vec3 scale, glm::vec3 offset)
{
model_data result;
std::ifstream file(path, std::ifstream::in | std::ifstream::binary);
if (file.is_open())
{
unsigned int num_vertices = 0;
file.read((char*)&num_vertices, sizeof(num_vertices));
for (int idx = 0 ; idx < num_vertices ; idx++) {
vertex vert;
vert.deserialise(file);
vert.pos *= scale;
vert.pos += offset;
result.vertices.push_back(vert);
}
file.close();
}
return result;
}
vertex_mesh_data::vertex_mesh_data(std::vector<vertex> vertices)
{
m_num_vertices = vertices.size();
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex)*vertices.size(), vertices.data(), GL_STATIC_DRAW);
m_min_dims = glm::vec3(std::numeric_limits<float>::max());
m_max_dims = glm::vec3(std::numeric_limits<float>::min());
for (auto& vert : vertices) {
for (int idx = 0 ; idx < 3 ; idx++) {
m_min_dims[idx] = std::min(m_min_dims[idx], vert.pos[idx]);
m_max_dims[idx] = std::max(m_max_dims[idx], vert.pos[idx]);
}
}
}
vertex_mesh_data::~vertex_mesh_data()
{
glDeleteBuffers(1, &m_vbo);
glDeleteVertexArrays(1, &m_vao);
}
void vertex_mesh_data::draw()
{
glDrawArrays(GL_TRIANGLES, 0, m_num_vertices);
}
void vertex_mesh_data::refresh(std::shared_ptr<shader> shader)
{
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
shader->use();
{
GLint pos_attrib = glGetAttribLocation(shader->m_shader_program, "position");
GLint uv_attrib = glGetAttribLocation(shader->m_shader_program, "uv");
GLint normal_attrib = glGetAttribLocation(shader->m_shader_program, "normal");
if (pos_attrib != -1) {
glEnableVertexAttribArray(pos_attrib);
glVertexAttribPointer(pos_attrib, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), 0);
}
if (normal_attrib != -1) {
glEnableVertexAttribArray(normal_attrib);
glVertexAttribPointer(normal_attrib, 3, GL_FLOAT, GL_FALSE, sizeof(vertex),
(void*)sizeof(vertex::pos));
}
if (uv_attrib != -1) {
glEnableVertexAttribArray(uv_attrib);
glVertexAttribPointer(uv_attrib, 2, GL_FLOAT, GL_FALSE, sizeof(vertex),
(void*)(sizeof(vertex::pos) + sizeof(vertex::normal)));
}
}
}
}