-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.cpp
112 lines (95 loc) · 2.64 KB
/
Model.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
105
106
107
108
109
110
111
//
// Created by adamyuan on 4/18/18.
//
#include "Model.hpp"
#include "Config.hpp"
#include <GLFW/glfw3.h>
#define TINYOBJLOADER_IMPLEMENTATION
#include <tiny_obj_loader.h>
void Mesh::Load()
{
object.Initialize();
object.SetData(vertices, GL_STATIC_DRAW);
object.SetAttributes(0, 3, 1, 3, 2, 2);
vertices.clear();
vertices.shrink_to_fit();
}
void Model::Load(const char *filename)
{
double start = glfwGetTime();
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, filename))
return;
if (!err.empty())
{
printf("Error when loading model: %s\n", err.c_str());
return;
}
meshes_.resize(materials.size());
// Loop over shapes
for(const auto &shape : shapes)
{
size_t index_offset = 0, face = 0;
// Loop over faces(polygon)
for(const auto &num_face_vertex : shape.mesh.num_face_vertices)
{
// Loop over vertices in the face.
for (size_t v = 0; v < num_face_vertex; v++)
{
// access to vertex
tinyobj::index_t index = shape.mesh.indices[index_offset + v];
Vertex vertex{};
vertex.position = {
attrib.vertices[3 * index.vertex_index + 0],
attrib.vertices[3 * index.vertex_index + 1],
attrib.vertices[3 * index.vertex_index + 2]
};
vertex.normal = {
attrib.normals[3 * index.normal_index + 0],
attrib.normals[3 * index.normal_index + 1],
attrib.normals[3 * index.normal_index + 2]
};
vertex.texcoords = {
attrib.texcoords[2 * index.texcoord_index + 0],
1.0f - attrib.texcoords[2 * index.texcoord_index + 1]
};
vertex.position /= kScale;
meshes_[shape.mesh.material_ids[face]].vertices.push_back(vertex);
}
index_offset += num_face_vertex;
face ++;
}
}
for(size_t i = 0; i < materials.size(); ++i)
{
meshes_[i].diffuse_texture = load_texture(materials[i].diffuse_texname);
meshes_[i].Load();
}
printf("Loading model lasted: %lf sec\n", glfwGetTime() - start);
}
void Model::Render() const
{
for(const Mesh &m : meshes_)
{
glBindTextureUnit(0, m.diffuse_texture);
m.object.Render(GL_TRIANGLES);
}
}
GLuint Model::load_texture(const std::string &filename)
{
auto iter = textures_.find(filename);
if(iter == textures_.end())
{
textures_.insert(std::make_pair(filename, mygl3::Texture2D{}));
iter = textures_.find(filename);
iter->second.Initialize();
iter->second.Load(mygl3::ImageLoader(filename.c_str()).GetInfo(), true);
iter->second.GenerateMipmap();
iter->second.SetWrapFilter(GL_REPEAT);
iter->second.SetSizeFilter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
}
return iter->second.Get();
}