-
Notifications
You must be signed in to change notification settings - Fork 1
/
objparser.cpp
133 lines (113 loc) · 4.14 KB
/
objparser.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
#include "objparser.h"
/*OBJ Parser.*/
/* 'v' signifies a vertex so make all the verticies
* 'vt' texture coordinate of one vertex
* 'vn' the normal of the vertex
* 'f' is a face
* - 3 groups of 3 numbers ##/##/##
- each ##/##/## describes a vertex
- 01/##/## says the index in the line vertex to use. (indexing starts with 1 and NOT 0) - 3 floats
- ##/02/## says the texture coord to use 2 floats
- ##/##/03 says the normal to use for this vertex
* Plan of attack : go through each line and save all the verts in an array <vector>
- save all the normals in a separate array <vector>
- go throught the f lines and assign the normals to each vertex
- make a local Geo. pos = verts[f[0] - 1]
normal = normals[f[2] - 1]
*/
ObjParser::ObjParser() {
}
ObjParser::ObjParser(string ifile) {
file = ifile;
}
vector<Primitive*> ObjParser::parse() {
vector<Primitive*> triangles;
ifstream infile;
infile.open(file.c_str());
const int num_line_chars = 50;
//cout << "Open file a second time.\n";
string linecontents;
char currLine[256];
int numLines;
if (!infile.good()) {
cout << "Error Reading File :/ Sucks to be you.\n";
} else {
while (!infile.eof()) {
//Grab the first line
getline(infile, linecontents);
// If a vertex and not a normal or texture coordinate
if ((linecontents[0] == 'v') && (linecontents[1] == ' ')) {
vector<float> vertex;
string currflt = "";
for(int i=2; i<linecontents.size();i++) { // ignore the v
if (linecontents[i] == ' ') {
try {
vertex.push_back(atof(currflt.c_str()));
} catch (int e) {
continue;
}
currflt = "";
} else {
currflt.push_back(linecontents[i]);
}
}
//cout << "last Elem = " << currflt << endl;
vertex.push_back(atof(currflt.c_str()));// this gets the last element
// cout << "Vertex " << verticies.size() + 1 << ": ( " << vertex[0] << ", " << vertex[1] << ", " << vertex[2] << ")" << endl;
verticies.push_back(vertex);
// if a face
} else if (linecontents[0] == 'f') {
vector<vector<float> > face; // holds the verticies
string currflt = "";
for(int i=2; i<linecontents.size();i++) { // ignore the f char
//ignore the values for textures and normals
if (linecontents[i] == '/') {
try {
int index = atoi(currflt.c_str()) - 1;
face.push_back(verticies[index]);
} catch (int e) {
}
//skip to the next white space
while(linecontents[i] != ' ') {
i++;
}
//reset the current string
currflt = "";
//If this is the value that we want
} else if (linecontents[i] == ' ') {
try {
int index = atoi(currflt.c_str()) - 1;
face.push_back(verticies[index]);
} catch (int e) {
}
currflt = "";
} else {
currflt.push_back(linecontents[i]);
}
}
// Adding the last vertex to the current face vector
try {
int index = atoi(currflt.c_str()) - 1;
face.push_back(verticies[index]);// this gets the last element
} catch (int e) {
}
//push this face to the list of faces
Vector vert1 = Vector(); vert1.x = face[0][0]; vert1.y = face[0][1]; vert1.z = face[0][2];
Vector vert2 = Vector(); vert2.x = face[1][0]; vert2.y = face[1][1]; vert2.z = face[1][2];
Vector vert3 = Vector(); vert3.x = face[2][0]; vert3.y = face[2][1]; vert3.z = face[2][2];
Triangle triangle = Triangle(vert1, vert2, vert3);
triangles.push_back(&triangle);
faces.push_back(face);
// cout << endl;
// cout << "Triangle " << triangles.size() << ": " << endl;
// // for(int k = 0; k<.size(); k++) {
// cout << "Vertex Num 1" << " : (" << triangle.v1.x << ", " << triangle.v1.y << ", " << triangle.v1.z << ")" << endl;
// cout << "Vertex Num 2" << " : (" << triangle.v2.x << ", " << triangle.v2.y << ", " << triangle.v2.z << ")" << endl;
// cout << "Vertex Num 3" << " : (" << triangle.v3.x << ", " << triangle.v3.y << ", " << triangle.v3.z << ")" << endl;
}
}
}
infile.close();
// cout << "SEgfault before returning the triangle list from oobjparser\n";
return triangles;
}