@@ -123,7 +123,7 @@ class timerutil {
123
123
};
124
124
125
125
typedef struct {
126
- GLuint vb ; // vertex buffer
126
+ GLuint vb_id ; // vertex buffer id
127
127
int numTriangles;
128
128
size_t material_id;
129
129
} DrawObject;
@@ -207,6 +207,9 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
207
207
tm.start ();
208
208
209
209
std::string base_dir = GetBaseDir (filename);
210
+ if (base_dir.empty ()) {
211
+ base_dir = " ." ;
212
+ }
210
213
#ifdef _WIN32
211
214
base_dir += " \\ " ;
212
215
#else
@@ -238,6 +241,10 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
238
241
// Append `default` material
239
242
materials.push_back (tinyobj::material_t ());
240
243
244
+ for (size_t i = 0 ; i < materials.size (); i++) {
245
+ printf (" material[%d].diffuse_texname = %s\n " , int (i), materials[i].diffuse_texname .c_str ());
246
+ }
247
+
241
248
// Load diffuse textures
242
249
{
243
250
for (size_t m = 0 ; m < materials.size (); m++) {
@@ -265,15 +272,19 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
265
272
std::cerr << " Unable to load texture: " << texture_filename << std::endl;
266
273
exit (1 );
267
274
}
275
+ std::cout << " Loaded texture: " << texture_filename << " , w = " << w << " , h = " << h << " , comp = " << comp << std::endl;
276
+
268
277
glGenTextures (1 , &texture_id);
269
278
glBindTexture (GL_TEXTURE_2D, texture_id);
270
279
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
271
- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
280
+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
272
281
if (comp == 3 ) {
273
282
glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGB, w, h, 0 , GL_RGB, GL_UNSIGNED_BYTE, image);
274
283
}
275
284
else if (comp == 4 ) {
276
285
glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA, w, h, 0 , GL_RGBA, GL_UNSIGNED_BYTE, image);
286
+ } else {
287
+ assert (0 ); // TODO
277
288
}
278
289
glBindTexture (GL_TEXTURE_2D, 0 );
279
290
stbi_image_free (image);
@@ -289,7 +300,7 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
289
300
{
290
301
for (size_t s = 0 ; s < shapes.size (); s++) {
291
302
DrawObject o;
292
- std::vector<float > vb ; // pos(3float), normal(3float), color(3float)
303
+ std::vector<float > buffer ; // pos(3float), normal(3float), color(3float)
293
304
for (size_t f = 0 ; f < shapes[s].mesh .indices .size () / 3 ; f++) {
294
305
tinyobj::index_t idx0 = shapes[s].mesh .indices [3 * f + 0 ];
295
306
tinyobj::index_t idx1 = shapes[s].mesh .indices [3 * f + 1 ];
@@ -314,6 +325,8 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
314
325
assert (attrib.texcoords .size () > 2 * idx0.texcoord_index + 1 );
315
326
assert (attrib.texcoords .size () > 2 * idx1.texcoord_index + 1 );
316
327
assert (attrib.texcoords .size () > 2 * idx2.texcoord_index + 1 );
328
+
329
+ // Flip Y coord.
317
330
tc[0 ][0 ] = attrib.texcoords [2 * idx0.texcoord_index ];
318
331
tc[0 ][1 ] = 1 .0f - attrib.texcoords [2 * idx0.texcoord_index + 1 ];
319
332
tc[1 ][0 ] = attrib.texcoords [2 * idx1.texcoord_index ];
@@ -374,12 +387,12 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
374
387
}
375
388
376
389
for (int k = 0 ; k < 3 ; k++) {
377
- vb .push_back (v[k][0 ]);
378
- vb .push_back (v[k][1 ]);
379
- vb .push_back (v[k][2 ]);
380
- vb .push_back (n[k][0 ]);
381
- vb .push_back (n[k][1 ]);
382
- vb .push_back (n[k][2 ]);
390
+ buffer .push_back (v[k][0 ]);
391
+ buffer .push_back (v[k][1 ]);
392
+ buffer .push_back (v[k][2 ]);
393
+ buffer .push_back (n[k][0 ]);
394
+ buffer .push_back (n[k][1 ]);
395
+ buffer .push_back (n[k][2 ]);
383
396
// Combine normal and diffuse to get color.
384
397
float normal_factor = 0.2 ;
385
398
float diffuse_factor = 1 - normal_factor;
@@ -396,32 +409,32 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
396
409
c[1 ] /= len;
397
410
c[2 ] /= len;
398
411
}
399
- vb .push_back (c[0 ] * 0.5 + 0.5 );
400
- vb .push_back (c[1 ] * 0.5 + 0.5 );
401
- vb .push_back (c[2 ] * 0.5 + 0.5 );
412
+ buffer .push_back (c[0 ] * 0.5 + 0.5 );
413
+ buffer .push_back (c[1 ] * 0.5 + 0.5 );
414
+ buffer .push_back (c[2 ] * 0.5 + 0.5 );
402
415
403
- vb .push_back (tc[k][0 ]);
404
- vb .push_back (tc[k][1 ]);
416
+ buffer .push_back (tc[k][0 ]);
417
+ buffer .push_back (tc[k][1 ]);
405
418
}
406
419
}
407
420
408
- o.vb = 0 ;
421
+ o.vb_id = 0 ;
409
422
o.numTriangles = 0 ;
410
423
411
424
// OpenGL viewer does not support texturing with per-face material.
412
425
if (shapes[s].mesh .material_ids .size () > 0 && shapes[s].mesh .material_ids .size () > s) {
413
- // Base case
414
- o.material_id = shapes[s].mesh .material_ids [s];
426
+ o.material_id = shapes[s].mesh .material_ids [0 ]; // use the material ID of the first face.
415
427
} else {
416
428
o.material_id = materials.size () - 1 ; // = ID for default material.
417
429
}
430
+ printf (" shape[%d] material_id %d\n " , int (s), int (o.material_id ));
418
431
419
- if (vb .size () > 0 ) {
420
- glGenBuffers (1 , &o.vb );
421
- glBindBuffer (GL_ARRAY_BUFFER, o.vb );
422
- glBufferData (GL_ARRAY_BUFFER, vb .size () * sizeof (float ), &vb .at (0 ),
432
+ if (buffer .size () > 0 ) {
433
+ glGenBuffers (1 , &o.vb_id );
434
+ glBindBuffer (GL_ARRAY_BUFFER, o.vb_id );
435
+ glBufferData (GL_ARRAY_BUFFER, buffer .size () * sizeof (float ), &buffer .at (0 ),
423
436
GL_STATIC_DRAW);
424
- o.numTriangles = vb .size () / (3 + 3 + 3 + 2 ) / 3 ; // 3:vtx, 3:normal, 3:col, 2:texcoord
437
+ o.numTriangles = buffer .size () / (3 + 3 + 3 + 2 ) / 3 ; // 3:vtx, 3:normal, 3:col, 2:texcoord
425
438
426
439
printf (" shape[%d] # of triangles = %d\n " , static_cast <int >(s),
427
440
o.numTriangles );
@@ -545,16 +558,17 @@ static void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj
545
558
GLsizei stride = (3 + 3 + 3 + 2 ) * sizeof (float );
546
559
for (size_t i = 0 ; i < drawObjects.size (); i++) {
547
560
DrawObject o = drawObjects[i];
548
- if (o.vb < 1 ) {
561
+ if (o.vb_id < 1 ) {
549
562
continue ;
550
563
}
551
564
552
- glBindBuffer (GL_ARRAY_BUFFER, o.vb );
565
+ glBindBuffer (GL_ARRAY_BUFFER, o.vb_id );
553
566
glEnableClientState (GL_VERTEX_ARRAY);
554
567
glEnableClientState (GL_NORMAL_ARRAY);
555
568
glEnableClientState (GL_COLOR_ARRAY);
556
569
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
557
570
571
+ glBindTexture (GL_TEXTURE_2D, 0 );
558
572
if ((o.material_id < materials.size ())) {
559
573
std::string diffuse_texname = materials[o.material_id ].diffuse_texname ;
560
574
if (textures.find (diffuse_texname) != textures.end ()) {
@@ -579,11 +593,11 @@ static void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj
579
593
glColor3f (0 .0f , 0 .0f , 0 .4f );
580
594
for (size_t i = 0 ; i < drawObjects.size (); i++) {
581
595
DrawObject o = drawObjects[i];
582
- if (o.vb < 1 ) {
596
+ if (o.vb_id < 1 ) {
583
597
continue ;
584
598
}
585
599
586
- glBindBuffer (GL_ARRAY_BUFFER, o.vb );
600
+ glBindBuffer (GL_ARRAY_BUFFER, o.vb_id );
587
601
glEnableClientState (GL_VERTEX_ARRAY);
588
602
glEnableClientState (GL_NORMAL_ARRAY);
589
603
glDisableClientState (GL_COLOR_ARRAY);
0 commit comments