forked from hkust-psan1/psan1-rendering-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
obj_file_processor.cpp
277 lines (236 loc) · 8.11 KB
/
obj_file_processor.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "obj_file_processor.h"
#include "utils.h"
const float pinRadius = 0.44; // from the obj file
const float pinDistance = 0.44 * 12 / (4.75 / 2); // 12 inches distance with 4.75 inches diameter
const float unitX = pinDistance * 1.73205 / 2;
const float unitZ = pinDistance / 2;
const float3 pinBasePosition = make_float3(10, 0, 0);
const float3 pinPositions[10] = {
make_float3(0, 0, 0) + pinBasePosition,
make_float3(unitX, 0, unitZ) + pinBasePosition,
make_float3(unitX, 0, - unitZ) + pinBasePosition,
make_float3(2 * unitX, 0, 2 * unitZ) + pinBasePosition,
make_float3(2 * unitX, 0, 0) + pinBasePosition,
make_float3(2 * unitX, 0, - 2 * unitZ) + pinBasePosition,
make_float3(3 * unitX, 0, - 3 * unitZ) + pinBasePosition,
make_float3(3 * unitX, 0, - 1 * unitZ) + pinBasePosition,
make_float3(3 * unitX, 0, 1 * unitZ) + pinBasePosition,
make_float3(3 * unitX, 0, 3 * unitZ) + pinBasePosition,
};
std::vector<SceneObject*> ObjFileProcessor::processObject(std::string filename, std::string targetDir) {
processObjFile(filename + ".obj", targetDir);
processMtlFile(filename + ".mtl", targetDir);
std::vector<SceneObject*> sceneObjects;
int pin_index = 0;
int droplet_index = 0;
for (auto it = objMtlMap.begin(); it != objMtlMap.end(); it++) {
std::cout << "Processing object: " << it->first << std::endl;
SceneObject* so = new SceneObject();
so->initGraphics(targetDir + it->first, targetDir + it->second, targetDir + "textures/");
sceneObjects.push_back(so);
// used for the kitchen scene
/*
if (it->first.find("Suzanne") != std::string::npos) {
Collider* c = new Collider;
c->setMass(1);
c->initPhysics(targetDir + it->first);
c->setInitialPosition(btVector3(0, 10, 0));
so->attachCollider(c);
} else if (it->first.find("Floor") != std::string::npos) {
Collider* c = new Collider;
c->setMass(0);
c->initPhysics(targetDir + it->first);
so->attachCollider(c);
}
*/
// used for the bowling scene
if (SCENE_NAME == "bowling") {
if (it->first.find("Pin") != std::string::npos) {
Collider* c = new Collider;
c->setMass(1);
c->initPhysics(targetDir + "../pin-phy.obj");
float3 pos = pinPositions[pin_index++];
c->setInitialPosition(btVector3(pos.x, pos.y, pos.z));
c->getRigidBody()->setRestitution(0.5);
so->attachCollider(c);
} else if (it->first.find("MainFloor") != std::string::npos) {
Collider* c = new Collider;
c->setMass(0);
c->initPhysics(targetDir + it->first);
c->getRigidBody()->setRestitution(0.5);
so->attachCollider(c);
} else if (it->first.find("BowlingBall") != std::string::npos) {
SphereCollider* sc = new SphereCollider(true);
sc->setMass(10);
sc->initPhysics(1);
sc->setInitialPosition(btVector3(-10, 2, 0));
sc->getRigidBody()->setLinearVelocity(btVector3(40, 0, 0.15));
sc->getRigidBody()->setRestitution(0.5);
so->attachCollider(sc);
}
} else if (SCENE_NAME == "bullet") {
if (it->first.find("Droplet") != std::string::npos) {
SphereCollider* sc = new SphereCollider();
sc->setMass(1);
sc->initPhysics(1);
sc->setInitialPosition(btVector3(random1() / 5, 10 + 3 * droplet_index++, random1() / 5));
sc->getRigidBody()->setRestitution(0.6);
so->attachCollider(sc);
/*
CylinderCollider* cc = new CylinderCollider();
cc->initPhysics(0.3, 0.95);
cc->setInitialPosition(btVector3(0, 10, 0));
cc->setInitialRotation(btQuaternion());
so->attachCollider(cc);
cc->getRigidBody()->setRestitution(0.6);
*/
} else if (it->first.find("Ground") != std::string::npos) {
Collider* c = new Collider;
c->setMass(0);
c->initPhysics(targetDir + it->first);
c->getRigidBody()->setRestitution(0.6);
so->attachCollider(c);
}
} else if (SCENE_NAME == "concave") {
if (it->first.find("Torus1") != std::string::npos) {
Collider* c = new Collider;
c->setMass(0);
c->initPhysics(targetDir + it->first);
c->getRigidBody()->setRestitution(0.6);
so->attachCollider(c);
} else if (it->first.find("Torus2") != std::string::npos) {
Collider* c = new Collider;
c->setMass(1);
c->initPhysics(targetDir + it->first);
c->getRigidBody()->setRestitution(0.6);
c->setInitialPosition(btVector3(0, -1, 0));
so->attachCollider(c);
} else if (it->first.find("Torus3") != std::string::npos) {
Collider* c = new Collider;
c->setMass(1);
c->initPhysics(targetDir + it->first);
c->getRigidBody()->setRestitution(0.6);
c->setInitialPosition(btVector3(0, -2, 0));
so->attachCollider(c);
}
}
// used for the throw scene
/*
if (it->first.find("Ground") != std::string::npos) {
Collider* c = new Collider;
c->setMass(0);
c->initPhysics(targetDir + it->first);
so->attachCollider(c);
} else if (it->first.find("Base") != std::string::npos) {
Collider* c = new Collider;
c->setMass(10);
c->initPhysics(targetDir + it->first);
so->attachCollider(c);
} else if (it->first.find("Pole") != std::string::npos) {
Collider* c = new Collider;
c->setMass(1);
c->initPhysics(targetDir + it->first);
c->setInitialPosition(btVector3(0, 2.1, 0));
so->attachCollider(c);
}
*/
}
return sceneObjects;
}
void ObjFileProcessor::processMtlFile(std::string filename, std::string targetDir) {
std::ifstream mtlInput(filename.c_str());
std::ofstream output;
std::string mtlName;
for (std::string line; getline(mtlInput, line); ) {
std::stringstream ss(line);
std::string part;
getline(ss, part, ' ');
if (part == "newmtl") {
getline(ss, part, '\n');
mtlName = part + ".mtl";
if (output) {
output.close();
}
output.open((targetDir + mtlName).c_str());
}
if (output) {
output << line << std::endl;
}
}
output.close();
}
void ObjFileProcessor::processObjFile(std::string filename, std::string targetDir) {
std::ifstream objInput(filename.c_str());
std::ofstream output;
std::string objName;
int vCount = 0, nCount = 0, tCount = 0;
int vOffset, nOffset, tOffset;
for (std::string line; getline(objInput, line); ) {
std::stringstream ss(line);
std::string part;
getline(ss, part, ' ');
if (part == "o") {
vOffset = vCount;
nOffset = nCount;
tOffset = tCount;
getline(ss, part, '\n');
objName = part + ".obj";
printf("parsing: %s\n", part.c_str());
if (output) { // output stream already initialized
output.close();
}
output.open((targetDir + objName).c_str());
output << line << std::endl;
} else if (part == "v") {
vCount++;
} else if (part == "vn") {
nCount++;
} else if (part == "vt") {
tCount++;
} else if (part == "f") {
// "part" is the line excluding leading "f"
getline(ss, part, '\n');
// each vSegment stands for a vertex in the format X/Y/Z
std::string vSegment;
// create a stringstream to handle "part", a series of vSegment
std::stringstream vStream(part);
// reset "line" to "f " and later add new face data to it
line = "";
std::stringstream newFaceStream;
newFaceStream << "f ";
while (getline(vStream, vSegment, ' ')) { // find all vertices of the face
std::string vStr, tStr, nStr;
// a stringstream for each vertex data item X/Y/Z
std::stringstream segmentStream(vSegment);
getline(segmentStream, vStr, '/');
getline(segmentStream, tStr, '/');
getline(segmentStream, nStr, '/');
if (nStr.empty()) { // cannot find normal vector
throw "normal vector not supplied";
}
int vIndex = atoi(vStr.c_str()) - vOffset;
int nIndex = atoi(nStr.c_str()) - nOffset;
int tIndex = -1;
if (!tStr.empty()) { // texture coord provided
tIndex = atoi(tStr.c_str()) - tOffset;
}
newFaceStream << vIndex << '/';
if (tIndex != -1) { // texture coord provided
newFaceStream << tIndex;
}
newFaceStream << '/';
newFaceStream << nIndex << ' ';
}
line = newFaceStream.str();
} else if (part == "usemtl") {
getline(ss, part, '\n');
std::string mtlFilename = targetDir + part + ".mtl";
// insert into the hashmap
objMtlMap.insert(std::pair<std::string, std::string>(objName, part + ".mtl"));
}
if (output) { // output stream already initialized
output << line << std::endl;
}
}
output.close();
}