5
5
6
6
#include " AOEmbree.h"
7
7
8
+ #define TINYOBJLOADER_IMPLEMENTATION
9
+ #include " tiny_obj_loader.h"
10
+
11
+ # define M_PI 3.14159265358979323846
8
12
9
13
void errorFunction (void * userPtr, enum RTCError error, const char * str)
10
14
{
@@ -204,7 +208,7 @@ void computeAOPerVert(float *verts, float *norms, int *tris, float *result,
204
208
float x = radius * cos (theta);
205
209
float y = radius * sin (theta);
206
210
if (y > 0 .0f )// Only keep the upper half of the sphere
207
- rayDir.push_back (vec3 (x, y, z));
211
+ rayDir.push_back (normalize ( vec3 (x, y, z) ));
208
212
}
209
213
210
214
float step = 1 .0f / samplesAO;
@@ -220,10 +224,7 @@ void computeAOPerVert(float *verts, float *norms, int *tris, float *result,
220
224
quat q = glm::rotation (oriVec, normal);
221
225
222
226
for (int s = 0 ; s < rayDir.size (); s++) {
223
- vec3 dir (
224
- rayDir[s].x + vertices[i * 3 ],
225
- rayDir[s].y + vertices[i * 3 + 1 ],
226
- rayDir[s].z + vertices[i * 3 + 2 ]);
227
+ vec3 dir = rayDir[s];
227
228
228
229
229
230
quat dirq = quat (dir.x , dir.y , dir.z , 0 .0f );
@@ -257,44 +258,94 @@ int main(int argc, char **argv) {
257
258
int samplesAO = 128 ;
258
259
float maxDist = 10 .0f ;
259
260
260
- std::ifstream infile (argv[1 ]);
261
+ tinyobj::attrib_t attrib;
262
+ std::vector<tinyobj::shape_t > shapes;
263
+ std::vector<tinyobj::material_t > materials;
264
+
265
+ std::string warn;
266
+ std::string err;
267
+
268
+ bool ret = tinyobj::LoadObj (&attrib, &shapes, &materials, &warn, &err, argv[1 ]);
269
+
270
+ if (!warn.empty ()) {
271
+ std::cout << warn << std::endl;
272
+ }
273
+
274
+ if (!err.empty ()) {
275
+ std::cerr << err << std::endl;
276
+ }
261
277
262
278
std::vector<float > verts;
263
279
std::vector<float > norms;
264
280
std::vector<int > ids;
265
281
266
- std::string line;
267
- while (std::getline (infile, line))
268
- {
282
+ verts = attrib.vertices ;
283
+ norms = attrib.normals ;
284
+ for (int f = 0 ; f < shapes[0 ].mesh .num_face_vertices .size (); f++) {
285
+ size_t index_offset = 0 ;
286
+ int fv = shapes[0 ].mesh .num_face_vertices [f];
269
287
270
- if (line.size () > 0 && line[0 ] == ' v' && line[1 ] == ' ' ) {
271
- std::vector<std::string> words;
272
- split1 (line.c_str (), words);
273
- float x = std::stof (words[1 ]);
274
- float y = std::stof (words[2 ]);
275
- float z = std::stof (words[3 ]);
276
- verts.push_back (x);
277
- verts.push_back (y);
278
- verts.push_back (z);
279
- }
280
- else if (line.size () > 0 && line[0 ] == ' f' ) {
281
- std::vector<std::string> words;
282
- split1 (line.c_str (), words);
283
- ids.push_back (std::stoi (words[1 ]) - 1 );
284
- ids.push_back (std::stoi (words[2 ]) - 1 );
285
- ids.push_back (std::stoi (words[3 ]) - 1 );
286
- }
287
- if (line.size () > 0 && line[0 ] == ' v' && line[1 ] == ' n' ) {
288
- std::vector<std::string> words;
289
- split1 (line.c_str (), words);
290
- float x = std::stof (words[1 ]);
291
- float y = std::stof (words[2 ]);
292
- float z = std::stof (words[3 ]);
293
- norms.push_back (x);
294
- norms.push_back (y);
295
- norms.push_back (z);
288
+ for (size_t v = 0 ; v < fv; v++) {
289
+ tinyobj::index_t idx = shapes[0 ].mesh .indices [index_offset + v];
290
+ ids.push_back (idx.vertex_index -1 );
296
291
}
297
292
}
293
+
294
+
295
+ // std::ifstream infile(argv[1]);
296
+
297
+
298
+
299
+ // std::string line;
300
+ // while (std::getline(infile, line))
301
+ // {
302
+
303
+ // if (line.size() > 0 && line[0] == 'v' && line[1] == ' ') {
304
+ // std::vector<std::string> words;
305
+ // split1(line.c_str(), words);
306
+ // float x = std::stof(words[1]);
307
+ // float y = std::stof(words[2]);
308
+ // float z = std::stof(words[3]);
309
+ // verts.push_back(x);
310
+ // verts.push_back(y);
311
+ // verts.push_back(z);
312
+ // }
313
+ // else if (line.size() > 0 && line[0] == 'f') {
314
+ // std::vector<std::string> words;
315
+ // split1(line.c_str(), words);
316
+ // if (words[0].find("/") != std::string::npos) {
317
+ // int a, b, c;
318
+ // sscanf(words[0].c_str(), "%d/%d/%d",&a,&b,&c);
319
+ // ids.push_back(a - 1);
320
+
321
+
322
+ // sscanf(words[1].c_str(), "%d/%d/%d", &a, &b, &c);
323
+ // ids.push_back(a - 1);
324
+
325
+
326
+ // sscanf(words[2].c_str(), "%d/%d/%d", &a, &b, &c);
327
+ // ids.push_back(a - 1);
328
+
329
+
330
+ //
331
+ // }
332
+ // else {
333
+ // ids.push_back(std::stoi(words[1]) - 1);
334
+ // ids.push_back(std::stoi(words[2]) - 1);
335
+ // ids.push_back(std::stoi(words[3]) - 1);
336
+ // }
337
+ // }
338
+ // if (line.size() > 0 && line[0] == 'v' && line[1] == 'n') {
339
+ // std::vector<std::string> words;
340
+ // split1(line.c_str(), words);
341
+ // float x = std::stof(words[1]);
342
+ // float y = std::stof(words[2]);
343
+ // float z = std::stof(words[3]);
344
+ // norms.push_back(x);
345
+ // norms.push_back(y);
346
+ // norms.push_back(z);
347
+ // }
348
+ // }
298
349
float *result = new float [verts.size () / 3 ];
299
350
computeAOPerVert (verts.data (), norms.data (), ids.data (), result,
300
351
verts.size () / 3 , ids.size () / 3 ,
0 commit comments