-
Notifications
You must be signed in to change notification settings - Fork 0
/
meshHandler.h
83 lines (69 loc) · 2.17 KB
/
meshHandler.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
#ifndef MESHHANDLER_H
#define MESHHANDLER_H
#include <glad/glad.h>
#include <vector>
#include <string>
#include <stdexcept>
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"
#include "vertex.h"
class Mesh {
public:
glm::mat4 mat;
Mesh(const char* path) {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err;
if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &err, path)) {
throw std::runtime_error(err);
}
for (const auto& shape : shapes) {
for (const auto& index : shape.mesh.indices) {
vertex vert = {};
vert.pos = {
glm::vec3(attrib.vertices[3 * index.vertex_index + 0],
attrib.vertices[3 * index.vertex_index + 1],
attrib.vertices[3 * index.vertex_index + 2])
};
vert.normal = {
glm::vec3(attrib.normals[3 * index.normal_index + 0],
attrib.normals[3 * index.normal_index + 1],
attrib.normals[3 * index.normal_index + 2])
};
vert.tex = {
glm::vec2(attrib.texcoords[2 * index.texcoord_index + 0],
attrib.texcoords[2 * index.texcoord_index + 1])
};
vertices.push_back(vert);
}
}
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//Render mesh.
void render() {
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
}
//Dispose of VAO and VBO.
void terminator() {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
}
private:
unsigned int VBO, VAO;
std::vector<vertex> vertices;
};
#endif