Change API for usemtl callback.

This commit is contained in:
Syoyo Fujita
2016-04-18 16:48:37 +09:00
parent 72ef6cbb76
commit 93acf63157

View File

@@ -117,8 +117,9 @@ typedef struct callback_t_ {
void (*texcoord_cb)(void *user_data, float x, float y); void (*texcoord_cb)(void *user_data, float x, float y);
// -2147483648 will be passed for undefined index // -2147483648 will be passed for undefined index
void (*index_cb)(void *user_data, int v_idx, int vn_idx, int vt_idx); void (*index_cb)(void *user_data, int v_idx, int vn_idx, int vt_idx);
// Index to material // `name` material name, `materialId` = the array index of material_t[]. -1 if a material not found in .mtl
void (*usemtl_cb)(void *user_data, int material_idx); void (*usemtl_cb)(void *user_data, const char* name, int materialId);
// `materials` = parsed material data.
void (*mtllib_cb)(void *user_data, const material_t *materials, void (*mtllib_cb)(void *user_data, const material_t *materials,
int num_materials); int num_materials);
// There may be multiple group names // There may be multiple group names
@@ -1178,13 +1179,9 @@ bool LoadObjWithCallback(void *user_data, const callback_t &callback,
MaterialReader *readMatFn) { MaterialReader *readMatFn) {
std::stringstream errss; std::stringstream errss;
std::string name;
// material // material
std::map<std::string, int> material_map; std::map<std::string, int> material_map;
int material = -1; int materialId = -1; // -1 = invalid
shape_t shape;
int maxchars = 8192; // Alloc enough size. int maxchars = 8192; // Alloc enough size.
std::vector<char> buf(static_cast<size_t>(maxchars)); // Alloc enough size. std::vector<char> buf(static_cast<size_t>(maxchars)); // Alloc enough size.
@@ -1284,12 +1281,12 @@ bool LoadObjWithCallback(void *user_data, const callback_t &callback,
// { error!! material not found } // { error!! material not found }
} }
if (newMaterialId != material) { if (newMaterialId != materialId) {
material = newMaterialId; materialId = newMaterialId;
} }
if (callback.usemtl_cb) { if (callback.usemtl_cb) {
callback.usemtl_cb(user_data, material); callback.usemtl_cb(user_data, namebuf, materialId);
} }
continue; continue;
@@ -1337,6 +1334,7 @@ bool LoadObjWithCallback(void *user_data, const callback_t &callback,
assert(names.size() > 0); assert(names.size() > 0);
std::string name;
// names[0] must be 'g', so skip the 0th element. // names[0] must be 'g', so skip the 0th element.
if (names.size() > 1) { if (names.size() > 1) {
name = names[1]; name = names[1];