-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathObjParser.h
159 lines (135 loc) · 3.67 KB
/
ObjParser.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
#ifndef OBJPARSER_H
#define OBJPARSER_H
#include <iostream>
#include <vector>
#include <map>
#include <fstream>
#include <string>
#include "Vec3.h"
#include "Shape.h"
using namespace std;
class ObjParser{
vector<Vec3f> points;
vector<Vec3f> normals;
vector<Vec3f> uv;
vector<face> faces;
map<Vec3f, vector<face>, Vec3Comp> vertexElements;
void clear(){
points.clear();
normals.clear();
uv.clear();
faces.clear();
vertexElements.clear();
}
void addVertexElements(face f){
Vec3f v0 = points.at(f.a);
Vec3f v1 = points.at(f.b);
Vec3f v2 = points.at(f.c);
Vec3f v3;
if(face::quad)
v3 = points.at(f.d);
if(vertexElements.find(v0) != vertexElements.end()){
vector<face>& tempList = vertexElements.at(v0);
tempList.push_back(f);
}else{
vector<face> temp;
temp.push_back(f);
vertexElements.insert(pair<Vec3f, vector<face> >(v0, temp));
}
if(vertexElements.find(v1) != vertexElements.end()){
vector<face>& tempList = vertexElements.at(v1);
tempList.push_back(f);
}else{
vector<face> temp;
temp.push_back(f);
vertexElements.insert(pair<Vec3f, vector<face> >(v1, temp));
}
if(vertexElements.find(v2) != vertexElements.end()){
vector<face>& tempList = vertexElements.at(v2);
tempList.push_back(f);
}else{
vector<face> temp;
temp.push_back(f);
vertexElements.insert(pair<Vec3f, vector<face> >(v2, temp));
}
if(face::quad){
if(vertexElements.find(v3) != vertexElements.end()){
vector<face>& tempList = vertexElements.at(v3);
tempList.push_back(f);
}else{
vector<face> temp;
temp.push_back(f);
vertexElements.insert(pair<Vec3f, vector<face> >(v3, temp));
}
}
}
public:
Shape parse(const char* filename){
clear();
cout<<"Loading mesh: "<<filename<<endl;
ifstream ifs(filename);
if(ifs.fail()){
cout<<"Error opening obj file!"<<endl;
exit(0);
}
string line;
char ss = ' ';
while(getline(ifs, line)){
if(line[0] == 'v' && line[1] == ' '){
Vec3f v;
sscanf(line.c_str(), "%c %f %f %f", &ss, &v.x, &v.y, &v.z);
points.push_back(v);
}else if(line[0] == 'v' && line[1] == 'n'){
Vec3f v;
sscanf(line.c_str(), "%c %f %f %f", &ss, &v.x, &v.y, &v.z);
normals.push_back(v);
}else if(line[0] == 'v' && line[1] == 't'){
Vec3f v;
sscanf(line.c_str(), "%c %f %f %f", &ss, &v.x, &v.y, &v.z);
uv.push_back(v);
}else if(line[0] == 'f'){
int slash_cnt = count(line.begin(), line.end(), '/');
face f;
if(slash_cnt == 4){
face::quad = true;
sscanf(line.c_str(), "%c %d/%d %d/%d %d/%d %d/%d", &ss, &f.a, &f.uv_a, &f.b, &f.uv_b, &f.c, &f.uv_c, &f.d, &f.uv_d);
f.a--; f.b--; f.c--; f.d--;
f.uv_a--; f.uv_b--; f.uv_c--; f.uv_d--;
Vec3f v0 = points.at(f.a);
Vec3f v1 = points.at(f.b);
Vec3f v2 = points.at(f.c);
Vec3f v3 = points.at(f.d);
Vec3f u0 = v1 - v0;
Vec3f u1 = v2 - v0;
Vec3f u2 = v3 - v0;
Vec3f n1 = cross(u0, u1);
Vec3f n2 = cross(u1, u2);
Vec3f n = (n1 + n2)/2;
n.normalize();
auto n_position = find(normals.begin(), normals.end(), n);
if(n_position == normals.end()){
normals.push_back(n);
f.n = normals.size() - 1;
}else{
f.n = n_position - normals.begin();
}
faces.push_back(f);
addVertexElements(f);
}
}else{
cout<<"This particular formal of Obj file is not supported at the moment."<<endl;
exit(1);
}
}
/*cout<<points.size()<<endl;
cout<<uv.size()<<endl;
cout<<faces.size()<<endl;
cout<<normals.size()<<endl;
cout<<vertexElements.size()<<endl;
cout<<vertexElements.at(points.at(100)).size()<<endl;*/
return Shape(points, normals, faces, vertexElements);
ifs.close();
}
};
#endif