-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobjLoader.cpp
More file actions
231 lines (203 loc) · 7.93 KB
/
objLoader.cpp
File metadata and controls
231 lines (203 loc) · 7.93 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "objLoader.hpp"
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "toolbox.hpp"
#include <assert.h>
//#define DEBUG
ObjLoader objectLoader;
ObjLoader::ObjLoader()
{
fprintf(stderr, "Objloader under replication...\n");
}
ObjLoader::~ObjLoader()
{
fprintf(stderr, "Objectloader being sold to Russians...\n");
}
// Open a .obj, load its contents into 3 arrays:
// 1. Vertices
// 2. Normals
// 3. Indices
// Returns a vector of vectors containing this data
std::vector<std::vector<double> > ObjLoader::readObjectFile(std::string path)
{
int const bufferSize(1024);
char lineBuffer[bufferSize];
std::string line = "";
int lineNumber = 0;
int i = 0;
std::fstream dataReader(path.c_str());
std::string decimalNumberString = "";
std::string integerNumberString = "";
int coordinatesAdded = 0;
double x, y, z = 0.0;
int a, b, c = 0;
std::vector<double> vertexData; // Read vertexes here
std::vector<double> normalData; // Read normals here
std::vector<double> indiceData; // Read indices here
std::vector<std::vector<double> > out; // Return in this container
if (!dataReader)
std::cout << "Error opening model file: " << path.c_str() << std::endl;
while (!dataReader.eof())
{
#ifdef DEBUG
std::cout << "Linebreak" << std::endl;
#endif
dataReader.getline(lineBuffer, bufferSize);
line = lineBuffer;
if (line[0] == 'v' && line[1] == ' ') // Vertex
{
coordinatesAdded = 0;
decimalNumberString = "";
// Start reading from the first number. Skip the leading v and whitespace
for (int i = 2; coordinatesAdded <= 3; i++)
{
if (line[i] == NULL)
break;
// If we are at a new coordinate
if (line[i] == ' ' ||
i == line.size() - 1)
{
// Convert the string into a double
if (coordinatesAdded == 0)
x = atof(decimalNumberString.c_str());
else if (coordinatesAdded == 1)
y = atof(decimalNumberString.c_str());
else if (coordinatesAdded == 2)
z = atof(decimalNumberString.c_str());
coordinatesAdded++;
decimalNumberString = "";
}
// Add the number or decimal dot to the string to be converted
decimalNumberString.append(tbox.charToString(line[i]));
if (coordinatesAdded > 3 && i > 40)
{
std::cout << "Something broke reading a 3D object. Consult your 3D model exporter" << std::endl;
break;
}
}
vertexData.push_back(x);
vertexData.push_back(y);
vertexData.push_back(z);
} else if (line[0] == 'v' && line[1] == 'n') // Vertex normal
{
coordinatesAdded = 0;
decimalNumberString = "";
// Start reading from the first number. Skip the leading v and whitespace
for (int i = 3; coordinatesAdded < 3; i++)
{
if (line[i] == NULL)
break;
// If we are at a new coordinate
if (line[i] == ' ')
{
// Convert the string into a double
if (coordinatesAdded == 0)
x = atof(decimalNumberString.c_str());
else if (coordinatesAdded == 1)
y = atof(decimalNumberString.c_str());
else if (coordinatesAdded == 2)
z = atof(decimalNumberString.c_str());
coordinatesAdded++;
decimalNumberString = "";
}
// Add the number or decimal dot to the string to be converted
decimalNumberString.append(tbox.charToString(line[i]));
if (coordinatesAdded > 3 && i > 40)
{
std::cout << "Something broke reading a 3D object. Consult your 3D model exporter" << std::endl;
break;
}
}
normalData.push_back(x);
normalData.push_back(y);
normalData.push_back(z);
} else if (line[0] == 'f' && line[1] == ' ') // Index
{
/*
The syntax for indices is as follows:
f 14//14474 4483//14474 7448//14474
Where the first value is the vertex index and the following is probably a texture index.
There is one value missing, and thus there are two "/" in sequence
*/
coordinatesAdded = 0;
integerNumberString = "";
bool valueAdded = false;
a = 0;
b = 0;
c = 0;
// Start reading from the first number. Skip the leading v and whitespace
for (int i = 2; coordinatesAdded < 3; i++)
{
if (line[i] == NULL)
break;
// If we are at a new coordinate
if (line[i] == ' ' || (coordinatesAdded == 2 && valueAdded == true) )
{
// Convert the string into an int
if (coordinatesAdded == 0)
{
a = atoi(integerNumberString.c_str());
#ifdef DEBUG
std::cout << "a: " << a << std::endl;
#endif
}
else if (coordinatesAdded == 1)
{
b = atoi(integerNumberString.c_str());
#ifdef DEBUG
std::cout << "b: " << b << std::endl;
#endif
}
else if (coordinatesAdded == 2)
{
c = atoi(integerNumberString.c_str());
#ifdef DEBUG
std::cout << "c: " << c << std::endl;
#endif
}
coordinatesAdded++;
integerNumberString = "";
valueAdded = false;
}
if (line[i] == '/')
{
valueAdded = true;
#ifdef DEBUG
std::cout << "Hit /" << std::endl;
#endif
}
// Add the number or decimal dot to the string to be converted
if (valueAdded == false)
{
#ifdef DEBUG
std::cout << "reading an integer into a string: " << line[i] << std::endl;
#endif
integerNumberString.append(tbox.charToString(line[i]));
}
if (coordinatesAdded > 3 && i > 40)
{
std::cout << "Something broke reading a 3D object. Consult your 3D model exporter" << std::endl;
break;
}
}
assert(a);
assert(b);
assert(c);
#ifdef DEBUG
std::cout << "Pushing indices to vector: " << a << ", " << b << ", " << c << std::endl;
#endif
indiceData.push_back(a-1);
indiceData.push_back(b-1);
indiceData.push_back(c-1);
} else {
// Non-interesting data
}
}
out.push_back(vertexData);
out.push_back(normalData);
out.push_back(indiceData);
dataReader.close();
return out;
}