Skip to content

Commit

Permalink
Fix seg fault when no material assigned to object in viewer example.
Browse files Browse the repository at this point in the history
Bump version v1.0.2.
  • Loading branch information
syoyo committed Oct 24, 2016
1 parent 6c6390f commit c2474e2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
30 changes: 20 additions & 10 deletions examples/viewer/viewer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
printf("# of materials = %d\n", (int)materials.size());
printf("# of shapes = %d\n", (int)shapes.size());

// Append `default` material
materials.push_back(tinyobj::material_t());

// Load diffuse textures
{
for (size_t m = 0; m < materials.size(); m++) {
Expand Down Expand Up @@ -248,24 +251,23 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],

{
for (size_t s = 0; s < shapes.size(); s++) {
size_t current_material_id = 0;
if (shapes[s].mesh.material_ids.size() > 0 && shapes[s].mesh.material_ids.size() > s) {
// Base case
current_material_id = shapes[s].mesh.material_ids[s];
}
DrawObject o;
std::vector<float> vb; // pos(3float), normal(3float), color(3float)
for (size_t f = 0; f < shapes[s].mesh.indices.size() / 3; f++) {
tinyobj::index_t idx0 = shapes[s].mesh.indices[3 * f + 0];
tinyobj::index_t idx1 = shapes[s].mesh.indices[3 * f + 1];
tinyobj::index_t idx2 = shapes[s].mesh.indices[3 * f + 2];

current_material_id = shapes[s].mesh.material_ids[f];
int current_material_id = shapes[s].mesh.material_ids[f];

if (current_material_id >= materials.size()) {
std::cerr << "Invalid material index: " << current_material_id << std::endl;
if ((current_material_id < 0) || (current_material_id >= static_cast<int>(materials.size()))) {
// Invaid material ID. Use default material.
current_material_id = materials.size() - 1; // Default material is added to the last item in `materials`.
}

//if (current_material_id >= materials.size()) {
// std::cerr << "Invalid material index: " << current_material_id << std::endl;
//}
//
float diffuse[3];
for (size_t i = 0; i < 3; i++) {
diffuse[i] = materials[current_material_id].diffuse[i];
Expand Down Expand Up @@ -364,7 +366,15 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],

o.vb = 0;
o.numTriangles = 0;
o.material_id = current_material_id;

// OpenGL viewer does not support texturing with per-face material.
if (shapes[s].mesh.material_ids.size() > 0 && shapes[s].mesh.material_ids.size() > s) {
// Base case
o.material_id = shapes[s].mesh.material_ids[s];
} else {
o.material_id = materials.size() - 1; // = ID for default material.
}

if (vb.size() > 0) {
glGenBuffers(1, &o.vb);
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
Expand Down
14 changes: 7 additions & 7 deletions loader_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
printf(" material.map_d = %s\n", materials[i].alpha_texname.c_str());
printf(" material.disp = %s\n", materials[i].displacement_texname.c_str());
printf(" <<PBR>>\n");
printf(" material.Pr = %f\n", materials[i].roughness);
printf(" material.Pm = %f\n", materials[i].metallic);
printf(" material.Ps = %f\n", materials[i].sheen);
printf(" material.Pc = %f\n", materials[i].clearcoat_thickness);
printf(" material.Pcr = %f\n", materials[i].clearcoat_thickness);
printf(" material.aniso = %f\n", materials[i].anisotropy);
printf(" material.anisor = %f\n", materials[i].anisotropy_rotation);
printf(" material.Pr = %f\n", static_cast<const double>(materials[i].roughness));
printf(" material.Pm = %f\n", static_cast<const double>(materials[i].metallic));
printf(" material.Ps = %f\n", static_cast<const double>(materials[i].sheen));
printf(" material.Pc = %f\n", static_cast<const double>(materials[i].clearcoat_thickness));
printf(" material.Pcr = %f\n", static_cast<const double>(materials[i].clearcoat_thickness));
printf(" material.aniso = %f\n", static_cast<const double>(materials[i].anisotropy));
printf(" material.anisor = %f\n", static_cast<const double>(materials[i].anisotropy_rotation));
printf(" material.map_Ke = %s\n", materials[i].emissive_texname.c_str());
printf(" material.map_Pr = %s\n", materials[i].roughness_texname.c_str());
printf(" material.map_Pm = %s\n", materials[i].metallic_texname.c_str());
Expand Down
2 changes: 2 additions & 0 deletions tiny_obj_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ THE SOFTWARE.
*/

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

0 comments on commit c2474e2

Please sign in to comment.