-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelLoader.cpp
133 lines (107 loc) · 2.92 KB
/
modelLoader.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "modelLoader.h"
#include <cassert>
#include <stdexcept>
#include <sstream>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <glm/glm.hpp>
std::ostream& operator<<(std::ostream& stream, const aiVector3D& vec)
{
stream << "(" << vec.x << ", " << vec.y << ", " << vec.z << ")";
return stream;
}
glm::vec3 aiToGlm(const aiVector3D& vector)
{
return glm::vec3(vector.x, vector.y, vector.z);
}
Vertex::Vertex(
float x, float y, float z,
float nx, float ny, float nz,
float tanx, float tany, float tanz, float tanHandedness,
float u, float v):
_x(x),
_y(y),
_z(z),
_nx(nx),
_ny(ny),
_nz(nz),
_tanx(tanx),
_tany(tany),
_tanz(tanz),
_tanHandedness(tanHandedness),
_u(u),
_v(v)
{}
void loadModel(
const std::string& filename,
std::vector<Vertex>& vertices,
std::vector<GLuint>& indices)
{
assert (vertices.size() == 0);
assert (indices.size() == 0);
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(filename, aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_CalcTangentSpace );
if (!scene)
{
throw std::runtime_error("Nepodařilo se přečíst model");
}
if (scene->mNumMeshes != 1)
{
throw std::runtime_error("Jsou podporovány pouze modely s počtem meshů = 1");
}
const aiMesh* mesh = scene->mMeshes[0];
if (!mesh->HasFaces())
throw std::runtime_error("Mesh musí mít facy");
if (!mesh->HasPositions())
throw std::runtime_error("Mesh musí mít pozice");
if (!mesh->HasNormals())
throw std::runtime_error("Mesh musí mít normály");
if (!mesh->HasTextureCoords(0))
throw std::runtime_error("Mesh musí mít tex coordy");
if (mesh->GetNumUVChannels() != 1)
{
std::stringstream ss;
ss << "Mesh musí mít jeden UV kanál, ne " << mesh->GetNumUVChannels() << " kanálů";
throw std::runtime_error(ss.str());
}
vertices.reserve(mesh->mNumVertices);
// Prevedeme vertexy
for (unsigned i = 0; i < mesh->mNumVertices; i++)
{
auto position = mesh->mVertices[i];
auto normal = aiToGlm(mesh->mNormals[i]);
auto tangent = aiToGlm(mesh->mTangents[i]);
auto bitangent = aiToGlm(mesh->mBitangents[i]);
auto uvw = mesh->mTextureCoords[0][i];
tangent = glm::normalize(tangent - normal * glm::dot(normal, tangent));
auto tanHandedness = glm::dot(glm::cross(normal, tangent), bitangent) < 0.0f ? -1.0f : 1.0f;
vertices.push_back(Vertex(
position.x,
position.y,
position.z,
normal.x,
normal.y,
normal.z,
tangent.x,
tangent.y,
tangent.z,
tanHandedness,
uvw.x,
uvw.y));
}
indices.reserve(mesh->mNumFaces * 3);
// Prevedeme indexy
for (unsigned i = 0; i < mesh->mNumFaces; i++)
{
const aiFace face = mesh->mFaces[i];
if (face.mNumIndices != 3)
{
throw std::runtime_error("Jsou podporovány pouze facy se třemi body");
}
for (unsigned j = 0; j < face.mNumIndices; j++)
{
indices.push_back(face.mIndices[j]);
}
}
}