Skip to content

Commit c2474e2

Browse files
committed
Fix seg fault when no material assigned to object in viewer example.
Bump version v1.0.2.
1 parent 6c6390f commit c2474e2

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

examples/viewer/viewer.cc

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
209209
printf("# of materials = %d\n", (int)materials.size());
210210
printf("# of shapes = %d\n", (int)shapes.size());
211211

212+
// Append `default` material
213+
materials.push_back(tinyobj::material_t());
214+
212215
// Load diffuse textures
213216
{
214217
for (size_t m = 0; m < materials.size(); m++) {
@@ -248,24 +251,23 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
248251

249252
{
250253
for (size_t s = 0; s < shapes.size(); s++) {
251-
size_t current_material_id = 0;
252-
if (shapes[s].mesh.material_ids.size() > 0 && shapes[s].mesh.material_ids.size() > s) {
253-
// Base case
254-
current_material_id = shapes[s].mesh.material_ids[s];
255-
}
256254
DrawObject o;
257255
std::vector<float> vb; // pos(3float), normal(3float), color(3float)
258256
for (size_t f = 0; f < shapes[s].mesh.indices.size() / 3; f++) {
259257
tinyobj::index_t idx0 = shapes[s].mesh.indices[3 * f + 0];
260258
tinyobj::index_t idx1 = shapes[s].mesh.indices[3 * f + 1];
261259
tinyobj::index_t idx2 = shapes[s].mesh.indices[3 * f + 2];
262260

263-
current_material_id = shapes[s].mesh.material_ids[f];
261+
int current_material_id = shapes[s].mesh.material_ids[f];
264262

265-
if (current_material_id >= materials.size()) {
266-
std::cerr << "Invalid material index: " << current_material_id << std::endl;
263+
if ((current_material_id < 0) || (current_material_id >= static_cast<int>(materials.size()))) {
264+
// Invaid material ID. Use default material.
265+
current_material_id = materials.size() - 1; // Default material is added to the last item in `materials`.
267266
}
268-
267+
//if (current_material_id >= materials.size()) {
268+
// std::cerr << "Invalid material index: " << current_material_id << std::endl;
269+
//}
270+
//
269271
float diffuse[3];
270272
for (size_t i = 0; i < 3; i++) {
271273
diffuse[i] = materials[current_material_id].diffuse[i];
@@ -364,7 +366,15 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
364366

365367
o.vb = 0;
366368
o.numTriangles = 0;
367-
o.material_id = current_material_id;
369+
370+
// OpenGL viewer does not support texturing with per-face material.
371+
if (shapes[s].mesh.material_ids.size() > 0 && shapes[s].mesh.material_ids.size() > s) {
372+
// Base case
373+
o.material_id = shapes[s].mesh.material_ids[s];
374+
} else {
375+
o.material_id = materials.size() - 1; // = ID for default material.
376+
}
377+
368378
if (vb.size() > 0) {
369379
glGenBuffers(1, &o.vb);
370380
glBindBuffer(GL_ARRAY_BUFFER, o.vb);

loader_example.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
236236
printf(" material.map_d = %s\n", materials[i].alpha_texname.c_str());
237237
printf(" material.disp = %s\n", materials[i].displacement_texname.c_str());
238238
printf(" <<PBR>>\n");
239-
printf(" material.Pr = %f\n", materials[i].roughness);
240-
printf(" material.Pm = %f\n", materials[i].metallic);
241-
printf(" material.Ps = %f\n", materials[i].sheen);
242-
printf(" material.Pc = %f\n", materials[i].clearcoat_thickness);
243-
printf(" material.Pcr = %f\n", materials[i].clearcoat_thickness);
244-
printf(" material.aniso = %f\n", materials[i].anisotropy);
245-
printf(" material.anisor = %f\n", materials[i].anisotropy_rotation);
239+
printf(" material.Pr = %f\n", static_cast<const double>(materials[i].roughness));
240+
printf(" material.Pm = %f\n", static_cast<const double>(materials[i].metallic));
241+
printf(" material.Ps = %f\n", static_cast<const double>(materials[i].sheen));
242+
printf(" material.Pc = %f\n", static_cast<const double>(materials[i].clearcoat_thickness));
243+
printf(" material.Pcr = %f\n", static_cast<const double>(materials[i].clearcoat_thickness));
244+
printf(" material.aniso = %f\n", static_cast<const double>(materials[i].anisotropy));
245+
printf(" material.anisor = %f\n", static_cast<const double>(materials[i].anisotropy_rotation));
246246
printf(" material.map_Ke = %s\n", materials[i].emissive_texname.c_str());
247247
printf(" material.map_Pr = %s\n", materials[i].roughness_texname.c_str());
248248
printf(" material.map_Pm = %s\n", materials[i].metallic_texname.c_str());

tiny_obj_loader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ THE SOFTWARE.
2323
*/
2424

2525
//
26+
// version 1.0.2 : Improve parsing speed by about a factor of 2 for large files(#105)
2627
// version 1.0.1 : Fixes a shape is lost if obj ends with a 'usemtl'(#104)
2728
// version 1.0.0 : Change data structure. Change license from BSD to MIT.
2829
//
@@ -75,6 +76,7 @@ typedef struct {
7576
float clearcoat_roughness; // [0, 1] default 0
7677
float anisotropy; // aniso. [0, 1] default 0
7778
float anisotropy_rotation; // anisor. [0, 1] default 0
79+
float pad0;
7880
std::string roughness_texname; // map_Pr
7981
std::string metallic_texname; // map_Pm
8082
std::string sheen_texname; // map_Ps

0 commit comments

Comments
 (0)