36 #ifndef TINY_OBJ_LOADER_H_
37 #define TINY_OBJ_LOADER_H_
51 float transmittance[3];
61 std::string ambient_texname;
62 std::string diffuse_texname;
63 std::string specular_texname;
64 std::string specular_highlight_texname;
65 std::string bump_texname;
66 std::string displacement_texname;
67 std::string alpha_texname;
74 float clearcoat_thickness;
75 float clearcoat_roughness;
77 float anisotropy_rotation;
78 std::string roughness_texname;
79 std::string metallic_texname;
80 std::string sheen_texname;
81 std::string emissive_texname;
82 std::string normal_texname;
84 std::map<std::string, std::string> unknown_parameter;
90 std::vector<int> intValues;
91 std::vector<float> floatValues;
92 std::vector<std::string> stringValues;
104 std::vector<index_t> indices;
105 std::vector<unsigned char> num_face_vertices;
108 std::vector<int> material_ids;
109 std::vector<tag_t> tags;
119 std::vector<float> vertices;
120 std::vector<float> normals;
121 std::vector<float> texcoords;
124 typedef struct callback_t_ {
126 void (*vertex_cb)(
void *user_data,
float x,
float y,
float z,
float w);
127 void (*normal_cb)(
void *user_data,
float x,
float y,
float z);
131 void (*texcoord_cb)(
void *user_data,
float x,
float y,
float z);
136 void (*index_cb)(
void *user_data, index_t *indices,
int num_indices);
140 void (*usemtl_cb)(
void *user_data,
const char *name,
int material_id);
142 void (*mtllib_cb)(
void *user_data,
const material_t *materials,
145 void (*group_cb)(
void *user_data,
const char **names,
int num_names);
146 void (*object_cb)(
void *user_data,
const char *name);
159 class MaterialReader {
162 virtual ~MaterialReader();
164 virtual bool operator()(
const std::string &matId,
165 std::vector<material_t> *materials,
166 std::map<std::string, int> *matMap,
167 std::string *err) = 0;
170 class MaterialFileReader :
public MaterialReader {
172 explicit MaterialFileReader(
const std::string &mtl_basepath)
173 : m_mtlBasePath(mtl_basepath) {}
174 virtual ~MaterialFileReader() {}
175 virtual bool operator()(
const std::string &matId,
176 std::vector<material_t> *materials,
177 std::map<std::string, int> *matMap, std::string *err);
180 std::string m_mtlBasePath;
183 class MaterialStreamReader :
public MaterialReader {
185 explicit MaterialStreamReader(std::istream &inStream)
186 : m_inStream(inStream) {}
187 virtual ~MaterialStreamReader() {}
188 virtual bool operator()(
const std::string &matId,
189 std::vector<material_t> *materials,
190 std::map<std::string, int> *matMap, std::string *err);
193 std::istream &m_inStream;
204 bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
205 std::vector<material_t> *materials, std::string *err,
206 const char *filename,
const char *mtl_basepath = NULL,
207 bool triangulate =
true);
215 bool LoadObjWithCallback(std::istream &inStream,
const callback_t &callback,
216 void *user_data = NULL,
217 MaterialReader *readMatFn = NULL,
218 std::string *err = NULL);
224 bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
225 std::vector<material_t> *materials, std::string *err,
226 std::istream *inStream, MaterialReader *readMatFn = NULL,
227 bool triangulate =
true);
230 void LoadMtl(std::map<std::string, int> *material_map,
231 std::vector<material_t> *materials, std::istream *inStream);
235 #ifdef TINYOBJLOADER_IMPLEMENTATION
249 MaterialReader::~MaterialReader() {}
251 #define TINYOBJ_SSCANF_BUFFER_SIZE (4096)
253 struct vertex_index {
254 int v_idx, vt_idx, vn_idx;
255 vertex_index() : v_idx(-1), vt_idx(-1), vn_idx(-1) {}
256 explicit vertex_index(
int idx) : v_idx(idx), vt_idx(idx), vn_idx(idx) {}
257 vertex_index(
int vidx,
int vtidx,
int vnidx)
258 : v_idx(vidx), vt_idx(vtidx), vn_idx(vnidx) {}
262 tag_sizes() : num_ints(0), num_floats(0), num_strings(0) {}
269 std::vector<float> v;
270 std::vector<float> vn;
271 std::vector<float> vt;
276 static std::istream &safeGetline(std::istream &is, std::string &t) {
285 std::istream::sentry se(is,
true);
286 std::streambuf *sb = is.rdbuf();
289 int c = sb->sbumpc();
294 if (sb->sgetc() ==
'\n') sb->sbumpc();
298 if (t.empty()) is.setstate(std::ios::eofbit);
301 t +=
static_cast<char>(c);
306 #define IS_SPACE(x) (((x) == ' ') || ((x) == '\t'))
307 #define IS_DIGIT(x) \
308 (static_cast<unsigned int>((x) - '0') < static_cast<unsigned int>(10))
309 #define IS_NEW_LINE(x) (((x) == '\r') || ((x) == '\n') || ((x) == '\0'))
312 static inline int fixIndex(
int idx,
int n) {
313 if (idx > 0)
return idx - 1;
314 if (idx == 0)
return 0;
318 static inline std::string parseString(
const char **token) {
320 (*token) += strspn((*token),
" \t");
321 size_t e = strcspn((*token),
" \t\r");
322 s = std::string((*token), &(*token)[e]);
327 static inline int parseInt(
const char **token) {
328 (*token) += strspn((*token),
" \t");
329 int i = atoi((*token));
330 (*token) += strcspn((*token),
" \t\r");
361 static bool tryParseDouble(
const char *s,
const char *s_end,
double *result) {
366 double mantissa = 0.0;
380 char const *curr = s;
385 bool end_not_reached =
false;
392 if (*curr ==
'+' || *curr ==
'-') {
395 }
else if (IS_DIGIT(*curr)) {
401 end_not_reached = (curr != s_end);
402 while (end_not_reached && IS_DIGIT(*curr)) {
404 mantissa +=
static_cast<int>(*curr - 0x30);
407 end_not_reached = (curr != s_end);
411 if (read == 0)
goto fail;
413 if (!end_not_reached)
goto assemble;
419 end_not_reached = (curr != s_end);
420 while (end_not_reached && IS_DIGIT(*curr)) {
422 mantissa +=
static_cast<int>(*curr - 0x30) * pow(10.0, -read);
425 end_not_reached = (curr != s_end);
427 }
else if (*curr ==
'e' || *curr ==
'E') {
432 if (!end_not_reached)
goto assemble;
435 if (*curr ==
'e' || *curr ==
'E') {
438 end_not_reached = (curr != s_end);
439 if (end_not_reached && (*curr ==
'+' || *curr ==
'-')) {
442 }
else if (IS_DIGIT(*curr)) {
449 end_not_reached = (curr != s_end);
450 while (end_not_reached && IS_DIGIT(*curr)) {
452 exponent +=
static_cast<int>(*curr - 0x30);
455 end_not_reached = (curr != s_end);
457 exponent *= (exp_sign ==
'+' ? 1 : -1);
458 if (read == 0)
goto fail;
463 (
sign ==
'+' ? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent);
469 static inline float parseFloat(
const char **token,
double default_value = 0.0) {
470 (*token) += strspn((*token),
" \t");
471 const char *end = (*token) + strcspn((*token),
" \t\r");
472 double val = default_value;
473 tryParseDouble((*token), end, &val);
474 float f =
static_cast<float>(val);
479 static inline void parseFloat2(
float *x,
float *y,
const char **token) {
480 (*x) = parseFloat(token);
481 (*y) = parseFloat(token);
484 static inline void parseFloat3(
float *x,
float *y,
float *z,
485 const char **token) {
486 (*x) = parseFloat(token);
487 (*y) = parseFloat(token);
488 (*z) = parseFloat(token);
491 static inline void parseV(
float *x,
float *y,
float *z,
float *w,
492 const char **token) {
493 (*x) = parseFloat(token);
494 (*y) = parseFloat(token);
495 (*z) = parseFloat(token);
496 (*w) = parseFloat(token, 1.0);
499 static tag_sizes parseTagTriple(
const char **token) {
502 ts.num_ints = atoi((*token));
503 (*token) += strcspn((*token),
"/ \t\r");
504 if ((*token)[0] !=
'/') {
509 ts.num_floats = atoi((*token));
510 (*token) += strcspn((*token),
"/ \t\r");
511 if ((*token)[0] !=
'/') {
516 ts.num_strings = atoi((*token));
517 (*token) += strcspn((*token),
"/ \t\r") + 1;
523 static vertex_index parseTriple(
const char **token,
int vsize,
int vnsize,
527 vi.v_idx = fixIndex(atoi((*token)), vsize);
528 (*token) += strcspn((*token),
"/ \t\r");
529 if ((*token)[0] !=
'/') {
535 if ((*token)[0] ==
'/') {
537 vi.vn_idx = fixIndex(atoi((*token)), vnsize);
538 (*token) += strcspn((*token),
"/ \t\r");
543 vi.vt_idx = fixIndex(atoi((*token)), vtsize);
544 (*token) += strcspn((*token),
"/ \t\r");
545 if ((*token)[0] !=
'/') {
551 vi.vn_idx = fixIndex(atoi((*token)), vnsize);
552 (*token) += strcspn((*token),
"/ \t\r");
557 static vertex_index parseRawTriple(
const char **token) {
558 vertex_index vi(
static_cast<int>(0));
560 vi.v_idx = atoi((*token));
561 (*token) += strcspn((*token),
"/ \t\r");
562 if ((*token)[0] !=
'/') {
568 if ((*token)[0] ==
'/') {
570 vi.vn_idx = atoi((*token));
571 (*token) += strcspn((*token),
"/ \t\r");
576 vi.vt_idx = atoi((*token));
577 (*token) += strcspn((*token),
"/ \t\r");
578 if ((*token)[0] !=
'/') {
584 vi.vn_idx = atoi((*token));
585 (*token) += strcspn((*token),
"/ \t\r");
589 static void InitMaterial(material_t *material) {
591 material->ambient_texname =
"";
592 material->diffuse_texname =
"";
593 material->specular_texname =
"";
594 material->specular_highlight_texname =
"";
595 material->bump_texname =
"";
596 material->displacement_texname =
"";
597 material->alpha_texname =
"";
598 for (
int i = 0; i < 3; i++) {
599 material->ambient[i] = 0.f;
600 material->diffuse[i] = 0.f;
601 material->specular[i] = 0.f;
602 material->transmittance[i] = 0.f;
603 material->emission[i] = 0.f;
606 material->dissolve = 1.f;
607 material->shininess = 1.f;
610 material->roughness = 0.f;
611 material->metallic = 0.f;
612 material->sheen = 0.f;
613 material->clearcoat_thickness = 0.f;
614 material->clearcoat_roughness = 0.f;
615 material->anisotropy_rotation = 0.f;
616 material->anisotropy = 0.f;
617 material->roughness_texname =
"";
618 material->metallic_texname =
"";
619 material->sheen_texname =
"";
620 material->emissive_texname =
"";
621 material->normal_texname =
"";
623 material->unknown_parameter.clear();
626 static bool exportFaceGroupToShape(
627 shape_t *shape,
const std::vector<std::vector<vertex_index> > &faceGroup,
628 const std::vector<tag_t> &tags,
const int material_id,
629 const std::string &name,
bool triangulate) {
630 if (faceGroup.empty()) {
635 for (
size_t i = 0; i < faceGroup.size(); i++) {
636 const std::vector<vertex_index> &face = faceGroup[i];
638 vertex_index i0 = face[0];
640 vertex_index i2 = face[1];
642 size_t npolys = face.size();
646 for (
size_t k = 2; k < npolys; k++) {
650 index_t idx0, idx1, idx2;
651 idx0.vertex_index = i0.v_idx;
652 idx0.normal_index = i0.vn_idx;
653 idx0.texcoord_index = i0.vt_idx;
654 idx1.vertex_index = i1.v_idx;
655 idx1.normal_index = i1.vn_idx;
656 idx1.texcoord_index = i1.vt_idx;
657 idx2.vertex_index = i2.v_idx;
658 idx2.normal_index = i2.vn_idx;
659 idx2.texcoord_index = i2.vt_idx;
661 shape->mesh.indices.push_back(idx0);
662 shape->mesh.indices.push_back(idx1);
663 shape->mesh.indices.push_back(idx2);
665 shape->mesh.num_face_vertices.push_back(3);
666 shape->mesh.material_ids.push_back(material_id);
669 for (
size_t k = 0; k < npolys; k++) {
671 idx.vertex_index = face[k].v_idx;
672 idx.normal_index = face[k].vn_idx;
673 idx.texcoord_index = face[k].vt_idx;
674 shape->mesh.indices.push_back(idx);
677 shape->mesh.num_face_vertices.push_back(
678 static_cast<unsigned char>(npolys));
679 shape->mesh.material_ids.push_back(material_id);
684 shape->mesh.tags = tags;
689 void LoadMtl(std::map<std::string, int> *material_map,
690 std::vector<material_t> *materials, std::istream *inStream) {
693 InitMaterial(&material);
695 while (inStream->peek() != -1) {
698 safeGetline(*inStream, linebuf);
701 if (linebuf.size() > 0) {
702 linebuf = linebuf.substr(0, linebuf.find_last_not_of(
" \t") + 1);
706 if (linebuf.size() > 0) {
707 if (linebuf[linebuf.size() - 1] ==
'\n')
708 linebuf.erase(linebuf.size() - 1);
710 if (linebuf.size() > 0) {
711 if (linebuf[linebuf.size() - 1] ==
'\r')
712 linebuf.erase(linebuf.size() - 1);
716 if (linebuf.empty()) {
721 const char *token = linebuf.c_str();
722 token += strspn(token,
" \t");
725 if (token[0] ==
'\0')
continue;
727 if (token[0] ==
'#')
continue;
730 if ((0 == strncmp(token,
"newmtl", 6)) && IS_SPACE((token[6]))) {
732 if (!material.name.empty()) {
733 material_map->insert(std::pair<std::string, int>(
734 material.name,
static_cast<int>(materials->size())));
735 materials->push_back(material);
739 InitMaterial(&material);
742 char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
745 sscanf_s(token,
"%s", namebuf, (
unsigned)_countof(namebuf));
747 sscanf(token,
"%s", namebuf);
749 material.name = namebuf;
754 if (token[0] ==
'K' && token[1] ==
'a' && IS_SPACE((token[2]))) {
757 parseFloat3(&r, &g, &b, &token);
758 material.ambient[0] = r;
759 material.ambient[1] = g;
760 material.ambient[2] = b;
765 if (token[0] ==
'K' && token[1] ==
'd' && IS_SPACE((token[2]))) {
768 parseFloat3(&r, &g, &b, &token);
769 material.diffuse[0] = r;
770 material.diffuse[1] = g;
771 material.diffuse[2] = b;
776 if (token[0] ==
'K' && token[1] ==
's' && IS_SPACE((token[2]))) {
779 parseFloat3(&r, &g, &b, &token);
780 material.specular[0] = r;
781 material.specular[1] = g;
782 material.specular[2] = b;
787 if ((token[0] ==
'K' && token[1] ==
't' && IS_SPACE((token[2]))) ||
788 (token[0] ==
'T' && token[1] ==
'f' && IS_SPACE((token[2])))) {
791 parseFloat3(&r, &g, &b, &token);
792 material.transmittance[0] = r;
793 material.transmittance[1] = g;
794 material.transmittance[2] = b;
799 if (token[0] ==
'N' && token[1] ==
'i' && IS_SPACE((token[2]))) {
801 material.ior = parseFloat(&token);
806 if (token[0] ==
'K' && token[1] ==
'e' && IS_SPACE(token[2])) {
809 parseFloat3(&r, &g, &b, &token);
810 material.emission[0] = r;
811 material.emission[1] = g;
812 material.emission[2] = b;
817 if (token[0] ==
'N' && token[1] ==
's' && IS_SPACE(token[2])) {
819 material.shininess = parseFloat(&token);
824 if (0 == strncmp(token,
"illum", 5) && IS_SPACE(token[5])) {
826 material.illum = parseInt(&token);
831 if ((token[0] ==
'd' && IS_SPACE(token[1]))) {
833 material.dissolve = parseFloat(&token);
836 if (token[0] ==
'T' && token[1] ==
'r' && IS_SPACE(token[2])) {
839 material.dissolve = 1.0f - parseFloat(&token);
844 if (token[0] ==
'P' && token[1] ==
'r' && IS_SPACE(token[2])) {
846 material.roughness = parseFloat(&token);
851 if (token[0] ==
'P' && token[1] ==
'm' && IS_SPACE(token[2])) {
853 material.metallic = parseFloat(&token);
858 if (token[0] ==
'P' && token[1] ==
's' && IS_SPACE(token[2])) {
860 material.sheen = parseFloat(&token);
865 if (token[0] ==
'P' && token[1] ==
'c' && IS_SPACE(token[2])) {
867 material.clearcoat_thickness = parseFloat(&token);
872 if ((0 == strncmp(token,
"Pcr", 3)) && IS_SPACE(token[3])) {
874 material.clearcoat_roughness = parseFloat(&token);
879 if ((0 == strncmp(token,
"aniso", 5)) && IS_SPACE(token[5])) {
881 material.anisotropy = parseFloat(&token);
886 if ((0 == strncmp(token,
"anisor", 6)) && IS_SPACE(token[6])) {
888 material.anisotropy_rotation = parseFloat(&token);
893 if ((0 == strncmp(token,
"map_Ka", 6)) && IS_SPACE(token[6])) {
895 material.ambient_texname = token;
900 if ((0 == strncmp(token,
"map_Kd", 6)) && IS_SPACE(token[6])) {
902 material.diffuse_texname = token;
907 if ((0 == strncmp(token,
"map_Ks", 6)) && IS_SPACE(token[6])) {
909 material.specular_texname = token;
914 if ((0 == strncmp(token,
"map_Ns", 6)) && IS_SPACE(token[6])) {
916 material.specular_highlight_texname = token;
921 if ((0 == strncmp(token,
"map_bump", 8)) && IS_SPACE(token[8])) {
923 material.bump_texname = token;
928 if ((0 == strncmp(token,
"map_d", 5)) && IS_SPACE(token[5])) {
930 material.alpha_texname = token;
935 if ((0 == strncmp(token,
"bump", 4)) && IS_SPACE(token[4])) {
937 material.bump_texname = token;
942 if ((0 == strncmp(token,
"disp", 4)) && IS_SPACE(token[4])) {
944 material.displacement_texname = token;
949 if ((0 == strncmp(token,
"map_Pr", 6)) && IS_SPACE(token[6])) {
951 material.roughness_texname = token;
956 if ((0 == strncmp(token,
"map_Pm", 6)) && IS_SPACE(token[6])) {
958 material.metallic_texname = token;
963 if ((0 == strncmp(token,
"map_Ps", 6)) && IS_SPACE(token[6])) {
965 material.sheen_texname = token;
970 if ((0 == strncmp(token,
"map_Ke", 6)) && IS_SPACE(token[6])) {
972 material.emissive_texname = token;
977 if ((0 == strncmp(token,
"norm", 4)) && IS_SPACE(token[4])) {
979 material.normal_texname = token;
984 const char *_space = strchr(token,
' ');
986 _space = strchr(token,
'\t');
989 std::ptrdiff_t len = _space - token;
990 std::string key(token,
static_cast<size_t>(len));
991 std::string value = _space + 1;
992 material.unknown_parameter.insert(
993 std::pair<std::string, std::string>(key, value));
997 material_map->insert(std::pair<std::string, int>(
998 material.name,
static_cast<int>(materials->size())));
999 materials->push_back(material);
1002 bool MaterialFileReader::operator()(
const std::string &matId,
1003 std::vector<material_t> *materials,
1004 std::map<std::string, int> *matMap,
1006 std::string filepath;
1008 if (!m_mtlBasePath.empty()) {
1009 filepath = std::string(m_mtlBasePath) + matId;
1014 std::ifstream matIStream(filepath.c_str());
1015 LoadMtl(matMap, materials, &matIStream);
1017 std::stringstream ss;
1018 ss <<
"WARN: Material file [ " << filepath
1019 <<
" ] not found. Created a default material.";
1027 bool MaterialStreamReader::operator()(
const std::string &matId,
1028 std::vector<material_t> *materials,
1029 std::map<std::string, int> *matMap,
1031 LoadMtl(matMap, materials, &m_inStream);
1033 std::stringstream ss;
1034 ss <<
"WARN: Material stream in error state."
1035 <<
" Created a default material.";
1043 bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
1044 std::vector<material_t> *materials, std::string *err,
1045 const char *filename,
const char *mtl_basepath,
1046 bool trianglulate) {
1047 attrib->vertices.clear();
1048 attrib->normals.clear();
1049 attrib->texcoords.clear();
1052 std::stringstream errss;
1054 std::ifstream ifs(filename);
1056 errss <<
"Cannot open file [" << filename <<
"]" << std::endl;
1058 (*err) = errss.str();
1063 std::string basePath;
1065 basePath = mtl_basepath;
1067 MaterialFileReader matFileReader(basePath);
1069 return LoadObj(attrib, shapes, materials, err, &ifs, &matFileReader,
1073 bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
1074 std::vector<material_t> *materials, std::string *err,
1075 std::istream *inStream,
1076 MaterialReader *readMatFn ,
1078 std::stringstream errss;
1080 std::vector<float> v;
1081 std::vector<float> vn;
1082 std::vector<float> vt;
1083 std::vector<tag_t> tags;
1084 std::vector<std::vector<vertex_index> > faceGroup;
1088 std::map<std::string, int> material_map;
1093 while (inStream->peek() != -1) {
1094 std::string linebuf;
1095 safeGetline(*inStream, linebuf);
1098 if (linebuf.size() > 0) {
1099 if (linebuf[linebuf.size() - 1] ==
'\n')
1100 linebuf.erase(linebuf.size() - 1);
1102 if (linebuf.size() > 0) {
1103 if (linebuf[linebuf.size() - 1] ==
'\r')
1104 linebuf.erase(linebuf.size() - 1);
1108 if (linebuf.empty()) {
1113 const char *token = linebuf.c_str();
1114 token += strspn(token,
" \t");
1117 if (token[0] ==
'\0')
continue;
1119 if (token[0] ==
'#')
continue;
1122 if (token[0] ==
'v' && IS_SPACE((token[1]))) {
1125 parseFloat3(&x, &y, &z, &token);
1133 if (token[0] ==
'v' && token[1] ==
'n' && IS_SPACE((token[2]))) {
1136 parseFloat3(&x, &y, &z, &token);
1144 if (token[0] ==
'v' && token[1] ==
't' && IS_SPACE((token[2]))) {
1147 parseFloat2(&x, &y, &token);
1154 if (token[0] ==
'f' && IS_SPACE((token[1]))) {
1156 token += strspn(token,
" \t");
1158 std::vector<vertex_index> face;
1161 while (!IS_NEW_LINE(token[0])) {
1162 vertex_index vi = parseTriple(&token,
static_cast<int>(v.size() / 3),
1163 static_cast<int>(vn.size() / 3),
1164 static_cast<int>(vt.size() / 2));
1166 size_t n = strspn(token,
" \t\r");
1171 faceGroup.push_back(std::vector<vertex_index>());
1172 faceGroup[faceGroup.size() - 1].swap(face);
1178 if ((0 == strncmp(token,
"usemtl", 6)) && IS_SPACE((token[6]))) {
1179 char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
1182 sscanf_s(token,
"%s", namebuf, (
unsigned)_countof(namebuf));
1184 sscanf(token,
"%s", namebuf);
1187 int newMaterialId = -1;
1188 if (material_map.find(namebuf) != material_map.end()) {
1189 newMaterialId = material_map[namebuf];
1194 if (newMaterialId != material) {
1196 exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
1199 material = newMaterialId;
1206 if ((0 == strncmp(token,
"mtllib", 6)) && IS_SPACE((token[6]))) {
1208 char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
1211 sscanf_s(token,
"%s", namebuf, (
unsigned)_countof(namebuf));
1213 sscanf(token,
"%s", namebuf);
1216 std::string err_mtl;
1217 bool ok = (*readMatFn)(namebuf, materials, &material_map, &err_mtl);
1232 if (token[0] ==
'g' && IS_SPACE((token[1]))) {
1234 bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
1237 shapes->push_back(shape);
1245 std::vector<std::string> names;
1248 while (!IS_NEW_LINE(token[0])) {
1249 std::string str = parseString(&token);
1250 names.push_back(str);
1251 token += strspn(token,
" \t\r");
1254 assert(names.size() > 0);
1257 if (names.size() > 1) {
1267 if (token[0] ==
'o' && IS_SPACE((token[1]))) {
1269 bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
1272 shapes->push_back(shape);
1280 char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
1283 sscanf_s(token,
"%s", namebuf, (
unsigned)_countof(namebuf));
1285 sscanf(token,
"%s", namebuf);
1287 name = std::string(namebuf);
1292 if (token[0] ==
't' && IS_SPACE(token[1])) {
1298 sscanf_s(token,
"%s", namebuf, (
unsigned)_countof(namebuf));
1300 sscanf(token,
"%s", namebuf);
1302 tag.name = std::string(namebuf);
1304 token += tag.name.size() + 1;
1306 tag_sizes ts = parseTagTriple(&token);
1308 tag.intValues.resize(
static_cast<size_t>(ts.num_ints));
1310 for (
size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
1311 tag.intValues[i] = atoi(token);
1312 token += strcspn(token,
"/ \t\r") + 1;
1315 tag.floatValues.resize(
static_cast<size_t>(ts.num_floats));
1316 for (
size_t i = 0; i < static_cast<size_t>(ts.num_floats); ++i) {
1317 tag.floatValues[i] = parseFloat(&token);
1318 token += strcspn(token,
"/ \t\r") + 1;
1321 tag.stringValues.resize(
static_cast<size_t>(ts.num_strings));
1322 for (
size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
1323 char stringValueBuffer[4096];
1326 sscanf_s(token,
"%s", stringValueBuffer,
1327 (
unsigned)_countof(stringValueBuffer));
1329 sscanf(token,
"%s", stringValueBuffer);
1331 tag.stringValues[i] = stringValueBuffer;
1332 token += tag.stringValues[i].size() + 1;
1335 tags.push_back(tag);
1341 bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
1344 shapes->push_back(shape);
1349 (*err) += errss.str();
1352 attrib->vertices.swap(v);
1353 attrib->normals.swap(vn);
1354 attrib->texcoords.swap(vt);
1359 bool LoadObjWithCallback(std::istream &inStream,
const callback_t &callback,
1361 MaterialReader *readMatFn ,
1362 std::string *err ) {
1363 std::stringstream errss;
1366 std::map<std::string, int> material_map;
1367 int material_id = -1;
1369 std::vector<index_t> indices;
1370 std::vector<material_t> materials;
1371 std::vector<std::string> names;
1374 std::vector<const char *> names_out;
1376 std::string linebuf;
1377 while (inStream.peek() != -1) {
1378 safeGetline(inStream, linebuf);
1381 if (linebuf.size() > 0) {
1382 if (linebuf[linebuf.size() - 1] ==
'\n')
1383 linebuf.erase(linebuf.size() - 1);
1385 if (linebuf.size() > 0) {
1386 if (linebuf[linebuf.size() - 1] ==
'\r')
1387 linebuf.erase(linebuf.size() - 1);
1391 if (linebuf.empty()) {
1396 const char *token = linebuf.c_str();
1397 token += strspn(token,
" \t");
1400 if (token[0] ==
'\0')
continue;
1402 if (token[0] ==
'#')
continue;
1405 if (token[0] ==
'v' && IS_SPACE((token[1]))) {
1408 parseV(&x, &y, &z, &w, &token);
1409 if (callback.vertex_cb) {
1410 callback.vertex_cb(user_data, x, y, z, w);
1416 if (token[0] ==
'v' && token[1] ==
'n' && IS_SPACE((token[2]))) {
1419 parseFloat3(&x, &y, &z, &token);
1420 if (callback.normal_cb) {
1421 callback.normal_cb(user_data, x, y, z);
1427 if (token[0] ==
'v' && token[1] ==
't' && IS_SPACE((token[2]))) {
1430 parseFloat3(&x, &y, &z, &token);
1431 if (callback.texcoord_cb) {
1432 callback.texcoord_cb(user_data, x, y, z);
1438 if (token[0] ==
'f' && IS_SPACE((token[1]))) {
1440 token += strspn(token,
" \t");
1443 while (!IS_NEW_LINE(token[0])) {
1444 vertex_index vi = parseRawTriple(&token);
1447 idx.vertex_index = vi.v_idx;
1448 idx.normal_index = vi.vn_idx;
1449 idx.texcoord_index = vi.vt_idx;
1451 indices.push_back(idx);
1452 size_t n = strspn(token,
" \t\r");
1456 if (callback.index_cb && indices.size() > 0) {
1457 callback.index_cb(user_data, &indices.at(0),
1458 static_cast<int>(indices.size()));
1465 if ((0 == strncmp(token,
"usemtl", 6)) && IS_SPACE((token[6]))) {
1466 char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
1469 sscanf_s(token,
"%s", namebuf,
1470 static_cast<unsigned int>(_countof(namebuf)));
1472 sscanf(token,
"%s", namebuf);
1475 int newMaterialId = -1;
1476 if (material_map.find(namebuf) != material_map.end()) {
1477 newMaterialId = material_map[namebuf];
1482 if (newMaterialId != material_id) {
1483 material_id = newMaterialId;
1486 if (callback.usemtl_cb) {
1487 callback.usemtl_cb(user_data, namebuf, material_id);
1494 if ((0 == strncmp(token,
"mtllib", 6)) && IS_SPACE((token[6]))) {
1496 char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
1499 sscanf_s(token,
"%s", namebuf, (
unsigned)_countof(namebuf));
1501 sscanf(token,
"%s", namebuf);
1504 std::string err_mtl;
1506 bool ok = (*readMatFn)(namebuf, &materials, &material_map, &err_mtl);
1515 if (callback.mtllib_cb) {
1516 callback.mtllib_cb(user_data, &materials.at(0),
1517 static_cast<int>(materials.size()));
1525 if (token[0] ==
'g' && IS_SPACE((token[1]))) {
1528 while (!IS_NEW_LINE(token[0])) {
1529 std::string str = parseString(&token);
1530 names.push_back(str);
1531 token += strspn(token,
" \t\r");
1534 assert(names.size() > 0);
1537 if (names.size() > 1) {
1543 if (callback.group_cb) {
1544 if (names.size() > 1) {
1546 names_out.resize(names.size() - 1);
1547 for (
size_t j = 0; j < names_out.size(); j++) {
1548 names_out[j] = names[j + 1].c_str();
1550 callback.group_cb(user_data, &names_out.at(0),
1551 static_cast<int>(names_out.size()));
1554 callback.group_cb(user_data, NULL, 0);
1562 if (token[0] ==
'o' && IS_SPACE((token[1]))) {
1564 char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
1567 sscanf_s(token,
"%s", namebuf, (
unsigned)_countof(namebuf));
1569 sscanf(token,
"%s", namebuf);
1571 std::string object_name = std::string(namebuf);
1573 if (callback.object_cb) {
1574 callback.object_cb(user_data, object_name.c_str());
1581 if (token[0] ==
't' && IS_SPACE(token[1])) {
1587 sscanf_s(token,
"%s", namebuf, (
unsigned)_countof(namebuf));
1589 sscanf(token,
"%s", namebuf);
1591 tag.name = std::string(namebuf);
1593 token += tag.name.size() + 1;
1595 tag_sizes ts = parseTagTriple(&token);
1597 tag.intValues.resize(
static_cast<size_t>(ts.num_ints));
1599 for (
size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
1600 tag.intValues[i] = atoi(token);
1601 token += strcspn(token,
"/ \t\r") + 1;
1604 tag.floatValues.resize(
static_cast<size_t>(ts.num_floats));
1605 for (
size_t i = 0; i < static_cast<size_t>(ts.num_floats); ++i) {
1606 tag.floatValues[i] = parseFloat(&token);
1607 token += strcspn(token,
"/ \t\r") + 1;
1610 tag.stringValues.resize(
static_cast<size_t>(ts.num_strings));
1611 for (
size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
1612 char stringValueBuffer[4096];
1615 sscanf_s(token,
"%s", stringValueBuffer,
1616 (
unsigned)_countof(stringValueBuffer));
1618 sscanf(token,
"%s", stringValueBuffer);
1620 tag.stringValues[i] = stringValueBuffer;
1621 token += tag.stringValues[i].size() + 1;
1624 tags.push_back(tag);
1632 (*err) += errss.str();
1641 #endif // TINY_OBJ_LOADER_H_