Skip to content

Commit d496d8e

Browse files
committed
Remove some invalid comments. Fix calling index_cb per f line. Fixes #87.
1 parent 22883de commit d496d8e

File tree

2 files changed

+83
-69
lines changed

2 files changed

+83
-69
lines changed

examples/callback_api/main.cc

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,121 @@
1+
//
2+
// An example of how to use callback API.
3+
// This example is minimum and incomplete. Just showing the usage of callback
4+
// API.
5+
// You need to implement your own Mesh data struct constrution based on this
6+
// example in practical.
7+
//
18
#define TINYOBJLOADER_IMPLEMENTATION
29
#include "tiny_obj_loader.h"
310

11+
#include <cassert>
412
#include <cstdio>
513
#include <cstdlib>
6-
#include <cassert>
14+
#include <fstream>
715
#include <iostream>
816
#include <sstream>
9-
#include <fstream>
1017

11-
typedef struct
12-
{
18+
typedef struct {
1319
std::vector<float> vertices;
1420
std::vector<float> normals;
1521
std::vector<float> texcoords;
16-
std::vector<int> v_indices;
17-
std::vector<int> vn_indices;
18-
std::vector<int> vt_indices;
22+
std::vector<int> v_indices;
23+
std::vector<int> vn_indices;
24+
std::vector<int> vt_indices;
1925

2026
std::vector<tinyobj::material_t> materials;
2127

2228
} MyMesh;
2329

24-
void vertex_cb(void *user_data, float x, float y, float z)
25-
{
26-
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
30+
void vertex_cb(void *user_data, float x, float y, float z) {
31+
MyMesh *mesh = reinterpret_cast<MyMesh *>(user_data);
2732
printf("v[%ld] = %f, %f, %f\n", mesh->vertices.size() / 3, x, y, z);
2833

2934
mesh->vertices.push_back(x);
3035
mesh->vertices.push_back(y);
3136
mesh->vertices.push_back(z);
3237
}
3338

34-
void normal_cb(void *user_data, float x, float y, float z)
35-
{
36-
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
39+
void normal_cb(void *user_data, float x, float y, float z) {
40+
MyMesh *mesh = reinterpret_cast<MyMesh *>(user_data);
3741
printf("vn[%ld] = %f, %f, %f\n", mesh->normals.size() / 3, x, y, z);
3842

3943
mesh->normals.push_back(x);
4044
mesh->normals.push_back(y);
4145
mesh->normals.push_back(z);
4246
}
4347

44-
void texcoord_cb(void *user_data, float x, float y)
45-
{
46-
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
48+
void texcoord_cb(void *user_data, float x, float y) {
49+
MyMesh *mesh = reinterpret_cast<MyMesh *>(user_data);
4750
printf("vt[%ld] = %f, %f\n", mesh->texcoords.size() / 2, x, y);
4851

4952
mesh->texcoords.push_back(x);
5053
mesh->texcoords.push_back(y);
5154
}
5255

53-
void index_cb(void *user_data, int v_idx, int vn_idx, int vt_idx)
54-
{
55-
// NOTE: the value of each index is raw value.
56+
void index_cb(void *user_data, tinyobj::index_t *indices, int num_indices) {
57+
// NOTE: the value of each index is raw value.
5658
// For example, the application must manually adjust the index with offset
57-
// (e.g. v_indices.size()) when the value is negative(relative index).
59+
// (e.g. v_indices.size()) when the value is negative(whic means relative
60+
// index).
61+
// Also, the first index starts with 1, not 0.
5862
// See fixIndex() function in tiny_obj_loader.h for details.
59-
// Also, -2147483648(0x80000000) is set for the index value which does not exist in .obj
60-
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
61-
printf("idx[%ld] = %d, %d, %d\n", mesh->v_indices.size(), v_idx, vn_idx, vt_idx);
62-
63-
if (v_idx != 0x80000000) {
64-
mesh->v_indices.push_back(v_idx);
65-
}
66-
if (vn_idx != 0x80000000) {
67-
mesh->vn_indices.push_back(vn_idx);
68-
}
69-
if (vt_idx != 0x80000000) {
70-
mesh->vt_indices.push_back(vt_idx);
63+
// Also, -2147483648(0x80000000 = -INT_MAX) is set for the index value which
64+
// does not exist in .obj
65+
MyMesh *mesh = reinterpret_cast<MyMesh *>(user_data);
66+
67+
for (int i = 0; i < num_indices; i++) {
68+
tinyobj::index_t idx = indices[i];
69+
printf("idx[%ld] = %d, %d, %d\n", mesh->v_indices.size(), idx.vertex_index,
70+
idx.normal_index, idx.texcoord_index);
71+
72+
if (idx.vertex_index != 0x80000000) {
73+
mesh->v_indices.push_back(idx.vertex_index);
74+
}
75+
if (idx.normal_index != 0x80000000) {
76+
mesh->vn_indices.push_back(idx.normal_index);
77+
}
78+
if (idx.texcoord_index != 0x80000000) {
79+
mesh->vt_indices.push_back(idx.texcoord_index);
80+
}
7181
}
7282
}
7383

74-
void usemtl_cb(void *user_data, const char* name, int material_idx)
75-
{
76-
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
84+
void usemtl_cb(void *user_data, const char *name, int material_idx) {
85+
MyMesh *mesh = reinterpret_cast<MyMesh *>(user_data);
7786
if ((material_idx > -1) && (material_idx < mesh->materials.size())) {
78-
printf("usemtl. material id = %d(name = %s)\n", material_idx, mesh->materials[material_idx].name.c_str());
87+
printf("usemtl. material id = %d(name = %s)\n", material_idx,
88+
mesh->materials[material_idx].name.c_str());
7989
} else {
8090
printf("usemtl. name = %s\n", name);
8191
}
8292
}
8393

84-
void mtllib_cb(void *user_data, const tinyobj::material_t *materials, int num_materials)
85-
{
86-
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
94+
void mtllib_cb(void *user_data, const tinyobj::material_t *materials,
95+
int num_materials) {
96+
MyMesh *mesh = reinterpret_cast<MyMesh *>(user_data);
8797
printf("mtllib. # of materials = %d\n", num_materials);
8898

8999
for (int i = 0; i < num_materials; i++) {
90100
mesh->materials.push_back(materials[i]);
91101
}
92102
}
93103

94-
void group_cb(void *user_data, const char **names, int num_names)
95-
{
96-
//MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
104+
void group_cb(void *user_data, const char **names, int num_names) {
105+
// MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
97106
printf("group : name = \n");
98107

99108
for (int i = 0; i < num_names; i++) {
100109
printf(" %s\n", names[i]);
101110
}
102111
}
103112

104-
void object_cb(void *user_data, const char *name)
105-
{
106-
//MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
113+
void object_cb(void *user_data, const char *name) {
114+
// MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
107115
printf("object : name = %s\n", name);
108-
109116
}
110117

111-
int
112-
main(int argc, char** argv)
113-
{
118+
int main(int argc, char **argv) {
114119
tinyobj::callback_t cb;
115120
cb.vertex_cb = vertex_cb;
116121
cb.normal_cb = normal_cb;
@@ -131,7 +136,7 @@ main(int argc, char** argv)
131136
}
132137

133138
tinyobj::MaterialFileReader mtlReader("../../models/");
134-
139+
135140
bool ret = tinyobj::LoadObjWithCallback(&mesh, cb, &err, &ifs, &mtlReader);
136141

137142
if (!err.empty()) {

tiny_obj_loader.h

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,16 @@ typedef struct callback_t_ {
151151
void (*vertex_cb)(void *user_data, float x, float y, float z);
152152
void (*normal_cb)(void *user_data, float x, float y, float z);
153153
void (*texcoord_cb)(void *user_data, float x, float y);
154-
// -2147483648 will be passed for undefined index
155-
void (*index_cb)(void *user_data, int v_idx, int vn_idx, int vt_idx);
156-
// `name` material name, `materialId` = the array index of material_t[]. -1 if
154+
155+
// called per 'f' line. num_indices is the number of face indices(e.g. 3 for
156+
// triangle, 4 for quad)
157+
// -2147483648(-INT_MAX) will be passed for undefined index in index_t
158+
// members.
159+
void (*index_cb)(void *user_data, index_t *indices, int num_indices);
160+
// `name` material name, `material_id` = the array index of material_t[]. -1
161+
// if
157162
// a material not found in .mtl
158-
void (*usemtl_cb)(void *user_data, const char *name, int materialId);
163+
void (*usemtl_cb)(void *user_data, const char *name, int material_id);
159164
// `materials` = parsed material data.
160165
void (*mtllib_cb)(void *user_data, const material_t *materials,
161166
int num_materials);
@@ -216,9 +221,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
216221
/// `callback.mtllib_cb`.
217222
/// Returns true when loading .obj/.mtl become success.
218223
/// Returns warning and error message into `err`
219-
/// 'mtl_basepath' is optional, and used for base path for .mtl file.
220-
/// 'triangulate' is optional, and used whether triangulate polygon face in .obj
221-
/// or not.
224+
/// See `examples/callback_api/` for how to use this function.
222225
bool LoadObjWithCallback(void *user_data, const callback_t &callback,
223226
std::string *err, std::istream *inStream,
224227
MaterialReader *readMatFn);
@@ -1312,14 +1315,11 @@ bool LoadObjWithCallback(void *user_data, const callback_t &callback,
13121315

13131316
// material
13141317
std::map<std::string, int> material_map;
1315-
int materialId = -1; // -1 = invalid
1318+
int material_id = -1; // -1 = invalid
13161319

1317-
int maxchars = 8192; // Alloc enough size.
1318-
std::vector<char> buf(static_cast<size_t>(maxchars)); // Alloc enough size.
13191320
while (inStream->peek() != -1) {
1320-
inStream->getline(&buf[0], maxchars);
1321-
1322-
std::string linebuf(&buf[0]);
1321+
std::string linebuf;
1322+
std::getline(*inStream, linebuf);
13231323

13241324
// Trim newline '\r\n' or '\n'
13251325
if (linebuf.size() > 0) {
@@ -1383,15 +1383,24 @@ bool LoadObjWithCallback(void *user_data, const callback_t &callback,
13831383
token += 2;
13841384
token += strspn(token, " \t");
13851385

1386+
std::vector<index_t> indices;
13861387
while (!IS_NEW_LINE(token[0])) {
13871388
vertex_index vi = parseRawTriple(&token);
1388-
if (callback.index_cb) {
1389-
callback.index_cb(user_data, vi.v_idx, vi.vn_idx, vi.vt_idx);
1390-
}
1389+
1390+
index_t idx;
1391+
idx.vertex_index = vi.v_idx;
1392+
idx.normal_index = vi.vn_idx;
1393+
idx.texcoord_index = vi.vt_idx;
1394+
1395+
indices.push_back(idx);
13911396
size_t n = strspn(token, " \t\r");
13921397
token += n;
13931398
}
13941399

1400+
if (callback.index_cb && indices.size() > 0) {
1401+
callback.index_cb(user_data, &indices.at(0), indices.size());
1402+
}
1403+
13951404
continue;
13961405
}
13971406

@@ -1412,12 +1421,12 @@ bool LoadObjWithCallback(void *user_data, const callback_t &callback,
14121421
// { error!! material not found }
14131422
}
14141423

1415-
if (newMaterialId != materialId) {
1416-
materialId = newMaterialId;
1424+
if (newMaterialId != material_id) {
1425+
material_id = newMaterialId;
14171426
}
14181427

14191428
if (callback.usemtl_cb) {
1420-
callback.usemtl_cb(user_data, namebuf, materialId);
1429+
callback.usemtl_cb(user_data, namebuf, material_id);
14211430
}
14221431

14231432
continue;

0 commit comments

Comments
 (0)