tiny_obj_loader.h 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2012-2018 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 2.0.0 : Add new object oriented API. 1.x API is still provided.
  22. // * Support line primitive.
  23. // * Support points primitive.
  24. // * Support multiple search path for .mtl(v1 API).
  25. // version 1.4.0 : Modifed ParseTextureNameAndOption API
  26. // version 1.3.1 : Make ParseTextureNameAndOption API public
  27. // version 1.3.0 : Separate warning and error message(breaking API of LoadObj)
  28. // version 1.2.3 : Added color space extension('-colorspace') to tex opts.
  29. // version 1.2.2 : Parse multiple group names.
  30. // version 1.2.1 : Added initial support for line('l') primitive(PR #178)
  31. // version 1.2.0 : Hardened implementation(#175)
  32. // version 1.1.1 : Support smoothing groups(#162)
  33. // version 1.1.0 : Support parsing vertex color(#144)
  34. // version 1.0.8 : Fix parsing `g` tag just after `usemtl`(#138)
  35. // version 1.0.7 : Support multiple tex options(#126)
  36. // version 1.0.6 : Add TINYOBJLOADER_USE_DOUBLE option(#124)
  37. // version 1.0.5 : Ignore `Tr` when `d` exists in MTL(#43)
  38. // version 1.0.4 : Support multiple filenames for 'mtllib'(#112)
  39. // version 1.0.3 : Support parsing texture options(#85)
  40. // version 1.0.2 : Improve parsing speed by about a factor of 2 for large
  41. // files(#105)
  42. // version 1.0.1 : Fixes a shape is lost if obj ends with a 'usemtl'(#104)
  43. // version 1.0.0 : Change data structure. Change license from BSD to MIT.
  44. //
  45. //
  46. // Use this in *one* .cc
  47. // #define TINYOBJLOADER_IMPLEMENTATION
  48. // #include "tiny_obj_loader.h"
  49. //
  50. #ifndef TINY_OBJ_LOADER_H_
  51. #define TINY_OBJ_LOADER_H_
  52. #include <map>
  53. #include <string>
  54. #include <vector>
  55. namespace tinyobj {
  56. // TODO(syoyo): Better C++11 detection for older compiler
  57. #if __cplusplus > 199711L
  58. #define TINYOBJ_OVERRIDE override
  59. #else
  60. #define TINYOBJ_OVERRIDE
  61. #endif
  62. #ifdef __clang__
  63. #pragma clang diagnostic push
  64. #if __has_warning("-Wzero-as-null-pointer-constant")
  65. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
  66. #endif
  67. #pragma clang diagnostic ignored "-Wpadded"
  68. #endif
  69. // https://en.wikipedia.org/wiki/Wavefront_.obj_file says ...
  70. //
  71. // -blendu on | off # set horizontal texture blending
  72. // (default on)
  73. // -blendv on | off # set vertical texture blending
  74. // (default on)
  75. // -boost real_value # boost mip-map sharpness
  76. // -mm base_value gain_value # modify texture map values (default
  77. // 0 1)
  78. // # base_value = brightness,
  79. // gain_value = contrast
  80. // -o u [v [w]] # Origin offset (default
  81. // 0 0 0)
  82. // -s u [v [w]] # Scale (default
  83. // 1 1 1)
  84. // -t u [v [w]] # Turbulence (default
  85. // 0 0 0)
  86. // -texres resolution # texture resolution to create
  87. // -clamp on | off # only render texels in the clamped
  88. // 0-1 range (default off)
  89. // # When unclamped, textures are
  90. // repeated across a surface,
  91. // # when clamped, only texels which
  92. // fall within the 0-1
  93. // # range are rendered.
  94. // -bm mult_value # bump multiplier (for bump maps
  95. // only)
  96. //
  97. // -imfchan r | g | b | m | l | z # specifies which channel of the file
  98. // is used to
  99. // # create a scalar or bump texture.
  100. // r:red, g:green,
  101. // # b:blue, m:matte, l:luminance,
  102. // z:z-depth..
  103. // # (the default for bump is 'l' and
  104. // for decal is 'm')
  105. // bump -imfchan r bumpmap.tga # says to use the red channel of
  106. // bumpmap.tga as the bumpmap
  107. //
  108. // For reflection maps...
  109. //
  110. // -type sphere # specifies a sphere for a "refl"
  111. // reflection map
  112. // -type cube_top | cube_bottom | # when using a cube map, the texture
  113. // file for each
  114. // cube_front | cube_back | # side of the cube is specified
  115. // separately
  116. // cube_left | cube_right
  117. //
  118. // TinyObjLoader extension.
  119. //
  120. // -colorspace SPACE # Color space of the texture. e.g.
  121. // 'sRGB` or 'linear'
  122. //
  123. #ifdef TINYOBJLOADER_USE_DOUBLE
  124. //#pragma message "using double"
  125. typedef double real_t;
  126. #else
  127. //#pragma message "using float"
  128. typedef float real_t;
  129. #endif
  130. typedef enum {
  131. TEXTURE_TYPE_NONE, // default
  132. TEXTURE_TYPE_SPHERE,
  133. TEXTURE_TYPE_CUBE_TOP,
  134. TEXTURE_TYPE_CUBE_BOTTOM,
  135. TEXTURE_TYPE_CUBE_FRONT,
  136. TEXTURE_TYPE_CUBE_BACK,
  137. TEXTURE_TYPE_CUBE_LEFT,
  138. TEXTURE_TYPE_CUBE_RIGHT
  139. } texture_type_t;
  140. struct texture_option_t {
  141. texture_type_t type; // -type (default TEXTURE_TYPE_NONE)
  142. real_t sharpness; // -boost (default 1.0?)
  143. real_t brightness; // base_value in -mm option (default 0)
  144. real_t contrast; // gain_value in -mm option (default 1)
  145. real_t origin_offset[3]; // -o u [v [w]] (default 0 0 0)
  146. real_t scale[3]; // -s u [v [w]] (default 1 1 1)
  147. real_t turbulence[3]; // -t u [v [w]] (default 0 0 0)
  148. int texture_resolution; // -texres resolution (No default value in the spec. We'll use -1)
  149. bool clamp; // -clamp (default false)
  150. char imfchan; // -imfchan (the default for bump is 'l' and for decal is 'm')
  151. bool blendu; // -blendu (default on)
  152. bool blendv; // -blendv (default on)
  153. real_t bump_multiplier; // -bm (for bump maps only, default 1.0)
  154. // extension
  155. std::string colorspace; // Explicitly specify color space of stored texel
  156. // value. Usually `sRGB` or `linear` (default empty).
  157. };
  158. struct material_t {
  159. std::string name;
  160. real_t ambient[3];
  161. real_t diffuse[3];
  162. real_t specular[3];
  163. real_t transmittance[3];
  164. real_t emission[3];
  165. real_t shininess;
  166. real_t ior; // index of refraction
  167. real_t dissolve; // 1 == opaque; 0 == fully transparent
  168. // illumination model (see http://www.fileformat.info/format/material/)
  169. int illum;
  170. int dummy; // Suppress padding warning.
  171. std::string ambient_texname; // map_Ka
  172. std::string diffuse_texname; // map_Kd
  173. std::string specular_texname; // map_Ks
  174. std::string specular_highlight_texname; // map_Ns
  175. std::string bump_texname; // map_bump, map_Bump, bump
  176. std::string displacement_texname; // disp
  177. std::string alpha_texname; // map_d
  178. std::string reflection_texname; // refl
  179. texture_option_t ambient_texopt;
  180. texture_option_t diffuse_texopt;
  181. texture_option_t specular_texopt;
  182. texture_option_t specular_highlight_texopt;
  183. texture_option_t bump_texopt;
  184. texture_option_t displacement_texopt;
  185. texture_option_t alpha_texopt;
  186. texture_option_t reflection_texopt;
  187. // PBR extension
  188. // http://exocortex.com/blog/extending_wavefront_mtl_to_support_pbr
  189. real_t roughness; // [0, 1] default 0
  190. real_t metallic; // [0, 1] default 0
  191. real_t sheen; // [0, 1] default 0
  192. real_t clearcoat_thickness; // [0, 1] default 0
  193. real_t clearcoat_roughness; // [0, 1] default 0
  194. real_t anisotropy; // aniso. [0, 1] default 0
  195. real_t anisotropy_rotation; // anisor. [0, 1] default 0
  196. real_t pad0;
  197. std::string roughness_texname; // map_Pr
  198. std::string metallic_texname; // map_Pm
  199. std::string sheen_texname; // map_Ps
  200. std::string emissive_texname; // map_Ke
  201. std::string normal_texname; // norm. For normal mapping.
  202. texture_option_t roughness_texopt;
  203. texture_option_t metallic_texopt;
  204. texture_option_t sheen_texopt;
  205. texture_option_t emissive_texopt;
  206. texture_option_t normal_texopt;
  207. int pad2;
  208. std::map<std::string, std::string> unknown_parameter;
  209. #ifdef TINY_OBJ_LOADER_PYTHON_BINDING
  210. // For pybind11
  211. std::array<double, 3> GetDiffuse() {
  212. std::array<double, 3> values;
  213. values[0] = double(diffuse[0]);
  214. values[1] = double(diffuse[1]);
  215. values[2] = double(diffuse[2]);
  216. return values;
  217. }
  218. std::array<double, 3> GetSpecular() {
  219. std::array<double, 3> values;
  220. values[0] = double(specular[0]);
  221. values[1] = double(specular[1]);
  222. values[2] = double(specular[2]);
  223. return values;
  224. }
  225. std::array<double, 3> GetTransmittance() {
  226. std::array<double, 3> values;
  227. values[0] = double(transmittance[0]);
  228. values[1] = double(transmittance[1]);
  229. values[2] = double(transmittance[2]);
  230. return values;
  231. }
  232. std::array<double, 3> GetEmission() {
  233. std::array<double, 3> values;
  234. values[0] = double(emission[0]);
  235. values[1] = double(emission[1]);
  236. values[2] = double(emission[2]);
  237. return values;
  238. }
  239. std::array<double, 3> GetAmbient() {
  240. std::array<double, 3> values;
  241. values[0] = double(ambient[0]);
  242. values[1] = double(ambient[1]);
  243. values[2] = double(ambient[2]);
  244. return values;
  245. }
  246. void SetDiffuse(std::array<double, 3> &a) {
  247. diffuse[0] = real_t(a[0]);
  248. diffuse[1] = real_t(a[1]);
  249. diffuse[2] = real_t(a[2]);
  250. }
  251. void SetAmbient(std::array<double, 3> &a) {
  252. ambient[0] = real_t(a[0]);
  253. ambient[1] = real_t(a[1]);
  254. ambient[2] = real_t(a[2]);
  255. }
  256. void SetSpecular(std::array<double, 3> &a) {
  257. specular[0] = real_t(a[0]);
  258. specular[1] = real_t(a[1]);
  259. specular[2] = real_t(a[2]);
  260. }
  261. void SetTransmittance(std::array<double, 3> &a) {
  262. transmittance[0] = real_t(a[0]);
  263. transmittance[1] = real_t(a[1]);
  264. transmittance[2] = real_t(a[2]);
  265. }
  266. std::string GetCustomParameter(const std::string &key) {
  267. std::map<std::string, std::string>::const_iterator it =
  268. unknown_parameter.find(key);
  269. if (it != unknown_parameter.end()) {
  270. return it->second;
  271. }
  272. return std::string();
  273. }
  274. #endif
  275. };
  276. struct tag_t {
  277. std::string name;
  278. std::vector<int> intValues;
  279. std::vector<real_t> floatValues;
  280. std::vector<std::string> stringValues;
  281. };
  282. // Index struct to support different indices for vtx/normal/texcoord.
  283. // -1 means not used.
  284. struct index_t {
  285. int vertex_index;
  286. int normal_index;
  287. int texcoord_index;
  288. };
  289. struct mesh_t {
  290. std::vector<index_t> indices;
  291. std::vector<unsigned char>
  292. num_face_vertices; // The number of vertices per
  293. // face. 3 = triangle, 4 = quad,
  294. // ... Up to 255 vertices per face.
  295. std::vector<int> material_ids; // per-face material ID
  296. std::vector<unsigned int> smoothing_group_ids; // per-face smoothing group
  297. // ID(0 = off. positive value
  298. // = group id)
  299. std::vector<tag_t> tags; // SubD tag
  300. };
  301. // struct path_t {
  302. // std::vector<int> indices; // pairs of indices for lines
  303. //};
  304. struct lines_t {
  305. // Linear flattened indices.
  306. std::vector<index_t> indices; // indices for vertices(poly lines)
  307. std::vector<int> num_line_vertices; // The number of vertices per line.
  308. };
  309. struct points_t {
  310. std::vector<index_t> indices; // indices for points
  311. };
  312. struct shape_t {
  313. std::string name;
  314. mesh_t mesh;
  315. lines_t lines;
  316. points_t points;
  317. };
  318. // Vertex attributes
  319. struct attrib_t {
  320. std::vector<real_t> vertices; // 'v'(xyz)
  321. // For backward compatibility, we store vertex weight in separate array.
  322. std::vector<real_t> vertex_weights; // 'v'(w)
  323. std::vector<real_t> normals; // 'vn'
  324. std::vector<real_t> texcoords; // 'vt'(uv)
  325. // For backward compatibility, we store texture coordinate 'w' in separate
  326. // array.
  327. std::vector<real_t> texcoord_ws; // 'vt'(w)
  328. std::vector<real_t> colors; // extension: vertex colors
  329. attrib_t() {}
  330. //
  331. // For pybind11
  332. //
  333. const std::vector<real_t> &GetVertices() const { return vertices; }
  334. const std::vector<real_t> &GetVertexWeights() const { return vertex_weights; }
  335. };
  336. struct callback_t {
  337. // W is optional and set to 1 if there is no `w` item in `v` line
  338. void (*vertex_cb)(void *user_data, real_t x, real_t y, real_t z, real_t w);
  339. void (*normal_cb)(void *user_data, real_t x, real_t y, real_t z);
  340. // y and z are optional and set to 0 if there is no `y` and/or `z` item(s) in
  341. // `vt` line.
  342. void (*texcoord_cb)(void *user_data, real_t x, real_t y, real_t z);
  343. // called per 'f' line. num_indices is the number of face indices(e.g. 3 for
  344. // triangle, 4 for quad)
  345. // 0 will be passed for undefined index in index_t members.
  346. void (*index_cb)(void *user_data, index_t *indices, int num_indices);
  347. // `name` material name, `material_id` = the array index of material_t[]. -1
  348. // if
  349. // a material not found in .mtl
  350. void (*usemtl_cb)(void *user_data, const char *name, int material_id);
  351. // `materials` = parsed material data.
  352. void (*mtllib_cb)(void *user_data, const material_t *materials,
  353. int num_materials);
  354. // There may be multiple group names
  355. void (*group_cb)(void *user_data, const char **names, int num_names);
  356. void (*object_cb)(void *user_data, const char *name);
  357. callback_t()
  358. : vertex_cb(NULL),
  359. normal_cb(NULL),
  360. texcoord_cb(NULL),
  361. index_cb(NULL),
  362. usemtl_cb(NULL),
  363. mtllib_cb(NULL),
  364. group_cb(NULL),
  365. object_cb(NULL) {}
  366. };
  367. class MaterialReader {
  368. public:
  369. MaterialReader() {}
  370. virtual ~MaterialReader();
  371. virtual bool operator()(const std::string &matId,
  372. std::vector<material_t> *materials,
  373. std::map<std::string, int> *matMap, std::string *warn,
  374. std::string *err) = 0;
  375. };
  376. ///
  377. /// Read .mtl from a file.
  378. ///
  379. class MaterialFileReader : public MaterialReader {
  380. public:
  381. // Path could contain separator(';' in Windows, ':' in Posix)
  382. explicit MaterialFileReader(const std::string &mtl_basedir)
  383. : m_mtlBaseDir(mtl_basedir) {}
  384. virtual ~MaterialFileReader() TINYOBJ_OVERRIDE {}
  385. virtual bool operator()(const std::string &matId,
  386. std::vector<material_t> *materials,
  387. std::map<std::string, int> *matMap, std::string *warn,
  388. std::string *err) TINYOBJ_OVERRIDE;
  389. private:
  390. std::string m_mtlBaseDir;
  391. };
  392. ///
  393. /// Read .mtl from a stream.
  394. ///
  395. class MaterialStreamReader : public MaterialReader {
  396. public:
  397. explicit MaterialStreamReader(std::istream &inStream)
  398. : m_inStream(inStream) {}
  399. virtual ~MaterialStreamReader() TINYOBJ_OVERRIDE {}
  400. virtual bool operator()(const std::string &matId,
  401. std::vector<material_t> *materials,
  402. std::map<std::string, int> *matMap, std::string *warn,
  403. std::string *err) TINYOBJ_OVERRIDE;
  404. private:
  405. std::istream &m_inStream;
  406. };
  407. // v2 API
  408. struct ObjReaderConfig {
  409. bool triangulate; // triangulate polygon?
  410. /// Parse vertex color.
  411. /// If vertex color is not present, its filled with default value.
  412. /// false = no vertex color
  413. /// This will increase memory of parsed .obj
  414. bool vertex_color;
  415. ///
  416. /// Search path to .mtl file.
  417. /// Default = "" = search from the same directory of .obj file.
  418. /// Valid only when loading .obj from a file.
  419. ///
  420. std::string mtl_search_path;
  421. ObjReaderConfig() : triangulate(true), vertex_color(true) {}
  422. };
  423. ///
  424. /// Wavefront .obj reader class(v2 API)
  425. ///
  426. class ObjReader {
  427. public:
  428. ObjReader() : valid_(false) {}
  429. ~ObjReader() {}
  430. ///
  431. /// Load .obj and .mtl from a file.
  432. ///
  433. /// @param[in] filename wavefront .obj filename
  434. /// @param[in] config Reader configuration
  435. ///
  436. bool ParseFromFile(const std::string &filename,
  437. const ObjReaderConfig &config = ObjReaderConfig());
  438. ///
  439. /// Parse .obj from a text string.
  440. /// Need to supply .mtl text string by `mtl_text`.
  441. /// This function ignores `mtllib` line in .obj text.
  442. ///
  443. /// @param[in] obj_text wavefront .obj filename
  444. /// @param[in] mtl_text wavefront .mtl filename
  445. /// @param[in] config Reader configuration
  446. ///
  447. bool ParseFromString(const std::string &obj_text, const std::string &mtl_text,
  448. const ObjReaderConfig &config = ObjReaderConfig());
  449. ///
  450. /// .obj was loaded or parsed correctly.
  451. ///
  452. bool Valid() const { return valid_; }
  453. const attrib_t &GetAttrib() const { return attrib_; }
  454. const std::vector<shape_t> &GetShapes() const { return shapes_; }
  455. const std::vector<material_t> &GetMaterials() const { return materials_; }
  456. ///
  457. /// Warning message(may be filled after `Load` or `Parse`)
  458. ///
  459. const std::string &Warning() const { return warning_; }
  460. ///
  461. /// Error message(filled when `Load` or `Parse` failed)
  462. ///
  463. const std::string &Error() const { return error_; }
  464. private:
  465. bool valid_;
  466. attrib_t attrib_;
  467. std::vector<shape_t> shapes_;
  468. std::vector<material_t> materials_;
  469. std::string warning_;
  470. std::string error_;
  471. };
  472. /// ==>>========= Legacy v1 API =============================================
  473. /// Loads .obj from a file.
  474. /// 'attrib', 'shapes' and 'materials' will be filled with parsed shape data
  475. /// 'shapes' will be filled with parsed shape data
  476. /// Returns true when loading .obj become success.
  477. /// Returns warning message into `warn`, and error message into `err`
  478. /// 'mtl_basedir' is optional, and used for base directory for .mtl file.
  479. /// In default(`NULL'), .mtl file is searched from an application's working
  480. /// directory.
  481. /// 'triangulate' is optional, and used whether triangulate polygon face in .obj
  482. /// or not.
  483. /// Option 'default_vcols_fallback' specifies whether vertex colors should
  484. /// always be defined, even if no colors are given (fallback to white).
  485. bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
  486. std::vector<material_t> *materials, std::string *warn,
  487. std::string *err, const char *filename,
  488. const char *mtl_basedir = NULL, bool triangulate = true,
  489. bool default_vcols_fallback = true);
  490. /// Loads .obj from a file with custom user callback.
  491. /// .mtl is loaded as usual and parsed material_t data will be passed to
  492. /// `callback.mtllib_cb`.
  493. /// Returns true when loading .obj/.mtl become success.
  494. /// Returns warning message into `warn`, and error message into `err`
  495. /// See `examples/callback_api/` for how to use this function.
  496. bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
  497. void *user_data = NULL,
  498. MaterialReader *readMatFn = NULL,
  499. std::string *warn = NULL, std::string *err = NULL);
  500. /// Loads object from a std::istream, uses `readMatFn` to retrieve
  501. /// std::istream for materials.
  502. /// Returns true when loading .obj become success.
  503. /// Returns warning and error message into `err`
  504. bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
  505. std::vector<material_t> *materials, std::string *warn,
  506. std::string *err, std::istream *inStream,
  507. MaterialReader *readMatFn = NULL, bool triangulate = true,
  508. bool default_vcols_fallback = true);
  509. /// Loads materials into std::map
  510. void LoadMtl(std::map<std::string, int> *material_map,
  511. std::vector<material_t> *materials, std::istream *inStream,
  512. std::string *warning, std::string *err);
  513. ///
  514. /// Parse texture name and texture option for custom texture parameter through
  515. /// material::unknown_parameter
  516. ///
  517. /// @param[out] texname Parsed texture name
  518. /// @param[out] texopt Parsed texopt
  519. /// @param[in] linebuf Input string
  520. ///
  521. bool ParseTextureNameAndOption(std::string *texname, texture_option_t *texopt,
  522. const char *linebuf);
  523. /// =<<========== Legacy v1 API =============================================
  524. } // namespace tinyobj
  525. #endif // TINY_OBJ_LOADER_H_
  526. #ifdef TINYOBJLOADER_IMPLEMENTATION
  527. #include <cassert>
  528. #include <cctype>
  529. #include <cmath>
  530. #include <cstddef>
  531. #include <cstdlib>
  532. #include <cstring>
  533. #include <limits>
  534. #include <utility>
  535. #include <fstream>
  536. #include <sstream>
  537. namespace tinyobj {
  538. MaterialReader::~MaterialReader() {}
  539. struct vertex_index_t {
  540. int v_idx, vt_idx, vn_idx;
  541. vertex_index_t() : v_idx(-1), vt_idx(-1), vn_idx(-1) {}
  542. explicit vertex_index_t(int idx) : v_idx(idx), vt_idx(idx), vn_idx(idx) {}
  543. vertex_index_t(int vidx, int vtidx, int vnidx)
  544. : v_idx(vidx), vt_idx(vtidx), vn_idx(vnidx) {}
  545. };
  546. // Internal data structure for face representation
  547. // index + smoothing group.
  548. struct face_t {
  549. unsigned int
  550. smoothing_group_id; // smoothing group id. 0 = smoothing groupd is off.
  551. int pad_;
  552. std::vector<vertex_index_t> vertex_indices; // face vertex indices.
  553. face_t() : smoothing_group_id(0), pad_(0) {}
  554. };
  555. // Internal data structure for line representation
  556. struct __line_t {
  557. // l v1/vt1 v2/vt2 ...
  558. // In the specification, line primitrive does not have normal index, but
  559. // TinyObjLoader allow it
  560. std::vector<vertex_index_t> vertex_indices;
  561. };
  562. // Internal data structure for points representation
  563. struct __points_t {
  564. // p v1 v2 ...
  565. // In the specification, point primitrive does not have normal index and
  566. // texture coord index, but TinyObjLoader allow it.
  567. std::vector<vertex_index_t> vertex_indices;
  568. };
  569. struct tag_sizes {
  570. tag_sizes() : num_ints(0), num_reals(0), num_strings(0) {}
  571. int num_ints;
  572. int num_reals;
  573. int num_strings;
  574. };
  575. struct obj_shape {
  576. std::vector<real_t> v;
  577. std::vector<real_t> vn;
  578. std::vector<real_t> vt;
  579. };
  580. //
  581. // Manages group of primitives(face, line, points, ...)
  582. struct PrimGroup {
  583. std::vector<face_t> faceGroup;
  584. std::vector<__line_t> lineGroup;
  585. std::vector<__points_t> pointsGroup;
  586. void clear() {
  587. faceGroup.clear();
  588. lineGroup.clear();
  589. pointsGroup.clear();
  590. }
  591. bool IsEmpty() const {
  592. return faceGroup.empty() && lineGroup.empty() && pointsGroup.empty();
  593. }
  594. // TODO(syoyo): bspline, surface, ...
  595. };
  596. // See
  597. // http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
  598. static std::istream &safeGetline(std::istream &is, std::string &t) {
  599. t.clear();
  600. // The characters in the stream are read one-by-one using a std::streambuf.
  601. // That is faster than reading them one-by-one using the std::istream.
  602. // Code that uses streambuf this way must be guarded by a sentry object.
  603. // The sentry object performs various tasks,
  604. // such as thread synchronization and updating the stream state.
  605. std::istream::sentry se(is, true);
  606. std::streambuf *sb = is.rdbuf();
  607. if (se) {
  608. for (;;) {
  609. int c = sb->sbumpc();
  610. switch (c) {
  611. case '\n':
  612. return is;
  613. case '\r':
  614. if (sb->sgetc() == '\n') sb->sbumpc();
  615. return is;
  616. case EOF:
  617. // Also handle the case when the last line has no line ending
  618. if (t.empty()) is.setstate(std::ios::eofbit);
  619. return is;
  620. default:
  621. t += static_cast<char>(c);
  622. }
  623. }
  624. }
  625. return is;
  626. }
  627. #define IS_SPACE(x) (((x) == ' ') || ((x) == '\t'))
  628. #define IS_DIGIT(x) \
  629. (static_cast<unsigned int>((x) - '0') < static_cast<unsigned int>(10))
  630. #define IS_NEW_LINE(x) (((x) == '\r') || ((x) == '\n') || ((x) == '\0'))
  631. // Make index zero-base, and also support relative index.
  632. static inline bool fixIndex(int idx, int n, int *ret) {
  633. if (!ret) {
  634. return false;
  635. }
  636. if (idx > 0) {
  637. (*ret) = idx - 1;
  638. return true;
  639. }
  640. if (idx == 0) {
  641. // zero is not allowed according to the spec.
  642. return false;
  643. }
  644. if (idx < 0) {
  645. (*ret) = n + idx; // negative value = relative
  646. return true;
  647. }
  648. return false; // never reach here.
  649. }
  650. static inline std::string parseString(const char **token) {
  651. std::string s;
  652. (*token) += strspn((*token), " \t");
  653. size_t e = strcspn((*token), " \t\r");
  654. s = std::string((*token), &(*token)[e]);
  655. (*token) += e;
  656. return s;
  657. }
  658. static inline int parseInt(const char **token) {
  659. (*token) += strspn((*token), " \t");
  660. int i = atoi((*token));
  661. (*token) += strcspn((*token), " \t\r");
  662. return i;
  663. }
  664. // Tries to parse a floating point number located at s.
  665. //
  666. // s_end should be a location in the string where reading should absolutely
  667. // stop. For example at the end of the string, to prevent buffer overflows.
  668. //
  669. // Parses the following EBNF grammar:
  670. // sign = "+" | "-" ;
  671. // END = ? anything not in digit ?
  672. // digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
  673. // integer = [sign] , digit , {digit} ;
  674. // decimal = integer , ["." , integer] ;
  675. // float = ( decimal , END ) | ( decimal , ("E" | "e") , integer , END ) ;
  676. //
  677. // Valid strings are for example:
  678. // -0 +3.1417e+2 -0.0E-3 1.0324 -1.41 11e2
  679. //
  680. // If the parsing is a success, result is set to the parsed value and true
  681. // is returned.
  682. //
  683. // The function is greedy and will parse until any of the following happens:
  684. // - a non-conforming character is encountered.
  685. // - s_end is reached.
  686. //
  687. // The following situations triggers a failure:
  688. // - s >= s_end.
  689. // - parse failure.
  690. //
  691. static bool tryParseDouble(const char *s, const char *s_end, double *result) {
  692. if (s >= s_end) {
  693. return false;
  694. }
  695. double mantissa = 0.0;
  696. // This exponent is base 2 rather than 10.
  697. // However the exponent we parse is supposed to be one of ten,
  698. // thus we must take care to convert the exponent/and or the
  699. // mantissa to a * 2^E, where a is the mantissa and E is the
  700. // exponent.
  701. // To get the final double we will use ldexp, it requires the
  702. // exponent to be in base 2.
  703. int exponent = 0;
  704. // NOTE: THESE MUST BE DECLARED HERE SINCE WE ARE NOT ALLOWED
  705. // TO JUMP OVER DEFINITIONS.
  706. char sign = '+';
  707. char exp_sign = '+';
  708. char const *curr = s;
  709. // How many characters were read in a loop.
  710. int read = 0;
  711. // Tells whether a loop terminated due to reaching s_end.
  712. bool end_not_reached = false;
  713. bool leading_decimal_dots = false;
  714. /*
  715. BEGIN PARSING.
  716. */
  717. // Find out what sign we've got.
  718. if (*curr == '+' || *curr == '-') {
  719. sign = *curr;
  720. curr++;
  721. if ((curr != s_end) && (*curr == '.')) {
  722. // accept. Somethig like `.7e+2`, `-.5234`
  723. leading_decimal_dots = true;
  724. }
  725. } else if (IS_DIGIT(*curr)) { /* Pass through. */
  726. } else if (*curr == '.') {
  727. // accept. Somethig like `.7e+2`, `-.5234`
  728. leading_decimal_dots = true;
  729. } else {
  730. goto fail;
  731. }
  732. // Read the integer part.
  733. end_not_reached = (curr != s_end);
  734. if (!leading_decimal_dots) {
  735. while (end_not_reached && IS_DIGIT(*curr)) {
  736. mantissa *= 10;
  737. mantissa += static_cast<int>(*curr - 0x30);
  738. curr++;
  739. read++;
  740. end_not_reached = (curr != s_end);
  741. }
  742. // We must make sure we actually got something.
  743. if (read == 0) goto fail;
  744. }
  745. // We allow numbers of form "#", "###" etc.
  746. if (!end_not_reached) goto assemble;
  747. // Read the decimal part.
  748. if (*curr == '.') {
  749. curr++;
  750. read = 1;
  751. end_not_reached = (curr != s_end);
  752. while (end_not_reached && IS_DIGIT(*curr)) {
  753. static const double pow_lut[] = {
  754. 1.0, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001,
  755. };
  756. const int lut_entries = sizeof pow_lut / sizeof pow_lut[0];
  757. // NOTE: Don't use powf here, it will absolutely murder precision.
  758. mantissa += static_cast<int>(*curr - 0x30) *
  759. (read < lut_entries ? pow_lut[read] : std::pow(10.0, -read));
  760. read++;
  761. curr++;
  762. end_not_reached = (curr != s_end);
  763. }
  764. } else if (*curr == 'e' || *curr == 'E') {
  765. } else {
  766. goto assemble;
  767. }
  768. if (!end_not_reached) goto assemble;
  769. // Read the exponent part.
  770. if (*curr == 'e' || *curr == 'E') {
  771. curr++;
  772. // Figure out if a sign is present and if it is.
  773. end_not_reached = (curr != s_end);
  774. if (end_not_reached && (*curr == '+' || *curr == '-')) {
  775. exp_sign = *curr;
  776. curr++;
  777. } else if (IS_DIGIT(*curr)) { /* Pass through. */
  778. } else {
  779. // Empty E is not allowed.
  780. goto fail;
  781. }
  782. read = 0;
  783. end_not_reached = (curr != s_end);
  784. while (end_not_reached && IS_DIGIT(*curr)) {
  785. exponent *= 10;
  786. exponent += static_cast<int>(*curr - 0x30);
  787. curr++;
  788. read++;
  789. end_not_reached = (curr != s_end);
  790. }
  791. exponent *= (exp_sign == '+' ? 1 : -1);
  792. if (read == 0) goto fail;
  793. }
  794. assemble:
  795. *result = (sign == '+' ? 1 : -1) *
  796. (exponent ? std::ldexp(mantissa * std::pow(5.0, exponent), exponent)
  797. : mantissa);
  798. return true;
  799. fail:
  800. return false;
  801. }
  802. static inline real_t parseReal(const char **token, double default_value = 0.0) {
  803. (*token) += strspn((*token), " \t");
  804. const char *end = (*token) + strcspn((*token), " \t\r");
  805. double val = default_value;
  806. tryParseDouble((*token), end, &val);
  807. real_t f = static_cast<real_t>(val);
  808. (*token) = end;
  809. return f;
  810. }
  811. static inline bool parseReal(const char **token, real_t *out) {
  812. (*token) += strspn((*token), " \t");
  813. const char *end = (*token) + strcspn((*token), " \t\r");
  814. double val;
  815. bool ret = tryParseDouble((*token), end, &val);
  816. if (ret) {
  817. real_t f = static_cast<real_t>(val);
  818. (*out) = f;
  819. }
  820. (*token) = end;
  821. return ret;
  822. }
  823. static inline void parseReal2(real_t *x, real_t *y, const char **token,
  824. const double default_x = 0.0,
  825. const double default_y = 0.0) {
  826. (*x) = parseReal(token, default_x);
  827. (*y) = parseReal(token, default_y);
  828. }
  829. static inline void parseReal3(real_t *x, real_t *y, real_t *z,
  830. const char **token, const double default_x = 0.0,
  831. const double default_y = 0.0,
  832. const double default_z = 0.0) {
  833. (*x) = parseReal(token, default_x);
  834. (*y) = parseReal(token, default_y);
  835. (*z) = parseReal(token, default_z);
  836. }
  837. static inline void parseV(real_t *x, real_t *y, real_t *z, real_t *w,
  838. const char **token, const double default_x = 0.0,
  839. const double default_y = 0.0,
  840. const double default_z = 0.0,
  841. const double default_w = 1.0) {
  842. (*x) = parseReal(token, default_x);
  843. (*y) = parseReal(token, default_y);
  844. (*z) = parseReal(token, default_z);
  845. (*w) = parseReal(token, default_w);
  846. }
  847. // Extension: parse vertex with colors(6 items)
  848. static inline bool parseVertexWithColor(real_t *x, real_t *y, real_t *z,
  849. real_t *r, real_t *g, real_t *b,
  850. const char **token,
  851. const double default_x = 0.0,
  852. const double default_y = 0.0,
  853. const double default_z = 0.0) {
  854. (*x) = parseReal(token, default_x);
  855. (*y) = parseReal(token, default_y);
  856. (*z) = parseReal(token, default_z);
  857. const bool found_color =
  858. parseReal(token, r) && parseReal(token, g) && parseReal(token, b);
  859. if (!found_color) {
  860. (*r) = (*g) = (*b) = 1.0;
  861. }
  862. return found_color;
  863. }
  864. static inline bool parseOnOff(const char **token, bool default_value = true) {
  865. (*token) += strspn((*token), " \t");
  866. const char *end = (*token) + strcspn((*token), " \t\r");
  867. bool ret = default_value;
  868. if ((0 == strncmp((*token), "on", 2))) {
  869. ret = true;
  870. } else if ((0 == strncmp((*token), "off", 3))) {
  871. ret = false;
  872. }
  873. (*token) = end;
  874. return ret;
  875. }
  876. static inline texture_type_t parseTextureType(
  877. const char **token, texture_type_t default_value = TEXTURE_TYPE_NONE) {
  878. (*token) += strspn((*token), " \t");
  879. const char *end = (*token) + strcspn((*token), " \t\r");
  880. texture_type_t ty = default_value;
  881. if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) {
  882. ty = TEXTURE_TYPE_CUBE_TOP;
  883. } else if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) {
  884. ty = TEXTURE_TYPE_CUBE_BOTTOM;
  885. } else if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) {
  886. ty = TEXTURE_TYPE_CUBE_LEFT;
  887. } else if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) {
  888. ty = TEXTURE_TYPE_CUBE_RIGHT;
  889. } else if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) {
  890. ty = TEXTURE_TYPE_CUBE_FRONT;
  891. } else if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) {
  892. ty = TEXTURE_TYPE_CUBE_BACK;
  893. } else if ((0 == strncmp((*token), "sphere", strlen("sphere")))) {
  894. ty = TEXTURE_TYPE_SPHERE;
  895. }
  896. (*token) = end;
  897. return ty;
  898. }
  899. static tag_sizes parseTagTriple(const char **token) {
  900. tag_sizes ts;
  901. (*token) += strspn((*token), " \t");
  902. ts.num_ints = atoi((*token));
  903. (*token) += strcspn((*token), "/ \t\r");
  904. if ((*token)[0] != '/') {
  905. return ts;
  906. }
  907. (*token)++; // Skip '/'
  908. (*token) += strspn((*token), " \t");
  909. ts.num_reals = atoi((*token));
  910. (*token) += strcspn((*token), "/ \t\r");
  911. if ((*token)[0] != '/') {
  912. return ts;
  913. }
  914. (*token)++; // Skip '/'
  915. ts.num_strings = parseInt(token);
  916. return ts;
  917. }
  918. // Parse triples with index offsets: i, i/j/k, i//k, i/j
  919. static bool parseTriple(const char **token, int vsize, int vnsize, int vtsize,
  920. vertex_index_t *ret) {
  921. if (!ret) {
  922. return false;
  923. }
  924. vertex_index_t vi(-1);
  925. if (!fixIndex(atoi((*token)), vsize, &(vi.v_idx))) {
  926. return false;
  927. }
  928. (*token) += strcspn((*token), "/ \t\r");
  929. if ((*token)[0] != '/') {
  930. (*ret) = vi;
  931. return true;
  932. }
  933. (*token)++;
  934. // i//k
  935. if ((*token)[0] == '/') {
  936. (*token)++;
  937. if (!fixIndex(atoi((*token)), vnsize, &(vi.vn_idx))) {
  938. return false;
  939. }
  940. (*token) += strcspn((*token), "/ \t\r");
  941. (*ret) = vi;
  942. return true;
  943. }
  944. // i/j/k or i/j
  945. if (!fixIndex(atoi((*token)), vtsize, &(vi.vt_idx))) {
  946. return false;
  947. }
  948. (*token) += strcspn((*token), "/ \t\r");
  949. if ((*token)[0] != '/') {
  950. (*ret) = vi;
  951. return true;
  952. }
  953. // i/j/k
  954. (*token)++; // skip '/'
  955. if (!fixIndex(atoi((*token)), vnsize, &(vi.vn_idx))) {
  956. return false;
  957. }
  958. (*token) += strcspn((*token), "/ \t\r");
  959. (*ret) = vi;
  960. return true;
  961. }
  962. // Parse raw triples: i, i/j/k, i//k, i/j
  963. static vertex_index_t parseRawTriple(const char **token) {
  964. vertex_index_t vi(static_cast<int>(0)); // 0 is an invalid index in OBJ
  965. vi.v_idx = atoi((*token));
  966. (*token) += strcspn((*token), "/ \t\r");
  967. if ((*token)[0] != '/') {
  968. return vi;
  969. }
  970. (*token)++;
  971. // i//k
  972. if ((*token)[0] == '/') {
  973. (*token)++;
  974. vi.vn_idx = atoi((*token));
  975. (*token) += strcspn((*token), "/ \t\r");
  976. return vi;
  977. }
  978. // i/j/k or i/j
  979. vi.vt_idx = atoi((*token));
  980. (*token) += strcspn((*token), "/ \t\r");
  981. if ((*token)[0] != '/') {
  982. return vi;
  983. }
  984. // i/j/k
  985. (*token)++; // skip '/'
  986. vi.vn_idx = atoi((*token));
  987. (*token) += strcspn((*token), "/ \t\r");
  988. return vi;
  989. }
  990. bool ParseTextureNameAndOption(std::string *texname, texture_option_t *texopt,
  991. const char *linebuf) {
  992. // @todo { write more robust lexer and parser. }
  993. bool found_texname = false;
  994. std::string texture_name;
  995. const char *token = linebuf; // Assume line ends with NULL
  996. while (!IS_NEW_LINE((*token))) {
  997. token += strspn(token, " \t"); // skip space
  998. if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) {
  999. token += 8;
  1000. texopt->blendu = parseOnOff(&token, /* default */ true);
  1001. } else if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) {
  1002. token += 8;
  1003. texopt->blendv = parseOnOff(&token, /* default */ true);
  1004. } else if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) {
  1005. token += 7;
  1006. texopt->clamp = parseOnOff(&token, /* default */ true);
  1007. } else if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) {
  1008. token += 7;
  1009. texopt->sharpness = parseReal(&token, 1.0);
  1010. } else if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) {
  1011. token += 4;
  1012. texopt->bump_multiplier = parseReal(&token, 1.0);
  1013. } else if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) {
  1014. token += 3;
  1015. parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]),
  1016. &(texopt->origin_offset[2]), &token);
  1017. } else if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) {
  1018. token += 3;
  1019. parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]),
  1020. &token, 1.0, 1.0, 1.0);
  1021. } else if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) {
  1022. token += 3;
  1023. parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]),
  1024. &(texopt->turbulence[2]), &token);
  1025. } else if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) {
  1026. token += 5;
  1027. texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE);
  1028. } else if ((0 == strncmp(token, "-texres", 7)) && IS_SPACE((token[7]))) {
  1029. token += 7;
  1030. // TODO(syoyo): Check if arg is int type.
  1031. texopt->texture_resolution = parseInt(&token);
  1032. } else if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) {
  1033. token += 9;
  1034. token += strspn(token, " \t");
  1035. const char *end = token + strcspn(token, " \t\r");
  1036. if ((end - token) == 1) { // Assume one char for -imfchan
  1037. texopt->imfchan = (*token);
  1038. }
  1039. token = end;
  1040. } else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) {
  1041. token += 4;
  1042. parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0);
  1043. } else if ((0 == strncmp(token, "-colorspace", 11)) &&
  1044. IS_SPACE((token[11]))) {
  1045. token += 12;
  1046. texopt->colorspace = parseString(&token);
  1047. } else {
  1048. // Assume texture filename
  1049. #if 0
  1050. size_t len = strcspn(token, " \t\r"); // untile next space
  1051. texture_name = std::string(token, token + len);
  1052. token += len;
  1053. token += strspn(token, " \t"); // skip space
  1054. #else
  1055. // Read filename until line end to parse filename containing whitespace
  1056. // TODO(syoyo): Support parsing texture option flag after the filename.
  1057. texture_name = std::string(token);
  1058. token += texture_name.length();
  1059. #endif
  1060. found_texname = true;
  1061. }
  1062. }
  1063. if (found_texname) {
  1064. (*texname) = texture_name;
  1065. return true;
  1066. } else {
  1067. return false;
  1068. }
  1069. }
  1070. static void InitTexOpt(texture_option_t *texopt, const bool is_bump) {
  1071. if (is_bump) {
  1072. texopt->imfchan = 'l';
  1073. } else {
  1074. texopt->imfchan = 'm';
  1075. }
  1076. texopt->bump_multiplier = static_cast<real_t>(1.0);
  1077. texopt->clamp = false;
  1078. texopt->blendu = true;
  1079. texopt->blendv = true;
  1080. texopt->sharpness = static_cast<real_t>(1.0);
  1081. texopt->brightness = static_cast<real_t>(0.0);
  1082. texopt->contrast = static_cast<real_t>(1.0);
  1083. texopt->origin_offset[0] = static_cast<real_t>(0.0);
  1084. texopt->origin_offset[1] = static_cast<real_t>(0.0);
  1085. texopt->origin_offset[2] = static_cast<real_t>(0.0);
  1086. texopt->scale[0] = static_cast<real_t>(1.0);
  1087. texopt->scale[1] = static_cast<real_t>(1.0);
  1088. texopt->scale[2] = static_cast<real_t>(1.0);
  1089. texopt->turbulence[0] = static_cast<real_t>(0.0);
  1090. texopt->turbulence[1] = static_cast<real_t>(0.0);
  1091. texopt->turbulence[2] = static_cast<real_t>(0.0);
  1092. texopt->texture_resolution = -1;
  1093. texopt->type = TEXTURE_TYPE_NONE;
  1094. }
  1095. static void InitMaterial(material_t *material) {
  1096. InitTexOpt(&material->ambient_texopt, /* is_bump */ false);
  1097. InitTexOpt(&material->diffuse_texopt, /* is_bump */ false);
  1098. InitTexOpt(&material->specular_texopt, /* is_bump */ false);
  1099. InitTexOpt(&material->specular_highlight_texopt, /* is_bump */ false);
  1100. InitTexOpt(&material->bump_texopt, /* is_bump */ true);
  1101. InitTexOpt(&material->displacement_texopt, /* is_bump */ false);
  1102. InitTexOpt(&material->alpha_texopt, /* is_bump */ false);
  1103. InitTexOpt(&material->reflection_texopt, /* is_bump */ false);
  1104. InitTexOpt(&material->roughness_texopt, /* is_bump */ false);
  1105. InitTexOpt(&material->metallic_texopt, /* is_bump */ false);
  1106. InitTexOpt(&material->sheen_texopt, /* is_bump */ false);
  1107. InitTexOpt(&material->emissive_texopt, /* is_bump */ false);
  1108. InitTexOpt(&material->normal_texopt,
  1109. /* is_bump */ false); // @fixme { is_bump will be true? }
  1110. material->name = "";
  1111. material->ambient_texname = "";
  1112. material->diffuse_texname = "";
  1113. material->specular_texname = "";
  1114. material->specular_highlight_texname = "";
  1115. material->bump_texname = "";
  1116. material->displacement_texname = "";
  1117. material->reflection_texname = "";
  1118. material->alpha_texname = "";
  1119. for (int i = 0; i < 3; i++) {
  1120. material->ambient[i] = static_cast<real_t>(0.0);
  1121. material->diffuse[i] = static_cast<real_t>(0.0);
  1122. material->specular[i] = static_cast<real_t>(0.0);
  1123. material->transmittance[i] = static_cast<real_t>(0.0);
  1124. material->emission[i] = static_cast<real_t>(0.0);
  1125. }
  1126. material->illum = 0;
  1127. material->dissolve = static_cast<real_t>(1.0);
  1128. material->shininess = static_cast<real_t>(1.0);
  1129. material->ior = static_cast<real_t>(1.0);
  1130. material->roughness = static_cast<real_t>(0.0);
  1131. material->metallic = static_cast<real_t>(0.0);
  1132. material->sheen = static_cast<real_t>(0.0);
  1133. material->clearcoat_thickness = static_cast<real_t>(0.0);
  1134. material->clearcoat_roughness = static_cast<real_t>(0.0);
  1135. material->anisotropy_rotation = static_cast<real_t>(0.0);
  1136. material->anisotropy = static_cast<real_t>(0.0);
  1137. material->roughness_texname = "";
  1138. material->metallic_texname = "";
  1139. material->sheen_texname = "";
  1140. material->emissive_texname = "";
  1141. material->normal_texname = "";
  1142. material->unknown_parameter.clear();
  1143. }
  1144. // code from https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
  1145. template <typename T>
  1146. static int pnpoly(int nvert, T *vertx, T *verty, T testx, T testy) {
  1147. int i, j, c = 0;
  1148. for (i = 0, j = nvert - 1; i < nvert; j = i++) {
  1149. if (((verty[i] > testy) != (verty[j] > testy)) &&
  1150. (testx <
  1151. (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) +
  1152. vertx[i]))
  1153. c = !c;
  1154. }
  1155. return c;
  1156. }
  1157. // TODO(syoyo): refactor function.
  1158. static bool exportGroupsToShape(shape_t *shape, const PrimGroup &prim_group,
  1159. const std::vector<tag_t> &tags,
  1160. const int material_id, const std::string &name,
  1161. bool triangulate,
  1162. const std::vector<real_t> &v) {
  1163. if (prim_group.IsEmpty()) {
  1164. return false;
  1165. }
  1166. shape->name = name;
  1167. // polygon
  1168. if (!prim_group.faceGroup.empty()) {
  1169. // Flatten vertices and indices
  1170. for (size_t i = 0; i < prim_group.faceGroup.size(); i++) {
  1171. const face_t &face = prim_group.faceGroup[i];
  1172. size_t npolys = face.vertex_indices.size();
  1173. if (npolys < 3) {
  1174. // Face must have 3+ vertices.
  1175. continue;
  1176. }
  1177. vertex_index_t i0 = face.vertex_indices[0];
  1178. vertex_index_t i1(-1);
  1179. vertex_index_t i2 = face.vertex_indices[1];
  1180. if (triangulate) {
  1181. // find the two axes to work in
  1182. size_t axes[2] = {1, 2};
  1183. for (size_t k = 0; k < npolys; ++k) {
  1184. i0 = face.vertex_indices[(k + 0) % npolys];
  1185. i1 = face.vertex_indices[(k + 1) % npolys];
  1186. i2 = face.vertex_indices[(k + 2) % npolys];
  1187. size_t vi0 = size_t(i0.v_idx);
  1188. size_t vi1 = size_t(i1.v_idx);
  1189. size_t vi2 = size_t(i2.v_idx);
  1190. if (((3 * vi0 + 2) >= v.size()) || ((3 * vi1 + 2) >= v.size()) ||
  1191. ((3 * vi2 + 2) >= v.size())) {
  1192. // Invalid triangle.
  1193. // FIXME(syoyo): Is it ok to simply skip this invalid triangle?
  1194. continue;
  1195. }
  1196. real_t v0x = v[vi0 * 3 + 0];
  1197. real_t v0y = v[vi0 * 3 + 1];
  1198. real_t v0z = v[vi0 * 3 + 2];
  1199. real_t v1x = v[vi1 * 3 + 0];
  1200. real_t v1y = v[vi1 * 3 + 1];
  1201. real_t v1z = v[vi1 * 3 + 2];
  1202. real_t v2x = v[vi2 * 3 + 0];
  1203. real_t v2y = v[vi2 * 3 + 1];
  1204. real_t v2z = v[vi2 * 3 + 2];
  1205. real_t e0x = v1x - v0x;
  1206. real_t e0y = v1y - v0y;
  1207. real_t e0z = v1z - v0z;
  1208. real_t e1x = v2x - v1x;
  1209. real_t e1y = v2y - v1y;
  1210. real_t e1z = v2z - v1z;
  1211. real_t cx = std::fabs(e0y * e1z - e0z * e1y);
  1212. real_t cy = std::fabs(e0z * e1x - e0x * e1z);
  1213. real_t cz = std::fabs(e0x * e1y - e0y * e1x);
  1214. const real_t epsilon = std::numeric_limits<real_t>::epsilon();
  1215. if (cx > epsilon || cy > epsilon || cz > epsilon) {
  1216. // found a corner
  1217. if (cx > cy && cx > cz) {
  1218. } else {
  1219. axes[0] = 0;
  1220. if (cz > cx && cz > cy) axes[1] = 1;
  1221. }
  1222. break;
  1223. }
  1224. }
  1225. real_t area = 0;
  1226. for (size_t k = 0; k < npolys; ++k) {
  1227. i0 = face.vertex_indices[(k + 0) % npolys];
  1228. i1 = face.vertex_indices[(k + 1) % npolys];
  1229. size_t vi0 = size_t(i0.v_idx);
  1230. size_t vi1 = size_t(i1.v_idx);
  1231. if (((vi0 * 3 + axes[0]) >= v.size()) ||
  1232. ((vi0 * 3 + axes[1]) >= v.size()) ||
  1233. ((vi1 * 3 + axes[0]) >= v.size()) ||
  1234. ((vi1 * 3 + axes[1]) >= v.size())) {
  1235. // Invalid index.
  1236. continue;
  1237. }
  1238. real_t v0x = v[vi0 * 3 + axes[0]];
  1239. real_t v0y = v[vi0 * 3 + axes[1]];
  1240. real_t v1x = v[vi1 * 3 + axes[0]];
  1241. real_t v1y = v[vi1 * 3 + axes[1]];
  1242. area += (v0x * v1y - v0y * v1x) * static_cast<real_t>(0.5);
  1243. }
  1244. face_t remainingFace = face; // copy
  1245. size_t guess_vert = 0;
  1246. vertex_index_t ind[3];
  1247. real_t vx[3];
  1248. real_t vy[3];
  1249. // How many iterations can we do without decreasing the remaining
  1250. // vertices.
  1251. size_t remainingIterations = face.vertex_indices.size();
  1252. size_t previousRemainingVertices = remainingFace.vertex_indices.size();
  1253. while (remainingFace.vertex_indices.size() > 3 &&
  1254. remainingIterations > 0) {
  1255. npolys = remainingFace.vertex_indices.size();
  1256. if (guess_vert >= npolys) {
  1257. guess_vert -= npolys;
  1258. }
  1259. if (previousRemainingVertices != npolys) {
  1260. // The number of remaining vertices decreased. Reset counters.
  1261. previousRemainingVertices = npolys;
  1262. remainingIterations = npolys;
  1263. } else {
  1264. // We didn't consume a vertex on previous iteration, reduce the
  1265. // available iterations.
  1266. remainingIterations--;
  1267. }
  1268. for (size_t k = 0; k < 3; k++) {
  1269. ind[k] = remainingFace.vertex_indices[(guess_vert + k) % npolys];
  1270. size_t vi = size_t(ind[k].v_idx);
  1271. if (((vi * 3 + axes[0]) >= v.size()) ||
  1272. ((vi * 3 + axes[1]) >= v.size())) {
  1273. // ???
  1274. vx[k] = static_cast<real_t>(0.0);
  1275. vy[k] = static_cast<real_t>(0.0);
  1276. } else {
  1277. vx[k] = v[vi * 3 + axes[0]];
  1278. vy[k] = v[vi * 3 + axes[1]];
  1279. }
  1280. }
  1281. real_t e0x = vx[1] - vx[0];
  1282. real_t e0y = vy[1] - vy[0];
  1283. real_t e1x = vx[2] - vx[1];
  1284. real_t e1y = vy[2] - vy[1];
  1285. real_t cross = e0x * e1y - e0y * e1x;
  1286. // if an internal angle
  1287. if (cross * area < static_cast<real_t>(0.0)) {
  1288. guess_vert += 1;
  1289. continue;
  1290. }
  1291. // check all other verts in case they are inside this triangle
  1292. bool overlap = false;
  1293. for (size_t otherVert = 3; otherVert < npolys; ++otherVert) {
  1294. size_t idx = (guess_vert + otherVert) % npolys;
  1295. if (idx >= remainingFace.vertex_indices.size()) {
  1296. // ???
  1297. continue;
  1298. }
  1299. size_t ovi = size_t(remainingFace.vertex_indices[idx].v_idx);
  1300. if (((ovi * 3 + axes[0]) >= v.size()) ||
  1301. ((ovi * 3 + axes[1]) >= v.size())) {
  1302. // ???
  1303. continue;
  1304. }
  1305. real_t tx = v[ovi * 3 + axes[0]];
  1306. real_t ty = v[ovi * 3 + axes[1]];
  1307. if (pnpoly(3, vx, vy, tx, ty)) {
  1308. overlap = true;
  1309. break;
  1310. }
  1311. }
  1312. if (overlap) {
  1313. guess_vert += 1;
  1314. continue;
  1315. }
  1316. // this triangle is an ear
  1317. {
  1318. index_t idx0, idx1, idx2;
  1319. idx0.vertex_index = ind[0].v_idx;
  1320. idx0.normal_index = ind[0].vn_idx;
  1321. idx0.texcoord_index = ind[0].vt_idx;
  1322. idx1.vertex_index = ind[1].v_idx;
  1323. idx1.normal_index = ind[1].vn_idx;
  1324. idx1.texcoord_index = ind[1].vt_idx;
  1325. idx2.vertex_index = ind[2].v_idx;
  1326. idx2.normal_index = ind[2].vn_idx;
  1327. idx2.texcoord_index = ind[2].vt_idx;
  1328. shape->mesh.indices.push_back(idx0);
  1329. shape->mesh.indices.push_back(idx1);
  1330. shape->mesh.indices.push_back(idx2);
  1331. shape->mesh.num_face_vertices.push_back(3);
  1332. shape->mesh.material_ids.push_back(material_id);
  1333. shape->mesh.smoothing_group_ids.push_back(face.smoothing_group_id);
  1334. }
  1335. // remove v1 from the list
  1336. size_t removed_vert_index = (guess_vert + 1) % npolys;
  1337. while (removed_vert_index + 1 < npolys) {
  1338. remainingFace.vertex_indices[removed_vert_index] =
  1339. remainingFace.vertex_indices[removed_vert_index + 1];
  1340. removed_vert_index += 1;
  1341. }
  1342. remainingFace.vertex_indices.pop_back();
  1343. }
  1344. if (remainingFace.vertex_indices.size() == 3) {
  1345. i0 = remainingFace.vertex_indices[0];
  1346. i1 = remainingFace.vertex_indices[1];
  1347. i2 = remainingFace.vertex_indices[2];
  1348. {
  1349. index_t idx0, idx1, idx2;
  1350. idx0.vertex_index = i0.v_idx;
  1351. idx0.normal_index = i0.vn_idx;
  1352. idx0.texcoord_index = i0.vt_idx;
  1353. idx1.vertex_index = i1.v_idx;
  1354. idx1.normal_index = i1.vn_idx;
  1355. idx1.texcoord_index = i1.vt_idx;
  1356. idx2.vertex_index = i2.v_idx;
  1357. idx2.normal_index = i2.vn_idx;
  1358. idx2.texcoord_index = i2.vt_idx;
  1359. shape->mesh.indices.push_back(idx0);
  1360. shape->mesh.indices.push_back(idx1);
  1361. shape->mesh.indices.push_back(idx2);
  1362. shape->mesh.num_face_vertices.push_back(3);
  1363. shape->mesh.material_ids.push_back(material_id);
  1364. shape->mesh.smoothing_group_ids.push_back(face.smoothing_group_id);
  1365. }
  1366. }
  1367. } else {
  1368. for (size_t k = 0; k < npolys; k++) {
  1369. index_t idx;
  1370. idx.vertex_index = face.vertex_indices[k].v_idx;
  1371. idx.normal_index = face.vertex_indices[k].vn_idx;
  1372. idx.texcoord_index = face.vertex_indices[k].vt_idx;
  1373. shape->mesh.indices.push_back(idx);
  1374. }
  1375. shape->mesh.num_face_vertices.push_back(
  1376. static_cast<unsigned char>(npolys));
  1377. shape->mesh.material_ids.push_back(material_id); // per face
  1378. shape->mesh.smoothing_group_ids.push_back(
  1379. face.smoothing_group_id); // per face
  1380. }
  1381. }
  1382. shape->mesh.tags = tags;
  1383. }
  1384. // line
  1385. if (!prim_group.lineGroup.empty()) {
  1386. // Flatten indices
  1387. for (size_t i = 0; i < prim_group.lineGroup.size(); i++) {
  1388. for (size_t j = 0; j < prim_group.lineGroup[i].vertex_indices.size();
  1389. j++) {
  1390. const vertex_index_t &vi = prim_group.lineGroup[i].vertex_indices[j];
  1391. index_t idx;
  1392. idx.vertex_index = vi.v_idx;
  1393. idx.normal_index = vi.vn_idx;
  1394. idx.texcoord_index = vi.vt_idx;
  1395. shape->lines.indices.push_back(idx);
  1396. }
  1397. shape->lines.num_line_vertices.push_back(
  1398. int(prim_group.lineGroup[i].vertex_indices.size()));
  1399. }
  1400. }
  1401. // points
  1402. if (!prim_group.pointsGroup.empty()) {
  1403. // Flatten & convert indices
  1404. for (size_t i = 0; i < prim_group.pointsGroup.size(); i++) {
  1405. for (size_t j = 0; j < prim_group.pointsGroup[i].vertex_indices.size();
  1406. j++) {
  1407. const vertex_index_t &vi = prim_group.pointsGroup[i].vertex_indices[j];
  1408. index_t idx;
  1409. idx.vertex_index = vi.v_idx;
  1410. idx.normal_index = vi.vn_idx;
  1411. idx.texcoord_index = vi.vt_idx;
  1412. shape->points.indices.push_back(idx);
  1413. }
  1414. }
  1415. }
  1416. return true;
  1417. }
  1418. // Split a string with specified delimiter character.
  1419. // http://stackoverflow.com/questions/236129/split-a-string-in-c
  1420. static void SplitString(const std::string &s, char delim,
  1421. std::vector<std::string> &elems) {
  1422. std::stringstream ss;
  1423. ss.str(s);
  1424. std::string item;
  1425. while (std::getline(ss, item, delim)) {
  1426. elems.push_back(item);
  1427. }
  1428. }
  1429. static std::string JoinPath(const std::string &dir,
  1430. const std::string &filename) {
  1431. if (dir.empty()) {
  1432. return filename;
  1433. } else {
  1434. // check '/'
  1435. char lastChar = *dir.rbegin();
  1436. if (lastChar != '/') {
  1437. return dir + std::string("/") + filename;
  1438. } else {
  1439. return dir + filename;
  1440. }
  1441. }
  1442. }
  1443. void LoadMtl(std::map<std::string, int> *material_map,
  1444. std::vector<material_t> *materials, std::istream *inStream,
  1445. std::string *warning, std::string *err) {
  1446. (void)err;
  1447. // Create a default material anyway.
  1448. material_t material;
  1449. InitMaterial(&material);
  1450. // Issue 43. `d` wins against `Tr` since `Tr` is not in the MTL specification.
  1451. bool has_d = false;
  1452. bool has_tr = false;
  1453. // has_kd is used to set a default diffuse value when map_Kd is present
  1454. // and Kd is not.
  1455. bool has_kd = false;
  1456. std::stringstream warn_ss;
  1457. size_t line_no = 0;
  1458. std::string linebuf;
  1459. while (inStream->peek() != -1) {
  1460. safeGetline(*inStream, linebuf);
  1461. line_no++;
  1462. // Trim trailing whitespace.
  1463. if (linebuf.size() > 0) {
  1464. linebuf = linebuf.substr(0, linebuf.find_last_not_of(" \t") + 1);
  1465. }
  1466. // Trim newline '\r\n' or '\n'
  1467. if (linebuf.size() > 0) {
  1468. if (linebuf[linebuf.size() - 1] == '\n')
  1469. linebuf.erase(linebuf.size() - 1);
  1470. }
  1471. if (linebuf.size() > 0) {
  1472. if (linebuf[linebuf.size() - 1] == '\r')
  1473. linebuf.erase(linebuf.size() - 1);
  1474. }
  1475. // Skip if empty line.
  1476. if (linebuf.empty()) {
  1477. continue;
  1478. }
  1479. // Skip leading space.
  1480. const char *token = linebuf.c_str();
  1481. token += strspn(token, " \t");
  1482. assert(token);
  1483. if (token[0] == '\0') continue; // empty line
  1484. if (token[0] == '#') continue; // comment line
  1485. // new mtl
  1486. if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) {
  1487. // flush previous material.
  1488. if (!material.name.empty()) {
  1489. material_map->insert(std::pair<std::string, int>(
  1490. material.name, static_cast<int>(materials->size())));
  1491. materials->push_back(material);
  1492. }
  1493. // initial temporary material
  1494. InitMaterial(&material);
  1495. has_d = false;
  1496. has_tr = false;
  1497. // set new mtl name
  1498. token += 7;
  1499. {
  1500. std::stringstream sstr;
  1501. sstr << token;
  1502. material.name = sstr.str();
  1503. }
  1504. continue;
  1505. }
  1506. // ambient
  1507. if (token[0] == 'K' && token[1] == 'a' && IS_SPACE((token[2]))) {
  1508. token += 2;
  1509. real_t r, g, b;
  1510. parseReal3(&r, &g, &b, &token);
  1511. material.ambient[0] = r;
  1512. material.ambient[1] = g;
  1513. material.ambient[2] = b;
  1514. continue;
  1515. }
  1516. // diffuse
  1517. if (token[0] == 'K' && token[1] == 'd' && IS_SPACE((token[2]))) {
  1518. token += 2;
  1519. real_t r, g, b;
  1520. parseReal3(&r, &g, &b, &token);
  1521. material.diffuse[0] = r;
  1522. material.diffuse[1] = g;
  1523. material.diffuse[2] = b;
  1524. has_kd = true;
  1525. continue;
  1526. }
  1527. // specular
  1528. if (token[0] == 'K' && token[1] == 's' && IS_SPACE((token[2]))) {
  1529. token += 2;
  1530. real_t r, g, b;
  1531. parseReal3(&r, &g, &b, &token);
  1532. material.specular[0] = r;
  1533. material.specular[1] = g;
  1534. material.specular[2] = b;
  1535. continue;
  1536. }
  1537. // transmittance
  1538. if ((token[0] == 'K' && token[1] == 't' && IS_SPACE((token[2]))) ||
  1539. (token[0] == 'T' && token[1] == 'f' && IS_SPACE((token[2])))) {
  1540. token += 2;
  1541. real_t r, g, b;
  1542. parseReal3(&r, &g, &b, &token);
  1543. material.transmittance[0] = r;
  1544. material.transmittance[1] = g;
  1545. material.transmittance[2] = b;
  1546. continue;
  1547. }
  1548. // ior(index of refraction)
  1549. if (token[0] == 'N' && token[1] == 'i' && IS_SPACE((token[2]))) {
  1550. token += 2;
  1551. material.ior = parseReal(&token);
  1552. continue;
  1553. }
  1554. // emission
  1555. if (token[0] == 'K' && token[1] == 'e' && IS_SPACE(token[2])) {
  1556. token += 2;
  1557. real_t r, g, b;
  1558. parseReal3(&r, &g, &b, &token);
  1559. material.emission[0] = r;
  1560. material.emission[1] = g;
  1561. material.emission[2] = b;
  1562. continue;
  1563. }
  1564. // shininess
  1565. if (token[0] == 'N' && token[1] == 's' && IS_SPACE(token[2])) {
  1566. token += 2;
  1567. material.shininess = parseReal(&token);
  1568. continue;
  1569. }
  1570. // illum model
  1571. if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) {
  1572. token += 6;
  1573. material.illum = parseInt(&token);
  1574. continue;
  1575. }
  1576. // dissolve
  1577. if ((token[0] == 'd' && IS_SPACE(token[1]))) {
  1578. token += 1;
  1579. material.dissolve = parseReal(&token);
  1580. if (has_tr) {
  1581. warn_ss << "Both `d` and `Tr` parameters defined for \""
  1582. << material.name
  1583. << "\". Use the value of `d` for dissolve (line " << line_no
  1584. << " in .mtl.)" << std::endl;
  1585. }
  1586. has_d = true;
  1587. continue;
  1588. }
  1589. if (token[0] == 'T' && token[1] == 'r' && IS_SPACE(token[2])) {
  1590. token += 2;
  1591. if (has_d) {
  1592. // `d` wins. Ignore `Tr` value.
  1593. warn_ss << "Both `d` and `Tr` parameters defined for \""
  1594. << material.name
  1595. << "\". Use the value of `d` for dissolve (line " << line_no
  1596. << " in .mtl.)" << std::endl;
  1597. } else {
  1598. // We invert value of Tr(assume Tr is in range [0, 1])
  1599. // NOTE: Interpretation of Tr is application(exporter) dependent. For
  1600. // some application(e.g. 3ds max obj exporter), Tr = d(Issue 43)
  1601. material.dissolve = static_cast<real_t>(1.0) - parseReal(&token);
  1602. }
  1603. has_tr = true;
  1604. continue;
  1605. }
  1606. // PBR: roughness
  1607. if (token[0] == 'P' && token[1] == 'r' && IS_SPACE(token[2])) {
  1608. token += 2;
  1609. material.roughness = parseReal(&token);
  1610. continue;
  1611. }
  1612. // PBR: metallic
  1613. if (token[0] == 'P' && token[1] == 'm' && IS_SPACE(token[2])) {
  1614. token += 2;
  1615. material.metallic = parseReal(&token);
  1616. continue;
  1617. }
  1618. // PBR: sheen
  1619. if (token[0] == 'P' && token[1] == 's' && IS_SPACE(token[2])) {
  1620. token += 2;
  1621. material.sheen = parseReal(&token);
  1622. continue;
  1623. }
  1624. // PBR: clearcoat thickness
  1625. if (token[0] == 'P' && token[1] == 'c' && IS_SPACE(token[2])) {
  1626. token += 2;
  1627. material.clearcoat_thickness = parseReal(&token);
  1628. continue;
  1629. }
  1630. // PBR: clearcoat roughness
  1631. if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) {
  1632. token += 4;
  1633. material.clearcoat_roughness = parseReal(&token);
  1634. continue;
  1635. }
  1636. // PBR: anisotropy
  1637. if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) {
  1638. token += 6;
  1639. material.anisotropy = parseReal(&token);
  1640. continue;
  1641. }
  1642. // PBR: anisotropy rotation
  1643. if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) {
  1644. token += 7;
  1645. material.anisotropy_rotation = parseReal(&token);
  1646. continue;
  1647. }
  1648. // ambient texture
  1649. if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) {
  1650. token += 7;
  1651. ParseTextureNameAndOption(&(material.ambient_texname),
  1652. &(material.ambient_texopt), token);
  1653. continue;
  1654. }
  1655. // diffuse texture
  1656. if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) {
  1657. token += 7;
  1658. ParseTextureNameAndOption(&(material.diffuse_texname),
  1659. &(material.diffuse_texopt), token);
  1660. // Set a decent diffuse default value if a diffuse texture is specified
  1661. // without a matching Kd value.
  1662. if (!has_kd)
  1663. {
  1664. material.diffuse[0] = static_cast<real_t>(0.6);
  1665. material.diffuse[1] = static_cast<real_t>(0.6);
  1666. material.diffuse[2] = static_cast<real_t>(0.6);
  1667. }
  1668. continue;
  1669. }
  1670. // specular texture
  1671. if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) {
  1672. token += 7;
  1673. ParseTextureNameAndOption(&(material.specular_texname),
  1674. &(material.specular_texopt), token);
  1675. continue;
  1676. }
  1677. // specular highlight texture
  1678. if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) {
  1679. token += 7;
  1680. ParseTextureNameAndOption(&(material.specular_highlight_texname),
  1681. &(material.specular_highlight_texopt), token);
  1682. continue;
  1683. }
  1684. // bump texture
  1685. if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) {
  1686. token += 9;
  1687. ParseTextureNameAndOption(&(material.bump_texname),
  1688. &(material.bump_texopt), token);
  1689. continue;
  1690. }
  1691. // bump texture
  1692. if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) {
  1693. token += 9;
  1694. ParseTextureNameAndOption(&(material.bump_texname),
  1695. &(material.bump_texopt), token);
  1696. continue;
  1697. }
  1698. // bump texture
  1699. if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) {
  1700. token += 5;
  1701. ParseTextureNameAndOption(&(material.bump_texname),
  1702. &(material.bump_texopt), token);
  1703. continue;
  1704. }
  1705. // alpha texture
  1706. if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) {
  1707. token += 6;
  1708. material.alpha_texname = token;
  1709. ParseTextureNameAndOption(&(material.alpha_texname),
  1710. &(material.alpha_texopt), token);
  1711. continue;
  1712. }
  1713. // displacement texture
  1714. if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) {
  1715. token += 5;
  1716. ParseTextureNameAndOption(&(material.displacement_texname),
  1717. &(material.displacement_texopt), token);
  1718. continue;
  1719. }
  1720. // reflection map
  1721. if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) {
  1722. token += 5;
  1723. ParseTextureNameAndOption(&(material.reflection_texname),
  1724. &(material.reflection_texopt), token);
  1725. continue;
  1726. }
  1727. // PBR: roughness texture
  1728. if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) {
  1729. token += 7;
  1730. ParseTextureNameAndOption(&(material.roughness_texname),
  1731. &(material.roughness_texopt), token);
  1732. continue;
  1733. }
  1734. // PBR: metallic texture
  1735. if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) {
  1736. token += 7;
  1737. ParseTextureNameAndOption(&(material.metallic_texname),
  1738. &(material.metallic_texopt), token);
  1739. continue;
  1740. }
  1741. // PBR: sheen texture
  1742. if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) {
  1743. token += 7;
  1744. ParseTextureNameAndOption(&(material.sheen_texname),
  1745. &(material.sheen_texopt), token);
  1746. continue;
  1747. }
  1748. // PBR: emissive texture
  1749. if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) {
  1750. token += 7;
  1751. ParseTextureNameAndOption(&(material.emissive_texname),
  1752. &(material.emissive_texopt), token);
  1753. continue;
  1754. }
  1755. // PBR: normal map texture
  1756. if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) {
  1757. token += 5;
  1758. ParseTextureNameAndOption(&(material.normal_texname),
  1759. &(material.normal_texopt), token);
  1760. continue;
  1761. }
  1762. // unknown parameter
  1763. const char *_space = strchr(token, ' ');
  1764. if (!_space) {
  1765. _space = strchr(token, '\t');
  1766. }
  1767. if (_space) {
  1768. std::ptrdiff_t len = _space - token;
  1769. std::string key(token, static_cast<size_t>(len));
  1770. std::string value = _space + 1;
  1771. material.unknown_parameter.insert(
  1772. std::pair<std::string, std::string>(key, value));
  1773. }
  1774. }
  1775. // flush last material.
  1776. material_map->insert(std::pair<std::string, int>(
  1777. material.name, static_cast<int>(materials->size())));
  1778. materials->push_back(material);
  1779. if (warning) {
  1780. (*warning) = warn_ss.str();
  1781. }
  1782. }
  1783. bool MaterialFileReader::operator()(const std::string &matId,
  1784. std::vector<material_t> *materials,
  1785. std::map<std::string, int> *matMap,
  1786. std::string *warn, std::string *err) {
  1787. if (!m_mtlBaseDir.empty()) {
  1788. #ifdef _WIN32
  1789. char sep = ';';
  1790. #else
  1791. char sep = ':';
  1792. #endif
  1793. // https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g
  1794. std::vector<std::string> paths;
  1795. std::istringstream f(m_mtlBaseDir);
  1796. std::string s;
  1797. while (getline(f, s, sep)) {
  1798. paths.push_back(s);
  1799. }
  1800. for (size_t i = 0; i < paths.size(); i++) {
  1801. std::string filepath = JoinPath(paths[i], matId);
  1802. std::ifstream matIStream(filepath.c_str());
  1803. if (matIStream) {
  1804. LoadMtl(matMap, materials, &matIStream, warn, err);
  1805. return true;
  1806. }
  1807. }
  1808. std::stringstream ss;
  1809. ss << "Material file [ " << matId
  1810. << " ] not found in a path : " << m_mtlBaseDir << std::endl;
  1811. if (warn) {
  1812. (*warn) += ss.str();
  1813. }
  1814. return false;
  1815. } else {
  1816. std::string filepath = matId;
  1817. std::ifstream matIStream(filepath.c_str());
  1818. if (matIStream) {
  1819. LoadMtl(matMap, materials, &matIStream, warn, err);
  1820. return true;
  1821. }
  1822. std::stringstream ss;
  1823. ss << "Material file [ " << filepath
  1824. << " ] not found in a path : " << m_mtlBaseDir << std::endl;
  1825. if (warn) {
  1826. (*warn) += ss.str();
  1827. }
  1828. return false;
  1829. }
  1830. }
  1831. bool MaterialStreamReader::operator()(const std::string &matId,
  1832. std::vector<material_t> *materials,
  1833. std::map<std::string, int> *matMap,
  1834. std::string *warn, std::string *err) {
  1835. (void)err;
  1836. (void)matId;
  1837. if (!m_inStream) {
  1838. std::stringstream ss;
  1839. ss << "Material stream in error state. " << std::endl;
  1840. if (warn) {
  1841. (*warn) += ss.str();
  1842. }
  1843. return false;
  1844. }
  1845. LoadMtl(matMap, materials, &m_inStream, warn, err);
  1846. return true;
  1847. }
  1848. bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
  1849. std::vector<material_t> *materials, std::string *warn,
  1850. std::string *err, const char *filename, const char *mtl_basedir,
  1851. bool trianglulate, bool default_vcols_fallback) {
  1852. attrib->vertices.clear();
  1853. attrib->normals.clear();
  1854. attrib->texcoords.clear();
  1855. attrib->colors.clear();
  1856. shapes->clear();
  1857. std::stringstream errss;
  1858. std::ifstream ifs(filename);
  1859. if (!ifs) {
  1860. errss << "Cannot open file [" << filename << "]" << std::endl;
  1861. if (err) {
  1862. (*err) = errss.str();
  1863. }
  1864. return false;
  1865. }
  1866. std::string baseDir = mtl_basedir ? mtl_basedir : "";
  1867. if (!baseDir.empty()) {
  1868. #ifndef _WIN32
  1869. const char dirsep = '/';
  1870. #else
  1871. const char dirsep = '\\';
  1872. #endif
  1873. if (baseDir[baseDir.length() - 1] != dirsep) baseDir += dirsep;
  1874. }
  1875. MaterialFileReader matFileReader(baseDir);
  1876. return LoadObj(attrib, shapes, materials, warn, err, &ifs, &matFileReader,
  1877. trianglulate, default_vcols_fallback);
  1878. }
  1879. bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
  1880. std::vector<material_t> *materials, std::string *warn,
  1881. std::string *err, std::istream *inStream,
  1882. MaterialReader *readMatFn /*= NULL*/, bool triangulate,
  1883. bool default_vcols_fallback) {
  1884. std::stringstream errss;
  1885. std::vector<real_t> v;
  1886. std::vector<real_t> vn;
  1887. std::vector<real_t> vt;
  1888. std::vector<real_t> vc;
  1889. std::vector<tag_t> tags;
  1890. PrimGroup prim_group;
  1891. std::string name;
  1892. // material
  1893. std::map<std::string, int> material_map;
  1894. int material = -1;
  1895. // smoothing group id
  1896. unsigned int current_smoothing_id =
  1897. 0; // Initial value. 0 means no smoothing.
  1898. int greatest_v_idx = -1;
  1899. int greatest_vn_idx = -1;
  1900. int greatest_vt_idx = -1;
  1901. shape_t shape;
  1902. bool found_all_colors = true;
  1903. size_t line_num = 0;
  1904. std::string linebuf;
  1905. while (inStream->peek() != -1) {
  1906. safeGetline(*inStream, linebuf);
  1907. line_num++;
  1908. // Trim newline '\r\n' or '\n'
  1909. if (linebuf.size() > 0) {
  1910. if (linebuf[linebuf.size() - 1] == '\n')
  1911. linebuf.erase(linebuf.size() - 1);
  1912. }
  1913. if (linebuf.size() > 0) {
  1914. if (linebuf[linebuf.size() - 1] == '\r')
  1915. linebuf.erase(linebuf.size() - 1);
  1916. }
  1917. // Skip if empty line.
  1918. if (linebuf.empty()) {
  1919. continue;
  1920. }
  1921. // Skip leading space.
  1922. const char *token = linebuf.c_str();
  1923. token += strspn(token, " \t");
  1924. assert(token);
  1925. if (token[0] == '\0') continue; // empty line
  1926. if (token[0] == '#') continue; // comment line
  1927. // vertex
  1928. if (token[0] == 'v' && IS_SPACE((token[1]))) {
  1929. token += 2;
  1930. real_t x, y, z;
  1931. real_t r, g, b;
  1932. found_all_colors &= parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token);
  1933. v.push_back(x);
  1934. v.push_back(y);
  1935. v.push_back(z);
  1936. if (found_all_colors || default_vcols_fallback) {
  1937. vc.push_back(r);
  1938. vc.push_back(g);
  1939. vc.push_back(b);
  1940. }
  1941. continue;
  1942. }
  1943. // normal
  1944. if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) {
  1945. token += 3;
  1946. real_t x, y, z;
  1947. parseReal3(&x, &y, &z, &token);
  1948. vn.push_back(x);
  1949. vn.push_back(y);
  1950. vn.push_back(z);
  1951. continue;
  1952. }
  1953. // texcoord
  1954. if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) {
  1955. token += 3;
  1956. real_t x, y;
  1957. parseReal2(&x, &y, &token);
  1958. vt.push_back(x);
  1959. vt.push_back(y);
  1960. continue;
  1961. }
  1962. // line
  1963. if (token[0] == 'l' && IS_SPACE((token[1]))) {
  1964. token += 2;
  1965. __line_t line;
  1966. while (!IS_NEW_LINE(token[0])) {
  1967. vertex_index_t vi;
  1968. if (!parseTriple(&token, static_cast<int>(v.size() / 3),
  1969. static_cast<int>(vn.size() / 3),
  1970. static_cast<int>(vt.size() / 2), &vi)) {
  1971. if (err) {
  1972. std::stringstream ss;
  1973. ss << "Failed parse `l' line(e.g. zero value for vertex index. "
  1974. "line "
  1975. << line_num << ".)\n";
  1976. (*err) += ss.str();
  1977. }
  1978. return false;
  1979. }
  1980. line.vertex_indices.push_back(vi);
  1981. size_t n = strspn(token, " \t\r");
  1982. token += n;
  1983. }
  1984. prim_group.lineGroup.push_back(line);
  1985. continue;
  1986. }
  1987. // points
  1988. if (token[0] == 'p' && IS_SPACE((token[1]))) {
  1989. token += 2;
  1990. __points_t pts;
  1991. while (!IS_NEW_LINE(token[0])) {
  1992. vertex_index_t vi;
  1993. if (!parseTriple(&token, static_cast<int>(v.size() / 3),
  1994. static_cast<int>(vn.size() / 3),
  1995. static_cast<int>(vt.size() / 2), &vi)) {
  1996. if (err) {
  1997. std::stringstream ss;
  1998. ss << "Failed parse `p' line(e.g. zero value for vertex index. "
  1999. "line "
  2000. << line_num << ".)\n";
  2001. (*err) += ss.str();
  2002. }
  2003. return false;
  2004. }
  2005. pts.vertex_indices.push_back(vi);
  2006. size_t n = strspn(token, " \t\r");
  2007. token += n;
  2008. }
  2009. prim_group.pointsGroup.push_back(pts);
  2010. continue;
  2011. }
  2012. // face
  2013. if (token[0] == 'f' && IS_SPACE((token[1]))) {
  2014. token += 2;
  2015. token += strspn(token, " \t");
  2016. face_t face;
  2017. face.smoothing_group_id = current_smoothing_id;
  2018. face.vertex_indices.reserve(3);
  2019. while (!IS_NEW_LINE(token[0])) {
  2020. vertex_index_t vi;
  2021. if (!parseTriple(&token, static_cast<int>(v.size() / 3),
  2022. static_cast<int>(vn.size() / 3),
  2023. static_cast<int>(vt.size() / 2), &vi)) {
  2024. if (err) {
  2025. std::stringstream ss;
  2026. ss << "Failed parse `f' line(e.g. zero value for face index. line "
  2027. << line_num << ".)\n";
  2028. (*err) += ss.str();
  2029. }
  2030. return false;
  2031. }
  2032. greatest_v_idx = greatest_v_idx > vi.v_idx ? greatest_v_idx : vi.v_idx;
  2033. greatest_vn_idx =
  2034. greatest_vn_idx > vi.vn_idx ? greatest_vn_idx : vi.vn_idx;
  2035. greatest_vt_idx =
  2036. greatest_vt_idx > vi.vt_idx ? greatest_vt_idx : vi.vt_idx;
  2037. face.vertex_indices.push_back(vi);
  2038. size_t n = strspn(token, " \t\r");
  2039. token += n;
  2040. }
  2041. // replace with emplace_back + std::move on C++11
  2042. prim_group.faceGroup.push_back(face);
  2043. continue;
  2044. }
  2045. // use mtl
  2046. if ((0 == strncmp(token, "usemtl", 6))) {
  2047. token += 6;
  2048. std::string namebuf = parseString(&token);
  2049. int newMaterialId = -1;
  2050. std::map<std::string, int>::const_iterator it = material_map.find(namebuf);
  2051. if (it != material_map.end()) {
  2052. newMaterialId = it->second;
  2053. } else {
  2054. // { error!! material not found }
  2055. if (warn) {
  2056. (*warn) += "material [ '" + namebuf + "' ] not found in .mtl\n";
  2057. }
  2058. }
  2059. if (newMaterialId != material) {
  2060. // Create per-face material. Thus we don't add `shape` to `shapes` at
  2061. // this time.
  2062. // just clear `faceGroup` after `exportGroupsToShape()` call.
  2063. exportGroupsToShape(&shape, prim_group, tags, material, name,
  2064. triangulate, v);
  2065. prim_group.faceGroup.clear();
  2066. material = newMaterialId;
  2067. }
  2068. continue;
  2069. }
  2070. // load mtl
  2071. if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) {
  2072. if (readMatFn) {
  2073. token += 7;
  2074. std::vector<std::string> filenames;
  2075. SplitString(std::string(token), ' ', filenames);
  2076. if (filenames.empty()) {
  2077. if (warn) {
  2078. std::stringstream ss;
  2079. ss << "Looks like empty filename for mtllib. Use default "
  2080. "material (line "
  2081. << line_num << ".)\n";
  2082. (*warn) += ss.str();
  2083. }
  2084. } else {
  2085. bool found = false;
  2086. for (size_t s = 0; s < filenames.size(); s++) {
  2087. std::string warn_mtl;
  2088. std::string err_mtl;
  2089. bool ok = (*readMatFn)(filenames[s].c_str(), materials,
  2090. &material_map, &warn_mtl, &err_mtl);
  2091. if (warn && (!warn_mtl.empty())) {
  2092. (*warn) += warn_mtl;
  2093. }
  2094. if (err && (!err_mtl.empty())) {
  2095. (*err) += err_mtl;
  2096. }
  2097. if (ok) {
  2098. found = true;
  2099. break;
  2100. }
  2101. }
  2102. if (!found) {
  2103. if (warn) {
  2104. (*warn) +=
  2105. "Failed to load material file(s). Use default "
  2106. "material.\n";
  2107. }
  2108. }
  2109. }
  2110. }
  2111. continue;
  2112. }
  2113. // group name
  2114. if (token[0] == 'g' && IS_SPACE((token[1]))) {
  2115. // flush previous face group.
  2116. bool ret = exportGroupsToShape(&shape, prim_group, tags, material, name,
  2117. triangulate, v);
  2118. (void)ret; // return value not used.
  2119. if (shape.mesh.indices.size() > 0) {
  2120. shapes->push_back(shape);
  2121. }
  2122. shape = shape_t();
  2123. // material = -1;
  2124. prim_group.clear();
  2125. std::vector<std::string> names;
  2126. while (!IS_NEW_LINE(token[0])) {
  2127. std::string str = parseString(&token);
  2128. names.push_back(str);
  2129. token += strspn(token, " \t\r"); // skip tag
  2130. }
  2131. // names[0] must be 'g'
  2132. if (names.size() < 2) {
  2133. // 'g' with empty names
  2134. if (warn) {
  2135. std::stringstream ss;
  2136. ss << "Empty group name. line: " << line_num << "\n";
  2137. (*warn) += ss.str();
  2138. name = "";
  2139. }
  2140. } else {
  2141. std::stringstream ss;
  2142. ss << names[1];
  2143. // tinyobjloader does not support multiple groups for a primitive.
  2144. // Currently we concatinate multiple group names with a space to get
  2145. // single group name.
  2146. for (size_t i = 2; i < names.size(); i++) {
  2147. ss << " " << names[i];
  2148. }
  2149. name = ss.str();
  2150. }
  2151. continue;
  2152. }
  2153. // object name
  2154. if (token[0] == 'o' && IS_SPACE((token[1]))) {
  2155. // flush previous face group.
  2156. bool ret = exportGroupsToShape(&shape, prim_group, tags, material, name,
  2157. triangulate, v);
  2158. (void)ret; // return value not used.
  2159. if (shape.mesh.indices.size() > 0 || shape.lines.indices.size() > 0 ||
  2160. shape.points.indices.size() > 0) {
  2161. shapes->push_back(shape);
  2162. }
  2163. // material = -1;
  2164. prim_group.clear();
  2165. shape = shape_t();
  2166. // @todo { multiple object name? }
  2167. token += 2;
  2168. std::stringstream ss;
  2169. ss << token;
  2170. name = ss.str();
  2171. continue;
  2172. }
  2173. if (token[0] == 't' && IS_SPACE(token[1])) {
  2174. const int max_tag_nums = 8192; // FIXME(syoyo): Parameterize.
  2175. tag_t tag;
  2176. token += 2;
  2177. tag.name = parseString(&token);
  2178. tag_sizes ts = parseTagTriple(&token);
  2179. if (ts.num_ints < 0) {
  2180. ts.num_ints = 0;
  2181. }
  2182. if (ts.num_ints > max_tag_nums) {
  2183. ts.num_ints = max_tag_nums;
  2184. }
  2185. if (ts.num_reals < 0) {
  2186. ts.num_reals = 0;
  2187. }
  2188. if (ts.num_reals > max_tag_nums) {
  2189. ts.num_reals = max_tag_nums;
  2190. }
  2191. if (ts.num_strings < 0) {
  2192. ts.num_strings = 0;
  2193. }
  2194. if (ts.num_strings > max_tag_nums) {
  2195. ts.num_strings = max_tag_nums;
  2196. }
  2197. tag.intValues.resize(static_cast<size_t>(ts.num_ints));
  2198. for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
  2199. tag.intValues[i] = parseInt(&token);
  2200. }
  2201. tag.floatValues.resize(static_cast<size_t>(ts.num_reals));
  2202. for (size_t i = 0; i < static_cast<size_t>(ts.num_reals); ++i) {
  2203. tag.floatValues[i] = parseReal(&token);
  2204. }
  2205. tag.stringValues.resize(static_cast<size_t>(ts.num_strings));
  2206. for (size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
  2207. tag.stringValues[i] = parseString(&token);
  2208. }
  2209. tags.push_back(tag);
  2210. continue;
  2211. }
  2212. if (token[0] == 's' && IS_SPACE(token[1])) {
  2213. // smoothing group id
  2214. token += 2;
  2215. // skip space.
  2216. token += strspn(token, " \t"); // skip space
  2217. if (token[0] == '\0') {
  2218. continue;
  2219. }
  2220. if (token[0] == '\r' || token[1] == '\n') {
  2221. continue;
  2222. }
  2223. if (strlen(token) >= 3 && token[0] == 'o' && token[1] == 'f' &&
  2224. token[2] == 'f') {
  2225. current_smoothing_id = 0;
  2226. } else {
  2227. // assume number
  2228. int smGroupId = parseInt(&token);
  2229. if (smGroupId < 0) {
  2230. // parse error. force set to 0.
  2231. // FIXME(syoyo): Report warning.
  2232. current_smoothing_id = 0;
  2233. } else {
  2234. current_smoothing_id = static_cast<unsigned int>(smGroupId);
  2235. }
  2236. }
  2237. continue;
  2238. } // smoothing group id
  2239. // Ignore unknown command.
  2240. }
  2241. // not all vertices have colors, no default colors desired? -> clear colors
  2242. if (!found_all_colors && !default_vcols_fallback) {
  2243. vc.clear();
  2244. }
  2245. if (greatest_v_idx >= static_cast<int>(v.size() / 3)) {
  2246. if (warn) {
  2247. std::stringstream ss;
  2248. ss << "Vertex indices out of bounds (line " << line_num << ".)\n"
  2249. << std::endl;
  2250. (*warn) += ss.str();
  2251. }
  2252. }
  2253. if (greatest_vn_idx >= static_cast<int>(vn.size() / 3)) {
  2254. if (warn) {
  2255. std::stringstream ss;
  2256. ss << "Vertex normal indices out of bounds (line " << line_num << ".)\n"
  2257. << std::endl;
  2258. (*warn) += ss.str();
  2259. }
  2260. }
  2261. if (greatest_vt_idx >= static_cast<int>(vt.size() / 2)) {
  2262. if (warn) {
  2263. std::stringstream ss;
  2264. ss << "Vertex texcoord indices out of bounds (line " << line_num << ".)\n"
  2265. << std::endl;
  2266. (*warn) += ss.str();
  2267. }
  2268. }
  2269. bool ret = exportGroupsToShape(&shape, prim_group, tags, material, name,
  2270. triangulate, v);
  2271. // exportGroupsToShape return false when `usemtl` is called in the last
  2272. // line.
  2273. // we also add `shape` to `shapes` when `shape.mesh` has already some
  2274. // faces(indices)
  2275. if (ret || shape.mesh.indices
  2276. .size()) { // FIXME(syoyo): Support other prims(e.g. lines)
  2277. shapes->push_back(shape);
  2278. }
  2279. prim_group.clear(); // for safety
  2280. if (err) {
  2281. (*err) += errss.str();
  2282. }
  2283. attrib->vertices.swap(v);
  2284. attrib->vertex_weights.swap(v);
  2285. attrib->normals.swap(vn);
  2286. attrib->texcoords.swap(vt);
  2287. attrib->texcoord_ws.swap(vt);
  2288. attrib->colors.swap(vc);
  2289. return true;
  2290. }
  2291. bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
  2292. void *user_data /*= NULL*/,
  2293. MaterialReader *readMatFn /*= NULL*/,
  2294. std::string *warn, /* = NULL*/
  2295. std::string *err /*= NULL*/) {
  2296. std::stringstream errss;
  2297. // material
  2298. std::map<std::string, int> material_map;
  2299. int material_id = -1; // -1 = invalid
  2300. std::vector<index_t> indices;
  2301. std::vector<material_t> materials;
  2302. std::vector<std::string> names;
  2303. names.reserve(2);
  2304. std::vector<const char *> names_out;
  2305. std::string linebuf;
  2306. while (inStream.peek() != -1) {
  2307. safeGetline(inStream, linebuf);
  2308. // Trim newline '\r\n' or '\n'
  2309. if (linebuf.size() > 0) {
  2310. if (linebuf[linebuf.size() - 1] == '\n')
  2311. linebuf.erase(linebuf.size() - 1);
  2312. }
  2313. if (linebuf.size() > 0) {
  2314. if (linebuf[linebuf.size() - 1] == '\r')
  2315. linebuf.erase(linebuf.size() - 1);
  2316. }
  2317. // Skip if empty line.
  2318. if (linebuf.empty()) {
  2319. continue;
  2320. }
  2321. // Skip leading space.
  2322. const char *token = linebuf.c_str();
  2323. token += strspn(token, " \t");
  2324. assert(token);
  2325. if (token[0] == '\0') continue; // empty line
  2326. if (token[0] == '#') continue; // comment line
  2327. // vertex
  2328. if (token[0] == 'v' && IS_SPACE((token[1]))) {
  2329. token += 2;
  2330. // TODO(syoyo): Support parsing vertex color extension.
  2331. real_t x, y, z, w; // w is optional. default = 1.0
  2332. parseV(&x, &y, &z, &w, &token);
  2333. if (callback.vertex_cb) {
  2334. callback.vertex_cb(user_data, x, y, z, w);
  2335. }
  2336. continue;
  2337. }
  2338. // normal
  2339. if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) {
  2340. token += 3;
  2341. real_t x, y, z;
  2342. parseReal3(&x, &y, &z, &token);
  2343. if (callback.normal_cb) {
  2344. callback.normal_cb(user_data, x, y, z);
  2345. }
  2346. continue;
  2347. }
  2348. // texcoord
  2349. if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) {
  2350. token += 3;
  2351. real_t x, y, z; // y and z are optional. default = 0.0
  2352. parseReal3(&x, &y, &z, &token);
  2353. if (callback.texcoord_cb) {
  2354. callback.texcoord_cb(user_data, x, y, z);
  2355. }
  2356. continue;
  2357. }
  2358. // face
  2359. if (token[0] == 'f' && IS_SPACE((token[1]))) {
  2360. token += 2;
  2361. token += strspn(token, " \t");
  2362. indices.clear();
  2363. while (!IS_NEW_LINE(token[0])) {
  2364. vertex_index_t vi = parseRawTriple(&token);
  2365. index_t idx;
  2366. idx.vertex_index = vi.v_idx;
  2367. idx.normal_index = vi.vn_idx;
  2368. idx.texcoord_index = vi.vt_idx;
  2369. indices.push_back(idx);
  2370. size_t n = strspn(token, " \t\r");
  2371. token += n;
  2372. }
  2373. if (callback.index_cb && indices.size() > 0) {
  2374. callback.index_cb(user_data, &indices.at(0),
  2375. static_cast<int>(indices.size()));
  2376. }
  2377. continue;
  2378. }
  2379. // use mtl
  2380. if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) {
  2381. token += 7;
  2382. std::stringstream ss;
  2383. ss << token;
  2384. std::string namebuf = ss.str();
  2385. int newMaterialId = -1;
  2386. std::map<std::string, int>::const_iterator it = material_map.find(namebuf);
  2387. if (it != material_map.end()) {
  2388. newMaterialId = it->second;
  2389. } else {
  2390. // { warn!! material not found }
  2391. if (warn && (!callback.usemtl_cb)) {
  2392. (*warn) += "material [ " + namebuf + " ] not found in .mtl\n";
  2393. }
  2394. }
  2395. if (newMaterialId != material_id) {
  2396. material_id = newMaterialId;
  2397. }
  2398. if (callback.usemtl_cb) {
  2399. callback.usemtl_cb(user_data, namebuf.c_str(), material_id);
  2400. }
  2401. continue;
  2402. }
  2403. // load mtl
  2404. if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) {
  2405. if (readMatFn) {
  2406. token += 7;
  2407. std::vector<std::string> filenames;
  2408. SplitString(std::string(token), ' ', filenames);
  2409. if (filenames.empty()) {
  2410. if (warn) {
  2411. (*warn) +=
  2412. "Looks like empty filename for mtllib. Use default "
  2413. "material. \n";
  2414. }
  2415. } else {
  2416. bool found = false;
  2417. for (size_t s = 0; s < filenames.size(); s++) {
  2418. std::string warn_mtl;
  2419. std::string err_mtl;
  2420. bool ok = (*readMatFn)(filenames[s].c_str(), &materials,
  2421. &material_map, &warn_mtl, &err_mtl);
  2422. if (warn && (!warn_mtl.empty())) {
  2423. (*warn) += warn_mtl; // This should be warn message.
  2424. }
  2425. if (err && (!err_mtl.empty())) {
  2426. (*err) += err_mtl;
  2427. }
  2428. if (ok) {
  2429. found = true;
  2430. break;
  2431. }
  2432. }
  2433. if (!found) {
  2434. if (warn) {
  2435. (*warn) +=
  2436. "Failed to load material file(s). Use default "
  2437. "material.\n";
  2438. }
  2439. } else {
  2440. if (callback.mtllib_cb) {
  2441. callback.mtllib_cb(user_data, &materials.at(0),
  2442. static_cast<int>(materials.size()));
  2443. }
  2444. }
  2445. }
  2446. }
  2447. continue;
  2448. }
  2449. // group name
  2450. if (token[0] == 'g' && IS_SPACE((token[1]))) {
  2451. names.clear();
  2452. while (!IS_NEW_LINE(token[0])) {
  2453. std::string str = parseString(&token);
  2454. names.push_back(str);
  2455. token += strspn(token, " \t\r"); // skip tag
  2456. }
  2457. assert(names.size() > 0);
  2458. if (callback.group_cb) {
  2459. if (names.size() > 1) {
  2460. // create const char* array.
  2461. names_out.resize(names.size() - 1);
  2462. for (size_t j = 0; j < names_out.size(); j++) {
  2463. names_out[j] = names[j + 1].c_str();
  2464. }
  2465. callback.group_cb(user_data, &names_out.at(0),
  2466. static_cast<int>(names_out.size()));
  2467. } else {
  2468. callback.group_cb(user_data, NULL, 0);
  2469. }
  2470. }
  2471. continue;
  2472. }
  2473. // object name
  2474. if (token[0] == 'o' && IS_SPACE((token[1]))) {
  2475. // @todo { multiple object name? }
  2476. token += 2;
  2477. std::stringstream ss;
  2478. ss << token;
  2479. std::string object_name = ss.str();
  2480. if (callback.object_cb) {
  2481. callback.object_cb(user_data, object_name.c_str());
  2482. }
  2483. continue;
  2484. }
  2485. #if 0 // @todo
  2486. if (token[0] == 't' && IS_SPACE(token[1])) {
  2487. tag_t tag;
  2488. token += 2;
  2489. std::stringstream ss;
  2490. ss << token;
  2491. tag.name = ss.str();
  2492. token += tag.name.size() + 1;
  2493. tag_sizes ts = parseTagTriple(&token);
  2494. tag.intValues.resize(static_cast<size_t>(ts.num_ints));
  2495. for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
  2496. tag.intValues[i] = atoi(token);
  2497. token += strcspn(token, "/ \t\r") + 1;
  2498. }
  2499. tag.floatValues.resize(static_cast<size_t>(ts.num_reals));
  2500. for (size_t i = 0; i < static_cast<size_t>(ts.num_reals); ++i) {
  2501. tag.floatValues[i] = parseReal(&token);
  2502. token += strcspn(token, "/ \t\r") + 1;
  2503. }
  2504. tag.stringValues.resize(static_cast<size_t>(ts.num_strings));
  2505. for (size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
  2506. std::stringstream ss;
  2507. ss << token;
  2508. tag.stringValues[i] = ss.str();
  2509. token += tag.stringValues[i].size() + 1;
  2510. }
  2511. tags.push_back(tag);
  2512. }
  2513. #endif
  2514. // Ignore unknown command.
  2515. }
  2516. if (err) {
  2517. (*err) += errss.str();
  2518. }
  2519. return true;
  2520. }
  2521. bool ObjReader::ParseFromFile(const std::string &filename,
  2522. const ObjReaderConfig &config) {
  2523. std::string mtl_search_path;
  2524. if (config.mtl_search_path.empty()) {
  2525. //
  2526. // split at last '/'(for unixish system) or '\\'(for windows) to get
  2527. // the base directory of .obj file
  2528. //
  2529. size_t pos = filename.find_last_of("/\\");
  2530. if (pos != std::string::npos) {
  2531. mtl_search_path = filename.substr(0, pos);
  2532. }
  2533. } else {
  2534. mtl_search_path = config.mtl_search_path;
  2535. }
  2536. valid_ = LoadObj(&attrib_, &shapes_, &materials_, &warning_, &error_,
  2537. filename.c_str(), mtl_search_path.c_str(),
  2538. config.triangulate, config.vertex_color);
  2539. return valid_;
  2540. }
  2541. bool ObjReader::ParseFromString(const std::string &obj_text,
  2542. const std::string &mtl_text,
  2543. const ObjReaderConfig &config) {
  2544. std::stringbuf obj_buf(obj_text);
  2545. std::stringbuf mtl_buf(mtl_text);
  2546. std::istream obj_ifs(&obj_buf);
  2547. std::istream mtl_ifs(&mtl_buf);
  2548. MaterialStreamReader mtl_ss(mtl_ifs);
  2549. valid_ = LoadObj(&attrib_, &shapes_, &materials_, &warning_, &error_,
  2550. &obj_ifs, &mtl_ss, config.triangulate, config.vertex_color);
  2551. return valid_;
  2552. }
  2553. #ifdef __clang__
  2554. #pragma clang diagnostic pop
  2555. #endif
  2556. } // namespace tinyobj
  2557. #endif