-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjReader.h
More file actions
95 lines (70 loc) · 2.11 KB
/
ObjReader.h
File metadata and controls
95 lines (70 loc) · 2.11 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
#include <fstream>
#include <unordered_map>
#include "Engine/Renderer/RenderTypes.h"
struct ObjFace {
unsigned int v_idx = 0;
unsigned int vt_idx = 0;
unsigned int vn_idx = 0;
bool operator == (const ObjFace& face) const noexcept {
return v_idx == face.v_idx && vt_idx == face.vt_idx && vn_idx == face.vn_idx;
}
Vertex GenerateVertex(
std::vector<glm::vec3>& o_vertex,
std::vector<glm::vec3>& o_vnormal,
std::vector<glm::vec2>& o_texcord){
Vertex v;
//handle negative index
if(v_idx < 0) {
v_idx = o_vertex.size() + v_idx -1;
}
if(vt_idx < 0) {
vt_idx = o_vertex.size() + vt_idx -1;
}
if(vn_idx < 0) {
vn_idx = o_vertex.size() + vn_idx -1;
}
//Fetch data
if(v_idx > 0){
v.pos = o_vertex[v_idx-1];
}
if(vt_idx > 0){
v.texCord = o_texcord[vt_idx-1];
}
if(vn_idx > 0){
v.normal = o_vnormal[vn_idx-1];
}
v.color = glm::vec4(1.0f);
return v;
}
};
namespace std {
template<>
struct hash<ObjFace> {
std::size_t operator()(const ObjFace& face) const noexcept {
std::size_t v = std::hash<unsigned int>{}(face.v_idx);
std::size_t vt = std::hash<unsigned int>{}(face.vt_idx);
std::size_t vn = std::hash<unsigned int>{}(face.vn_idx);
return v^(vt << 1)^(vn << 2);
}
};
}
struct ObjMaterial {
glm::vec4 color;
bool hasColor;
};
class ObjReader {
private:
bool m_ObjParam[4];
public:
//Should return a mesh
enum ObjData{
VERTEX, TEXCORD, NORMAL, COLOR
};
ObjReader();
~ObjReader();
void SetReadParam(ObjData param, bool value);
void NormalizeMesh(Mesh& mesh);
std::vector<ObjFace> ReadFaceData(std::string& line);
Mesh ReadObject(const std::string& path);
std::unordered_map<std::string, ObjMaterial> ReadMaterial(const std::string& path);
};