-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjParser.cpp
More file actions
104 lines (90 loc) · 2.69 KB
/
Copy pathObjParser.cpp
File metadata and controls
104 lines (90 loc) · 2.69 KB
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <glm/glm.hpp>
#include "ObjParser.h"
#include "Models/Material.h"
////////////////////////////////////////////////////////////////////////////////
/// @brief Parse an obj file into a mesh
/// @param _filename Filename
/// @return Loaded mesh
Mesh
ObjParser::
parse_obj_file(const std::string& _filename) {
std::ifstream ifs(_filename);
std::string mtllib, mtl;
if(!ifs) {
return Mesh();
}
//std::cout << "Parsing: " << _filename << std::endl;
std::string line;
while(getline(ifs, line)) {
std::istringstream iss(line);
std::string tag;
iss >> tag;
if(tag == "v") {
glm::vec3 p;
iss >> p.x >> p.y >> p.z;
positions.emplace_back(p);
}
else if(tag == "vt") {
glm::vec2 t;
iss >> t.x >> t.y;
textures.emplace_back(t);
}
else if(tag == "vn") {
glm::vec3 n;
iss >> n.x >> n.y >> n.z;
normals.emplace_back(n);
}
else if(tag == "f") {
for(size_t i = 0; i < 3; ++i) {
std::string vert;
iss >> vert;
size_t p, t, n;
sscanf(vert.c_str(), "%zu/%zu/%zu", &p, &t, &n);
vertices.emplace_back(positions[p-1], normals[n-1], textures[t-1]);
}
}
else if (tag == "mtllib"){
iss >> mtllib;
}
else if (tag == "usemtl"){
iss >> mtl;
}
}
computeTangent();
// // debug info
// std::cout << "Number of positions: " << positions.size() << std::endl;
// std::cout << "Number of textures: " << textures.size() << std::endl;
// std::cout << "Number of normals: " << normals.size() << std::endl;
// std::cout << "Number of tangents: " << tangents.size() << std::endl;
// std::cout << "Number of faces: " << (vertices.size()/3) << std::endl;
return Mesh(vertices, Material(mtllib, mtl));
}
void
ObjParser::
computeTangent() {
for ( int i=0; i<vertices.size(); i+=3){
// Shortcuts for vertices
glm::vec3 & v0 = vertices[i+0].m_p;
glm::vec3 & v1 = vertices[i+1].m_p;
glm::vec3 & v2 = vertices[i+2].m_p;
// Shortcuts for UVs
glm::vec2 & uv0 = vertices[i+0].m_t;
glm::vec2 & uv1 = vertices[i+1].m_t;
glm::vec2 & uv2 = vertices[i+2].m_t;
// Edges of the triangle : position delta
glm::vec3 deltaPos1 = v1-v0;
glm::vec3 deltaPos2 = v2-v0;
// UV delta
glm::vec2 deltaUV1 = uv1-uv0;
glm::vec2 deltaUV2 = uv2-uv0;
float r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
glm::vec3 tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
vertices[i+0].m_tangent = tangent;
vertices[i+1].m_tangent = tangent;
vertices[i+2].m_tangent = tangent;
}
}