@@ -207,6 +207,9 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
207207 tm.start ();
208208
209209 std::string base_dir = GetBaseDir (filename);
210+ if (base_dir.empty ()) {
211+ base_dir = " ." ;
212+ }
210213#ifdef _WIN32
211214 base_dir += " \\ " ;
212215#else
@@ -238,6 +241,10 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
238241 // Append `default` material
239242 materials.push_back (tinyobj::material_t ());
240243
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+
241248 // Load diffuse textures
242249 {
243250 for (size_t m = 0 ; m < materials.size (); m++) {
@@ -265,15 +272,19 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
265272 std::cerr << " Unable to load texture: " << texture_filename << std::endl;
266273 exit (1 );
267274 }
275+ std::cout << " Loaded texture: " << texture_filename << " , w = " << w << " , h = " << h << " , comp = " << comp << std::endl;
276+
268277 glGenTextures (1 , &texture_id);
269278 glBindTexture (GL_TEXTURE_2D, texture_id);
270279 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 );
272281 if (comp == 3 ) {
273282 glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGB, w, h, 0 , GL_RGB, GL_UNSIGNED_BYTE, image);
274283 }
275284 else if (comp == 4 ) {
276285 glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA, w, h, 0 , GL_RGBA, GL_UNSIGNED_BYTE, image);
286+ } else {
287+ assert (0 ); // TODO
277288 }
278289 glBindTexture (GL_TEXTURE_2D, 0 );
279290 stbi_image_free (image);
@@ -314,6 +325,8 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
314325 assert (attrib.texcoords .size () > 2 * idx0.texcoord_index + 1 );
315326 assert (attrib.texcoords .size () > 2 * idx1.texcoord_index + 1 );
316327 assert (attrib.texcoords .size () > 2 * idx2.texcoord_index + 1 );
328+
329+ // Flip Y coord.
317330 tc[0 ][0 ] = attrib.texcoords [2 * idx0.texcoord_index ];
318331 tc[0 ][1 ] = 1 .0f - attrib.texcoords [2 * idx0.texcoord_index + 1 ];
319332 tc[1 ][0 ] = attrib.texcoords [2 * idx1.texcoord_index ];
@@ -410,11 +423,11 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
410423
411424 // OpenGL viewer does not support texturing with per-face material.
412425 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.
415427 } else {
416428 o.material_id = materials.size () - 1 ; // = ID for default material.
417429 }
430+ printf (" shape[%d] material_id %d\n " , int (s), int (o.material_id ));
418431
419432 if (buffer.size () > 0 ) {
420433 glGenBuffers (1 , &o.vb_id );
@@ -555,6 +568,7 @@ static void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj
555568 glEnableClientState (GL_COLOR_ARRAY);
556569 glEnableClientState (GL_TEXTURE_COORD_ARRAY);
557570
571+ glBindTexture (GL_TEXTURE_2D, 0 );
558572 if ((o.material_id < materials.size ())) {
559573 std::string diffuse_texname = materials[o.material_id ].diffuse_texname ;
560574 if (textures.find (diffuse_texname) != textures.end ()) {
0 commit comments