DKGL2 sample codes

tiny_obj_loader.h 59KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2012-2016 Syoyo Fujita and many contributors.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. //
  21. // version 1.0.6 : Add TINYOBJLOADER_USE_DOUBLE option(#124)
  22. // version 1.0.5 : Ignore `Tr` when `d` exists in MTL(#43)
  23. // version 1.0.4 : Support multiple filenames for 'mtllib'(#112)
  24. // version 1.0.3 : Support parsing texture options(#85)
  25. // version 1.0.2 : Improve parsing speed by about a factor of 2 for large
  26. // files(#105)
  27. // version 1.0.1 : Fixes a shape is lost if obj ends with a 'usemtl'(#104)
  28. // version 1.0.0 : Change data structure. Change license from BSD to MIT.
  29. //
  30. //
  31. // Use this in *one* .cc
  32. // #define TINYOBJLOADER_IMPLEMENTATION
  33. // #include "tiny_obj_loader.h"
  34. //
  35. #ifndef TINY_OBJ_LOADER_H_
  36. #define TINY_OBJ_LOADER_H_
  37. #include <map>
  38. #include <string>
  39. #include <vector>
  40. namespace tinyobj {
  41. // https://en.wikipedia.org/wiki/Wavefront_.obj_file says ...
  42. //
  43. // -blendu on | off # set horizontal texture blending
  44. // (default on)
  45. // -blendv on | off # set vertical texture blending
  46. // (default on)
  47. // -boost real_value # boost mip-map sharpness
  48. // -mm base_value gain_value # modify texture map values (default
  49. // 0 1)
  50. // # base_value = brightness,
  51. // gain_value = contrast
  52. // -o u [v [w]] # Origin offset (default
  53. // 0 0 0)
  54. // -s u [v [w]] # Scale (default
  55. // 1 1 1)
  56. // -t u [v [w]] # Turbulence (default
  57. // 0 0 0)
  58. // -texres resolution # texture resolution to create
  59. // -clamp on | off # only render texels in the clamped
  60. // 0-1 range (default off)
  61. // # When unclamped, textures are
  62. // repeated across a surface,
  63. // # when clamped, only texels which
  64. // fall within the 0-1
  65. // # range are rendered.
  66. // -bm mult_value # bump multiplier (for bump maps
  67. // only)
  68. //
  69. // -imfchan r | g | b | m | l | z # specifies which channel of the file
  70. // is used to
  71. // # create a scalar or bump texture.
  72. // r:red, g:green,
  73. // # b:blue, m:matte, l:luminance,
  74. // z:z-depth..
  75. // # (the default for bump is 'l' and
  76. // for decal is 'm')
  77. // bump -imfchan r bumpmap.tga # says to use the red channel of
  78. // bumpmap.tga as the bumpmap
  79. //
  80. // For reflection maps...
  81. //
  82. // -type sphere # specifies a sphere for a "refl"
  83. // reflection map
  84. // -type cube_top | cube_bottom | # when using a cube map, the texture
  85. // file for each
  86. // cube_front | cube_back | # side of the cube is specified
  87. // separately
  88. // cube_left | cube_right
  89. #ifdef TINYOBJLOADER_USE_DOUBLE
  90. //#pragma message "using double"
  91. typedef double real_t;
  92. #else
  93. //#pragma message "using float"
  94. typedef float real_t;
  95. #endif
  96. typedef enum {
  97. TEXTURE_TYPE_NONE, // default
  98. TEXTURE_TYPE_SPHERE,
  99. TEXTURE_TYPE_CUBE_TOP,
  100. TEXTURE_TYPE_CUBE_BOTTOM,
  101. TEXTURE_TYPE_CUBE_FRONT,
  102. TEXTURE_TYPE_CUBE_BACK,
  103. TEXTURE_TYPE_CUBE_LEFT,
  104. TEXTURE_TYPE_CUBE_RIGHT
  105. } texture_type_t;
  106. typedef struct {
  107. texture_type_t type; // -type (default TEXTURE_TYPE_NONE)
  108. real_t sharpness; // -boost (default 1.0?)
  109. real_t brightness; // base_value in -mm option (default 0)
  110. real_t contrast; // gain_value in -mm option (default 1)
  111. real_t origin_offset[3]; // -o u [v [w]] (default 0 0 0)
  112. real_t scale[3]; // -s u [v [w]] (default 1 1 1)
  113. real_t turbulence[3]; // -t u [v [w]] (default 0 0 0)
  114. // int texture_resolution; // -texres resolution (default = ?) TODO
  115. bool clamp; // -clamp (default false)
  116. char imfchan; // -imfchan (the default for bump is 'l' and for decal is 'm')
  117. bool blendu; // -blendu (default on)
  118. bool blendv; // -blendv (default on)
  119. real_t bump_multiplier; // -bm (for bump maps only, default 1.0)
  120. } texture_option_t;
  121. typedef struct {
  122. std::string name;
  123. real_t ambient[3];
  124. real_t diffuse[3];
  125. real_t specular[3];
  126. real_t transmittance[3];
  127. real_t emission[3];
  128. real_t shininess;
  129. real_t ior; // index of refraction
  130. real_t dissolve; // 1 == opaque; 0 == fully transparent
  131. // illumination model (see http://www.fileformat.info/format/material/)
  132. int illum;
  133. int dummy; // Suppress padding warning.
  134. std::string ambient_texname; // map_Ka
  135. std::string diffuse_texname; // map_Kd
  136. std::string specular_texname; // map_Ks
  137. std::string specular_highlight_texname; // map_Ns
  138. std::string bump_texname; // map_bump, bump
  139. std::string displacement_texname; // disp
  140. std::string alpha_texname; // map_d
  141. texture_option_t ambient_texopt;
  142. texture_option_t diffuse_texopt;
  143. texture_option_t specular_texopt;
  144. texture_option_t specular_highlight_texopt;
  145. texture_option_t bump_texopt;
  146. texture_option_t displacement_texopt;
  147. texture_option_t alpha_texopt;
  148. // PBR extension
  149. // http://exocortex.com/blog/extending_wavefront_mtl_to_support_pbr
  150. real_t roughness; // [0, 1] default 0
  151. real_t metallic; // [0, 1] default 0
  152. real_t sheen; // [0, 1] default 0
  153. real_t clearcoat_thickness; // [0, 1] default 0
  154. real_t clearcoat_roughness; // [0, 1] default 0
  155. real_t anisotropy; // aniso. [0, 1] default 0
  156. real_t anisotropy_rotation; // anisor. [0, 1] default 0
  157. real_t pad0;
  158. real_t pad1;
  159. std::string roughness_texname; // map_Pr
  160. std::string metallic_texname; // map_Pm
  161. std::string sheen_texname; // map_Ps
  162. std::string emissive_texname; // map_Ke
  163. std::string normal_texname; // norm. For normal mapping.
  164. texture_option_t roughness_texopt;
  165. texture_option_t metallic_texopt;
  166. texture_option_t sheen_texopt;
  167. texture_option_t emissive_texopt;
  168. texture_option_t normal_texopt;
  169. int pad2;
  170. std::map<std::string, std::string> unknown_parameter;
  171. } material_t;
  172. typedef struct {
  173. std::string name;
  174. std::vector<int> intValues;
  175. std::vector<real_t> floatValues;
  176. std::vector<std::string> stringValues;
  177. } tag_t;
  178. // Index struct to support different indices for vtx/normal/texcoord.
  179. // -1 means not used.
  180. typedef struct {
  181. int vertex_index;
  182. int normal_index;
  183. int texcoord_index;
  184. } index_t;
  185. typedef struct {
  186. std::vector<index_t> indices;
  187. std::vector<unsigned char> num_face_vertices; // The number of vertices per
  188. // face. 3 = polygon, 4 = quad,
  189. // ... Up to 255.
  190. std::vector<int> material_ids; // per-face material ID
  191. std::vector<tag_t> tags; // SubD tag
  192. } mesh_t;
  193. typedef struct {
  194. std::string name;
  195. mesh_t mesh;
  196. } shape_t;
  197. // Vertex attributes
  198. typedef struct {
  199. std::vector<real_t> vertices; // 'v'
  200. std::vector<real_t> normals; // 'vn'
  201. std::vector<real_t> texcoords; // 'vt'
  202. } attrib_t;
  203. typedef struct callback_t_ {
  204. // W is optional and set to 1 if there is no `w` item in `v` line
  205. void (*vertex_cb)(void *user_data, real_t x, real_t y, real_t z, real_t w);
  206. void (*normal_cb)(void *user_data, real_t x, real_t y, real_t z);
  207. // y and z are optional and set to 0 if there is no `y` and/or `z` item(s) in
  208. // `vt` line.
  209. void (*texcoord_cb)(void *user_data, real_t x, real_t y, real_t z);
  210. // called per 'f' line. num_indices is the number of face indices(e.g. 3 for
  211. // triangle, 4 for quad)
  212. // 0 will be passed for undefined index in index_t members.
  213. void (*index_cb)(void *user_data, index_t *indices, int num_indices);
  214. // `name` material name, `material_id` = the array index of material_t[]. -1
  215. // if
  216. // a material not found in .mtl
  217. void (*usemtl_cb)(void *user_data, const char *name, int material_id);
  218. // `materials` = parsed material data.
  219. void (*mtllib_cb)(void *user_data, const material_t *materials,
  220. int num_materials);
  221. // There may be multiple group names
  222. void (*group_cb)(void *user_data, const char **names, int num_names);
  223. void (*object_cb)(void *user_data, const char *name);
  224. callback_t_()
  225. : vertex_cb(NULL),
  226. normal_cb(NULL),
  227. texcoord_cb(NULL),
  228. index_cb(NULL),
  229. usemtl_cb(NULL),
  230. mtllib_cb(NULL),
  231. group_cb(NULL),
  232. object_cb(NULL) {}
  233. } callback_t;
  234. class MaterialReader {
  235. public:
  236. MaterialReader() {}
  237. virtual ~MaterialReader();
  238. virtual bool operator()(const std::string &matId,
  239. std::vector<material_t> *materials,
  240. std::map<std::string, int> *matMap,
  241. std::string *err) = 0;
  242. };
  243. class MaterialFileReader : public MaterialReader {
  244. public:
  245. explicit MaterialFileReader(const std::string &mtl_basedir)
  246. : m_mtlBaseDir(mtl_basedir) {}
  247. virtual ~MaterialFileReader() {}
  248. virtual bool operator()(const std::string &matId,
  249. std::vector<material_t> *materials,
  250. std::map<std::string, int> *matMap, std::string *err);
  251. private:
  252. std::string m_mtlBaseDir;
  253. };
  254. class MaterialStreamReader : public MaterialReader {
  255. public:
  256. explicit MaterialStreamReader(std::istream &inStream)
  257. : m_inStream(inStream) {}
  258. virtual ~MaterialStreamReader() {}
  259. virtual bool operator()(const std::string &matId,
  260. std::vector<material_t> *materials,
  261. std::map<std::string, int> *matMap, std::string *err);
  262. private:
  263. std::istream &m_inStream;
  264. };
  265. /// Loads .obj from a file.
  266. /// 'attrib', 'shapes' and 'materials' will be filled with parsed shape data
  267. /// 'shapes' will be filled with parsed shape data
  268. /// Returns true when loading .obj become success.
  269. /// Returns warning and error message into `err`
  270. /// 'mtl_basedir' is optional, and used for base directory for .mtl file.
  271. /// In default(`NULL'), .mtl file is searched from an application's working
  272. /// directory.
  273. /// 'triangulate' is optional, and used whether triangulate polygon face in .obj
  274. /// or not.
  275. bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
  276. std::vector<material_t> *materials, std::string *err,
  277. const char *filename, const char *mtl_basedir = NULL,
  278. bool triangulate = true);
  279. /// Loads .obj from a file with custom user callback.
  280. /// .mtl is loaded as usual and parsed material_t data will be passed to
  281. /// `callback.mtllib_cb`.
  282. /// Returns true when loading .obj/.mtl become success.
  283. /// Returns warning and error message into `err`
  284. /// See `examples/callback_api/` for how to use this function.
  285. bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
  286. void *user_data = NULL,
  287. MaterialReader *readMatFn = NULL,
  288. std::string *err = NULL);
  289. /// Loads object from a std::istream, uses GetMtlIStreamFn to retrieve
  290. /// std::istream for materials.
  291. /// Returns true when loading .obj become success.
  292. /// Returns warning and error message into `err`
  293. bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
  294. std::vector<material_t> *materials, std::string *err,
  295. std::istream *inStream, MaterialReader *readMatFn = NULL,
  296. bool triangulate = true);
  297. /// Loads materials into std::map
  298. void LoadMtl(std::map<std::string, int> *material_map,
  299. std::vector<material_t> *materials, std::istream *inStream,
  300. std::string *warning);
  301. } // namespace tinyobj
  302. #endif // TINY_OBJ_LOADER_H_
  303. #ifdef TINYOBJLOADER_IMPLEMENTATION
  304. #include <cassert>
  305. #include <cctype>
  306. #include <cmath>
  307. #include <cstddef>
  308. #include <cstdlib>
  309. #include <cstring>
  310. #include <utility>
  311. #include <fstream>
  312. #include <sstream>
  313. namespace tinyobj {
  314. MaterialReader::~MaterialReader() {}
  315. #define TINYOBJ_SSCANF_BUFFER_SIZE (4096)
  316. struct vertex_index {
  317. int v_idx, vt_idx, vn_idx;
  318. vertex_index() : v_idx(-1), vt_idx(-1), vn_idx(-1) {}
  319. explicit vertex_index(int idx) : v_idx(idx), vt_idx(idx), vn_idx(idx) {}
  320. vertex_index(int vidx, int vtidx, int vnidx)
  321. : v_idx(vidx), vt_idx(vtidx), vn_idx(vnidx) {}
  322. };
  323. struct tag_sizes {
  324. tag_sizes() : num_ints(0), num_reals(0), num_strings(0) {}
  325. int num_ints;
  326. int num_reals;
  327. int num_strings;
  328. };
  329. struct obj_shape {
  330. std::vector<real_t> v;
  331. std::vector<real_t> vn;
  332. std::vector<real_t> vt;
  333. };
  334. // See
  335. // http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
  336. static std::istream &safeGetline(std::istream &is, std::string &t) {
  337. t.clear();
  338. // The characters in the stream are read one-by-one using a std::streambuf.
  339. // That is faster than reading them one-by-one using the std::istream.
  340. // Code that uses streambuf this way must be guarded by a sentry object.
  341. // The sentry object performs various tasks,
  342. // such as thread synchronization and updating the stream state.
  343. std::istream::sentry se(is, true);
  344. std::streambuf *sb = is.rdbuf();
  345. for (;;) {
  346. int c = sb->sbumpc();
  347. switch (c) {
  348. case '\n':
  349. return is;
  350. case '\r':
  351. if (sb->sgetc() == '\n') sb->sbumpc();
  352. return is;
  353. case EOF:
  354. // Also handle the case when the last line has no line ending
  355. if (t.empty()) is.setstate(std::ios::eofbit);
  356. return is;
  357. default:
  358. t += static_cast<char>(c);
  359. }
  360. }
  361. }
  362. #define IS_SPACE(x) (((x) == ' ') || ((x) == '\t'))
  363. #define IS_DIGIT(x) \
  364. (static_cast<unsigned int>((x) - '0') < static_cast<unsigned int>(10))
  365. #define IS_NEW_LINE(x) (((x) == '\r') || ((x) == '\n') || ((x) == '\0'))
  366. // Make index zero-base, and also support relative index.
  367. static inline int fixIndex(int idx, int n) {
  368. if (idx > 0) return idx - 1;
  369. if (idx == 0) return 0;
  370. return n + idx; // negative value = relative
  371. }
  372. static inline std::string parseString(const char **token) {
  373. std::string s;
  374. (*token) += strspn((*token), " \t");
  375. size_t e = strcspn((*token), " \t\r");
  376. s = std::string((*token), &(*token)[e]);
  377. (*token) += e;
  378. return s;
  379. }
  380. static inline int parseInt(const char **token) {
  381. (*token) += strspn((*token), " \t");
  382. int i = atoi((*token));
  383. (*token) += strcspn((*token), " \t\r");
  384. return i;
  385. }
  386. // Tries to parse a floating point number located at s.
  387. //
  388. // s_end should be a location in the string where reading should absolutely
  389. // stop. For example at the end of the string, to prevent buffer overflows.
  390. //
  391. // Parses the following EBNF grammar:
  392. // sign = "+" | "-" ;
  393. // END = ? anything not in digit ?
  394. // digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
  395. // integer = [sign] , digit , {digit} ;
  396. // decimal = integer , ["." , integer] ;
  397. // float = ( decimal , END ) | ( decimal , ("E" | "e") , integer , END ) ;
  398. //
  399. // Valid strings are for example:
  400. // -0 +3.1417e+2 -0.0E-3 1.0324 -1.41 11e2
  401. //
  402. // If the parsing is a success, result is set to the parsed value and true
  403. // is returned.
  404. //
  405. // The function is greedy and will parse until any of the following happens:
  406. // - a non-conforming character is encountered.
  407. // - s_end is reached.
  408. //
  409. // The following situations triggers a failure:
  410. // - s >= s_end.
  411. // - parse failure.
  412. //
  413. static bool tryParseDouble(const char *s, const char *s_end, double *result) {
  414. if (s >= s_end) {
  415. return false;
  416. }
  417. double mantissa = 0.0;
  418. // This exponent is base 2 rather than 10.
  419. // However the exponent we parse is supposed to be one of ten,
  420. // thus we must take care to convert the exponent/and or the
  421. // mantissa to a * 2^E, where a is the mantissa and E is the
  422. // exponent.
  423. // To get the final double we will use ldexp, it requires the
  424. // exponent to be in base 2.
  425. int exponent = 0;
  426. // NOTE: THESE MUST BE DECLARED HERE SINCE WE ARE NOT ALLOWED
  427. // TO JUMP OVER DEFINITIONS.
  428. char sign = '+';
  429. char exp_sign = '+';
  430. char const *curr = s;
  431. // How many characters were read in a loop.
  432. int read = 0;
  433. // Tells whether a loop terminated due to reaching s_end.
  434. bool end_not_reached = false;
  435. /*
  436. BEGIN PARSING.
  437. */
  438. // Find out what sign we've got.
  439. if (*curr == '+' || *curr == '-') {
  440. sign = *curr;
  441. curr++;
  442. } else if (IS_DIGIT(*curr)) { /* Pass through. */
  443. } else {
  444. goto fail;
  445. }
  446. // Read the integer part.
  447. end_not_reached = (curr != s_end);
  448. while (end_not_reached && IS_DIGIT(*curr)) {
  449. mantissa *= 10;
  450. mantissa += static_cast<int>(*curr - 0x30);
  451. curr++;
  452. read++;
  453. end_not_reached = (curr != s_end);
  454. }
  455. // We must make sure we actually got something.
  456. if (read == 0) goto fail;
  457. // We allow numbers of form "#", "###" etc.
  458. if (!end_not_reached) goto assemble;
  459. // Read the decimal part.
  460. if (*curr == '.') {
  461. curr++;
  462. read = 1;
  463. end_not_reached = (curr != s_end);
  464. while (end_not_reached && IS_DIGIT(*curr)) {
  465. static const double pow_lut[] = {
  466. 1.0, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001,
  467. };
  468. const int lut_entries = sizeof pow_lut / sizeof pow_lut[0];
  469. // NOTE: Don't use powf here, it will absolutely murder precision.
  470. mantissa += static_cast<int>(*curr - 0x30) *
  471. (read < lut_entries ? pow_lut[read] : std::pow(10.0, -read));
  472. read++;
  473. curr++;
  474. end_not_reached = (curr != s_end);
  475. }
  476. } else if (*curr == 'e' || *curr == 'E') {
  477. } else {
  478. goto assemble;
  479. }
  480. if (!end_not_reached) goto assemble;
  481. // Read the exponent part.
  482. if (*curr == 'e' || *curr == 'E') {
  483. curr++;
  484. // Figure out if a sign is present and if it is.
  485. end_not_reached = (curr != s_end);
  486. if (end_not_reached && (*curr == '+' || *curr == '-')) {
  487. exp_sign = *curr;
  488. curr++;
  489. } else if (IS_DIGIT(*curr)) { /* Pass through. */
  490. } else {
  491. // Empty E is not allowed.
  492. goto fail;
  493. }
  494. read = 0;
  495. end_not_reached = (curr != s_end);
  496. while (end_not_reached && IS_DIGIT(*curr)) {
  497. exponent *= 10;
  498. exponent += static_cast<int>(*curr - 0x30);
  499. curr++;
  500. read++;
  501. end_not_reached = (curr != s_end);
  502. }
  503. exponent *= (exp_sign == '+' ? 1 : -1);
  504. if (read == 0) goto fail;
  505. }
  506. assemble:
  507. *result =
  508. (sign == '+' ? 1 : -1) *
  509. (exponent ? std::ldexp(mantissa * std::pow(5.0, exponent), exponent) : mantissa);
  510. return true;
  511. fail:
  512. return false;
  513. }
  514. static inline real_t parseReal(const char **token, double default_value = 0.0) {
  515. (*token) += strspn((*token), " \t");
  516. const char *end = (*token) + strcspn((*token), " \t\r");
  517. double val = default_value;
  518. tryParseDouble((*token), end, &val);
  519. real_t f = static_cast<real_t>(val);
  520. (*token) = end;
  521. return f;
  522. }
  523. static inline void parseReal2(real_t *x, real_t *y, const char **token,
  524. const double default_x = 0.0,
  525. const double default_y = 0.0) {
  526. (*x) = parseReal(token, default_x);
  527. (*y) = parseReal(token, default_y);
  528. }
  529. static inline void parseReal3(real_t *x, real_t *y, real_t *z, const char **token,
  530. const double default_x = 0.0,
  531. const double default_y = 0.0,
  532. const double default_z = 0.0) {
  533. (*x) = parseReal(token, default_x);
  534. (*y) = parseReal(token, default_y);
  535. (*z) = parseReal(token, default_z);
  536. }
  537. static inline void parseV(real_t *x, real_t *y, real_t *z, real_t *w,
  538. const char **token, const double default_x = 0.0,
  539. const double default_y = 0.0,
  540. const double default_z = 0.0,
  541. const double default_w = 1.0) {
  542. (*x) = parseReal(token, default_x);
  543. (*y) = parseReal(token, default_y);
  544. (*z) = parseReal(token, default_z);
  545. (*w) = parseReal(token, default_w);
  546. }
  547. static inline bool parseOnOff(const char **token, bool default_value = true) {
  548. (*token) += strspn((*token), " \t");
  549. const char *end = (*token) + strcspn((*token), " \t\r");
  550. bool ret = default_value;
  551. if ((0 == strncmp((*token), "on", 2))) {
  552. ret = true;
  553. } else if ((0 == strncmp((*token), "off", 3))) {
  554. ret = false;
  555. }
  556. (*token) = end;
  557. return ret;
  558. }
  559. static inline texture_type_t parseTextureType(
  560. const char **token, texture_type_t default_value = TEXTURE_TYPE_NONE) {
  561. (*token) += strspn((*token), " \t");
  562. const char *end = (*token) + strcspn((*token), " \t\r");
  563. texture_type_t ty = default_value;
  564. if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) {
  565. ty = TEXTURE_TYPE_CUBE_TOP;
  566. } else if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) {
  567. ty = TEXTURE_TYPE_CUBE_BOTTOM;
  568. } else if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) {
  569. ty = TEXTURE_TYPE_CUBE_LEFT;
  570. } else if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) {
  571. ty = TEXTURE_TYPE_CUBE_RIGHT;
  572. } else if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) {
  573. ty = TEXTURE_TYPE_CUBE_FRONT;
  574. } else if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) {
  575. ty = TEXTURE_TYPE_CUBE_BACK;
  576. } else if ((0 == strncmp((*token), "sphere", strlen("sphere")))) {
  577. ty = TEXTURE_TYPE_SPHERE;
  578. }
  579. (*token) = end;
  580. return ty;
  581. }
  582. static tag_sizes parseTagTriple(const char **token) {
  583. tag_sizes ts;
  584. ts.num_ints = atoi((*token));
  585. (*token) += strcspn((*token), "/ \t\r");
  586. if ((*token)[0] != '/') {
  587. return ts;
  588. }
  589. (*token)++;
  590. ts.num_reals = atoi((*token));
  591. (*token) += strcspn((*token), "/ \t\r");
  592. if ((*token)[0] != '/') {
  593. return ts;
  594. }
  595. (*token)++;
  596. ts.num_strings = atoi((*token));
  597. (*token) += strcspn((*token), "/ \t\r") + 1;
  598. return ts;
  599. }
  600. // Parse triples with index offsets: i, i/j/k, i//k, i/j
  601. static vertex_index parseTriple(const char **token, int vsize, int vnsize,
  602. int vtsize) {
  603. vertex_index vi(-1);
  604. vi.v_idx = fixIndex(atoi((*token)), vsize);
  605. (*token) += strcspn((*token), "/ \t\r");
  606. if ((*token)[0] != '/') {
  607. return vi;
  608. }
  609. (*token)++;
  610. // i//k
  611. if ((*token)[0] == '/') {
  612. (*token)++;
  613. vi.vn_idx = fixIndex(atoi((*token)), vnsize);
  614. (*token) += strcspn((*token), "/ \t\r");
  615. return vi;
  616. }
  617. // i/j/k or i/j
  618. vi.vt_idx = fixIndex(atoi((*token)), vtsize);
  619. (*token) += strcspn((*token), "/ \t\r");
  620. if ((*token)[0] != '/') {
  621. return vi;
  622. }
  623. // i/j/k
  624. (*token)++; // skip '/'
  625. vi.vn_idx = fixIndex(atoi((*token)), vnsize);
  626. (*token) += strcspn((*token), "/ \t\r");
  627. return vi;
  628. }
  629. // Parse raw triples: i, i/j/k, i//k, i/j
  630. static vertex_index parseRawTriple(const char **token) {
  631. vertex_index vi(static_cast<int>(0)); // 0 is an invalid index in OBJ
  632. vi.v_idx = atoi((*token));
  633. (*token) += strcspn((*token), "/ \t\r");
  634. if ((*token)[0] != '/') {
  635. return vi;
  636. }
  637. (*token)++;
  638. // i//k
  639. if ((*token)[0] == '/') {
  640. (*token)++;
  641. vi.vn_idx = atoi((*token));
  642. (*token) += strcspn((*token), "/ \t\r");
  643. return vi;
  644. }
  645. // i/j/k or i/j
  646. vi.vt_idx = atoi((*token));
  647. (*token) += strcspn((*token), "/ \t\r");
  648. if ((*token)[0] != '/') {
  649. return vi;
  650. }
  651. // i/j/k
  652. (*token)++; // skip '/'
  653. vi.vn_idx = atoi((*token));
  654. (*token) += strcspn((*token), "/ \t\r");
  655. return vi;
  656. }
  657. static bool ParseTextureNameAndOption(std::string *texname,
  658. texture_option_t *texopt,
  659. const char *linebuf, const bool is_bump) {
  660. // @todo { write more robust lexer and parser. }
  661. bool found_texname = false;
  662. std::string texture_name;
  663. // Fill with default value for texopt.
  664. if (is_bump) {
  665. texopt->imfchan = 'l';
  666. } else {
  667. texopt->imfchan = 'm';
  668. }
  669. texopt->bump_multiplier = 1.0f;
  670. texopt->clamp = false;
  671. texopt->blendu = true;
  672. texopt->blendv = true;
  673. texopt->sharpness = 1.0f;
  674. texopt->brightness = 0.0f;
  675. texopt->contrast = 1.0f;
  676. texopt->origin_offset[0] = 0.0f;
  677. texopt->origin_offset[1] = 0.0f;
  678. texopt->origin_offset[2] = 0.0f;
  679. texopt->scale[0] = 1.0f;
  680. texopt->scale[1] = 1.0f;
  681. texopt->scale[2] = 1.0f;
  682. texopt->turbulence[0] = 0.0f;
  683. texopt->turbulence[1] = 0.0f;
  684. texopt->turbulence[2] = 0.0f;
  685. texopt->type = TEXTURE_TYPE_NONE;
  686. const char *token = linebuf; // Assume line ends with NULL
  687. while (!IS_NEW_LINE((*token))) {
  688. if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) {
  689. token += 8;
  690. texopt->blendu = parseOnOff(&token, /* default */ true);
  691. } else if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) {
  692. token += 8;
  693. texopt->blendv = parseOnOff(&token, /* default */ true);
  694. } else if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) {
  695. token += 7;
  696. texopt->clamp = parseOnOff(&token, /* default */ true);
  697. } else if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) {
  698. token += 7;
  699. texopt->sharpness = parseReal(&token, 1.0);
  700. } else if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) {
  701. token += 4;
  702. texopt->bump_multiplier = parseReal(&token, 1.0);
  703. } else if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) {
  704. token += 3;
  705. parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]),
  706. &(texopt->origin_offset[2]), &token);
  707. } else if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) {
  708. token += 3;
  709. parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]),
  710. &token, 1.0, 1.0, 1.0);
  711. } else if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) {
  712. token += 3;
  713. parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]),
  714. &(texopt->turbulence[2]), &token);
  715. } else if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) {
  716. token += 5;
  717. texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE);
  718. } else if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) {
  719. token += 9;
  720. token += strspn(token, " \t");
  721. const char *end = token + strcspn(token, " \t\r");
  722. if ((end - token) == 1) { // Assume one char for -imfchan
  723. texopt->imfchan = (*token);
  724. }
  725. token = end;
  726. } else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) {
  727. token += 4;
  728. parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0);
  729. } else {
  730. // Assume texture filename
  731. token += strspn(token, " \t"); // skip space
  732. size_t len = strcspn(token, " \t\r"); // untile next space
  733. texture_name = std::string(token, token + len);
  734. token += len;
  735. token += strspn(token, " \t"); // skip space
  736. found_texname = true;
  737. }
  738. }
  739. if (found_texname) {
  740. (*texname) = texture_name;
  741. return true;
  742. } else {
  743. return false;
  744. }
  745. }
  746. static void InitMaterial(material_t *material) {
  747. material->name = "";
  748. material->ambient_texname = "";
  749. material->diffuse_texname = "";
  750. material->specular_texname = "";
  751. material->specular_highlight_texname = "";
  752. material->bump_texname = "";
  753. material->displacement_texname = "";
  754. material->alpha_texname = "";
  755. for (int i = 0; i < 3; i++) {
  756. material->ambient[i] = 0.f;
  757. material->diffuse[i] = 0.f;
  758. material->specular[i] = 0.f;
  759. material->transmittance[i] = 0.f;
  760. material->emission[i] = 0.f;
  761. }
  762. material->illum = 0;
  763. material->dissolve = 1.f;
  764. material->shininess = 1.f;
  765. material->ior = 1.f;
  766. material->roughness = 0.f;
  767. material->metallic = 0.f;
  768. material->sheen = 0.f;
  769. material->clearcoat_thickness = 0.f;
  770. material->clearcoat_roughness = 0.f;
  771. material->anisotropy_rotation = 0.f;
  772. material->anisotropy = 0.f;
  773. material->roughness_texname = "";
  774. material->metallic_texname = "";
  775. material->sheen_texname = "";
  776. material->emissive_texname = "";
  777. material->normal_texname = "";
  778. material->unknown_parameter.clear();
  779. }
  780. static bool exportFaceGroupToShape(
  781. shape_t *shape, const std::vector<std::vector<vertex_index> > &faceGroup,
  782. const std::vector<tag_t> &tags, const int material_id,
  783. const std::string &name, bool triangulate) {
  784. if (faceGroup.empty()) {
  785. return false;
  786. }
  787. // Flatten vertices and indices
  788. for (size_t i = 0; i < faceGroup.size(); i++) {
  789. const std::vector<vertex_index> &face = faceGroup[i];
  790. vertex_index i0 = face[0];
  791. vertex_index i1(-1);
  792. vertex_index i2 = face[1];
  793. size_t npolys = face.size();
  794. if (triangulate) {
  795. // Polygon -> triangle fan conversion
  796. for (size_t k = 2; k < npolys; k++) {
  797. i1 = i2;
  798. i2 = face[k];
  799. index_t idx0, idx1, idx2;
  800. idx0.vertex_index = i0.v_idx;
  801. idx0.normal_index = i0.vn_idx;
  802. idx0.texcoord_index = i0.vt_idx;
  803. idx1.vertex_index = i1.v_idx;
  804. idx1.normal_index = i1.vn_idx;
  805. idx1.texcoord_index = i1.vt_idx;
  806. idx2.vertex_index = i2.v_idx;
  807. idx2.normal_index = i2.vn_idx;
  808. idx2.texcoord_index = i2.vt_idx;
  809. shape->mesh.indices.push_back(idx0);
  810. shape->mesh.indices.push_back(idx1);
  811. shape->mesh.indices.push_back(idx2);
  812. shape->mesh.num_face_vertices.push_back(3);
  813. shape->mesh.material_ids.push_back(material_id);
  814. }
  815. } else {
  816. for (size_t k = 0; k < npolys; k++) {
  817. index_t idx;
  818. idx.vertex_index = face[k].v_idx;
  819. idx.normal_index = face[k].vn_idx;
  820. idx.texcoord_index = face[k].vt_idx;
  821. shape->mesh.indices.push_back(idx);
  822. }
  823. shape->mesh.num_face_vertices.push_back(
  824. static_cast<unsigned char>(npolys));
  825. shape->mesh.material_ids.push_back(material_id); // per face
  826. }
  827. }
  828. shape->name = name;
  829. shape->mesh.tags = tags;
  830. return true;
  831. }
  832. // Split a string with specified delimiter character.
  833. // http://stackoverflow.com/questions/236129/split-a-string-in-c
  834. static void SplitString(const std::string &s, char delim,
  835. std::vector<std::string> &elems) {
  836. std::stringstream ss;
  837. ss.str(s);
  838. std::string item;
  839. while (std::getline(ss, item, delim)) {
  840. elems.push_back(item);
  841. }
  842. }
  843. void LoadMtl(std::map<std::string, int> *material_map,
  844. std::vector<material_t> *materials, std::istream *inStream,
  845. std::string *warning) {
  846. // Create a default material anyway.
  847. material_t material;
  848. InitMaterial(&material);
  849. // Issue 43. `d` wins against `Tr` since `Tr` is not in the MTL specification.
  850. bool has_d = false;
  851. bool has_tr = false;
  852. std::stringstream ss;
  853. std::string linebuf;
  854. while (inStream->peek() != -1) {
  855. safeGetline(*inStream, linebuf);
  856. // Trim trailing whitespace.
  857. if (linebuf.size() > 0) {
  858. linebuf = linebuf.substr(0, linebuf.find_last_not_of(" \t") + 1);
  859. }
  860. // Trim newline '\r\n' or '\n'
  861. if (linebuf.size() > 0) {
  862. if (linebuf[linebuf.size() - 1] == '\n')
  863. linebuf.erase(linebuf.size() - 1);
  864. }
  865. if (linebuf.size() > 0) {
  866. if (linebuf[linebuf.size() - 1] == '\r')
  867. linebuf.erase(linebuf.size() - 1);
  868. }
  869. // Skip if empty line.
  870. if (linebuf.empty()) {
  871. continue;
  872. }
  873. // Skip leading space.
  874. const char *token = linebuf.c_str();
  875. token += strspn(token, " \t");
  876. assert(token);
  877. if (token[0] == '\0') continue; // empty line
  878. if (token[0] == '#') continue; // comment line
  879. // new mtl
  880. if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) {
  881. // flush previous material.
  882. if (!material.name.empty()) {
  883. material_map->insert(std::pair<std::string, int>(
  884. material.name, static_cast<int>(materials->size())));
  885. materials->push_back(material);
  886. }
  887. // initial temporary material
  888. InitMaterial(&material);
  889. has_d = false;
  890. has_tr = false;
  891. // set new mtl name
  892. char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
  893. token += 7;
  894. #ifdef _MSC_VER
  895. sscanf_s(token, "%s", namebuf, (unsigned)_countof(namebuf));
  896. #else
  897. std::sscanf(token, "%s", namebuf);
  898. #endif
  899. material.name = namebuf;
  900. continue;
  901. }
  902. // ambient
  903. if (token[0] == 'K' && token[1] == 'a' && IS_SPACE((token[2]))) {
  904. token += 2;
  905. real_t r, g, b;
  906. parseReal3(&r, &g, &b, &token);
  907. material.ambient[0] = r;
  908. material.ambient[1] = g;
  909. material.ambient[2] = b;
  910. continue;
  911. }
  912. // diffuse
  913. if (token[0] == 'K' && token[1] == 'd' && IS_SPACE((token[2]))) {
  914. token += 2;
  915. real_t r, g, b;
  916. parseReal3(&r, &g, &b, &token);
  917. material.diffuse[0] = r;
  918. material.diffuse[1] = g;
  919. material.diffuse[2] = b;
  920. continue;
  921. }
  922. // specular
  923. if (token[0] == 'K' && token[1] == 's' && IS_SPACE((token[2]))) {
  924. token += 2;
  925. real_t r, g, b;
  926. parseReal3(&r, &g, &b, &token);
  927. material.specular[0] = r;
  928. material.specular[1] = g;
  929. material.specular[2] = b;
  930. continue;
  931. }
  932. // transmittance
  933. if ((token[0] == 'K' && token[1] == 't' && IS_SPACE((token[2]))) ||
  934. (token[0] == 'T' && token[1] == 'f' && IS_SPACE((token[2])))) {
  935. token += 2;
  936. real_t r, g, b;
  937. parseReal3(&r, &g, &b, &token);
  938. material.transmittance[0] = r;
  939. material.transmittance[1] = g;
  940. material.transmittance[2] = b;
  941. continue;
  942. }
  943. // ior(index of refraction)
  944. if (token[0] == 'N' && token[1] == 'i' && IS_SPACE((token[2]))) {
  945. token += 2;
  946. material.ior = parseReal(&token);
  947. continue;
  948. }
  949. // emission
  950. if (token[0] == 'K' && token[1] == 'e' && IS_SPACE(token[2])) {
  951. token += 2;
  952. real_t r, g, b;
  953. parseReal3(&r, &g, &b, &token);
  954. material.emission[0] = r;
  955. material.emission[1] = g;
  956. material.emission[2] = b;
  957. continue;
  958. }
  959. // shininess
  960. if (token[0] == 'N' && token[1] == 's' && IS_SPACE(token[2])) {
  961. token += 2;
  962. material.shininess = parseReal(&token);
  963. continue;
  964. }
  965. // illum model
  966. if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) {
  967. token += 6;
  968. material.illum = parseInt(&token);
  969. continue;
  970. }
  971. // dissolve
  972. if ((token[0] == 'd' && IS_SPACE(token[1]))) {
  973. token += 1;
  974. material.dissolve = parseReal(&token);
  975. if (has_tr) {
  976. ss << "WARN: Both `d` and `Tr` parameters defined for \""
  977. << material.name << "\". Use the value of `d` for dissolve."
  978. << std::endl;
  979. }
  980. has_d = true;
  981. continue;
  982. }
  983. if (token[0] == 'T' && token[1] == 'r' && IS_SPACE(token[2])) {
  984. token += 2;
  985. if (has_d) {
  986. // `d` wins. Ignore `Tr` value.
  987. ss << "WARN: Both `d` and `Tr` parameters defined for \""
  988. << material.name << "\". Use the value of `d` for dissolve."
  989. << std::endl;
  990. } else {
  991. // We invert value of Tr(assume Tr is in range [0, 1])
  992. // NOTE: Interpretation of Tr is application(exporter) dependent. For
  993. // some application(e.g. 3ds max obj exporter), Tr = d(Issue 43)
  994. material.dissolve = 1.0f - parseReal(&token);
  995. }
  996. has_tr = true;
  997. continue;
  998. }
  999. // PBR: roughness
  1000. if (token[0] == 'P' && token[1] == 'r' && IS_SPACE(token[2])) {
  1001. token += 2;
  1002. material.roughness = parseReal(&token);
  1003. continue;
  1004. }
  1005. // PBR: metallic
  1006. if (token[0] == 'P' && token[1] == 'm' && IS_SPACE(token[2])) {
  1007. token += 2;
  1008. material.metallic = parseReal(&token);
  1009. continue;
  1010. }
  1011. // PBR: sheen
  1012. if (token[0] == 'P' && token[1] == 's' && IS_SPACE(token[2])) {
  1013. token += 2;
  1014. material.sheen = parseReal(&token);
  1015. continue;
  1016. }
  1017. // PBR: clearcoat thickness
  1018. if (token[0] == 'P' && token[1] == 'c' && IS_SPACE(token[2])) {
  1019. token += 2;
  1020. material.clearcoat_thickness = parseReal(&token);
  1021. continue;
  1022. }
  1023. // PBR: clearcoat roughness
  1024. if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) {
  1025. token += 4;
  1026. material.clearcoat_roughness = parseReal(&token);
  1027. continue;
  1028. }
  1029. // PBR: anisotropy
  1030. if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) {
  1031. token += 6;
  1032. material.anisotropy = parseReal(&token);
  1033. continue;
  1034. }
  1035. // PBR: anisotropy rotation
  1036. if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) {
  1037. token += 7;
  1038. material.anisotropy_rotation = parseReal(&token);
  1039. continue;
  1040. }
  1041. // ambient texture
  1042. if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) {
  1043. token += 7;
  1044. ParseTextureNameAndOption(&(material.ambient_texname),
  1045. &(material.ambient_texopt), token,
  1046. /* is_bump */ false);
  1047. continue;
  1048. }
  1049. // diffuse texture
  1050. if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) {
  1051. token += 7;
  1052. ParseTextureNameAndOption(&(material.diffuse_texname),
  1053. &(material.diffuse_texopt), token,
  1054. /* is_bump */ false);
  1055. continue;
  1056. }
  1057. // specular texture
  1058. if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) {
  1059. token += 7;
  1060. ParseTextureNameAndOption(&(material.specular_texname),
  1061. &(material.specular_texopt), token,
  1062. /* is_bump */ false);
  1063. continue;
  1064. }
  1065. // specular highlight texture
  1066. if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) {
  1067. token += 7;
  1068. ParseTextureNameAndOption(&(material.specular_highlight_texname),
  1069. &(material.specular_highlight_texopt), token,
  1070. /* is_bump */ false);
  1071. continue;
  1072. }
  1073. // bump texture
  1074. if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) {
  1075. token += 9;
  1076. ParseTextureNameAndOption(&(material.bump_texname),
  1077. &(material.bump_texopt), token,
  1078. /* is_bump */ true);
  1079. continue;
  1080. }
  1081. // bump texture
  1082. if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) {
  1083. token += 5;
  1084. ParseTextureNameAndOption(&(material.bump_texname),
  1085. &(material.bump_texopt), token,
  1086. /* is_bump */ true);
  1087. continue;
  1088. }
  1089. // alpha texture
  1090. if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) {
  1091. token += 6;
  1092. material.alpha_texname = token;
  1093. ParseTextureNameAndOption(&(material.alpha_texname),
  1094. &(material.alpha_texopt), token,
  1095. /* is_bump */ false);
  1096. continue;
  1097. }
  1098. // displacement texture
  1099. if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) {
  1100. token += 5;
  1101. ParseTextureNameAndOption(&(material.displacement_texname),
  1102. &(material.displacement_texopt), token,
  1103. /* is_bump */ false);
  1104. continue;
  1105. }
  1106. // PBR: roughness texture
  1107. if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) {
  1108. token += 7;
  1109. ParseTextureNameAndOption(&(material.roughness_texname),
  1110. &(material.roughness_texopt), token,
  1111. /* is_bump */ false);
  1112. continue;
  1113. }
  1114. // PBR: metallic texture
  1115. if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) {
  1116. token += 7;
  1117. ParseTextureNameAndOption(&(material.metallic_texname),
  1118. &(material.metallic_texopt), token,
  1119. /* is_bump */ false);
  1120. continue;
  1121. }
  1122. // PBR: sheen texture
  1123. if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) {
  1124. token += 7;
  1125. ParseTextureNameAndOption(&(material.sheen_texname),
  1126. &(material.sheen_texopt), token,
  1127. /* is_bump */ false);
  1128. continue;
  1129. }
  1130. // PBR: emissive texture
  1131. if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) {
  1132. token += 7;
  1133. ParseTextureNameAndOption(&(material.emissive_texname),
  1134. &(material.emissive_texopt), token,
  1135. /* is_bump */ false);
  1136. continue;
  1137. }
  1138. // PBR: normal map texture
  1139. if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) {
  1140. token += 5;
  1141. ParseTextureNameAndOption(
  1142. &(material.normal_texname), &(material.normal_texopt), token,
  1143. /* is_bump */ false); // @fixme { is_bump will be true? }
  1144. continue;
  1145. }
  1146. // unknown parameter
  1147. const char *_space = strchr(token, ' ');
  1148. if (!_space) {
  1149. _space = strchr(token, '\t');
  1150. }
  1151. if (_space) {
  1152. std::ptrdiff_t len = _space - token;
  1153. std::string key(token, static_cast<size_t>(len));
  1154. std::string value = _space + 1;
  1155. material.unknown_parameter.insert(
  1156. std::pair<std::string, std::string>(key, value));
  1157. }
  1158. }
  1159. // flush last material.
  1160. material_map->insert(std::pair<std::string, int>(
  1161. material.name, static_cast<int>(materials->size())));
  1162. materials->push_back(material);
  1163. if (warning) {
  1164. (*warning) = ss.str();
  1165. }
  1166. }
  1167. bool MaterialFileReader::operator()(const std::string &matId,
  1168. std::vector<material_t> *materials,
  1169. std::map<std::string, int> *matMap,
  1170. std::string *err) {
  1171. std::string filepath;
  1172. if (!m_mtlBaseDir.empty()) {
  1173. filepath = std::string(m_mtlBaseDir) + matId;
  1174. } else {
  1175. filepath = matId;
  1176. }
  1177. std::ifstream matIStream(filepath.c_str());
  1178. if (!matIStream) {
  1179. std::stringstream ss;
  1180. ss << "WARN: Material file [ " << filepath << " ] not found." << std::endl;
  1181. if (err) {
  1182. (*err) += ss.str();
  1183. }
  1184. return false;
  1185. }
  1186. std::string warning;
  1187. LoadMtl(matMap, materials, &matIStream, &warning);
  1188. if (!warning.empty()) {
  1189. if (err) {
  1190. (*err) += warning;
  1191. }
  1192. }
  1193. return true;
  1194. }
  1195. bool MaterialStreamReader::operator()(const std::string &matId,
  1196. std::vector<material_t> *materials,
  1197. std::map<std::string, int> *matMap,
  1198. std::string *err) {
  1199. (void)matId;
  1200. if (!m_inStream) {
  1201. std::stringstream ss;
  1202. ss << "WARN: Material stream in error state. " << std::endl;
  1203. if (err) {
  1204. (*err) += ss.str();
  1205. }
  1206. return false;
  1207. }
  1208. std::string warning;
  1209. LoadMtl(matMap, materials, &m_inStream, &warning);
  1210. if (!warning.empty()) {
  1211. if (err) {
  1212. (*err) += warning;
  1213. }
  1214. }
  1215. return true;
  1216. }
  1217. bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
  1218. std::vector<material_t> *materials, std::string *err,
  1219. const char *filename, const char *mtl_basedir, bool trianglulate) {
  1220. attrib->vertices.clear();
  1221. attrib->normals.clear();
  1222. attrib->texcoords.clear();
  1223. shapes->clear();
  1224. std::stringstream errss;
  1225. std::ifstream ifs(filename);
  1226. if (!ifs) {
  1227. errss << "Cannot open file [" << filename << "]" << std::endl;
  1228. if (err) {
  1229. (*err) = errss.str();
  1230. }
  1231. return false;
  1232. }
  1233. std::string baseDir;
  1234. if (mtl_basedir) {
  1235. baseDir = mtl_basedir;
  1236. }
  1237. MaterialFileReader matFileReader(baseDir);
  1238. return LoadObj(attrib, shapes, materials, err, &ifs, &matFileReader,
  1239. trianglulate);
  1240. }
  1241. bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
  1242. std::vector<material_t> *materials, std::string *err,
  1243. std::istream *inStream, MaterialReader *readMatFn /*= NULL*/,
  1244. bool triangulate) {
  1245. std::stringstream errss;
  1246. std::vector<real_t> v;
  1247. std::vector<real_t> vn;
  1248. std::vector<real_t> vt;
  1249. std::vector<tag_t> tags;
  1250. std::vector<std::vector<vertex_index> > faceGroup;
  1251. std::string name;
  1252. // material
  1253. std::map<std::string, int> material_map;
  1254. int material = -1;
  1255. shape_t shape;
  1256. std::string linebuf;
  1257. while (inStream->peek() != -1) {
  1258. safeGetline(*inStream, linebuf);
  1259. // Trim newline '\r\n' or '\n'
  1260. if (linebuf.size() > 0) {
  1261. if (linebuf[linebuf.size() - 1] == '\n')
  1262. linebuf.erase(linebuf.size() - 1);
  1263. }
  1264. if (linebuf.size() > 0) {
  1265. if (linebuf[linebuf.size() - 1] == '\r')
  1266. linebuf.erase(linebuf.size() - 1);
  1267. }
  1268. // Skip if empty line.
  1269. if (linebuf.empty()) {
  1270. continue;
  1271. }
  1272. // Skip leading space.
  1273. const char *token = linebuf.c_str();
  1274. token += strspn(token, " \t");
  1275. assert(token);
  1276. if (token[0] == '\0') continue; // empty line
  1277. if (token[0] == '#') continue; // comment line
  1278. // vertex
  1279. if (token[0] == 'v' && IS_SPACE((token[1]))) {
  1280. token += 2;
  1281. real_t x, y, z;
  1282. parseReal3(&x, &y, &z, &token);
  1283. v.push_back(x);
  1284. v.push_back(y);
  1285. v.push_back(z);
  1286. continue;
  1287. }
  1288. // normal
  1289. if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) {
  1290. token += 3;
  1291. real_t x, y, z;
  1292. parseReal3(&x, &y, &z, &token);
  1293. vn.push_back(x);
  1294. vn.push_back(y);
  1295. vn.push_back(z);
  1296. continue;
  1297. }
  1298. // texcoord
  1299. if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) {
  1300. token += 3;
  1301. real_t x, y;
  1302. parseReal2(&x, &y, &token);
  1303. vt.push_back(x);
  1304. vt.push_back(y);
  1305. continue;
  1306. }
  1307. // face
  1308. if (token[0] == 'f' && IS_SPACE((token[1]))) {
  1309. token += 2;
  1310. token += strspn(token, " \t");
  1311. std::vector<vertex_index> face;
  1312. face.reserve(3);
  1313. while (!IS_NEW_LINE(token[0])) {
  1314. vertex_index vi = parseTriple(&token, static_cast<int>(v.size() / 3),
  1315. static_cast<int>(vn.size() / 3),
  1316. static_cast<int>(vt.size() / 2));
  1317. face.push_back(vi);
  1318. size_t n = strspn(token, " \t\r");
  1319. token += n;
  1320. }
  1321. // replace with emplace_back + std::move on C++11
  1322. faceGroup.push_back(std::vector<vertex_index>());
  1323. faceGroup[faceGroup.size() - 1].swap(face);
  1324. continue;
  1325. }
  1326. // use mtl
  1327. if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) {
  1328. char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
  1329. token += 7;
  1330. #ifdef _MSC_VER
  1331. sscanf_s(token, "%s", namebuf, (unsigned)_countof(namebuf));
  1332. #else
  1333. std::sscanf(token, "%s", namebuf);
  1334. #endif
  1335. int newMaterialId = -1;
  1336. if (material_map.find(namebuf) != material_map.end()) {
  1337. newMaterialId = material_map[namebuf];
  1338. } else {
  1339. // { error!! material not found }
  1340. }
  1341. if (newMaterialId != material) {
  1342. // Create per-face material. Thus we don't add `shape` to `shapes` at
  1343. // this time.
  1344. // just clear `faceGroup` after `exportFaceGroupToShape()` call.
  1345. exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
  1346. triangulate);
  1347. faceGroup.clear();
  1348. material = newMaterialId;
  1349. }
  1350. continue;
  1351. }
  1352. // load mtl
  1353. if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) {
  1354. if (readMatFn) {
  1355. token += 7;
  1356. std::vector<std::string> filenames;
  1357. SplitString(std::string(token), ' ', filenames);
  1358. if (filenames.empty()) {
  1359. if (err) {
  1360. (*err) +=
  1361. "WARN: Looks like empty filename for mtllib. Use default "
  1362. "material. \n";
  1363. }
  1364. } else {
  1365. bool found = false;
  1366. for (size_t s = 0; s < filenames.size(); s++) {
  1367. std::string err_mtl;
  1368. bool ok = (*readMatFn)(filenames[s].c_str(), materials,
  1369. &material_map, &err_mtl);
  1370. if (err && (!err_mtl.empty())) {
  1371. (*err) += err_mtl; // This should be warn message.
  1372. }
  1373. if (ok) {
  1374. found = true;
  1375. break;
  1376. }
  1377. }
  1378. if (!found) {
  1379. if (err) {
  1380. (*err) +=
  1381. "WARN: Failed to load material file(s). Use default "
  1382. "material.\n";
  1383. }
  1384. }
  1385. }
  1386. }
  1387. continue;
  1388. }
  1389. // group name
  1390. if (token[0] == 'g' && IS_SPACE((token[1]))) {
  1391. // flush previous face group.
  1392. bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
  1393. triangulate);
  1394. if (ret) {
  1395. shapes->push_back(shape);
  1396. }
  1397. shape = shape_t();
  1398. // material = -1;
  1399. faceGroup.clear();
  1400. std::vector<std::string> names;
  1401. names.reserve(2);
  1402. while (!IS_NEW_LINE(token[0])) {
  1403. std::string str = parseString(&token);
  1404. names.push_back(str);
  1405. token += strspn(token, " \t\r"); // skip tag
  1406. }
  1407. assert(names.size() > 0);
  1408. // names[0] must be 'g', so skip the 0th element.
  1409. if (names.size() > 1) {
  1410. name = names[1];
  1411. } else {
  1412. name = "";
  1413. }
  1414. continue;
  1415. }
  1416. // object name
  1417. if (token[0] == 'o' && IS_SPACE((token[1]))) {
  1418. // flush previous face group.
  1419. bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
  1420. triangulate);
  1421. if (ret) {
  1422. shapes->push_back(shape);
  1423. }
  1424. // material = -1;
  1425. faceGroup.clear();
  1426. shape = shape_t();
  1427. // @todo { multiple object name? }
  1428. char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
  1429. token += 2;
  1430. #ifdef _MSC_VER
  1431. sscanf_s(token, "%s", namebuf, (unsigned)_countof(namebuf));
  1432. #else
  1433. std::sscanf(token, "%s", namebuf);
  1434. #endif
  1435. name = std::string(namebuf);
  1436. continue;
  1437. }
  1438. if (token[0] == 't' && IS_SPACE(token[1])) {
  1439. tag_t tag;
  1440. char namebuf[4096];
  1441. token += 2;
  1442. #ifdef _MSC_VER
  1443. sscanf_s(token, "%s", namebuf, (unsigned)_countof(namebuf));
  1444. #else
  1445. std::sscanf(token, "%s", namebuf);
  1446. #endif
  1447. tag.name = std::string(namebuf);
  1448. token += tag.name.size() + 1;
  1449. tag_sizes ts = parseTagTriple(&token);
  1450. tag.intValues.resize(static_cast<size_t>(ts.num_ints));
  1451. for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
  1452. tag.intValues[i] = atoi(token);
  1453. token += strcspn(token, "/ \t\r") + 1;
  1454. }
  1455. tag.floatValues.resize(static_cast<size_t>(ts.num_reals));
  1456. for (size_t i = 0; i < static_cast<size_t>(ts.num_reals); ++i) {
  1457. tag.floatValues[i] = parseReal(&token);
  1458. token += strcspn(token, "/ \t\r") + 1;
  1459. }
  1460. tag.stringValues.resize(static_cast<size_t>(ts.num_strings));
  1461. for (size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
  1462. char stringValueBuffer[4096];
  1463. #ifdef _MSC_VER
  1464. sscanf_s(token, "%s", stringValueBuffer,
  1465. (unsigned)_countof(stringValueBuffer));
  1466. #else
  1467. std::sscanf(token, "%s", stringValueBuffer);
  1468. #endif
  1469. tag.stringValues[i] = stringValueBuffer;
  1470. token += tag.stringValues[i].size() + 1;
  1471. }
  1472. tags.push_back(tag);
  1473. }
  1474. // Ignore unknown command.
  1475. }
  1476. bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
  1477. triangulate);
  1478. // exportFaceGroupToShape return false when `usemtl` is called in the last
  1479. // line.
  1480. // we also add `shape` to `shapes` when `shape.mesh` has already some
  1481. // faces(indices)
  1482. if (ret || shape.mesh.indices.size()) {
  1483. shapes->push_back(shape);
  1484. }
  1485. faceGroup.clear(); // for safety
  1486. if (err) {
  1487. (*err) += errss.str();
  1488. }
  1489. attrib->vertices.swap(v);
  1490. attrib->normals.swap(vn);
  1491. attrib->texcoords.swap(vt);
  1492. return true;
  1493. }
  1494. bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
  1495. void *user_data /*= NULL*/,
  1496. MaterialReader *readMatFn /*= NULL*/,
  1497. std::string *err /*= NULL*/) {
  1498. std::stringstream errss;
  1499. // material
  1500. std::map<std::string, int> material_map;
  1501. int material_id = -1; // -1 = invalid
  1502. std::vector<index_t> indices;
  1503. std::vector<material_t> materials;
  1504. std::vector<std::string> names;
  1505. names.reserve(2);
  1506. std::string name;
  1507. std::vector<const char *> names_out;
  1508. std::string linebuf;
  1509. while (inStream.peek() != -1) {
  1510. safeGetline(inStream, linebuf);
  1511. // Trim newline '\r\n' or '\n'
  1512. if (linebuf.size() > 0) {
  1513. if (linebuf[linebuf.size() - 1] == '\n')
  1514. linebuf.erase(linebuf.size() - 1);
  1515. }
  1516. if (linebuf.size() > 0) {
  1517. if (linebuf[linebuf.size() - 1] == '\r')
  1518. linebuf.erase(linebuf.size() - 1);
  1519. }
  1520. // Skip if empty line.
  1521. if (linebuf.empty()) {
  1522. continue;
  1523. }
  1524. // Skip leading space.
  1525. const char *token = linebuf.c_str();
  1526. token += strspn(token, " \t");
  1527. assert(token);
  1528. if (token[0] == '\0') continue; // empty line
  1529. if (token[0] == '#') continue; // comment line
  1530. // vertex
  1531. if (token[0] == 'v' && IS_SPACE((token[1]))) {
  1532. token += 2;
  1533. real_t x, y, z, w; // w is optional. default = 1.0
  1534. parseV(&x, &y, &z, &w, &token);
  1535. if (callback.vertex_cb) {
  1536. callback.vertex_cb(user_data, x, y, z, w);
  1537. }
  1538. continue;
  1539. }
  1540. // normal
  1541. if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) {
  1542. token += 3;
  1543. real_t x, y, z;
  1544. parseReal3(&x, &y, &z, &token);
  1545. if (callback.normal_cb) {
  1546. callback.normal_cb(user_data, x, y, z);
  1547. }
  1548. continue;
  1549. }
  1550. // texcoord
  1551. if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) {
  1552. token += 3;
  1553. real_t x, y, z; // y and z are optional. default = 0.0
  1554. parseReal3(&x, &y, &z, &token);
  1555. if (callback.texcoord_cb) {
  1556. callback.texcoord_cb(user_data, x, y, z);
  1557. }
  1558. continue;
  1559. }
  1560. // face
  1561. if (token[0] == 'f' && IS_SPACE((token[1]))) {
  1562. token += 2;
  1563. token += strspn(token, " \t");
  1564. indices.clear();
  1565. while (!IS_NEW_LINE(token[0])) {
  1566. vertex_index vi = parseRawTriple(&token);
  1567. index_t idx;
  1568. idx.vertex_index = vi.v_idx;
  1569. idx.normal_index = vi.vn_idx;
  1570. idx.texcoord_index = vi.vt_idx;
  1571. indices.push_back(idx);
  1572. size_t n = strspn(token, " \t\r");
  1573. token += n;
  1574. }
  1575. if (callback.index_cb && indices.size() > 0) {
  1576. callback.index_cb(user_data, &indices.at(0),
  1577. static_cast<int>(indices.size()));
  1578. }
  1579. continue;
  1580. }
  1581. // use mtl
  1582. if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) {
  1583. char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
  1584. token += 7;
  1585. #ifdef _MSC_VER
  1586. sscanf_s(token, "%s", namebuf,
  1587. static_cast<unsigned int>(_countof(namebuf)));
  1588. #else
  1589. std::sscanf(token, "%s", namebuf);
  1590. #endif
  1591. int newMaterialId = -1;
  1592. if (material_map.find(namebuf) != material_map.end()) {
  1593. newMaterialId = material_map[namebuf];
  1594. } else {
  1595. // { error!! material not found }
  1596. }
  1597. if (newMaterialId != material_id) {
  1598. material_id = newMaterialId;
  1599. }
  1600. if (callback.usemtl_cb) {
  1601. callback.usemtl_cb(user_data, namebuf, material_id);
  1602. }
  1603. continue;
  1604. }
  1605. // load mtl
  1606. if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) {
  1607. if (readMatFn) {
  1608. token += 7;
  1609. std::vector<std::string> filenames;
  1610. SplitString(std::string(token), ' ', filenames);
  1611. if (filenames.empty()) {
  1612. if (err) {
  1613. (*err) +=
  1614. "WARN: Looks like empty filename for mtllib. Use default "
  1615. "material. \n";
  1616. }
  1617. } else {
  1618. bool found = false;
  1619. for (size_t s = 0; s < filenames.size(); s++) {
  1620. std::string err_mtl;
  1621. bool ok = (*readMatFn)(filenames[s].c_str(), &materials,
  1622. &material_map, &err_mtl);
  1623. if (err && (!err_mtl.empty())) {
  1624. (*err) += err_mtl; // This should be warn message.
  1625. }
  1626. if (ok) {
  1627. found = true;
  1628. break;
  1629. }
  1630. }
  1631. if (!found) {
  1632. if (err) {
  1633. (*err) +=
  1634. "WARN: Failed to load material file(s). Use default "
  1635. "material.\n";
  1636. }
  1637. } else {
  1638. if (callback.mtllib_cb) {
  1639. callback.mtllib_cb(user_data, &materials.at(0),
  1640. static_cast<int>(materials.size()));
  1641. }
  1642. }
  1643. }
  1644. }
  1645. continue;
  1646. }
  1647. // group name
  1648. if (token[0] == 'g' && IS_SPACE((token[1]))) {
  1649. names.clear();
  1650. while (!IS_NEW_LINE(token[0])) {
  1651. std::string str = parseString(&token);
  1652. names.push_back(str);
  1653. token += strspn(token, " \t\r"); // skip tag
  1654. }
  1655. assert(names.size() > 0);
  1656. // names[0] must be 'g', so skip the 0th element.
  1657. if (names.size() > 1) {
  1658. name = names[1];
  1659. } else {
  1660. name.clear();
  1661. }
  1662. if (callback.group_cb) {
  1663. if (names.size() > 1) {
  1664. // create const char* array.
  1665. names_out.resize(names.size() - 1);
  1666. for (size_t j = 0; j < names_out.size(); j++) {
  1667. names_out[j] = names[j + 1].c_str();
  1668. }
  1669. callback.group_cb(user_data, &names_out.at(0),
  1670. static_cast<int>(names_out.size()));
  1671. } else {
  1672. callback.group_cb(user_data, NULL, 0);
  1673. }
  1674. }
  1675. continue;
  1676. }
  1677. // object name
  1678. if (token[0] == 'o' && IS_SPACE((token[1]))) {
  1679. // @todo { multiple object name? }
  1680. char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
  1681. token += 2;
  1682. #ifdef _MSC_VER
  1683. sscanf_s(token, "%s", namebuf, (unsigned)_countof(namebuf));
  1684. #else
  1685. std::sscanf(token, "%s", namebuf);
  1686. #endif
  1687. std::string object_name = std::string(namebuf);
  1688. if (callback.object_cb) {
  1689. callback.object_cb(user_data, object_name.c_str());
  1690. }
  1691. continue;
  1692. }
  1693. #if 0 // @todo
  1694. if (token[0] == 't' && IS_SPACE(token[1])) {
  1695. tag_t tag;
  1696. char namebuf[4096];
  1697. token += 2;
  1698. #ifdef _MSC_VER
  1699. sscanf_s(token, "%s", namebuf, (unsigned)_countof(namebuf));
  1700. #else
  1701. std::sscanf(token, "%s", namebuf);
  1702. #endif
  1703. tag.name = std::string(namebuf);
  1704. token += tag.name.size() + 1;
  1705. tag_sizes ts = parseTagTriple(&token);
  1706. tag.intValues.resize(static_cast<size_t>(ts.num_ints));
  1707. for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
  1708. tag.intValues[i] = atoi(token);
  1709. token += strcspn(token, "/ \t\r") + 1;
  1710. }
  1711. tag.floatValues.resize(static_cast<size_t>(ts.num_reals));
  1712. for (size_t i = 0; i < static_cast<size_t>(ts.num_reals); ++i) {
  1713. tag.floatValues[i] = parseReal(&token);
  1714. token += strcspn(token, "/ \t\r") + 1;
  1715. }
  1716. tag.stringValues.resize(static_cast<size_t>(ts.num_strings));
  1717. for (size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
  1718. char stringValueBuffer[4096];
  1719. #ifdef _MSC_VER
  1720. sscanf_s(token, "%s", stringValueBuffer,
  1721. (unsigned)_countof(stringValueBuffer));
  1722. #else
  1723. std::sscanf(token, "%s", stringValueBuffer);
  1724. #endif
  1725. tag.stringValues[i] = stringValueBuffer;
  1726. token += tag.stringValues[i].size() + 1;
  1727. }
  1728. tags.push_back(tag);
  1729. }
  1730. #endif
  1731. // Ignore unknown command.
  1732. }
  1733. if (err) {
  1734. (*err) += errss.str();
  1735. }
  1736. return true;
  1737. }
  1738. } // namespace tinyobj
  1739. #endif