-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.cpp
45 lines (35 loc) · 939 Bytes
/
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
#include "stdafx.h"
#include <tiny_obj_loader.h>
#include "mesh.h"
#include "renderer.h"
MeshData MeshData::Load(std::string path)
{
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err;
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, path.c_str());
std::vector<mesh_vertex> vertices;
std::vector<uint16_t> indices;
auto& shape = shapes.front();
vertices.reserve(attrib.vertices.size() / 3);
for (auto it = attrib.vertices.begin(); it != attrib.vertices.end(); std::advance(it, 3))
{
vertices.push_back(
mesh_vertex{
glm::vec3{
*it,
*std::next(it),
*std::next(it,2)
},
glm::vec3{0.0f,1.0f,0.0f}
}
);
}
indices.reserve(shape.mesh.indices.size());
for (auto& shape : shape.mesh.indices)
{
indices.push_back(shape.vertex_index);
}
return {std::move(path), std::move(vertices), std::move(indices)};
}