cgltf.h 173 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372
  1. /**
  2. * cgltf - a single-file glTF 2.0 parser written in C99.
  3. *
  4. * Version: 1.9
  5. *
  6. * Website: https://github.com/jkuhlmann/cgltf
  7. *
  8. * Distributed under the MIT License, see notice at the end of this file.
  9. *
  10. * Building:
  11. * Include this file where you need the struct and function
  12. * declarations. Have exactly one source file where you define
  13. * `CGLTF_IMPLEMENTATION` before including this file to get the
  14. * function definitions.
  15. *
  16. * Reference:
  17. * `cgltf_result cgltf_parse(const cgltf_options*, const void*,
  18. * cgltf_size, cgltf_data**)` parses both glTF and GLB data. If
  19. * this function returns `cgltf_result_success`, you have to call
  20. * `cgltf_free()` on the created `cgltf_data*` variable.
  21. * Note that contents of external files for buffers and images are not
  22. * automatically loaded. You'll need to read these files yourself using
  23. * URIs in the `cgltf_data` structure.
  24. *
  25. * `cgltf_options` is the struct passed to `cgltf_parse()` to control
  26. * parts of the parsing process. You can use it to force the file type
  27. * and provide memory allocation as well as file operation callbacks.
  28. * Should be zero-initialized to trigger default behavior.
  29. *
  30. * `cgltf_data` is the struct allocated and filled by `cgltf_parse()`.
  31. * It generally mirrors the glTF format as described by the spec (see
  32. * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0).
  33. *
  34. * `void cgltf_free(cgltf_data*)` frees the allocated `cgltf_data`
  35. * variable.
  36. *
  37. * `cgltf_result cgltf_load_buffers(const cgltf_options*, cgltf_data*,
  38. * const char* gltf_path)` can be optionally called to open and read buffer
  39. * files using the `FILE*` APIs. The `gltf_path` argument is the path to
  40. * the original glTF file, which allows the parser to resolve the path to
  41. * buffer files.
  42. *
  43. * `cgltf_result cgltf_load_buffer_base64(const cgltf_options* options,
  44. * cgltf_size size, const char* base64, void** out_data)` decodes
  45. * base64-encoded data content. Used internally by `cgltf_load_buffers()`
  46. * and may be useful if you're not dealing with normal files.
  47. *
  48. * `cgltf_result cgltf_parse_file(const cgltf_options* options, const
  49. * char* path, cgltf_data** out_data)` can be used to open the given
  50. * file using `FILE*` APIs and parse the data using `cgltf_parse()`.
  51. *
  52. * `cgltf_result cgltf_validate(cgltf_data*)` can be used to do additional
  53. * checks to make sure the parsed glTF data is valid.
  54. *
  55. * `cgltf_node_transform_local` converts the translation / rotation / scale properties of a node
  56. * into a mat4.
  57. *
  58. * `cgltf_node_transform_world` calls `cgltf_node_transform_local` on every ancestor in order
  59. * to compute the root-to-node transformation.
  60. *
  61. * `cgltf_accessor_unpack_floats` reads in the data from an accessor, applies sparse data (if any),
  62. * and converts them to floating point. Assumes that `cgltf_load_buffers` has already been called.
  63. * By passing null for the output pointer, users can find out how many floats are required in the
  64. * output buffer.
  65. *
  66. * `cgltf_accessor_num_components` is a tiny utility that tells you the dimensionality of
  67. * a certain accessor type. This can be used before `cgltf_accessor_unpack_floats` to help allocate
  68. * the necessary amount of memory.
  69. *
  70. * `cgltf_accessor_read_float` reads a certain element from a non-sparse accessor and converts it to
  71. * floating point, assuming that `cgltf_load_buffers` has already been called. The passed-in element
  72. * size is the number of floats in the output buffer, which should be in the range [1, 16]. Returns
  73. * false if the passed-in element_size is too small, or if the accessor is sparse.
  74. *
  75. * `cgltf_accessor_read_uint` is similar to its floating-point counterpart, but limited to reading
  76. * vector types and does not support matrix types. The passed-in element size is the number of uints
  77. * in the output buffer, which should be in the range [1, 4]. Returns false if the passed-in
  78. * element_size is too small, or if the accessor is sparse.
  79. *
  80. * `cgltf_accessor_read_index` is similar to its floating-point counterpart, but it returns size_t
  81. * and only works with single-component data types.
  82. *
  83. * `cgltf_result cgltf_copy_extras_json(const cgltf_data*, const cgltf_extras*,
  84. * char* dest, cgltf_size* dest_size)` allows users to retrieve the "extras" data that
  85. * can be attached to many glTF objects (which can be arbitrary JSON data). The
  86. * `cgltf_extras` struct stores the offsets of the start and end of the extras JSON data
  87. * as it appears in the complete glTF JSON data. This function copies the extras data
  88. * into the provided buffer. If `dest` is NULL, the length of the data is written into
  89. * `dest_size`. You can then parse this data using your own JSON parser
  90. * or, if you've included the cgltf implementation using the integrated JSMN JSON parser.
  91. */
  92. #ifndef CGLTF_H_INCLUDED__
  93. #define CGLTF_H_INCLUDED__
  94. #include <stddef.h>
  95. #ifdef __cplusplus
  96. extern "C" {
  97. #endif
  98. typedef size_t cgltf_size;
  99. typedef float cgltf_float;
  100. typedef int cgltf_int;
  101. typedef unsigned int cgltf_uint;
  102. typedef int cgltf_bool;
  103. typedef enum cgltf_file_type
  104. {
  105. cgltf_file_type_invalid,
  106. cgltf_file_type_gltf,
  107. cgltf_file_type_glb,
  108. } cgltf_file_type;
  109. typedef enum cgltf_result
  110. {
  111. cgltf_result_success,
  112. cgltf_result_data_too_short,
  113. cgltf_result_unknown_format,
  114. cgltf_result_invalid_json,
  115. cgltf_result_invalid_gltf,
  116. cgltf_result_invalid_options,
  117. cgltf_result_file_not_found,
  118. cgltf_result_io_error,
  119. cgltf_result_out_of_memory,
  120. cgltf_result_legacy_gltf,
  121. } cgltf_result;
  122. typedef struct cgltf_memory_options
  123. {
  124. void* (*alloc)(void* user, cgltf_size size);
  125. void (*free) (void* user, void* ptr);
  126. void* user_data;
  127. } cgltf_memory_options;
  128. typedef struct cgltf_file_options
  129. {
  130. cgltf_result(*read)(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, const char* path, cgltf_size* size, void** data);
  131. void (*release)(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data);
  132. void* user_data;
  133. } cgltf_file_options;
  134. typedef struct cgltf_options
  135. {
  136. cgltf_file_type type; /* invalid == auto detect */
  137. cgltf_size json_token_count; /* 0 == auto */
  138. cgltf_memory_options memory;
  139. cgltf_file_options file;
  140. } cgltf_options;
  141. typedef enum cgltf_buffer_view_type
  142. {
  143. cgltf_buffer_view_type_invalid,
  144. cgltf_buffer_view_type_indices,
  145. cgltf_buffer_view_type_vertices,
  146. } cgltf_buffer_view_type;
  147. typedef enum cgltf_attribute_type
  148. {
  149. cgltf_attribute_type_invalid,
  150. cgltf_attribute_type_position,
  151. cgltf_attribute_type_normal,
  152. cgltf_attribute_type_tangent,
  153. cgltf_attribute_type_texcoord,
  154. cgltf_attribute_type_color,
  155. cgltf_attribute_type_joints,
  156. cgltf_attribute_type_weights,
  157. } cgltf_attribute_type;
  158. typedef enum cgltf_component_type
  159. {
  160. cgltf_component_type_invalid,
  161. cgltf_component_type_r_8, /* BYTE */
  162. cgltf_component_type_r_8u, /* UNSIGNED_BYTE */
  163. cgltf_component_type_r_16, /* SHORT */
  164. cgltf_component_type_r_16u, /* UNSIGNED_SHORT */
  165. cgltf_component_type_r_32u, /* UNSIGNED_INT */
  166. cgltf_component_type_r_32f, /* FLOAT */
  167. } cgltf_component_type;
  168. typedef enum cgltf_type
  169. {
  170. cgltf_type_invalid,
  171. cgltf_type_scalar,
  172. cgltf_type_vec2,
  173. cgltf_type_vec3,
  174. cgltf_type_vec4,
  175. cgltf_type_mat2,
  176. cgltf_type_mat3,
  177. cgltf_type_mat4,
  178. } cgltf_type;
  179. typedef enum cgltf_primitive_type
  180. {
  181. cgltf_primitive_type_points,
  182. cgltf_primitive_type_lines,
  183. cgltf_primitive_type_line_loop,
  184. cgltf_primitive_type_line_strip,
  185. cgltf_primitive_type_triangles,
  186. cgltf_primitive_type_triangle_strip,
  187. cgltf_primitive_type_triangle_fan,
  188. } cgltf_primitive_type;
  189. typedef enum cgltf_alpha_mode
  190. {
  191. cgltf_alpha_mode_opaque,
  192. cgltf_alpha_mode_mask,
  193. cgltf_alpha_mode_blend,
  194. } cgltf_alpha_mode;
  195. typedef enum cgltf_animation_path_type {
  196. cgltf_animation_path_type_invalid,
  197. cgltf_animation_path_type_translation,
  198. cgltf_animation_path_type_rotation,
  199. cgltf_animation_path_type_scale,
  200. cgltf_animation_path_type_weights,
  201. } cgltf_animation_path_type;
  202. typedef enum cgltf_interpolation_type {
  203. cgltf_interpolation_type_linear,
  204. cgltf_interpolation_type_step,
  205. cgltf_interpolation_type_cubic_spline,
  206. } cgltf_interpolation_type;
  207. typedef enum cgltf_camera_type {
  208. cgltf_camera_type_invalid,
  209. cgltf_camera_type_perspective,
  210. cgltf_camera_type_orthographic,
  211. } cgltf_camera_type;
  212. typedef enum cgltf_light_type {
  213. cgltf_light_type_invalid,
  214. cgltf_light_type_directional,
  215. cgltf_light_type_point,
  216. cgltf_light_type_spot,
  217. } cgltf_light_type;
  218. typedef struct cgltf_extras {
  219. cgltf_size start_offset;
  220. cgltf_size end_offset;
  221. } cgltf_extras;
  222. typedef struct cgltf_extension {
  223. char* name;
  224. char* data;
  225. } cgltf_extension;
  226. typedef struct cgltf_buffer
  227. {
  228. cgltf_size size;
  229. char* uri;
  230. void* data; /* loaded by cgltf_load_buffers */
  231. cgltf_extras extras;
  232. cgltf_size extensions_count;
  233. cgltf_extension* extensions;
  234. } cgltf_buffer;
  235. typedef enum cgltf_meshopt_compression_mode {
  236. cgltf_meshopt_compression_mode_invalid,
  237. cgltf_meshopt_compression_mode_attributes,
  238. cgltf_meshopt_compression_mode_triangles,
  239. cgltf_meshopt_compression_mode_indices,
  240. } cgltf_meshopt_compression_mode;
  241. typedef enum cgltf_meshopt_compression_filter {
  242. cgltf_meshopt_compression_filter_none,
  243. cgltf_meshopt_compression_filter_octahedral,
  244. cgltf_meshopt_compression_filter_quaternion,
  245. cgltf_meshopt_compression_filter_exponential,
  246. } cgltf_meshopt_compression_filter;
  247. typedef struct cgltf_meshopt_compression
  248. {
  249. cgltf_buffer* buffer;
  250. cgltf_size offset;
  251. cgltf_size size;
  252. cgltf_size stride;
  253. cgltf_size count;
  254. cgltf_meshopt_compression_mode mode;
  255. cgltf_meshopt_compression_filter filter;
  256. } cgltf_meshopt_compression;
  257. typedef struct cgltf_buffer_view
  258. {
  259. cgltf_buffer* buffer;
  260. cgltf_size offset;
  261. cgltf_size size;
  262. cgltf_size stride; /* 0 == automatically determined by accessor */
  263. cgltf_buffer_view_type type;
  264. void* data; /* overrides buffer->data if present, filled by extensions */
  265. cgltf_bool has_meshopt_compression;
  266. cgltf_meshopt_compression meshopt_compression;
  267. cgltf_extras extras;
  268. cgltf_size extensions_count;
  269. cgltf_extension* extensions;
  270. } cgltf_buffer_view;
  271. typedef struct cgltf_accessor_sparse
  272. {
  273. cgltf_size count;
  274. cgltf_buffer_view* indices_buffer_view;
  275. cgltf_size indices_byte_offset;
  276. cgltf_component_type indices_component_type;
  277. cgltf_buffer_view* values_buffer_view;
  278. cgltf_size values_byte_offset;
  279. cgltf_extras extras;
  280. cgltf_extras indices_extras;
  281. cgltf_extras values_extras;
  282. cgltf_size extensions_count;
  283. cgltf_extension* extensions;
  284. cgltf_size indices_extensions_count;
  285. cgltf_extension* indices_extensions;
  286. cgltf_size values_extensions_count;
  287. cgltf_extension* values_extensions;
  288. } cgltf_accessor_sparse;
  289. typedef struct cgltf_accessor
  290. {
  291. cgltf_component_type component_type;
  292. cgltf_bool normalized;
  293. cgltf_type type;
  294. cgltf_size offset;
  295. cgltf_size count;
  296. cgltf_size stride;
  297. cgltf_buffer_view* buffer_view;
  298. cgltf_bool has_min;
  299. cgltf_float min[16];
  300. cgltf_bool has_max;
  301. cgltf_float max[16];
  302. cgltf_bool is_sparse;
  303. cgltf_accessor_sparse sparse;
  304. cgltf_extras extras;
  305. cgltf_size extensions_count;
  306. cgltf_extension* extensions;
  307. } cgltf_accessor;
  308. typedef struct cgltf_attribute
  309. {
  310. char* name;
  311. cgltf_attribute_type type;
  312. cgltf_int index;
  313. cgltf_accessor* data;
  314. } cgltf_attribute;
  315. typedef struct cgltf_image
  316. {
  317. char* name;
  318. char* uri;
  319. cgltf_buffer_view* buffer_view;
  320. char* mime_type;
  321. cgltf_extras extras;
  322. cgltf_size extensions_count;
  323. cgltf_extension* extensions;
  324. } cgltf_image;
  325. typedef struct cgltf_sampler
  326. {
  327. cgltf_int mag_filter;
  328. cgltf_int min_filter;
  329. cgltf_int wrap_s;
  330. cgltf_int wrap_t;
  331. cgltf_extras extras;
  332. cgltf_size extensions_count;
  333. cgltf_extension* extensions;
  334. } cgltf_sampler;
  335. typedef struct cgltf_texture
  336. {
  337. char* name;
  338. cgltf_image* image;
  339. cgltf_sampler* sampler;
  340. cgltf_extras extras;
  341. cgltf_size extensions_count;
  342. cgltf_extension* extensions;
  343. } cgltf_texture;
  344. typedef struct cgltf_texture_transform
  345. {
  346. cgltf_float offset[2];
  347. cgltf_float rotation;
  348. cgltf_float scale[2];
  349. cgltf_int texcoord;
  350. } cgltf_texture_transform;
  351. typedef struct cgltf_texture_view
  352. {
  353. cgltf_texture* texture;
  354. cgltf_int texcoord;
  355. cgltf_float scale; /* equivalent to strength for occlusion_texture */
  356. cgltf_bool has_transform;
  357. cgltf_texture_transform transform;
  358. cgltf_extras extras;
  359. cgltf_size extensions_count;
  360. cgltf_extension* extensions;
  361. } cgltf_texture_view;
  362. typedef struct cgltf_pbr_metallic_roughness
  363. {
  364. cgltf_texture_view base_color_texture;
  365. cgltf_texture_view metallic_roughness_texture;
  366. cgltf_float base_color_factor[4];
  367. cgltf_float metallic_factor;
  368. cgltf_float roughness_factor;
  369. cgltf_extras extras;
  370. } cgltf_pbr_metallic_roughness;
  371. typedef struct cgltf_pbr_specular_glossiness
  372. {
  373. cgltf_texture_view diffuse_texture;
  374. cgltf_texture_view specular_glossiness_texture;
  375. cgltf_float diffuse_factor[4];
  376. cgltf_float specular_factor[3];
  377. cgltf_float glossiness_factor;
  378. } cgltf_pbr_specular_glossiness;
  379. typedef struct cgltf_clearcoat
  380. {
  381. cgltf_texture_view clearcoat_texture;
  382. cgltf_texture_view clearcoat_roughness_texture;
  383. cgltf_texture_view clearcoat_normal_texture;
  384. cgltf_float clearcoat_factor;
  385. cgltf_float clearcoat_roughness_factor;
  386. } cgltf_clearcoat;
  387. typedef struct cgltf_transmission
  388. {
  389. cgltf_texture_view transmission_texture;
  390. cgltf_float transmission_factor;
  391. } cgltf_transmission;
  392. typedef struct cgltf_ior
  393. {
  394. cgltf_float ior;
  395. } cgltf_ior;
  396. typedef struct cgltf_specular
  397. {
  398. cgltf_texture_view specular_texture;
  399. cgltf_texture_view specular_color_texture;
  400. cgltf_float specular_color_factor[3];
  401. cgltf_float specular_factor;
  402. } cgltf_specular;
  403. typedef struct cgltf_volume
  404. {
  405. cgltf_texture_view thickness_texture;
  406. cgltf_float thickness_factor;
  407. cgltf_float attenuation_color[3];
  408. cgltf_float attenuation_distance;
  409. } cgltf_volume;
  410. typedef struct cgltf_sheen
  411. {
  412. cgltf_texture_view sheen_color_texture;
  413. cgltf_float sheen_color_factor[3];
  414. cgltf_texture_view sheen_roughness_texture;
  415. cgltf_float sheen_roughness_factor;
  416. } cgltf_sheen;
  417. typedef struct cgltf_material
  418. {
  419. char* name;
  420. cgltf_bool has_pbr_metallic_roughness;
  421. cgltf_bool has_pbr_specular_glossiness;
  422. cgltf_bool has_clearcoat;
  423. cgltf_bool has_transmission;
  424. cgltf_bool has_volume;
  425. cgltf_bool has_ior;
  426. cgltf_bool has_specular;
  427. cgltf_bool has_sheen;
  428. cgltf_pbr_metallic_roughness pbr_metallic_roughness;
  429. cgltf_pbr_specular_glossiness pbr_specular_glossiness;
  430. cgltf_clearcoat clearcoat;
  431. cgltf_ior ior;
  432. cgltf_specular specular;
  433. cgltf_sheen sheen;
  434. cgltf_transmission transmission;
  435. cgltf_volume volume;
  436. cgltf_texture_view normal_texture;
  437. cgltf_texture_view occlusion_texture;
  438. cgltf_texture_view emissive_texture;
  439. cgltf_float emissive_factor[3];
  440. cgltf_alpha_mode alpha_mode;
  441. cgltf_float alpha_cutoff;
  442. cgltf_bool double_sided;
  443. cgltf_bool unlit;
  444. cgltf_extras extras;
  445. cgltf_size extensions_count;
  446. cgltf_extension* extensions;
  447. } cgltf_material;
  448. typedef struct cgltf_material_mapping
  449. {
  450. cgltf_size variant;
  451. cgltf_material* material;
  452. cgltf_extras extras;
  453. } cgltf_material_mapping;
  454. typedef struct cgltf_morph_target {
  455. cgltf_attribute* attributes;
  456. cgltf_size attributes_count;
  457. } cgltf_morph_target;
  458. typedef struct cgltf_draco_mesh_compression {
  459. cgltf_buffer_view* buffer_view;
  460. cgltf_attribute* attributes;
  461. cgltf_size attributes_count;
  462. } cgltf_draco_mesh_compression;
  463. typedef struct cgltf_primitive {
  464. cgltf_primitive_type type;
  465. cgltf_accessor* indices;
  466. cgltf_material* material;
  467. cgltf_attribute* attributes;
  468. cgltf_size attributes_count;
  469. cgltf_morph_target* targets;
  470. cgltf_size targets_count;
  471. cgltf_extras extras;
  472. cgltf_bool has_draco_mesh_compression;
  473. cgltf_draco_mesh_compression draco_mesh_compression;
  474. cgltf_material_mapping* mappings;
  475. cgltf_size mappings_count;
  476. cgltf_size extensions_count;
  477. cgltf_extension* extensions;
  478. } cgltf_primitive;
  479. typedef struct cgltf_mesh {
  480. char* name;
  481. cgltf_primitive* primitives;
  482. cgltf_size primitives_count;
  483. cgltf_float* weights;
  484. cgltf_size weights_count;
  485. char** target_names;
  486. cgltf_size target_names_count;
  487. cgltf_extras extras;
  488. cgltf_size extensions_count;
  489. cgltf_extension* extensions;
  490. } cgltf_mesh;
  491. typedef struct cgltf_node cgltf_node;
  492. typedef struct cgltf_skin {
  493. char* name;
  494. cgltf_node** joints;
  495. cgltf_size joints_count;
  496. cgltf_node* skeleton;
  497. cgltf_accessor* inverse_bind_matrices;
  498. cgltf_extras extras;
  499. cgltf_size extensions_count;
  500. cgltf_extension* extensions;
  501. } cgltf_skin;
  502. typedef struct cgltf_camera_perspective {
  503. cgltf_float aspect_ratio;
  504. cgltf_float yfov;
  505. cgltf_float zfar;
  506. cgltf_float znear;
  507. cgltf_extras extras;
  508. } cgltf_camera_perspective;
  509. typedef struct cgltf_camera_orthographic {
  510. cgltf_float xmag;
  511. cgltf_float ymag;
  512. cgltf_float zfar;
  513. cgltf_float znear;
  514. cgltf_extras extras;
  515. } cgltf_camera_orthographic;
  516. typedef struct cgltf_camera {
  517. char* name;
  518. cgltf_camera_type type;
  519. union {
  520. cgltf_camera_perspective perspective;
  521. cgltf_camera_orthographic orthographic;
  522. } data;
  523. cgltf_extras extras;
  524. cgltf_size extensions_count;
  525. cgltf_extension* extensions;
  526. } cgltf_camera;
  527. typedef struct cgltf_light {
  528. char* name;
  529. cgltf_float color[3];
  530. cgltf_float intensity;
  531. cgltf_light_type type;
  532. cgltf_float range;
  533. cgltf_float spot_inner_cone_angle;
  534. cgltf_float spot_outer_cone_angle;
  535. } cgltf_light;
  536. struct cgltf_node {
  537. char* name;
  538. cgltf_node* parent;
  539. cgltf_node** children;
  540. cgltf_size children_count;
  541. cgltf_skin* skin;
  542. cgltf_mesh* mesh;
  543. cgltf_camera* camera;
  544. cgltf_light* light;
  545. cgltf_float* weights;
  546. cgltf_size weights_count;
  547. cgltf_bool has_translation;
  548. cgltf_bool has_rotation;
  549. cgltf_bool has_scale;
  550. cgltf_bool has_matrix;
  551. cgltf_float translation[3];
  552. cgltf_float rotation[4];
  553. cgltf_float scale[3];
  554. cgltf_float matrix[16];
  555. cgltf_extras extras;
  556. cgltf_size extensions_count;
  557. cgltf_extension* extensions;
  558. };
  559. typedef struct cgltf_scene {
  560. char* name;
  561. cgltf_node** nodes;
  562. cgltf_size nodes_count;
  563. cgltf_extras extras;
  564. cgltf_size extensions_count;
  565. cgltf_extension* extensions;
  566. } cgltf_scene;
  567. typedef struct cgltf_animation_sampler {
  568. cgltf_accessor* input;
  569. cgltf_accessor* output;
  570. cgltf_interpolation_type interpolation;
  571. cgltf_extras extras;
  572. cgltf_size extensions_count;
  573. cgltf_extension* extensions;
  574. } cgltf_animation_sampler;
  575. typedef struct cgltf_animation_channel {
  576. cgltf_animation_sampler* sampler;
  577. cgltf_node* target_node;
  578. cgltf_animation_path_type target_path;
  579. cgltf_extras extras;
  580. cgltf_size extensions_count;
  581. cgltf_extension* extensions;
  582. } cgltf_animation_channel;
  583. typedef struct cgltf_animation {
  584. char* name;
  585. cgltf_animation_sampler* samplers;
  586. cgltf_size samplers_count;
  587. cgltf_animation_channel* channels;
  588. cgltf_size channels_count;
  589. cgltf_extras extras;
  590. cgltf_size extensions_count;
  591. cgltf_extension* extensions;
  592. } cgltf_animation;
  593. typedef struct cgltf_material_variant
  594. {
  595. char* name;
  596. cgltf_extras extras;
  597. } cgltf_material_variant;
  598. typedef struct cgltf_asset {
  599. char* copyright;
  600. char* generator;
  601. char* version;
  602. char* min_version;
  603. cgltf_extras extras;
  604. cgltf_size extensions_count;
  605. cgltf_extension* extensions;
  606. } cgltf_asset;
  607. typedef struct cgltf_data
  608. {
  609. cgltf_file_type file_type;
  610. void* file_data;
  611. cgltf_asset asset;
  612. cgltf_mesh* meshes;
  613. cgltf_size meshes_count;
  614. cgltf_material* materials;
  615. cgltf_size materials_count;
  616. cgltf_accessor* accessors;
  617. cgltf_size accessors_count;
  618. cgltf_buffer_view* buffer_views;
  619. cgltf_size buffer_views_count;
  620. cgltf_buffer* buffers;
  621. cgltf_size buffers_count;
  622. cgltf_image* images;
  623. cgltf_size images_count;
  624. cgltf_texture* textures;
  625. cgltf_size textures_count;
  626. cgltf_sampler* samplers;
  627. cgltf_size samplers_count;
  628. cgltf_skin* skins;
  629. cgltf_size skins_count;
  630. cgltf_camera* cameras;
  631. cgltf_size cameras_count;
  632. cgltf_light* lights;
  633. cgltf_size lights_count;
  634. cgltf_node* nodes;
  635. cgltf_size nodes_count;
  636. cgltf_scene* scenes;
  637. cgltf_size scenes_count;
  638. cgltf_scene* scene;
  639. cgltf_animation* animations;
  640. cgltf_size animations_count;
  641. cgltf_material_variant* variants;
  642. cgltf_size variants_count;
  643. cgltf_extras extras;
  644. cgltf_size data_extensions_count;
  645. cgltf_extension* data_extensions;
  646. char** extensions_used;
  647. cgltf_size extensions_used_count;
  648. char** extensions_required;
  649. cgltf_size extensions_required_count;
  650. const char* json;
  651. cgltf_size json_size;
  652. const void* bin;
  653. cgltf_size bin_size;
  654. cgltf_memory_options memory;
  655. cgltf_file_options file;
  656. } cgltf_data;
  657. cgltf_result cgltf_parse(
  658. const cgltf_options* options,
  659. const void* data,
  660. cgltf_size size,
  661. cgltf_data** out_data);
  662. cgltf_result cgltf_parse_file(
  663. const cgltf_options* options,
  664. const char* path,
  665. cgltf_data** out_data);
  666. cgltf_result cgltf_load_buffers(
  667. const cgltf_options* options,
  668. cgltf_data* data,
  669. const char* gltf_path);
  670. cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data);
  671. void cgltf_decode_uri(char* uri);
  672. cgltf_result cgltf_validate(cgltf_data* data);
  673. void cgltf_free(cgltf_data* data);
  674. void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix);
  675. void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix);
  676. cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size);
  677. cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size);
  678. cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index);
  679. cgltf_size cgltf_num_components(cgltf_type type);
  680. cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_float* out, cgltf_size float_count);
  681. cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* extras, char* dest, cgltf_size* dest_size);
  682. #ifdef __cplusplus
  683. }
  684. #endif
  685. #endif /* #ifndef CGLTF_H_INCLUDED__ */
  686. /*
  687. *
  688. * Stop now, if you are only interested in the API.
  689. * Below, you find the implementation.
  690. *
  691. */
  692. #if defined(__INTELLISENSE__) || defined(__JETBRAINS_IDE__)
  693. /* This makes MSVC/CLion intellisense work. */
  694. #define CGLTF_IMPLEMENTATION
  695. #endif
  696. #ifdef CGLTF_IMPLEMENTATION
  697. #include <stdint.h> /* For uint8_t, uint32_t */
  698. #include <string.h> /* For strncpy */
  699. #include <stdio.h> /* For fopen */
  700. #include <limits.h> /* For UINT_MAX etc */
  701. #include <float.h> /* For FLT_MAX */
  702. #if !defined(CGLTF_MALLOC) || !defined(CGLTF_FREE) || !defined(CGLTF_ATOI) || !defined(CGLTF_ATOF)
  703. #include <stdlib.h> /* For malloc, free, atoi, atof */
  704. #endif
  705. /* JSMN_PARENT_LINKS is necessary to make parsing large structures linear in input size */
  706. #define JSMN_PARENT_LINKS
  707. /* JSMN_STRICT is necessary to reject invalid JSON documents */
  708. #define JSMN_STRICT
  709. /*
  710. * -- jsmn.h start --
  711. * Source: https://github.com/zserge/jsmn
  712. * License: MIT
  713. */
  714. typedef enum {
  715. JSMN_UNDEFINED = 0,
  716. JSMN_OBJECT = 1,
  717. JSMN_ARRAY = 2,
  718. JSMN_STRING = 3,
  719. JSMN_PRIMITIVE = 4
  720. } jsmntype_t;
  721. enum jsmnerr {
  722. /* Not enough tokens were provided */
  723. JSMN_ERROR_NOMEM = -1,
  724. /* Invalid character inside JSON string */
  725. JSMN_ERROR_INVAL = -2,
  726. /* The string is not a full JSON packet, more bytes expected */
  727. JSMN_ERROR_PART = -3
  728. };
  729. typedef struct {
  730. jsmntype_t type;
  731. int start;
  732. int end;
  733. int size;
  734. #ifdef JSMN_PARENT_LINKS
  735. int parent;
  736. #endif
  737. } jsmntok_t;
  738. typedef struct {
  739. unsigned int pos; /* offset in the JSON string */
  740. unsigned int toknext; /* next token to allocate */
  741. int toksuper; /* superior token node, e.g parent object or array */
  742. } jsmn_parser;
  743. static void jsmn_init(jsmn_parser *parser);
  744. static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, size_t num_tokens);
  745. /*
  746. * -- jsmn.h end --
  747. */
  748. static const cgltf_size GlbHeaderSize = 12;
  749. static const cgltf_size GlbChunkHeaderSize = 8;
  750. static const uint32_t GlbVersion = 2;
  751. static const uint32_t GlbMagic = 0x46546C67;
  752. static const uint32_t GlbMagicJsonChunk = 0x4E4F534A;
  753. static const uint32_t GlbMagicBinChunk = 0x004E4942;
  754. #ifndef CGLTF_MALLOC
  755. #define CGLTF_MALLOC(size) malloc(size)
  756. #endif
  757. #ifndef CGLTF_FREE
  758. #define CGLTF_FREE(ptr) free(ptr)
  759. #endif
  760. #ifndef CGLTF_ATOI
  761. #define CGLTF_ATOI(str) atoi(str)
  762. #endif
  763. #ifndef CGLTF_ATOF
  764. #define CGLTF_ATOF(str) atof(str)
  765. #endif
  766. static void* cgltf_default_alloc(void* user, cgltf_size size)
  767. {
  768. (void)user;
  769. return CGLTF_MALLOC(size);
  770. }
  771. static void cgltf_default_free(void* user, void* ptr)
  772. {
  773. (void)user;
  774. CGLTF_FREE(ptr);
  775. }
  776. static void* cgltf_calloc(cgltf_options* options, size_t element_size, cgltf_size count)
  777. {
  778. if (SIZE_MAX / element_size < count)
  779. {
  780. return NULL;
  781. }
  782. void* result = options->memory.alloc(options->memory.user_data, element_size * count);
  783. if (!result)
  784. {
  785. return NULL;
  786. }
  787. memset(result, 0, element_size * count);
  788. return result;
  789. }
  790. static cgltf_result cgltf_default_file_read(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, const char* path, cgltf_size* size, void** data)
  791. {
  792. (void)file_options;
  793. void* (*memory_alloc)(void*, cgltf_size) = memory_options->alloc ? memory_options->alloc : &cgltf_default_alloc;
  794. void (*memory_free)(void*, void*) = memory_options->free ? memory_options->free : &cgltf_default_free;
  795. FILE* file = fopen(path, "rb");
  796. if (!file)
  797. {
  798. return cgltf_result_file_not_found;
  799. }
  800. cgltf_size file_size = size ? *size : 0;
  801. if (file_size == 0)
  802. {
  803. fseek(file, 0, SEEK_END);
  804. long length = ftell(file);
  805. if (length < 0)
  806. {
  807. fclose(file);
  808. return cgltf_result_io_error;
  809. }
  810. fseek(file, 0, SEEK_SET);
  811. file_size = (cgltf_size)length;
  812. }
  813. char* file_data = (char*)memory_alloc(memory_options->user_data, file_size);
  814. if (!file_data)
  815. {
  816. fclose(file);
  817. return cgltf_result_out_of_memory;
  818. }
  819. cgltf_size read_size = fread(file_data, 1, file_size, file);
  820. fclose(file);
  821. if (read_size != file_size)
  822. {
  823. memory_free(memory_options->user_data, file_data);
  824. return cgltf_result_io_error;
  825. }
  826. if (size)
  827. {
  828. *size = file_size;
  829. }
  830. if (data)
  831. {
  832. *data = file_data;
  833. }
  834. return cgltf_result_success;
  835. }
  836. static void cgltf_default_file_release(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data)
  837. {
  838. (void)file_options;
  839. void (*memfree)(void*, void*) = memory_options->free ? memory_options->free : &cgltf_default_free;
  840. memfree(memory_options->user_data, data);
  841. }
  842. static cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data);
  843. cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_size size, cgltf_data** out_data)
  844. {
  845. if (size < GlbHeaderSize)
  846. {
  847. return cgltf_result_data_too_short;
  848. }
  849. if (options == NULL)
  850. {
  851. return cgltf_result_invalid_options;
  852. }
  853. cgltf_options fixed_options = *options;
  854. if (fixed_options.memory.alloc == NULL)
  855. {
  856. fixed_options.memory.alloc = &cgltf_default_alloc;
  857. }
  858. if (fixed_options.memory.free == NULL)
  859. {
  860. fixed_options.memory.free = &cgltf_default_free;
  861. }
  862. uint32_t tmp;
  863. // Magic
  864. memcpy(&tmp, data, 4);
  865. if (tmp != GlbMagic)
  866. {
  867. if (fixed_options.type == cgltf_file_type_invalid)
  868. {
  869. fixed_options.type = cgltf_file_type_gltf;
  870. }
  871. else if (fixed_options.type == cgltf_file_type_glb)
  872. {
  873. return cgltf_result_unknown_format;
  874. }
  875. }
  876. if (fixed_options.type == cgltf_file_type_gltf)
  877. {
  878. cgltf_result json_result = cgltf_parse_json(&fixed_options, (const uint8_t*)data, size, out_data);
  879. if (json_result != cgltf_result_success)
  880. {
  881. return json_result;
  882. }
  883. (*out_data)->file_type = cgltf_file_type_gltf;
  884. return cgltf_result_success;
  885. }
  886. const uint8_t* ptr = (const uint8_t*)data;
  887. // Version
  888. memcpy(&tmp, ptr + 4, 4);
  889. uint32_t version = tmp;
  890. if (version != GlbVersion)
  891. {
  892. return version < GlbVersion ? cgltf_result_legacy_gltf : cgltf_result_unknown_format;
  893. }
  894. // Total length
  895. memcpy(&tmp, ptr + 8, 4);
  896. if (tmp > size)
  897. {
  898. return cgltf_result_data_too_short;
  899. }
  900. const uint8_t* json_chunk = ptr + GlbHeaderSize;
  901. if (GlbHeaderSize + GlbChunkHeaderSize > size)
  902. {
  903. return cgltf_result_data_too_short;
  904. }
  905. // JSON chunk: length
  906. uint32_t json_length;
  907. memcpy(&json_length, json_chunk, 4);
  908. if (GlbHeaderSize + GlbChunkHeaderSize + json_length > size)
  909. {
  910. return cgltf_result_data_too_short;
  911. }
  912. // JSON chunk: magic
  913. memcpy(&tmp, json_chunk + 4, 4);
  914. if (tmp != GlbMagicJsonChunk)
  915. {
  916. return cgltf_result_unknown_format;
  917. }
  918. json_chunk += GlbChunkHeaderSize;
  919. const void* bin = 0;
  920. cgltf_size bin_size = 0;
  921. if (GlbHeaderSize + GlbChunkHeaderSize + json_length + GlbChunkHeaderSize <= size)
  922. {
  923. // We can read another chunk
  924. const uint8_t* bin_chunk = json_chunk + json_length;
  925. // Bin chunk: length
  926. uint32_t bin_length;
  927. memcpy(&bin_length, bin_chunk, 4);
  928. if (GlbHeaderSize + GlbChunkHeaderSize + json_length + GlbChunkHeaderSize + bin_length > size)
  929. {
  930. return cgltf_result_data_too_short;
  931. }
  932. // Bin chunk: magic
  933. memcpy(&tmp, bin_chunk + 4, 4);
  934. if (tmp != GlbMagicBinChunk)
  935. {
  936. return cgltf_result_unknown_format;
  937. }
  938. bin_chunk += GlbChunkHeaderSize;
  939. bin = bin_chunk;
  940. bin_size = bin_length;
  941. }
  942. cgltf_result json_result = cgltf_parse_json(&fixed_options, json_chunk, json_length, out_data);
  943. if (json_result != cgltf_result_success)
  944. {
  945. return json_result;
  946. }
  947. (*out_data)->file_type = cgltf_file_type_glb;
  948. (*out_data)->bin = bin;
  949. (*out_data)->bin_size = bin_size;
  950. return cgltf_result_success;
  951. }
  952. cgltf_result cgltf_parse_file(const cgltf_options* options, const char* path, cgltf_data** out_data)
  953. {
  954. if (options == NULL)
  955. {
  956. return cgltf_result_invalid_options;
  957. }
  958. void (*memory_free)(void*, void*) = options->memory.free ? options->memory.free : &cgltf_default_free;
  959. cgltf_result (*file_read)(const struct cgltf_memory_options*, const struct cgltf_file_options*, const char*, cgltf_size*, void**) = options->file.read ? options->file.read : &cgltf_default_file_read;
  960. void* file_data = NULL;
  961. cgltf_size file_size = 0;
  962. cgltf_result result = file_read(&options->memory, &options->file, path, &file_size, &file_data);
  963. if (result != cgltf_result_success)
  964. {
  965. return result;
  966. }
  967. result = cgltf_parse(options, file_data, file_size, out_data);
  968. if (result != cgltf_result_success)
  969. {
  970. memory_free(options->memory.user_data, file_data);
  971. return result;
  972. }
  973. (*out_data)->file_data = file_data;
  974. return cgltf_result_success;
  975. }
  976. static void cgltf_combine_paths(char* path, const char* base, const char* uri)
  977. {
  978. const char* s0 = strrchr(base, '/');
  979. const char* s1 = strrchr(base, '\\');
  980. const char* slash = s0 ? (s1 && s1 > s0 ? s1 : s0) : s1;
  981. if (slash)
  982. {
  983. size_t prefix = slash - base + 1;
  984. strncpy(path, base, prefix);
  985. strcpy(path + prefix, uri);
  986. }
  987. else
  988. {
  989. strcpy(path, uri);
  990. }
  991. }
  992. static cgltf_result cgltf_load_buffer_file(const cgltf_options* options, cgltf_size size, const char* uri, const char* gltf_path, void** out_data)
  993. {
  994. void* (*memory_alloc)(void*, cgltf_size) = options->memory.alloc ? options->memory.alloc : &cgltf_default_alloc;
  995. void (*memory_free)(void*, void*) = options->memory.free ? options->memory.free : &cgltf_default_free;
  996. cgltf_result (*file_read)(const struct cgltf_memory_options*, const struct cgltf_file_options*, const char*, cgltf_size*, void**) = options->file.read ? options->file.read : &cgltf_default_file_read;
  997. char* path = (char*)memory_alloc(options->memory.user_data, strlen(uri) + strlen(gltf_path) + 1);
  998. if (!path)
  999. {
  1000. return cgltf_result_out_of_memory;
  1001. }
  1002. cgltf_combine_paths(path, gltf_path, uri);
  1003. // after combining, the tail of the resulting path is a uri; decode_uri converts it into path
  1004. cgltf_decode_uri(path + strlen(path) - strlen(uri));
  1005. void* file_data = NULL;
  1006. cgltf_result result = file_read(&options->memory, &options->file, path, &size, &file_data);
  1007. memory_free(options->memory.user_data, path);
  1008. *out_data = (result == cgltf_result_success) ? file_data : NULL;
  1009. return result;
  1010. }
  1011. cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data)
  1012. {
  1013. void* (*memory_alloc)(void*, cgltf_size) = options->memory.alloc ? options->memory.alloc : &cgltf_default_alloc;
  1014. void (*memory_free)(void*, void*) = options->memory.free ? options->memory.free : &cgltf_default_free;
  1015. unsigned char* data = (unsigned char*)memory_alloc(options->memory.user_data, size);
  1016. if (!data)
  1017. {
  1018. return cgltf_result_out_of_memory;
  1019. }
  1020. unsigned int buffer = 0;
  1021. unsigned int buffer_bits = 0;
  1022. for (cgltf_size i = 0; i < size; ++i)
  1023. {
  1024. while (buffer_bits < 8)
  1025. {
  1026. char ch = *base64++;
  1027. int index =
  1028. (unsigned)(ch - 'A') < 26 ? (ch - 'A') :
  1029. (unsigned)(ch - 'a') < 26 ? (ch - 'a') + 26 :
  1030. (unsigned)(ch - '0') < 10 ? (ch - '0') + 52 :
  1031. ch == '+' ? 62 :
  1032. ch == '/' ? 63 :
  1033. -1;
  1034. if (index < 0)
  1035. {
  1036. memory_free(options->memory.user_data, data);
  1037. return cgltf_result_io_error;
  1038. }
  1039. buffer = (buffer << 6) | index;
  1040. buffer_bits += 6;
  1041. }
  1042. data[i] = (unsigned char)(buffer >> (buffer_bits - 8));
  1043. buffer_bits -= 8;
  1044. }
  1045. *out_data = data;
  1046. return cgltf_result_success;
  1047. }
  1048. static int cgltf_unhex(char ch)
  1049. {
  1050. return
  1051. (unsigned)(ch - '0') < 10 ? (ch - '0') :
  1052. (unsigned)(ch - 'A') < 6 ? (ch - 'A') + 10 :
  1053. (unsigned)(ch - 'a') < 6 ? (ch - 'a') + 10 :
  1054. -1;
  1055. }
  1056. void cgltf_decode_uri(char* uri)
  1057. {
  1058. char* write = uri;
  1059. char* i = uri;
  1060. while (*i)
  1061. {
  1062. if (*i == '%')
  1063. {
  1064. int ch1 = cgltf_unhex(i[1]);
  1065. if (ch1 >= 0)
  1066. {
  1067. int ch2 = cgltf_unhex(i[2]);
  1068. if (ch2 >= 0)
  1069. {
  1070. *write++ = (char)(ch1 * 16 + ch2);
  1071. i += 3;
  1072. continue;
  1073. }
  1074. }
  1075. }
  1076. *write++ = *i++;
  1077. }
  1078. *write = 0;
  1079. }
  1080. cgltf_result cgltf_load_buffers(const cgltf_options* options, cgltf_data* data, const char* gltf_path)
  1081. {
  1082. if (options == NULL)
  1083. {
  1084. return cgltf_result_invalid_options;
  1085. }
  1086. if (data->buffers_count && data->buffers[0].data == NULL && data->buffers[0].uri == NULL && data->bin)
  1087. {
  1088. if (data->bin_size < data->buffers[0].size)
  1089. {
  1090. return cgltf_result_data_too_short;
  1091. }
  1092. data->buffers[0].data = (void*)data->bin;
  1093. }
  1094. for (cgltf_size i = 0; i < data->buffers_count; ++i)
  1095. {
  1096. if (data->buffers[i].data)
  1097. {
  1098. continue;
  1099. }
  1100. const char* uri = data->buffers[i].uri;
  1101. if (uri == NULL)
  1102. {
  1103. continue;
  1104. }
  1105. if (strncmp(uri, "data:", 5) == 0)
  1106. {
  1107. const char* comma = strchr(uri, ',');
  1108. if (comma && comma - uri >= 7 && strncmp(comma - 7, ";base64", 7) == 0)
  1109. {
  1110. cgltf_result res = cgltf_load_buffer_base64(options, data->buffers[i].size, comma + 1, &data->buffers[i].data);
  1111. if (res != cgltf_result_success)
  1112. {
  1113. return res;
  1114. }
  1115. }
  1116. else
  1117. {
  1118. return cgltf_result_unknown_format;
  1119. }
  1120. }
  1121. else if (strstr(uri, "://") == NULL && gltf_path)
  1122. {
  1123. cgltf_result res = cgltf_load_buffer_file(options, data->buffers[i].size, uri, gltf_path, &data->buffers[i].data);
  1124. if (res != cgltf_result_success)
  1125. {
  1126. return res;
  1127. }
  1128. }
  1129. else
  1130. {
  1131. return cgltf_result_unknown_format;
  1132. }
  1133. }
  1134. return cgltf_result_success;
  1135. }
  1136. static cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type);
  1137. static cgltf_size cgltf_calc_index_bound(cgltf_buffer_view* buffer_view, cgltf_size offset, cgltf_component_type component_type, cgltf_size count)
  1138. {
  1139. char* data = (char*)buffer_view->buffer->data + offset + buffer_view->offset;
  1140. cgltf_size bound = 0;
  1141. switch (component_type)
  1142. {
  1143. case cgltf_component_type_r_8u:
  1144. for (size_t i = 0; i < count; ++i)
  1145. {
  1146. cgltf_size v = ((unsigned char*)data)[i];
  1147. bound = bound > v ? bound : v;
  1148. }
  1149. break;
  1150. case cgltf_component_type_r_16u:
  1151. for (size_t i = 0; i < count; ++i)
  1152. {
  1153. cgltf_size v = ((unsigned short*)data)[i];
  1154. bound = bound > v ? bound : v;
  1155. }
  1156. break;
  1157. case cgltf_component_type_r_32u:
  1158. for (size_t i = 0; i < count; ++i)
  1159. {
  1160. cgltf_size v = ((unsigned int*)data)[i];
  1161. bound = bound > v ? bound : v;
  1162. }
  1163. break;
  1164. default:
  1165. ;
  1166. }
  1167. return bound;
  1168. }
  1169. cgltf_result cgltf_validate(cgltf_data* data)
  1170. {
  1171. for (cgltf_size i = 0; i < data->accessors_count; ++i)
  1172. {
  1173. cgltf_accessor* accessor = &data->accessors[i];
  1174. cgltf_size element_size = cgltf_calc_size(accessor->type, accessor->component_type);
  1175. if (accessor->buffer_view)
  1176. {
  1177. cgltf_size req_size = accessor->offset + accessor->stride * (accessor->count - 1) + element_size;
  1178. if (accessor->buffer_view->size < req_size)
  1179. {
  1180. return cgltf_result_data_too_short;
  1181. }
  1182. }
  1183. if (accessor->is_sparse)
  1184. {
  1185. cgltf_accessor_sparse* sparse = &accessor->sparse;
  1186. cgltf_size indices_component_size = cgltf_calc_size(cgltf_type_scalar, sparse->indices_component_type);
  1187. cgltf_size indices_req_size = sparse->indices_byte_offset + indices_component_size * sparse->count;
  1188. cgltf_size values_req_size = sparse->values_byte_offset + element_size * sparse->count;
  1189. if (sparse->indices_buffer_view->size < indices_req_size ||
  1190. sparse->values_buffer_view->size < values_req_size)
  1191. {
  1192. return cgltf_result_data_too_short;
  1193. }
  1194. if (sparse->indices_component_type != cgltf_component_type_r_8u &&
  1195. sparse->indices_component_type != cgltf_component_type_r_16u &&
  1196. sparse->indices_component_type != cgltf_component_type_r_32u)
  1197. {
  1198. return cgltf_result_invalid_gltf;
  1199. }
  1200. if (sparse->indices_buffer_view->buffer->data)
  1201. {
  1202. cgltf_size index_bound = cgltf_calc_index_bound(sparse->indices_buffer_view, sparse->indices_byte_offset, sparse->indices_component_type, sparse->count);
  1203. if (index_bound >= accessor->count)
  1204. {
  1205. return cgltf_result_data_too_short;
  1206. }
  1207. }
  1208. }
  1209. }
  1210. for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
  1211. {
  1212. cgltf_size req_size = data->buffer_views[i].offset + data->buffer_views[i].size;
  1213. if (data->buffer_views[i].buffer && data->buffer_views[i].buffer->size < req_size)
  1214. {
  1215. return cgltf_result_data_too_short;
  1216. }
  1217. if (data->buffer_views[i].has_meshopt_compression)
  1218. {
  1219. cgltf_meshopt_compression* mc = &data->buffer_views[i].meshopt_compression;
  1220. if (mc->buffer == NULL || mc->buffer->size < mc->offset + mc->size)
  1221. {
  1222. return cgltf_result_data_too_short;
  1223. }
  1224. if (data->buffer_views[i].stride && mc->stride != data->buffer_views[i].stride)
  1225. {
  1226. return cgltf_result_invalid_gltf;
  1227. }
  1228. if (data->buffer_views[i].size != mc->stride * mc->count)
  1229. {
  1230. return cgltf_result_invalid_gltf;
  1231. }
  1232. if (mc->mode == cgltf_meshopt_compression_mode_invalid)
  1233. {
  1234. return cgltf_result_invalid_gltf;
  1235. }
  1236. if (mc->mode == cgltf_meshopt_compression_mode_attributes && !(mc->stride % 4 == 0 && mc->stride <= 256))
  1237. {
  1238. return cgltf_result_invalid_gltf;
  1239. }
  1240. if (mc->mode == cgltf_meshopt_compression_mode_triangles && mc->count % 3 != 0)
  1241. {
  1242. return cgltf_result_invalid_gltf;
  1243. }
  1244. if ((mc->mode == cgltf_meshopt_compression_mode_triangles || mc->mode == cgltf_meshopt_compression_mode_indices) && mc->stride != 2 && mc->stride != 4)
  1245. {
  1246. return cgltf_result_invalid_gltf;
  1247. }
  1248. if ((mc->mode == cgltf_meshopt_compression_mode_triangles || mc->mode == cgltf_meshopt_compression_mode_indices) && mc->filter != cgltf_meshopt_compression_filter_none)
  1249. {
  1250. return cgltf_result_invalid_gltf;
  1251. }
  1252. if (mc->filter == cgltf_meshopt_compression_filter_octahedral && mc->stride != 4 && mc->stride != 8)
  1253. {
  1254. return cgltf_result_invalid_gltf;
  1255. }
  1256. if (mc->filter == cgltf_meshopt_compression_filter_quaternion && mc->stride != 8)
  1257. {
  1258. return cgltf_result_invalid_gltf;
  1259. }
  1260. }
  1261. }
  1262. for (cgltf_size i = 0; i < data->meshes_count; ++i)
  1263. {
  1264. if (data->meshes[i].weights)
  1265. {
  1266. if (data->meshes[i].primitives_count && data->meshes[i].primitives[0].targets_count != data->meshes[i].weights_count)
  1267. {
  1268. return cgltf_result_invalid_gltf;
  1269. }
  1270. }
  1271. if (data->meshes[i].target_names)
  1272. {
  1273. if (data->meshes[i].primitives_count && data->meshes[i].primitives[0].targets_count != data->meshes[i].target_names_count)
  1274. {
  1275. return cgltf_result_invalid_gltf;
  1276. }
  1277. }
  1278. for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
  1279. {
  1280. if (data->meshes[i].primitives[j].targets_count != data->meshes[i].primitives[0].targets_count)
  1281. {
  1282. return cgltf_result_invalid_gltf;
  1283. }
  1284. if (data->meshes[i].primitives[j].attributes_count)
  1285. {
  1286. cgltf_accessor* first = data->meshes[i].primitives[j].attributes[0].data;
  1287. for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
  1288. {
  1289. if (data->meshes[i].primitives[j].attributes[k].data->count != first->count)
  1290. {
  1291. return cgltf_result_invalid_gltf;
  1292. }
  1293. }
  1294. for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
  1295. {
  1296. for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
  1297. {
  1298. if (data->meshes[i].primitives[j].targets[k].attributes[m].data->count != first->count)
  1299. {
  1300. return cgltf_result_invalid_gltf;
  1301. }
  1302. }
  1303. }
  1304. cgltf_accessor* indices = data->meshes[i].primitives[j].indices;
  1305. if (indices &&
  1306. indices->component_type != cgltf_component_type_r_8u &&
  1307. indices->component_type != cgltf_component_type_r_16u &&
  1308. indices->component_type != cgltf_component_type_r_32u)
  1309. {
  1310. return cgltf_result_invalid_gltf;
  1311. }
  1312. if (indices && indices->buffer_view && indices->buffer_view->buffer->data)
  1313. {
  1314. cgltf_size index_bound = cgltf_calc_index_bound(indices->buffer_view, indices->offset, indices->component_type, indices->count);
  1315. if (index_bound >= first->count)
  1316. {
  1317. return cgltf_result_data_too_short;
  1318. }
  1319. }
  1320. for (cgltf_size k = 0; k < data->meshes[i].primitives[j].mappings_count; ++k)
  1321. {
  1322. if (data->meshes[i].primitives[j].mappings[k].variant >= data->variants_count)
  1323. {
  1324. return cgltf_result_invalid_gltf;
  1325. }
  1326. }
  1327. }
  1328. }
  1329. }
  1330. for (cgltf_size i = 0; i < data->nodes_count; ++i)
  1331. {
  1332. if (data->nodes[i].weights && data->nodes[i].mesh)
  1333. {
  1334. if (data->nodes[i].mesh->primitives_count && data->nodes[i].mesh->primitives[0].targets_count != data->nodes[i].weights_count)
  1335. {
  1336. return cgltf_result_invalid_gltf;
  1337. }
  1338. }
  1339. }
  1340. for (cgltf_size i = 0; i < data->nodes_count; ++i)
  1341. {
  1342. cgltf_node* p1 = data->nodes[i].parent;
  1343. cgltf_node* p2 = p1 ? p1->parent : NULL;
  1344. while (p1 && p2)
  1345. {
  1346. if (p1 == p2)
  1347. {
  1348. return cgltf_result_invalid_gltf;
  1349. }
  1350. p1 = p1->parent;
  1351. p2 = p2->parent ? p2->parent->parent : NULL;
  1352. }
  1353. }
  1354. for (cgltf_size i = 0; i < data->scenes_count; ++i)
  1355. {
  1356. for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j)
  1357. {
  1358. if (data->scenes[i].nodes[j]->parent)
  1359. {
  1360. return cgltf_result_invalid_gltf;
  1361. }
  1362. }
  1363. }
  1364. for (cgltf_size i = 0; i < data->animations_count; ++i)
  1365. {
  1366. for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
  1367. {
  1368. cgltf_animation_channel* channel = &data->animations[i].channels[j];
  1369. if (!channel->target_node)
  1370. {
  1371. continue;
  1372. }
  1373. cgltf_size components = 1;
  1374. if (channel->target_path == cgltf_animation_path_type_weights)
  1375. {
  1376. if (!channel->target_node->mesh || !channel->target_node->mesh->primitives_count)
  1377. {
  1378. return cgltf_result_invalid_gltf;
  1379. }
  1380. components = channel->target_node->mesh->primitives[0].targets_count;
  1381. }
  1382. cgltf_size values = channel->sampler->interpolation == cgltf_interpolation_type_cubic_spline ? 3 : 1;
  1383. if (channel->sampler->input->count * components * values != channel->sampler->output->count)
  1384. {
  1385. return cgltf_result_data_too_short;
  1386. }
  1387. }
  1388. }
  1389. return cgltf_result_success;
  1390. }
  1391. cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* extras, char* dest, cgltf_size* dest_size)
  1392. {
  1393. cgltf_size json_size = extras->end_offset - extras->start_offset;
  1394. if (!dest)
  1395. {
  1396. if (dest_size)
  1397. {
  1398. *dest_size = json_size + 1;
  1399. return cgltf_result_success;
  1400. }
  1401. return cgltf_result_invalid_options;
  1402. }
  1403. if (*dest_size + 1 < json_size)
  1404. {
  1405. strncpy(dest, data->json + extras->start_offset, *dest_size - 1);
  1406. dest[*dest_size - 1] = 0;
  1407. }
  1408. else
  1409. {
  1410. strncpy(dest, data->json + extras->start_offset, json_size);
  1411. dest[json_size] = 0;
  1412. }
  1413. return cgltf_result_success;
  1414. }
  1415. void cgltf_free_extensions(cgltf_data* data, cgltf_extension* extensions, cgltf_size extensions_count)
  1416. {
  1417. for (cgltf_size i = 0; i < extensions_count; ++i)
  1418. {
  1419. data->memory.free(data->memory.user_data, extensions[i].name);
  1420. data->memory.free(data->memory.user_data, extensions[i].data);
  1421. }
  1422. data->memory.free(data->memory.user_data, extensions);
  1423. }
  1424. void cgltf_free(cgltf_data* data)
  1425. {
  1426. if (!data)
  1427. {
  1428. return;
  1429. }
  1430. void (*file_release)(const struct cgltf_memory_options*, const struct cgltf_file_options*, void* data) = data->file.release ? data->file.release : cgltf_default_file_release;
  1431. data->memory.free(data->memory.user_data, data->asset.copyright);
  1432. data->memory.free(data->memory.user_data, data->asset.generator);
  1433. data->memory.free(data->memory.user_data, data->asset.version);
  1434. data->memory.free(data->memory.user_data, data->asset.min_version);
  1435. cgltf_free_extensions(data, data->asset.extensions, data->asset.extensions_count);
  1436. for (cgltf_size i = 0; i < data->accessors_count; ++i)
  1437. {
  1438. if(data->accessors[i].is_sparse)
  1439. {
  1440. cgltf_free_extensions(data, data->accessors[i].sparse.extensions, data->accessors[i].sparse.extensions_count);
  1441. cgltf_free_extensions(data, data->accessors[i].sparse.indices_extensions, data->accessors[i].sparse.indices_extensions_count);
  1442. cgltf_free_extensions(data, data->accessors[i].sparse.values_extensions, data->accessors[i].sparse.values_extensions_count);
  1443. }
  1444. cgltf_free_extensions(data, data->accessors[i].extensions, data->accessors[i].extensions_count);
  1445. }
  1446. data->memory.free(data->memory.user_data, data->accessors);
  1447. for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
  1448. {
  1449. data->memory.free(data->memory.user_data, data->buffer_views[i].data);
  1450. cgltf_free_extensions(data, data->buffer_views[i].extensions, data->buffer_views[i].extensions_count);
  1451. }
  1452. data->memory.free(data->memory.user_data, data->buffer_views);
  1453. for (cgltf_size i = 0; i < data->buffers_count; ++i)
  1454. {
  1455. if (data->buffers[i].data != data->bin)
  1456. {
  1457. file_release(&data->memory, &data->file, data->buffers[i].data);
  1458. }
  1459. data->memory.free(data->memory.user_data, data->buffers[i].uri);
  1460. cgltf_free_extensions(data, data->buffers[i].extensions, data->buffers[i].extensions_count);
  1461. }
  1462. data->memory.free(data->memory.user_data, data->buffers);
  1463. for (cgltf_size i = 0; i < data->meshes_count; ++i)
  1464. {
  1465. data->memory.free(data->memory.user_data, data->meshes[i].name);
  1466. for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
  1467. {
  1468. for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
  1469. {
  1470. data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].attributes[k].name);
  1471. }
  1472. data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].attributes);
  1473. for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
  1474. {
  1475. for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
  1476. {
  1477. data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes[m].name);
  1478. }
  1479. data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes);
  1480. }
  1481. data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets);
  1482. if (data->meshes[i].primitives[j].has_draco_mesh_compression)
  1483. {
  1484. for (cgltf_size k = 0; k < data->meshes[i].primitives[j].draco_mesh_compression.attributes_count; ++k)
  1485. {
  1486. data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].draco_mesh_compression.attributes[k].name);
  1487. }
  1488. data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].draco_mesh_compression.attributes);
  1489. }
  1490. data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].mappings);
  1491. cgltf_free_extensions(data, data->meshes[i].primitives[j].extensions, data->meshes[i].primitives[j].extensions_count);
  1492. }
  1493. data->memory.free(data->memory.user_data, data->meshes[i].primitives);
  1494. data->memory.free(data->memory.user_data, data->meshes[i].weights);
  1495. for (cgltf_size j = 0; j < data->meshes[i].target_names_count; ++j)
  1496. {
  1497. data->memory.free(data->memory.user_data, data->meshes[i].target_names[j]);
  1498. }
  1499. cgltf_free_extensions(data, data->meshes[i].extensions, data->meshes[i].extensions_count);
  1500. data->memory.free(data->memory.user_data, data->meshes[i].target_names);
  1501. }
  1502. data->memory.free(data->memory.user_data, data->meshes);
  1503. for (cgltf_size i = 0; i < data->materials_count; ++i)
  1504. {
  1505. data->memory.free(data->memory.user_data, data->materials[i].name);
  1506. if(data->materials[i].has_pbr_metallic_roughness)
  1507. {
  1508. cgltf_free_extensions(data, data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.extensions, data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.extensions_count);
  1509. cgltf_free_extensions(data, data->materials[i].pbr_metallic_roughness.base_color_texture.extensions, data->materials[i].pbr_metallic_roughness.base_color_texture.extensions_count);
  1510. }
  1511. if(data->materials[i].has_pbr_specular_glossiness)
  1512. {
  1513. cgltf_free_extensions(data, data->materials[i].pbr_specular_glossiness.diffuse_texture.extensions, data->materials[i].pbr_specular_glossiness.diffuse_texture.extensions_count);
  1514. cgltf_free_extensions(data, data->materials[i].pbr_specular_glossiness.specular_glossiness_texture.extensions, data->materials[i].pbr_specular_glossiness.specular_glossiness_texture.extensions_count);
  1515. }
  1516. if(data->materials[i].has_clearcoat)
  1517. {
  1518. cgltf_free_extensions(data, data->materials[i].clearcoat.clearcoat_texture.extensions, data->materials[i].clearcoat.clearcoat_texture.extensions_count);
  1519. cgltf_free_extensions(data, data->materials[i].clearcoat.clearcoat_roughness_texture.extensions, data->materials[i].clearcoat.clearcoat_roughness_texture.extensions_count);
  1520. cgltf_free_extensions(data, data->materials[i].clearcoat.clearcoat_normal_texture.extensions, data->materials[i].clearcoat.clearcoat_normal_texture.extensions_count);
  1521. }
  1522. if(data->materials[i].has_specular)
  1523. {
  1524. cgltf_free_extensions(data, data->materials[i].specular.specular_texture.extensions, data->materials[i].specular.specular_texture.extensions_count);
  1525. cgltf_free_extensions(data, data->materials[i].specular.specular_color_texture.extensions, data->materials[i].specular.specular_color_texture.extensions_count);
  1526. }
  1527. if(data->materials[i].has_transmission)
  1528. {
  1529. cgltf_free_extensions(data, data->materials[i].transmission.transmission_texture.extensions, data->materials[i].transmission.transmission_texture.extensions_count);
  1530. }
  1531. if (data->materials[i].has_volume)
  1532. {
  1533. cgltf_free_extensions(data, data->materials[i].volume.thickness_texture.extensions, data->materials[i].volume.thickness_texture.extensions_count);
  1534. }
  1535. if(data->materials[i].has_sheen)
  1536. {
  1537. cgltf_free_extensions(data, data->materials[i].sheen.sheen_color_texture.extensions, data->materials[i].sheen.sheen_color_texture.extensions_count);
  1538. cgltf_free_extensions(data, data->materials[i].sheen.sheen_roughness_texture.extensions, data->materials[i].sheen.sheen_roughness_texture.extensions_count);
  1539. }
  1540. cgltf_free_extensions(data, data->materials[i].normal_texture.extensions, data->materials[i].normal_texture.extensions_count);
  1541. cgltf_free_extensions(data, data->materials[i].occlusion_texture.extensions, data->materials[i].occlusion_texture.extensions_count);
  1542. cgltf_free_extensions(data, data->materials[i].emissive_texture.extensions, data->materials[i].emissive_texture.extensions_count);
  1543. cgltf_free_extensions(data, data->materials[i].extensions, data->materials[i].extensions_count);
  1544. }
  1545. data->memory.free(data->memory.user_data, data->materials);
  1546. for (cgltf_size i = 0; i < data->images_count; ++i)
  1547. {
  1548. data->memory.free(data->memory.user_data, data->images[i].name);
  1549. data->memory.free(data->memory.user_data, data->images[i].uri);
  1550. data->memory.free(data->memory.user_data, data->images[i].mime_type);
  1551. cgltf_free_extensions(data, data->images[i].extensions, data->images[i].extensions_count);
  1552. }
  1553. data->memory.free(data->memory.user_data, data->images);
  1554. for (cgltf_size i = 0; i < data->textures_count; ++i)
  1555. {
  1556. data->memory.free(data->memory.user_data, data->textures[i].name);
  1557. cgltf_free_extensions(data, data->textures[i].extensions, data->textures[i].extensions_count);
  1558. }
  1559. data->memory.free(data->memory.user_data, data->textures);
  1560. for (cgltf_size i = 0; i < data->samplers_count; ++i)
  1561. {
  1562. cgltf_free_extensions(data, data->samplers[i].extensions, data->samplers[i].extensions_count);
  1563. }
  1564. data->memory.free(data->memory.user_data, data->samplers);
  1565. for (cgltf_size i = 0; i < data->skins_count; ++i)
  1566. {
  1567. data->memory.free(data->memory.user_data, data->skins[i].name);
  1568. data->memory.free(data->memory.user_data, data->skins[i].joints);
  1569. cgltf_free_extensions(data, data->skins[i].extensions, data->skins[i].extensions_count);
  1570. }
  1571. data->memory.free(data->memory.user_data, data->skins);
  1572. for (cgltf_size i = 0; i < data->cameras_count; ++i)
  1573. {
  1574. data->memory.free(data->memory.user_data, data->cameras[i].name);
  1575. cgltf_free_extensions(data, data->cameras[i].extensions, data->cameras[i].extensions_count);
  1576. }
  1577. data->memory.free(data->memory.user_data, data->cameras);
  1578. for (cgltf_size i = 0; i < data->lights_count; ++i)
  1579. {
  1580. data->memory.free(data->memory.user_data, data->lights[i].name);
  1581. }
  1582. data->memory.free(data->memory.user_data, data->lights);
  1583. for (cgltf_size i = 0; i < data->nodes_count; ++i)
  1584. {
  1585. data->memory.free(data->memory.user_data, data->nodes[i].name);
  1586. data->memory.free(data->memory.user_data, data->nodes[i].children);
  1587. data->memory.free(data->memory.user_data, data->nodes[i].weights);
  1588. cgltf_free_extensions(data, data->nodes[i].extensions, data->nodes[i].extensions_count);
  1589. }
  1590. data->memory.free(data->memory.user_data, data->nodes);
  1591. for (cgltf_size i = 0; i < data->scenes_count; ++i)
  1592. {
  1593. data->memory.free(data->memory.user_data, data->scenes[i].name);
  1594. data->memory.free(data->memory.user_data, data->scenes[i].nodes);
  1595. cgltf_free_extensions(data, data->scenes[i].extensions, data->scenes[i].extensions_count);
  1596. }
  1597. data->memory.free(data->memory.user_data, data->scenes);
  1598. for (cgltf_size i = 0; i < data->animations_count; ++i)
  1599. {
  1600. data->memory.free(data->memory.user_data, data->animations[i].name);
  1601. for (cgltf_size j = 0; j < data->animations[i].samplers_count; ++j)
  1602. {
  1603. cgltf_free_extensions(data, data->animations[i].samplers[j].extensions, data->animations[i].samplers[j].extensions_count);
  1604. }
  1605. data->memory.free(data->memory.user_data, data->animations[i].samplers);
  1606. for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
  1607. {
  1608. cgltf_free_extensions(data, data->animations[i].channels[j].extensions, data->animations[i].channels[j].extensions_count);
  1609. }
  1610. data->memory.free(data->memory.user_data, data->animations[i].channels);
  1611. cgltf_free_extensions(data, data->animations[i].extensions, data->animations[i].extensions_count);
  1612. }
  1613. data->memory.free(data->memory.user_data, data->animations);
  1614. for (cgltf_size i = 0; i < data->variants_count; ++i)
  1615. {
  1616. data->memory.free(data->memory.user_data, data->variants[i].name);
  1617. }
  1618. data->memory.free(data->memory.user_data, data->variants);
  1619. cgltf_free_extensions(data, data->data_extensions, data->data_extensions_count);
  1620. for (cgltf_size i = 0; i < data->extensions_used_count; ++i)
  1621. {
  1622. data->memory.free(data->memory.user_data, data->extensions_used[i]);
  1623. }
  1624. data->memory.free(data->memory.user_data, data->extensions_used);
  1625. for (cgltf_size i = 0; i < data->extensions_required_count; ++i)
  1626. {
  1627. data->memory.free(data->memory.user_data, data->extensions_required[i]);
  1628. }
  1629. data->memory.free(data->memory.user_data, data->extensions_required);
  1630. file_release(&data->memory, &data->file, data->file_data);
  1631. data->memory.free(data->memory.user_data, data);
  1632. }
  1633. void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix)
  1634. {
  1635. cgltf_float* lm = out_matrix;
  1636. if (node->has_matrix)
  1637. {
  1638. memcpy(lm, node->matrix, sizeof(float) * 16);
  1639. }
  1640. else
  1641. {
  1642. float tx = node->translation[0];
  1643. float ty = node->translation[1];
  1644. float tz = node->translation[2];
  1645. float qx = node->rotation[0];
  1646. float qy = node->rotation[1];
  1647. float qz = node->rotation[2];
  1648. float qw = node->rotation[3];
  1649. float sx = node->scale[0];
  1650. float sy = node->scale[1];
  1651. float sz = node->scale[2];
  1652. lm[0] = (1 - 2 * qy*qy - 2 * qz*qz) * sx;
  1653. lm[1] = (2 * qx*qy + 2 * qz*qw) * sx;
  1654. lm[2] = (2 * qx*qz - 2 * qy*qw) * sx;
  1655. lm[3] = 0.f;
  1656. lm[4] = (2 * qx*qy - 2 * qz*qw) * sy;
  1657. lm[5] = (1 - 2 * qx*qx - 2 * qz*qz) * sy;
  1658. lm[6] = (2 * qy*qz + 2 * qx*qw) * sy;
  1659. lm[7] = 0.f;
  1660. lm[8] = (2 * qx*qz + 2 * qy*qw) * sz;
  1661. lm[9] = (2 * qy*qz - 2 * qx*qw) * sz;
  1662. lm[10] = (1 - 2 * qx*qx - 2 * qy*qy) * sz;
  1663. lm[11] = 0.f;
  1664. lm[12] = tx;
  1665. lm[13] = ty;
  1666. lm[14] = tz;
  1667. lm[15] = 1.f;
  1668. }
  1669. }
  1670. void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix)
  1671. {
  1672. cgltf_float* lm = out_matrix;
  1673. cgltf_node_transform_local(node, lm);
  1674. const cgltf_node* parent = node->parent;
  1675. while (parent)
  1676. {
  1677. float pm[16];
  1678. cgltf_node_transform_local(parent, pm);
  1679. for (int i = 0; i < 4; ++i)
  1680. {
  1681. float l0 = lm[i * 4 + 0];
  1682. float l1 = lm[i * 4 + 1];
  1683. float l2 = lm[i * 4 + 2];
  1684. float r0 = l0 * pm[0] + l1 * pm[4] + l2 * pm[8];
  1685. float r1 = l0 * pm[1] + l1 * pm[5] + l2 * pm[9];
  1686. float r2 = l0 * pm[2] + l1 * pm[6] + l2 * pm[10];
  1687. lm[i * 4 + 0] = r0;
  1688. lm[i * 4 + 1] = r1;
  1689. lm[i * 4 + 2] = r2;
  1690. }
  1691. lm[12] += pm[12];
  1692. lm[13] += pm[13];
  1693. lm[14] += pm[14];
  1694. parent = parent->parent;
  1695. }
  1696. }
  1697. static cgltf_size cgltf_component_read_index(const void* in, cgltf_component_type component_type)
  1698. {
  1699. switch (component_type)
  1700. {
  1701. case cgltf_component_type_r_16:
  1702. return *((const int16_t*) in);
  1703. case cgltf_component_type_r_16u:
  1704. return *((const uint16_t*) in);
  1705. case cgltf_component_type_r_32u:
  1706. return *((const uint32_t*) in);
  1707. case cgltf_component_type_r_32f:
  1708. return (cgltf_size)*((const float*) in);
  1709. case cgltf_component_type_r_8:
  1710. return *((const int8_t*) in);
  1711. case cgltf_component_type_r_8u:
  1712. return *((const uint8_t*) in);
  1713. default:
  1714. return 0;
  1715. }
  1716. }
  1717. static cgltf_float cgltf_component_read_float(const void* in, cgltf_component_type component_type, cgltf_bool normalized)
  1718. {
  1719. if (component_type == cgltf_component_type_r_32f)
  1720. {
  1721. return *((const float*) in);
  1722. }
  1723. if (normalized)
  1724. {
  1725. switch (component_type)
  1726. {
  1727. // note: glTF spec doesn't currently define normalized conversions for 32-bit integers
  1728. case cgltf_component_type_r_16:
  1729. return *((const int16_t*) in) / (cgltf_float)32767;
  1730. case cgltf_component_type_r_16u:
  1731. return *((const uint16_t*) in) / (cgltf_float)65535;
  1732. case cgltf_component_type_r_8:
  1733. return *((const int8_t*) in) / (cgltf_float)127;
  1734. case cgltf_component_type_r_8u:
  1735. return *((const uint8_t*) in) / (cgltf_float)255;
  1736. default:
  1737. return 0;
  1738. }
  1739. }
  1740. return (cgltf_float)cgltf_component_read_index(in, component_type);
  1741. }
  1742. static cgltf_size cgltf_component_size(cgltf_component_type component_type);
  1743. static cgltf_bool cgltf_element_read_float(const uint8_t* element, cgltf_type type, cgltf_component_type component_type, cgltf_bool normalized, cgltf_float* out, cgltf_size element_size)
  1744. {
  1745. cgltf_size num_components = cgltf_num_components(type);
  1746. if (element_size < num_components) {
  1747. return 0;
  1748. }
  1749. // There are three special cases for component extraction, see #data-alignment in the 2.0 spec.
  1750. cgltf_size component_size = cgltf_component_size(component_type);
  1751. if (type == cgltf_type_mat2 && component_size == 1)
  1752. {
  1753. out[0] = cgltf_component_read_float(element, component_type, normalized);
  1754. out[1] = cgltf_component_read_float(element + 1, component_type, normalized);
  1755. out[2] = cgltf_component_read_float(element + 4, component_type, normalized);
  1756. out[3] = cgltf_component_read_float(element + 5, component_type, normalized);
  1757. return 1;
  1758. }
  1759. if (type == cgltf_type_mat3 && component_size == 1)
  1760. {
  1761. out[0] = cgltf_component_read_float(element, component_type, normalized);
  1762. out[1] = cgltf_component_read_float(element + 1, component_type, normalized);
  1763. out[2] = cgltf_component_read_float(element + 2, component_type, normalized);
  1764. out[3] = cgltf_component_read_float(element + 4, component_type, normalized);
  1765. out[4] = cgltf_component_read_float(element + 5, component_type, normalized);
  1766. out[5] = cgltf_component_read_float(element + 6, component_type, normalized);
  1767. out[6] = cgltf_component_read_float(element + 8, component_type, normalized);
  1768. out[7] = cgltf_component_read_float(element + 9, component_type, normalized);
  1769. out[8] = cgltf_component_read_float(element + 10, component_type, normalized);
  1770. return 1;
  1771. }
  1772. if (type == cgltf_type_mat3 && component_size == 2)
  1773. {
  1774. out[0] = cgltf_component_read_float(element, component_type, normalized);
  1775. out[1] = cgltf_component_read_float(element + 2, component_type, normalized);
  1776. out[2] = cgltf_component_read_float(element + 4, component_type, normalized);
  1777. out[3] = cgltf_component_read_float(element + 8, component_type, normalized);
  1778. out[4] = cgltf_component_read_float(element + 10, component_type, normalized);
  1779. out[5] = cgltf_component_read_float(element + 12, component_type, normalized);
  1780. out[6] = cgltf_component_read_float(element + 16, component_type, normalized);
  1781. out[7] = cgltf_component_read_float(element + 18, component_type, normalized);
  1782. out[8] = cgltf_component_read_float(element + 20, component_type, normalized);
  1783. return 1;
  1784. }
  1785. for (cgltf_size i = 0; i < num_components; ++i)
  1786. {
  1787. out[i] = cgltf_component_read_float(element + component_size * i, component_type, normalized);
  1788. }
  1789. return 1;
  1790. }
  1791. const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view)
  1792. {
  1793. if (view->data)
  1794. return (const uint8_t*)view->data;
  1795. if (!view->buffer->data)
  1796. return NULL;
  1797. const uint8_t* result = (const uint8_t*)view->buffer->data;
  1798. result += view->offset;
  1799. return result;
  1800. }
  1801. cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size)
  1802. {
  1803. if (accessor->is_sparse)
  1804. {
  1805. return 0;
  1806. }
  1807. if (accessor->buffer_view == NULL)
  1808. {
  1809. memset(out, 0, element_size * sizeof(cgltf_float));
  1810. return 1;
  1811. }
  1812. const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
  1813. if (element == NULL)
  1814. {
  1815. return 0;
  1816. }
  1817. element += accessor->offset + accessor->stride * index;
  1818. return cgltf_element_read_float(element, accessor->type, accessor->component_type, accessor->normalized, out, element_size);
  1819. }
  1820. cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_float* out, cgltf_size float_count)
  1821. {
  1822. cgltf_size floats_per_element = cgltf_num_components(accessor->type);
  1823. cgltf_size available_floats = accessor->count * floats_per_element;
  1824. if (out == NULL)
  1825. {
  1826. return available_floats;
  1827. }
  1828. float_count = available_floats < float_count ? available_floats : float_count;
  1829. cgltf_size element_count = float_count / floats_per_element;
  1830. // First pass: convert each element in the base accessor.
  1831. cgltf_float* dest = out;
  1832. cgltf_accessor dense = *accessor;
  1833. dense.is_sparse = 0;
  1834. for (cgltf_size index = 0; index < element_count; index++, dest += floats_per_element)
  1835. {
  1836. if (!cgltf_accessor_read_float(&dense, index, dest, floats_per_element))
  1837. {
  1838. return 0;
  1839. }
  1840. }
  1841. // Second pass: write out each element in the sparse accessor.
  1842. if (accessor->is_sparse)
  1843. {
  1844. const cgltf_accessor_sparse* sparse = &dense.sparse;
  1845. const uint8_t* index_data = cgltf_buffer_view_data(sparse->indices_buffer_view);
  1846. const uint8_t* reader_head = cgltf_buffer_view_data(sparse->values_buffer_view);
  1847. if (index_data == NULL || reader_head == NULL)
  1848. {
  1849. return 0;
  1850. }
  1851. index_data += sparse->indices_byte_offset;
  1852. reader_head += sparse->values_byte_offset;
  1853. cgltf_size index_stride = cgltf_component_size(sparse->indices_component_type);
  1854. for (cgltf_size reader_index = 0; reader_index < sparse->count; reader_index++, index_data += index_stride)
  1855. {
  1856. size_t writer_index = cgltf_component_read_index(index_data, sparse->indices_component_type);
  1857. float* writer_head = out + writer_index * floats_per_element;
  1858. if (!cgltf_element_read_float(reader_head, dense.type, dense.component_type, dense.normalized, writer_head, floats_per_element))
  1859. {
  1860. return 0;
  1861. }
  1862. reader_head += dense.stride;
  1863. }
  1864. }
  1865. return element_count * floats_per_element;
  1866. }
  1867. static cgltf_uint cgltf_component_read_uint(const void* in, cgltf_component_type component_type)
  1868. {
  1869. switch (component_type)
  1870. {
  1871. case cgltf_component_type_r_8:
  1872. return *((const int8_t*) in);
  1873. case cgltf_component_type_r_8u:
  1874. return *((const uint8_t*) in);
  1875. case cgltf_component_type_r_16:
  1876. return *((const int16_t*) in);
  1877. case cgltf_component_type_r_16u:
  1878. return *((const uint16_t*) in);
  1879. case cgltf_component_type_r_32u:
  1880. return *((const uint32_t*) in);
  1881. default:
  1882. return 0;
  1883. }
  1884. }
  1885. static cgltf_bool cgltf_element_read_uint(const uint8_t* element, cgltf_type type, cgltf_component_type component_type, cgltf_uint* out, cgltf_size element_size)
  1886. {
  1887. cgltf_size num_components = cgltf_num_components(type);
  1888. if (element_size < num_components)
  1889. {
  1890. return 0;
  1891. }
  1892. // Reading integer matrices is not a valid use case
  1893. if (type == cgltf_type_mat2 || type == cgltf_type_mat3 || type == cgltf_type_mat4)
  1894. {
  1895. return 0;
  1896. }
  1897. cgltf_size component_size = cgltf_component_size(component_type);
  1898. for (cgltf_size i = 0; i < num_components; ++i)
  1899. {
  1900. out[i] = cgltf_component_read_uint(element + component_size * i, component_type);
  1901. }
  1902. return 1;
  1903. }
  1904. cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size)
  1905. {
  1906. if (accessor->is_sparse)
  1907. {
  1908. return 0;
  1909. }
  1910. if (accessor->buffer_view == NULL)
  1911. {
  1912. memset(out, 0, element_size * sizeof( cgltf_uint ));
  1913. return 1;
  1914. }
  1915. const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
  1916. if (element == NULL)
  1917. {
  1918. return 0;
  1919. }
  1920. element += accessor->offset + accessor->stride * index;
  1921. return cgltf_element_read_uint(element, accessor->type, accessor->component_type, out, element_size);
  1922. }
  1923. cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index)
  1924. {
  1925. if (accessor->is_sparse)
  1926. {
  1927. return 0; // This is an error case, but we can't communicate the error with existing interface.
  1928. }
  1929. if (accessor->buffer_view == NULL)
  1930. {
  1931. return 0;
  1932. }
  1933. const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
  1934. if (element == NULL)
  1935. {
  1936. return 0; // This is an error case, but we can't communicate the error with existing interface.
  1937. }
  1938. element += accessor->offset + accessor->stride * index;
  1939. return cgltf_component_read_index(element, accessor->component_type);
  1940. }
  1941. #define CGLTF_ERROR_JSON -1
  1942. #define CGLTF_ERROR_NOMEM -2
  1943. #define CGLTF_ERROR_LEGACY -3
  1944. #define CGLTF_CHECK_TOKTYPE(tok_, type_) if ((tok_).type != (type_)) { return CGLTF_ERROR_JSON; }
  1945. #define CGLTF_CHECK_KEY(tok_) if ((tok_).type != JSMN_STRING || (tok_).size == 0) { return CGLTF_ERROR_JSON; } /* checking size for 0 verifies that a value follows the key */
  1946. #define CGLTF_PTRINDEX(type, idx) (type*)((cgltf_size)idx + 1)
  1947. #define CGLTF_PTRFIXUP(var, data, size) if (var) { if ((cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1]; }
  1948. #define CGLTF_PTRFIXUP_REQ(var, data, size) if (!var || (cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1];
  1949. static int cgltf_json_strcmp(jsmntok_t const* tok, const uint8_t* json_chunk, const char* str)
  1950. {
  1951. CGLTF_CHECK_TOKTYPE(*tok, JSMN_STRING);
  1952. size_t const str_len = strlen(str);
  1953. size_t const name_length = tok->end - tok->start;
  1954. return (str_len == name_length) ? strncmp((const char*)json_chunk + tok->start, str, str_len) : 128;
  1955. }
  1956. static int cgltf_json_to_int(jsmntok_t const* tok, const uint8_t* json_chunk)
  1957. {
  1958. CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
  1959. char tmp[128];
  1960. int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : (int)(sizeof(tmp) - 1);
  1961. strncpy(tmp, (const char*)json_chunk + tok->start, size);
  1962. tmp[size] = 0;
  1963. return CGLTF_ATOI(tmp);
  1964. }
  1965. static cgltf_float cgltf_json_to_float(jsmntok_t const* tok, const uint8_t* json_chunk)
  1966. {
  1967. CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
  1968. char tmp[128];
  1969. int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : (int)(sizeof(tmp) - 1);
  1970. strncpy(tmp, (const char*)json_chunk + tok->start, size);
  1971. tmp[size] = 0;
  1972. return (cgltf_float)CGLTF_ATOF(tmp);
  1973. }
  1974. static cgltf_bool cgltf_json_to_bool(jsmntok_t const* tok, const uint8_t* json_chunk)
  1975. {
  1976. int size = tok->end - tok->start;
  1977. return size == 4 && memcmp(json_chunk + tok->start, "true", 4) == 0;
  1978. }
  1979. static int cgltf_skip_json(jsmntok_t const* tokens, int i)
  1980. {
  1981. int end = i + 1;
  1982. while (i < end)
  1983. {
  1984. switch (tokens[i].type)
  1985. {
  1986. case JSMN_OBJECT:
  1987. end += tokens[i].size * 2;
  1988. break;
  1989. case JSMN_ARRAY:
  1990. end += tokens[i].size;
  1991. break;
  1992. case JSMN_PRIMITIVE:
  1993. case JSMN_STRING:
  1994. break;
  1995. default:
  1996. return -1;
  1997. }
  1998. i++;
  1999. }
  2000. return i;
  2001. }
  2002. static void cgltf_fill_float_array(float* out_array, int size, float value)
  2003. {
  2004. for (int j = 0; j < size; ++j)
  2005. {
  2006. out_array[j] = value;
  2007. }
  2008. }
  2009. static int cgltf_parse_json_float_array(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, float* out_array, int size)
  2010. {
  2011. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
  2012. if (tokens[i].size != size)
  2013. {
  2014. return CGLTF_ERROR_JSON;
  2015. }
  2016. ++i;
  2017. for (int j = 0; j < size; ++j)
  2018. {
  2019. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
  2020. out_array[j] = cgltf_json_to_float(tokens + i, json_chunk);
  2021. ++i;
  2022. }
  2023. return i;
  2024. }
  2025. static int cgltf_parse_json_string(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, char** out_string)
  2026. {
  2027. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_STRING);
  2028. if (*out_string)
  2029. {
  2030. return CGLTF_ERROR_JSON;
  2031. }
  2032. int size = tokens[i].end - tokens[i].start;
  2033. char* result = (char*)options->memory.alloc(options->memory.user_data, size + 1);
  2034. if (!result)
  2035. {
  2036. return CGLTF_ERROR_NOMEM;
  2037. }
  2038. strncpy(result, (const char*)json_chunk + tokens[i].start, size);
  2039. result[size] = 0;
  2040. *out_string = result;
  2041. return i + 1;
  2042. }
  2043. static int cgltf_parse_json_array(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, size_t element_size, void** out_array, cgltf_size* out_size)
  2044. {
  2045. (void)json_chunk;
  2046. if (tokens[i].type != JSMN_ARRAY)
  2047. {
  2048. return tokens[i].type == JSMN_OBJECT ? CGLTF_ERROR_LEGACY : CGLTF_ERROR_JSON;
  2049. }
  2050. if (*out_array)
  2051. {
  2052. return CGLTF_ERROR_JSON;
  2053. }
  2054. int size = tokens[i].size;
  2055. void* result = cgltf_calloc(options, element_size, size);
  2056. if (!result)
  2057. {
  2058. return CGLTF_ERROR_NOMEM;
  2059. }
  2060. *out_array = result;
  2061. *out_size = size;
  2062. return i + 1;
  2063. }
  2064. static int cgltf_parse_json_string_array(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, char*** out_array, cgltf_size* out_size)
  2065. {
  2066. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
  2067. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(char*), (void**)out_array, out_size);
  2068. if (i < 0)
  2069. {
  2070. return i;
  2071. }
  2072. for (cgltf_size j = 0; j < *out_size; ++j)
  2073. {
  2074. i = cgltf_parse_json_string(options, tokens, i, json_chunk, j + (*out_array));
  2075. if (i < 0)
  2076. {
  2077. return i;
  2078. }
  2079. }
  2080. return i;
  2081. }
  2082. static void cgltf_parse_attribute_type(const char* name, cgltf_attribute_type* out_type, int* out_index)
  2083. {
  2084. const char* us = strchr(name, '_');
  2085. size_t len = us ? (size_t)(us - name) : strlen(name);
  2086. if (len == 8 && strncmp(name, "POSITION", 8) == 0)
  2087. {
  2088. *out_type = cgltf_attribute_type_position;
  2089. }
  2090. else if (len == 6 && strncmp(name, "NORMAL", 6) == 0)
  2091. {
  2092. *out_type = cgltf_attribute_type_normal;
  2093. }
  2094. else if (len == 7 && strncmp(name, "TANGENT", 7) == 0)
  2095. {
  2096. *out_type = cgltf_attribute_type_tangent;
  2097. }
  2098. else if (len == 8 && strncmp(name, "TEXCOORD", 8) == 0)
  2099. {
  2100. *out_type = cgltf_attribute_type_texcoord;
  2101. }
  2102. else if (len == 5 && strncmp(name, "COLOR", 5) == 0)
  2103. {
  2104. *out_type = cgltf_attribute_type_color;
  2105. }
  2106. else if (len == 6 && strncmp(name, "JOINTS", 6) == 0)
  2107. {
  2108. *out_type = cgltf_attribute_type_joints;
  2109. }
  2110. else if (len == 7 && strncmp(name, "WEIGHTS", 7) == 0)
  2111. {
  2112. *out_type = cgltf_attribute_type_weights;
  2113. }
  2114. else
  2115. {
  2116. *out_type = cgltf_attribute_type_invalid;
  2117. }
  2118. if (us && *out_type != cgltf_attribute_type_invalid)
  2119. {
  2120. *out_index = CGLTF_ATOI(us + 1);
  2121. }
  2122. }
  2123. static int cgltf_parse_json_attribute_list(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_attribute** out_attributes, cgltf_size* out_attributes_count)
  2124. {
  2125. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2126. if (*out_attributes)
  2127. {
  2128. return CGLTF_ERROR_JSON;
  2129. }
  2130. *out_attributes_count = tokens[i].size;
  2131. *out_attributes = (cgltf_attribute*)cgltf_calloc(options, sizeof(cgltf_attribute), *out_attributes_count);
  2132. ++i;
  2133. if (!*out_attributes)
  2134. {
  2135. return CGLTF_ERROR_NOMEM;
  2136. }
  2137. for (cgltf_size j = 0; j < *out_attributes_count; ++j)
  2138. {
  2139. CGLTF_CHECK_KEY(tokens[i]);
  2140. i = cgltf_parse_json_string(options, tokens, i, json_chunk, &(*out_attributes)[j].name);
  2141. if (i < 0)
  2142. {
  2143. return CGLTF_ERROR_JSON;
  2144. }
  2145. cgltf_parse_attribute_type((*out_attributes)[j].name, &(*out_attributes)[j].type, &(*out_attributes)[j].index);
  2146. (*out_attributes)[j].data = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
  2147. ++i;
  2148. }
  2149. return i;
  2150. }
  2151. static int cgltf_parse_json_extras(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_extras* out_extras)
  2152. {
  2153. (void)json_chunk;
  2154. out_extras->start_offset = tokens[i].start;
  2155. out_extras->end_offset = tokens[i].end;
  2156. i = cgltf_skip_json(tokens, i);
  2157. return i;
  2158. }
  2159. static int cgltf_parse_json_unprocessed_extension(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_extension* out_extension)
  2160. {
  2161. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_STRING);
  2162. CGLTF_CHECK_TOKTYPE(tokens[i+1], JSMN_OBJECT);
  2163. if (out_extension->name)
  2164. {
  2165. return CGLTF_ERROR_JSON;
  2166. }
  2167. cgltf_size name_length = tokens[i].end - tokens[i].start;
  2168. out_extension->name = (char*)options->memory.alloc(options->memory.user_data, name_length + 1);
  2169. if (!out_extension->name)
  2170. {
  2171. return CGLTF_ERROR_NOMEM;
  2172. }
  2173. strncpy(out_extension->name, (const char*)json_chunk + tokens[i].start, name_length);
  2174. out_extension->name[name_length] = 0;
  2175. i++;
  2176. size_t start = tokens[i].start;
  2177. size_t size = tokens[i].end - start;
  2178. out_extension->data = (char*)options->memory.alloc(options->memory.user_data, size + 1);
  2179. if (!out_extension->data)
  2180. {
  2181. return CGLTF_ERROR_NOMEM;
  2182. }
  2183. strncpy(out_extension->data, (const char*)json_chunk + start, size);
  2184. out_extension->data[size] = '\0';
  2185. i = cgltf_skip_json(tokens, i);
  2186. return i;
  2187. }
  2188. static int cgltf_parse_json_unprocessed_extensions(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_size* out_extensions_count, cgltf_extension** out_extensions)
  2189. {
  2190. ++i;
  2191. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2192. if(*out_extensions)
  2193. {
  2194. return CGLTF_ERROR_JSON;
  2195. }
  2196. int extensions_size = tokens[i].size;
  2197. *out_extensions_count = 0;
  2198. *out_extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
  2199. if (!*out_extensions)
  2200. {
  2201. return CGLTF_ERROR_NOMEM;
  2202. }
  2203. ++i;
  2204. for (int j = 0; j < extensions_size; ++j)
  2205. {
  2206. CGLTF_CHECK_KEY(tokens[i]);
  2207. cgltf_size extension_index = (*out_extensions_count)++;
  2208. cgltf_extension* extension = &((*out_extensions)[extension_index]);
  2209. i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, extension);
  2210. if (i < 0)
  2211. {
  2212. return i;
  2213. }
  2214. }
  2215. return i;
  2216. }
  2217. static int cgltf_parse_json_draco_mesh_compression(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_draco_mesh_compression* out_draco_mesh_compression)
  2218. {
  2219. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2220. int size = tokens[i].size;
  2221. ++i;
  2222. for (int j = 0; j < size; ++j)
  2223. {
  2224. CGLTF_CHECK_KEY(tokens[i]);
  2225. if (cgltf_json_strcmp(tokens + i, json_chunk, "attributes") == 0)
  2226. {
  2227. i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_draco_mesh_compression->attributes, &out_draco_mesh_compression->attributes_count);
  2228. }
  2229. else if (cgltf_json_strcmp(tokens + i, json_chunk, "bufferView") == 0)
  2230. {
  2231. ++i;
  2232. out_draco_mesh_compression->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
  2233. ++i;
  2234. }
  2235. if (i < 0)
  2236. {
  2237. return i;
  2238. }
  2239. }
  2240. return i;
  2241. }
  2242. static int cgltf_parse_json_material_mapping_data(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_material_mapping* out_mappings, cgltf_size* offset)
  2243. {
  2244. (void)options;
  2245. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
  2246. int size = tokens[i].size;
  2247. ++i;
  2248. for (int j = 0; j < size; ++j)
  2249. {
  2250. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2251. int obj_size = tokens[i].size;
  2252. ++i;
  2253. int material = -1;
  2254. int variants_tok = -1;
  2255. cgltf_extras extras = {0, 0};
  2256. for (int k = 0; k < obj_size; ++k)
  2257. {
  2258. CGLTF_CHECK_KEY(tokens[i]);
  2259. if (cgltf_json_strcmp(tokens + i, json_chunk, "material") == 0)
  2260. {
  2261. ++i;
  2262. material = cgltf_json_to_int(tokens + i, json_chunk);
  2263. ++i;
  2264. }
  2265. else if (cgltf_json_strcmp(tokens + i, json_chunk, "variants") == 0)
  2266. {
  2267. variants_tok = i+1;
  2268. CGLTF_CHECK_TOKTYPE(tokens[variants_tok], JSMN_ARRAY);
  2269. i = cgltf_skip_json(tokens, i+1);
  2270. }
  2271. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  2272. {
  2273. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &extras);
  2274. }
  2275. else
  2276. {
  2277. i = cgltf_skip_json(tokens, i+1);
  2278. }
  2279. if (i < 0)
  2280. {
  2281. return i;
  2282. }
  2283. }
  2284. if (material < 0 || variants_tok < 0)
  2285. {
  2286. return CGLTF_ERROR_JSON;
  2287. }
  2288. if (out_mappings)
  2289. {
  2290. for (int k = 0; k < tokens[variants_tok].size; ++k)
  2291. {
  2292. int variant = cgltf_json_to_int(&tokens[variants_tok + 1 + k], json_chunk);
  2293. if (variant < 0)
  2294. return variant;
  2295. out_mappings[*offset].material = CGLTF_PTRINDEX(cgltf_material, material);
  2296. out_mappings[*offset].variant = variant;
  2297. out_mappings[*offset].extras = extras;
  2298. (*offset)++;
  2299. }
  2300. }
  2301. else
  2302. {
  2303. (*offset) += tokens[variants_tok].size;
  2304. }
  2305. }
  2306. return i;
  2307. }
  2308. static int cgltf_parse_json_material_mappings(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_primitive* out_prim)
  2309. {
  2310. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2311. int size = tokens[i].size;
  2312. ++i;
  2313. for (int j = 0; j < size; ++j)
  2314. {
  2315. CGLTF_CHECK_KEY(tokens[i]);
  2316. if (cgltf_json_strcmp(tokens + i, json_chunk, "mappings") == 0)
  2317. {
  2318. if (out_prim->mappings)
  2319. {
  2320. return CGLTF_ERROR_JSON;
  2321. }
  2322. cgltf_size mappings_offset = 0;
  2323. int k = cgltf_parse_json_material_mapping_data(options, tokens, i + 1, json_chunk, NULL, &mappings_offset);
  2324. if (k < 0)
  2325. {
  2326. return k;
  2327. }
  2328. out_prim->mappings_count = mappings_offset;
  2329. out_prim->mappings = (cgltf_material_mapping*)cgltf_calloc(options, sizeof(cgltf_material_mapping), out_prim->mappings_count);
  2330. mappings_offset = 0;
  2331. i = cgltf_parse_json_material_mapping_data(options, tokens, i + 1, json_chunk, out_prim->mappings, &mappings_offset);
  2332. }
  2333. else
  2334. {
  2335. i = cgltf_skip_json(tokens, i+1);
  2336. }
  2337. if (i < 0)
  2338. {
  2339. return i;
  2340. }
  2341. }
  2342. return i;
  2343. }
  2344. static int cgltf_parse_json_primitive(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_primitive* out_prim)
  2345. {
  2346. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2347. out_prim->type = cgltf_primitive_type_triangles;
  2348. int size = tokens[i].size;
  2349. ++i;
  2350. for (int j = 0; j < size; ++j)
  2351. {
  2352. CGLTF_CHECK_KEY(tokens[i]);
  2353. if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
  2354. {
  2355. ++i;
  2356. out_prim->type
  2357. = (cgltf_primitive_type)
  2358. cgltf_json_to_int(tokens+i, json_chunk);
  2359. ++i;
  2360. }
  2361. else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
  2362. {
  2363. ++i;
  2364. out_prim->indices = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
  2365. ++i;
  2366. }
  2367. else if (cgltf_json_strcmp(tokens+i, json_chunk, "material") == 0)
  2368. {
  2369. ++i;
  2370. out_prim->material = CGLTF_PTRINDEX(cgltf_material, cgltf_json_to_int(tokens + i, json_chunk));
  2371. ++i;
  2372. }
  2373. else if (cgltf_json_strcmp(tokens+i, json_chunk, "attributes") == 0)
  2374. {
  2375. i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_prim->attributes, &out_prim->attributes_count);
  2376. }
  2377. else if (cgltf_json_strcmp(tokens+i, json_chunk, "targets") == 0)
  2378. {
  2379. i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_morph_target), (void**)&out_prim->targets, &out_prim->targets_count);
  2380. if (i < 0)
  2381. {
  2382. return i;
  2383. }
  2384. for (cgltf_size k = 0; k < out_prim->targets_count; ++k)
  2385. {
  2386. i = cgltf_parse_json_attribute_list(options, tokens, i, json_chunk, &out_prim->targets[k].attributes, &out_prim->targets[k].attributes_count);
  2387. if (i < 0)
  2388. {
  2389. return i;
  2390. }
  2391. }
  2392. }
  2393. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  2394. {
  2395. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_prim->extras);
  2396. }
  2397. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  2398. {
  2399. ++i;
  2400. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2401. if(out_prim->extensions)
  2402. {
  2403. return CGLTF_ERROR_JSON;
  2404. }
  2405. int extensions_size = tokens[i].size;
  2406. out_prim->extensions_count = 0;
  2407. out_prim->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
  2408. if (!out_prim->extensions)
  2409. {
  2410. return CGLTF_ERROR_NOMEM;
  2411. }
  2412. ++i;
  2413. for (int k = 0; k < extensions_size; ++k)
  2414. {
  2415. CGLTF_CHECK_KEY(tokens[i]);
  2416. if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_draco_mesh_compression") == 0)
  2417. {
  2418. out_prim->has_draco_mesh_compression = 1;
  2419. i = cgltf_parse_json_draco_mesh_compression(options, tokens, i + 1, json_chunk, &out_prim->draco_mesh_compression);
  2420. }
  2421. else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_variants") == 0)
  2422. {
  2423. i = cgltf_parse_json_material_mappings(options, tokens, i + 1, json_chunk, out_prim);
  2424. }
  2425. else
  2426. {
  2427. i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_prim->extensions[out_prim->extensions_count++]));
  2428. }
  2429. if (i < 0)
  2430. {
  2431. return i;
  2432. }
  2433. }
  2434. }
  2435. else
  2436. {
  2437. i = cgltf_skip_json(tokens, i+1);
  2438. }
  2439. if (i < 0)
  2440. {
  2441. return i;
  2442. }
  2443. }
  2444. return i;
  2445. }
  2446. static int cgltf_parse_json_mesh(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_mesh* out_mesh)
  2447. {
  2448. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2449. int size = tokens[i].size;
  2450. ++i;
  2451. for (int j = 0; j < size; ++j)
  2452. {
  2453. CGLTF_CHECK_KEY(tokens[i]);
  2454. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  2455. {
  2456. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_mesh->name);
  2457. }
  2458. else if (cgltf_json_strcmp(tokens+i, json_chunk, "primitives") == 0)
  2459. {
  2460. i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_primitive), (void**)&out_mesh->primitives, &out_mesh->primitives_count);
  2461. if (i < 0)
  2462. {
  2463. return i;
  2464. }
  2465. for (cgltf_size prim_index = 0; prim_index < out_mesh->primitives_count; ++prim_index)
  2466. {
  2467. i = cgltf_parse_json_primitive(options, tokens, i, json_chunk, &out_mesh->primitives[prim_index]);
  2468. if (i < 0)
  2469. {
  2470. return i;
  2471. }
  2472. }
  2473. }
  2474. else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
  2475. {
  2476. i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_mesh->weights, &out_mesh->weights_count);
  2477. if (i < 0)
  2478. {
  2479. return i;
  2480. }
  2481. i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_mesh->weights, (int)out_mesh->weights_count);
  2482. }
  2483. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  2484. {
  2485. ++i;
  2486. out_mesh->extras.start_offset = tokens[i].start;
  2487. out_mesh->extras.end_offset = tokens[i].end;
  2488. if (tokens[i].type == JSMN_OBJECT)
  2489. {
  2490. int extras_size = tokens[i].size;
  2491. ++i;
  2492. for (int k = 0; k < extras_size; ++k)
  2493. {
  2494. CGLTF_CHECK_KEY(tokens[i]);
  2495. if (cgltf_json_strcmp(tokens+i, json_chunk, "targetNames") == 0 && tokens[i+1].type == JSMN_ARRAY)
  2496. {
  2497. i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_mesh->target_names, &out_mesh->target_names_count);
  2498. }
  2499. else
  2500. {
  2501. i = cgltf_skip_json(tokens, i+1);
  2502. }
  2503. if (i < 0)
  2504. {
  2505. return i;
  2506. }
  2507. }
  2508. }
  2509. else
  2510. {
  2511. i = cgltf_skip_json(tokens, i);
  2512. }
  2513. }
  2514. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  2515. {
  2516. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_mesh->extensions_count, &out_mesh->extensions);
  2517. }
  2518. else
  2519. {
  2520. i = cgltf_skip_json(tokens, i+1);
  2521. }
  2522. if (i < 0)
  2523. {
  2524. return i;
  2525. }
  2526. }
  2527. return i;
  2528. }
  2529. static int cgltf_parse_json_meshes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  2530. {
  2531. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_mesh), (void**)&out_data->meshes, &out_data->meshes_count);
  2532. if (i < 0)
  2533. {
  2534. return i;
  2535. }
  2536. for (cgltf_size j = 0; j < out_data->meshes_count; ++j)
  2537. {
  2538. i = cgltf_parse_json_mesh(options, tokens, i, json_chunk, &out_data->meshes[j]);
  2539. if (i < 0)
  2540. {
  2541. return i;
  2542. }
  2543. }
  2544. return i;
  2545. }
  2546. static cgltf_component_type cgltf_json_to_component_type(jsmntok_t const* tok, const uint8_t* json_chunk)
  2547. {
  2548. int type = cgltf_json_to_int(tok, json_chunk);
  2549. switch (type)
  2550. {
  2551. case 5120:
  2552. return cgltf_component_type_r_8;
  2553. case 5121:
  2554. return cgltf_component_type_r_8u;
  2555. case 5122:
  2556. return cgltf_component_type_r_16;
  2557. case 5123:
  2558. return cgltf_component_type_r_16u;
  2559. case 5125:
  2560. return cgltf_component_type_r_32u;
  2561. case 5126:
  2562. return cgltf_component_type_r_32f;
  2563. default:
  2564. return cgltf_component_type_invalid;
  2565. }
  2566. }
  2567. static int cgltf_parse_json_accessor_sparse(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_accessor_sparse* out_sparse)
  2568. {
  2569. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2570. int size = tokens[i].size;
  2571. ++i;
  2572. for (int j = 0; j < size; ++j)
  2573. {
  2574. CGLTF_CHECK_KEY(tokens[i]);
  2575. if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
  2576. {
  2577. ++i;
  2578. out_sparse->count = cgltf_json_to_int(tokens + i, json_chunk);
  2579. ++i;
  2580. }
  2581. else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
  2582. {
  2583. ++i;
  2584. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2585. int indices_size = tokens[i].size;
  2586. ++i;
  2587. for (int k = 0; k < indices_size; ++k)
  2588. {
  2589. CGLTF_CHECK_KEY(tokens[i]);
  2590. if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
  2591. {
  2592. ++i;
  2593. out_sparse->indices_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
  2594. ++i;
  2595. }
  2596. else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
  2597. {
  2598. ++i;
  2599. out_sparse->indices_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
  2600. ++i;
  2601. }
  2602. else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
  2603. {
  2604. ++i;
  2605. out_sparse->indices_component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
  2606. ++i;
  2607. }
  2608. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  2609. {
  2610. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sparse->indices_extras);
  2611. }
  2612. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  2613. {
  2614. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sparse->indices_extensions_count, &out_sparse->indices_extensions);
  2615. }
  2616. else
  2617. {
  2618. i = cgltf_skip_json(tokens, i+1);
  2619. }
  2620. if (i < 0)
  2621. {
  2622. return i;
  2623. }
  2624. }
  2625. }
  2626. else if (cgltf_json_strcmp(tokens+i, json_chunk, "values") == 0)
  2627. {
  2628. ++i;
  2629. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2630. int values_size = tokens[i].size;
  2631. ++i;
  2632. for (int k = 0; k < values_size; ++k)
  2633. {
  2634. CGLTF_CHECK_KEY(tokens[i]);
  2635. if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
  2636. {
  2637. ++i;
  2638. out_sparse->values_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
  2639. ++i;
  2640. }
  2641. else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
  2642. {
  2643. ++i;
  2644. out_sparse->values_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
  2645. ++i;
  2646. }
  2647. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  2648. {
  2649. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sparse->values_extras);
  2650. }
  2651. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  2652. {
  2653. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sparse->values_extensions_count, &out_sparse->values_extensions);
  2654. }
  2655. else
  2656. {
  2657. i = cgltf_skip_json(tokens, i+1);
  2658. }
  2659. if (i < 0)
  2660. {
  2661. return i;
  2662. }
  2663. }
  2664. }
  2665. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  2666. {
  2667. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sparse->extras);
  2668. }
  2669. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  2670. {
  2671. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sparse->extensions_count, &out_sparse->extensions);
  2672. }
  2673. else
  2674. {
  2675. i = cgltf_skip_json(tokens, i+1);
  2676. }
  2677. if (i < 0)
  2678. {
  2679. return i;
  2680. }
  2681. }
  2682. return i;
  2683. }
  2684. static int cgltf_parse_json_accessor(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_accessor* out_accessor)
  2685. {
  2686. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2687. int size = tokens[i].size;
  2688. ++i;
  2689. for (int j = 0; j < size; ++j)
  2690. {
  2691. CGLTF_CHECK_KEY(tokens[i]);
  2692. if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
  2693. {
  2694. ++i;
  2695. out_accessor->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
  2696. ++i;
  2697. }
  2698. else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
  2699. {
  2700. ++i;
  2701. out_accessor->offset =
  2702. cgltf_json_to_int(tokens+i, json_chunk);
  2703. ++i;
  2704. }
  2705. else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
  2706. {
  2707. ++i;
  2708. out_accessor->component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
  2709. ++i;
  2710. }
  2711. else if (cgltf_json_strcmp(tokens+i, json_chunk, "normalized") == 0)
  2712. {
  2713. ++i;
  2714. out_accessor->normalized = cgltf_json_to_bool(tokens+i, json_chunk);
  2715. ++i;
  2716. }
  2717. else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
  2718. {
  2719. ++i;
  2720. out_accessor->count =
  2721. cgltf_json_to_int(tokens+i, json_chunk);
  2722. ++i;
  2723. }
  2724. else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
  2725. {
  2726. ++i;
  2727. if (cgltf_json_strcmp(tokens+i, json_chunk, "SCALAR") == 0)
  2728. {
  2729. out_accessor->type = cgltf_type_scalar;
  2730. }
  2731. else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC2") == 0)
  2732. {
  2733. out_accessor->type = cgltf_type_vec2;
  2734. }
  2735. else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC3") == 0)
  2736. {
  2737. out_accessor->type = cgltf_type_vec3;
  2738. }
  2739. else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC4") == 0)
  2740. {
  2741. out_accessor->type = cgltf_type_vec4;
  2742. }
  2743. else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT2") == 0)
  2744. {
  2745. out_accessor->type = cgltf_type_mat2;
  2746. }
  2747. else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT3") == 0)
  2748. {
  2749. out_accessor->type = cgltf_type_mat3;
  2750. }
  2751. else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT4") == 0)
  2752. {
  2753. out_accessor->type = cgltf_type_mat4;
  2754. }
  2755. ++i;
  2756. }
  2757. else if (cgltf_json_strcmp(tokens + i, json_chunk, "min") == 0)
  2758. {
  2759. ++i;
  2760. out_accessor->has_min = 1;
  2761. // note: we can't parse the precise number of elements since type may not have been computed yet
  2762. int min_size = tokens[i].size > 16 ? 16 : tokens[i].size;
  2763. i = cgltf_parse_json_float_array(tokens, i, json_chunk, out_accessor->min, min_size);
  2764. }
  2765. else if (cgltf_json_strcmp(tokens + i, json_chunk, "max") == 0)
  2766. {
  2767. ++i;
  2768. out_accessor->has_max = 1;
  2769. // note: we can't parse the precise number of elements since type may not have been computed yet
  2770. int max_size = tokens[i].size > 16 ? 16 : tokens[i].size;
  2771. i = cgltf_parse_json_float_array(tokens, i, json_chunk, out_accessor->max, max_size);
  2772. }
  2773. else if (cgltf_json_strcmp(tokens + i, json_chunk, "sparse") == 0)
  2774. {
  2775. out_accessor->is_sparse = 1;
  2776. i = cgltf_parse_json_accessor_sparse(options, tokens, i + 1, json_chunk, &out_accessor->sparse);
  2777. }
  2778. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  2779. {
  2780. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_accessor->extras);
  2781. }
  2782. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  2783. {
  2784. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_accessor->extensions_count, &out_accessor->extensions);
  2785. }
  2786. else
  2787. {
  2788. i = cgltf_skip_json(tokens, i+1);
  2789. }
  2790. if (i < 0)
  2791. {
  2792. return i;
  2793. }
  2794. }
  2795. return i;
  2796. }
  2797. static int cgltf_parse_json_texture_transform(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture_transform* out_texture_transform)
  2798. {
  2799. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2800. int size = tokens[i].size;
  2801. ++i;
  2802. for (int j = 0; j < size; ++j)
  2803. {
  2804. CGLTF_CHECK_KEY(tokens[i]);
  2805. if (cgltf_json_strcmp(tokens + i, json_chunk, "offset") == 0)
  2806. {
  2807. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->offset, 2);
  2808. }
  2809. else if (cgltf_json_strcmp(tokens + i, json_chunk, "rotation") == 0)
  2810. {
  2811. ++i;
  2812. out_texture_transform->rotation = cgltf_json_to_float(tokens + i, json_chunk);
  2813. ++i;
  2814. }
  2815. else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
  2816. {
  2817. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->scale, 2);
  2818. }
  2819. else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
  2820. {
  2821. ++i;
  2822. out_texture_transform->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
  2823. ++i;
  2824. }
  2825. else
  2826. {
  2827. i = cgltf_skip_json(tokens, i + 1);
  2828. }
  2829. if (i < 0)
  2830. {
  2831. return i;
  2832. }
  2833. }
  2834. return i;
  2835. }
  2836. static int cgltf_parse_json_texture_view(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture_view* out_texture_view)
  2837. {
  2838. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2839. out_texture_view->scale = 1.0f;
  2840. cgltf_fill_float_array(out_texture_view->transform.scale, 2, 1.0f);
  2841. int size = tokens[i].size;
  2842. ++i;
  2843. for (int j = 0; j < size; ++j)
  2844. {
  2845. CGLTF_CHECK_KEY(tokens[i]);
  2846. if (cgltf_json_strcmp(tokens + i, json_chunk, "index") == 0)
  2847. {
  2848. ++i;
  2849. out_texture_view->texture = CGLTF_PTRINDEX(cgltf_texture, cgltf_json_to_int(tokens + i, json_chunk));
  2850. ++i;
  2851. }
  2852. else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
  2853. {
  2854. ++i;
  2855. out_texture_view->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
  2856. ++i;
  2857. }
  2858. else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
  2859. {
  2860. ++i;
  2861. out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
  2862. ++i;
  2863. }
  2864. else if (cgltf_json_strcmp(tokens + i, json_chunk, "strength") == 0)
  2865. {
  2866. ++i;
  2867. out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
  2868. ++i;
  2869. }
  2870. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  2871. {
  2872. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_texture_view->extras);
  2873. }
  2874. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  2875. {
  2876. ++i;
  2877. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2878. if(out_texture_view->extensions)
  2879. {
  2880. return CGLTF_ERROR_JSON;
  2881. }
  2882. int extensions_size = tokens[i].size;
  2883. out_texture_view->extensions_count = 0;
  2884. out_texture_view->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
  2885. if (!out_texture_view->extensions)
  2886. {
  2887. return CGLTF_ERROR_NOMEM;
  2888. }
  2889. ++i;
  2890. for (int k = 0; k < extensions_size; ++k)
  2891. {
  2892. CGLTF_CHECK_KEY(tokens[i]);
  2893. if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_texture_transform") == 0)
  2894. {
  2895. out_texture_view->has_transform = 1;
  2896. i = cgltf_parse_json_texture_transform(tokens, i + 1, json_chunk, &out_texture_view->transform);
  2897. }
  2898. else
  2899. {
  2900. i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_texture_view->extensions[out_texture_view->extensions_count++]));
  2901. }
  2902. if (i < 0)
  2903. {
  2904. return i;
  2905. }
  2906. }
  2907. }
  2908. else
  2909. {
  2910. i = cgltf_skip_json(tokens, i + 1);
  2911. }
  2912. if (i < 0)
  2913. {
  2914. return i;
  2915. }
  2916. }
  2917. return i;
  2918. }
  2919. static int cgltf_parse_json_pbr_metallic_roughness(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_pbr_metallic_roughness* out_pbr)
  2920. {
  2921. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2922. int size = tokens[i].size;
  2923. ++i;
  2924. for (int j = 0; j < size; ++j)
  2925. {
  2926. CGLTF_CHECK_KEY(tokens[i]);
  2927. if (cgltf_json_strcmp(tokens+i, json_chunk, "metallicFactor") == 0)
  2928. {
  2929. ++i;
  2930. out_pbr->metallic_factor =
  2931. cgltf_json_to_float(tokens + i, json_chunk);
  2932. ++i;
  2933. }
  2934. else if (cgltf_json_strcmp(tokens+i, json_chunk, "roughnessFactor") == 0)
  2935. {
  2936. ++i;
  2937. out_pbr->roughness_factor =
  2938. cgltf_json_to_float(tokens+i, json_chunk);
  2939. ++i;
  2940. }
  2941. else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorFactor") == 0)
  2942. {
  2943. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->base_color_factor, 4);
  2944. }
  2945. else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorTexture") == 0)
  2946. {
  2947. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
  2948. &out_pbr->base_color_texture);
  2949. }
  2950. else if (cgltf_json_strcmp(tokens + i, json_chunk, "metallicRoughnessTexture") == 0)
  2951. {
  2952. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
  2953. &out_pbr->metallic_roughness_texture);
  2954. }
  2955. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  2956. {
  2957. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_pbr->extras);
  2958. }
  2959. else
  2960. {
  2961. i = cgltf_skip_json(tokens, i+1);
  2962. }
  2963. if (i < 0)
  2964. {
  2965. return i;
  2966. }
  2967. }
  2968. return i;
  2969. }
  2970. static int cgltf_parse_json_pbr_specular_glossiness(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_pbr_specular_glossiness* out_pbr)
  2971. {
  2972. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  2973. int size = tokens[i].size;
  2974. ++i;
  2975. for (int j = 0; j < size; ++j)
  2976. {
  2977. CGLTF_CHECK_KEY(tokens[i]);
  2978. if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseFactor") == 0)
  2979. {
  2980. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->diffuse_factor, 4);
  2981. }
  2982. else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
  2983. {
  2984. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->specular_factor, 3);
  2985. }
  2986. else if (cgltf_json_strcmp(tokens+i, json_chunk, "glossinessFactor") == 0)
  2987. {
  2988. ++i;
  2989. out_pbr->glossiness_factor = cgltf_json_to_float(tokens + i, json_chunk);
  2990. ++i;
  2991. }
  2992. else if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseTexture") == 0)
  2993. {
  2994. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->diffuse_texture);
  2995. }
  2996. else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularGlossinessTexture") == 0)
  2997. {
  2998. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->specular_glossiness_texture);
  2999. }
  3000. else
  3001. {
  3002. i = cgltf_skip_json(tokens, i+1);
  3003. }
  3004. if (i < 0)
  3005. {
  3006. return i;
  3007. }
  3008. }
  3009. return i;
  3010. }
  3011. static int cgltf_parse_json_clearcoat(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_clearcoat* out_clearcoat)
  3012. {
  3013. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3014. int size = tokens[i].size;
  3015. ++i;
  3016. for (int j = 0; j < size; ++j)
  3017. {
  3018. CGLTF_CHECK_KEY(tokens[i]);
  3019. if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatFactor") == 0)
  3020. {
  3021. ++i;
  3022. out_clearcoat->clearcoat_factor = cgltf_json_to_float(tokens + i, json_chunk);
  3023. ++i;
  3024. }
  3025. else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatRoughnessFactor") == 0)
  3026. {
  3027. ++i;
  3028. out_clearcoat->clearcoat_roughness_factor = cgltf_json_to_float(tokens + i, json_chunk);
  3029. ++i;
  3030. }
  3031. else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatTexture") == 0)
  3032. {
  3033. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_texture);
  3034. }
  3035. else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatRoughnessTexture") == 0)
  3036. {
  3037. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_roughness_texture);
  3038. }
  3039. else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatNormalTexture") == 0)
  3040. {
  3041. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_normal_texture);
  3042. }
  3043. else
  3044. {
  3045. i = cgltf_skip_json(tokens, i+1);
  3046. }
  3047. if (i < 0)
  3048. {
  3049. return i;
  3050. }
  3051. }
  3052. return i;
  3053. }
  3054. static int cgltf_parse_json_ior(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_ior* out_ior)
  3055. {
  3056. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3057. int size = tokens[i].size;
  3058. ++i;
  3059. // Default values
  3060. out_ior->ior = 1.5f;
  3061. for (int j = 0; j < size; ++j)
  3062. {
  3063. CGLTF_CHECK_KEY(tokens[i]);
  3064. if (cgltf_json_strcmp(tokens+i, json_chunk, "ior") == 0)
  3065. {
  3066. ++i;
  3067. out_ior->ior = cgltf_json_to_float(tokens + i, json_chunk);
  3068. ++i;
  3069. }
  3070. else
  3071. {
  3072. i = cgltf_skip_json(tokens, i+1);
  3073. }
  3074. if (i < 0)
  3075. {
  3076. return i;
  3077. }
  3078. }
  3079. return i;
  3080. }
  3081. static int cgltf_parse_json_specular(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_specular* out_specular)
  3082. {
  3083. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3084. int size = tokens[i].size;
  3085. ++i;
  3086. // Default values
  3087. out_specular->specular_factor = 1.0f;
  3088. cgltf_fill_float_array(out_specular->specular_color_factor, 3, 1.0f);
  3089. for (int j = 0; j < size; ++j)
  3090. {
  3091. CGLTF_CHECK_KEY(tokens[i]);
  3092. if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
  3093. {
  3094. ++i;
  3095. out_specular->specular_factor = cgltf_json_to_float(tokens + i, json_chunk);
  3096. ++i;
  3097. }
  3098. else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularColorFactor") == 0)
  3099. {
  3100. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_specular->specular_color_factor, 3);
  3101. }
  3102. else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularTexture") == 0)
  3103. {
  3104. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_specular->specular_texture);
  3105. }
  3106. else if (cgltf_json_strcmp(tokens + i, json_chunk, "specularColorTexture") == 0)
  3107. {
  3108. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_specular->specular_color_texture);
  3109. }
  3110. else
  3111. {
  3112. i = cgltf_skip_json(tokens, i+1);
  3113. }
  3114. if (i < 0)
  3115. {
  3116. return i;
  3117. }
  3118. }
  3119. return i;
  3120. }
  3121. static int cgltf_parse_json_transmission(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_transmission* out_transmission)
  3122. {
  3123. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3124. int size = tokens[i].size;
  3125. ++i;
  3126. for (int j = 0; j < size; ++j)
  3127. {
  3128. CGLTF_CHECK_KEY(tokens[i]);
  3129. if (cgltf_json_strcmp(tokens+i, json_chunk, "transmissionFactor") == 0)
  3130. {
  3131. ++i;
  3132. out_transmission->transmission_factor = cgltf_json_to_float(tokens + i, json_chunk);
  3133. ++i;
  3134. }
  3135. else if (cgltf_json_strcmp(tokens+i, json_chunk, "transmissionTexture") == 0)
  3136. {
  3137. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_transmission->transmission_texture);
  3138. }
  3139. else
  3140. {
  3141. i = cgltf_skip_json(tokens, i+1);
  3142. }
  3143. if (i < 0)
  3144. {
  3145. return i;
  3146. }
  3147. }
  3148. return i;
  3149. }
  3150. static int cgltf_parse_json_volume(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_volume* out_volume)
  3151. {
  3152. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3153. int size = tokens[i].size;
  3154. ++i;
  3155. for (int j = 0; j < size; ++j)
  3156. {
  3157. CGLTF_CHECK_KEY(tokens[i]);
  3158. if (cgltf_json_strcmp(tokens + i, json_chunk, "thicknessFactor") == 0)
  3159. {
  3160. ++i;
  3161. out_volume->thickness_factor = cgltf_json_to_float(tokens + i, json_chunk);
  3162. ++i;
  3163. }
  3164. else if (cgltf_json_strcmp(tokens + i, json_chunk, "thicknessTexture") == 0)
  3165. {
  3166. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_volume->thickness_texture);
  3167. }
  3168. else if (cgltf_json_strcmp(tokens + i, json_chunk, "attenuationColor") == 0)
  3169. {
  3170. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_volume->attenuation_color, 3);
  3171. }
  3172. else if (cgltf_json_strcmp(tokens + i, json_chunk, "attenuationDistance") == 0)
  3173. {
  3174. ++i;
  3175. out_volume->attenuation_distance = cgltf_json_to_float(tokens + i, json_chunk);
  3176. ++i;
  3177. }
  3178. else
  3179. {
  3180. i = cgltf_skip_json(tokens, i + 1);
  3181. }
  3182. if (i < 0)
  3183. {
  3184. return i;
  3185. }
  3186. }
  3187. return i;
  3188. }
  3189. static int cgltf_parse_json_sheen(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_sheen* out_sheen)
  3190. {
  3191. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3192. int size = tokens[i].size;
  3193. ++i;
  3194. for (int j = 0; j < size; ++j)
  3195. {
  3196. CGLTF_CHECK_KEY(tokens[i]);
  3197. if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorFactor") == 0)
  3198. {
  3199. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_sheen->sheen_color_factor, 3);
  3200. }
  3201. else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorTexture") == 0)
  3202. {
  3203. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_color_texture);
  3204. }
  3205. else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessFactor") == 0)
  3206. {
  3207. ++i;
  3208. out_sheen->sheen_roughness_factor = cgltf_json_to_float(tokens + i, json_chunk);
  3209. ++i;
  3210. }
  3211. else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessTexture") == 0)
  3212. {
  3213. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_roughness_texture);
  3214. }
  3215. else
  3216. {
  3217. i = cgltf_skip_json(tokens, i+1);
  3218. }
  3219. if (i < 0)
  3220. {
  3221. return i;
  3222. }
  3223. }
  3224. return i;
  3225. }
  3226. static int cgltf_parse_json_image(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_image* out_image)
  3227. {
  3228. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3229. int size = tokens[i].size;
  3230. ++i;
  3231. for (int j = 0; j < size; ++j)
  3232. {
  3233. CGLTF_CHECK_KEY(tokens[i]);
  3234. if (cgltf_json_strcmp(tokens + i, json_chunk, "uri") == 0)
  3235. {
  3236. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->uri);
  3237. }
  3238. else if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
  3239. {
  3240. ++i;
  3241. out_image->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
  3242. ++i;
  3243. }
  3244. else if (cgltf_json_strcmp(tokens + i, json_chunk, "mimeType") == 0)
  3245. {
  3246. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->mime_type);
  3247. }
  3248. else if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
  3249. {
  3250. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->name);
  3251. }
  3252. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  3253. {
  3254. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_image->extras);
  3255. }
  3256. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  3257. {
  3258. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_image->extensions_count, &out_image->extensions);
  3259. }
  3260. else
  3261. {
  3262. i = cgltf_skip_json(tokens, i + 1);
  3263. }
  3264. if (i < 0)
  3265. {
  3266. return i;
  3267. }
  3268. }
  3269. return i;
  3270. }
  3271. static int cgltf_parse_json_sampler(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_sampler* out_sampler)
  3272. {
  3273. (void)options;
  3274. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3275. out_sampler->wrap_s = 10497;
  3276. out_sampler->wrap_t = 10497;
  3277. int size = tokens[i].size;
  3278. ++i;
  3279. for (int j = 0; j < size; ++j)
  3280. {
  3281. CGLTF_CHECK_KEY(tokens[i]);
  3282. if (cgltf_json_strcmp(tokens + i, json_chunk, "magFilter") == 0)
  3283. {
  3284. ++i;
  3285. out_sampler->mag_filter
  3286. = cgltf_json_to_int(tokens + i, json_chunk);
  3287. ++i;
  3288. }
  3289. else if (cgltf_json_strcmp(tokens + i, json_chunk, "minFilter") == 0)
  3290. {
  3291. ++i;
  3292. out_sampler->min_filter
  3293. = cgltf_json_to_int(tokens + i, json_chunk);
  3294. ++i;
  3295. }
  3296. else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapS") == 0)
  3297. {
  3298. ++i;
  3299. out_sampler->wrap_s
  3300. = cgltf_json_to_int(tokens + i, json_chunk);
  3301. ++i;
  3302. }
  3303. else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapT") == 0)
  3304. {
  3305. ++i;
  3306. out_sampler->wrap_t
  3307. = cgltf_json_to_int(tokens + i, json_chunk);
  3308. ++i;
  3309. }
  3310. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  3311. {
  3312. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sampler->extras);
  3313. }
  3314. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  3315. {
  3316. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sampler->extensions_count, &out_sampler->extensions);
  3317. }
  3318. else
  3319. {
  3320. i = cgltf_skip_json(tokens, i + 1);
  3321. }
  3322. if (i < 0)
  3323. {
  3324. return i;
  3325. }
  3326. }
  3327. return i;
  3328. }
  3329. static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture* out_texture)
  3330. {
  3331. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3332. int size = tokens[i].size;
  3333. ++i;
  3334. for (int j = 0; j < size; ++j)
  3335. {
  3336. CGLTF_CHECK_KEY(tokens[i]);
  3337. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  3338. {
  3339. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_texture->name);
  3340. }
  3341. else if (cgltf_json_strcmp(tokens + i, json_chunk, "sampler") == 0)
  3342. {
  3343. ++i;
  3344. out_texture->sampler = CGLTF_PTRINDEX(cgltf_sampler, cgltf_json_to_int(tokens + i, json_chunk));
  3345. ++i;
  3346. }
  3347. else if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0)
  3348. {
  3349. ++i;
  3350. out_texture->image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk));
  3351. ++i;
  3352. }
  3353. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  3354. {
  3355. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_texture->extras);
  3356. }
  3357. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  3358. {
  3359. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_texture->extensions_count, &out_texture->extensions);
  3360. }
  3361. else
  3362. {
  3363. i = cgltf_skip_json(tokens, i + 1);
  3364. }
  3365. if (i < 0)
  3366. {
  3367. return i;
  3368. }
  3369. }
  3370. return i;
  3371. }
  3372. static int cgltf_parse_json_material(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_material* out_material)
  3373. {
  3374. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3375. cgltf_fill_float_array(out_material->pbr_metallic_roughness.base_color_factor, 4, 1.0f);
  3376. out_material->pbr_metallic_roughness.metallic_factor = 1.0f;
  3377. out_material->pbr_metallic_roughness.roughness_factor = 1.0f;
  3378. cgltf_fill_float_array(out_material->pbr_specular_glossiness.diffuse_factor, 4, 1.0f);
  3379. cgltf_fill_float_array(out_material->pbr_specular_glossiness.specular_factor, 3, 1.0f);
  3380. out_material->pbr_specular_glossiness.glossiness_factor = 1.0f;
  3381. cgltf_fill_float_array(out_material->volume.attenuation_color, 3, 1.0f);
  3382. out_material->volume.attenuation_distance = FLT_MAX;
  3383. out_material->alpha_cutoff = 0.5f;
  3384. int size = tokens[i].size;
  3385. ++i;
  3386. for (int j = 0; j < size; ++j)
  3387. {
  3388. CGLTF_CHECK_KEY(tokens[i]);
  3389. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  3390. {
  3391. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_material->name);
  3392. }
  3393. else if (cgltf_json_strcmp(tokens+i, json_chunk, "pbrMetallicRoughness") == 0)
  3394. {
  3395. out_material->has_pbr_metallic_roughness = 1;
  3396. i = cgltf_parse_json_pbr_metallic_roughness(options, tokens, i + 1, json_chunk, &out_material->pbr_metallic_roughness);
  3397. }
  3398. else if (cgltf_json_strcmp(tokens+i, json_chunk, "emissiveFactor") == 0)
  3399. {
  3400. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_material->emissive_factor, 3);
  3401. }
  3402. else if (cgltf_json_strcmp(tokens + i, json_chunk, "normalTexture") == 0)
  3403. {
  3404. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
  3405. &out_material->normal_texture);
  3406. }
  3407. else if (cgltf_json_strcmp(tokens + i, json_chunk, "occlusionTexture") == 0)
  3408. {
  3409. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
  3410. &out_material->occlusion_texture);
  3411. }
  3412. else if (cgltf_json_strcmp(tokens + i, json_chunk, "emissiveTexture") == 0)
  3413. {
  3414. i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
  3415. &out_material->emissive_texture);
  3416. }
  3417. else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaMode") == 0)
  3418. {
  3419. ++i;
  3420. if (cgltf_json_strcmp(tokens + i, json_chunk, "OPAQUE") == 0)
  3421. {
  3422. out_material->alpha_mode = cgltf_alpha_mode_opaque;
  3423. }
  3424. else if (cgltf_json_strcmp(tokens + i, json_chunk, "MASK") == 0)
  3425. {
  3426. out_material->alpha_mode = cgltf_alpha_mode_mask;
  3427. }
  3428. else if (cgltf_json_strcmp(tokens + i, json_chunk, "BLEND") == 0)
  3429. {
  3430. out_material->alpha_mode = cgltf_alpha_mode_blend;
  3431. }
  3432. ++i;
  3433. }
  3434. else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaCutoff") == 0)
  3435. {
  3436. ++i;
  3437. out_material->alpha_cutoff = cgltf_json_to_float(tokens + i, json_chunk);
  3438. ++i;
  3439. }
  3440. else if (cgltf_json_strcmp(tokens + i, json_chunk, "doubleSided") == 0)
  3441. {
  3442. ++i;
  3443. out_material->double_sided =
  3444. cgltf_json_to_bool(tokens + i, json_chunk);
  3445. ++i;
  3446. }
  3447. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  3448. {
  3449. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_material->extras);
  3450. }
  3451. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  3452. {
  3453. ++i;
  3454. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3455. if(out_material->extensions)
  3456. {
  3457. return CGLTF_ERROR_JSON;
  3458. }
  3459. int extensions_size = tokens[i].size;
  3460. ++i;
  3461. out_material->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
  3462. out_material->extensions_count= 0;
  3463. if (!out_material->extensions)
  3464. {
  3465. return CGLTF_ERROR_NOMEM;
  3466. }
  3467. for (int k = 0; k < extensions_size; ++k)
  3468. {
  3469. CGLTF_CHECK_KEY(tokens[i]);
  3470. if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_pbrSpecularGlossiness") == 0)
  3471. {
  3472. out_material->has_pbr_specular_glossiness = 1;
  3473. i = cgltf_parse_json_pbr_specular_glossiness(options, tokens, i + 1, json_chunk, &out_material->pbr_specular_glossiness);
  3474. }
  3475. else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_unlit") == 0)
  3476. {
  3477. out_material->unlit = 1;
  3478. i = cgltf_skip_json(tokens, i+1);
  3479. }
  3480. else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_clearcoat") == 0)
  3481. {
  3482. out_material->has_clearcoat = 1;
  3483. i = cgltf_parse_json_clearcoat(options, tokens, i + 1, json_chunk, &out_material->clearcoat);
  3484. }
  3485. else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_ior") == 0)
  3486. {
  3487. out_material->has_ior = 1;
  3488. i = cgltf_parse_json_ior(tokens, i + 1, json_chunk, &out_material->ior);
  3489. }
  3490. else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_specular") == 0)
  3491. {
  3492. out_material->has_specular = 1;
  3493. i = cgltf_parse_json_specular(options, tokens, i + 1, json_chunk, &out_material->specular);
  3494. }
  3495. else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_transmission") == 0)
  3496. {
  3497. out_material->has_transmission = 1;
  3498. i = cgltf_parse_json_transmission(options, tokens, i + 1, json_chunk, &out_material->transmission);
  3499. }
  3500. else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_volume") == 0)
  3501. {
  3502. out_material->has_volume = 1;
  3503. i = cgltf_parse_json_volume(options, tokens, i + 1, json_chunk, &out_material->volume);
  3504. }
  3505. else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_sheen") == 0)
  3506. {
  3507. out_material->has_sheen = 1;
  3508. i = cgltf_parse_json_sheen(options, tokens, i + 1, json_chunk, &out_material->sheen);
  3509. }
  3510. else
  3511. {
  3512. i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_material->extensions[out_material->extensions_count++]));
  3513. }
  3514. if (i < 0)
  3515. {
  3516. return i;
  3517. }
  3518. }
  3519. }
  3520. else
  3521. {
  3522. i = cgltf_skip_json(tokens, i+1);
  3523. }
  3524. if (i < 0)
  3525. {
  3526. return i;
  3527. }
  3528. }
  3529. return i;
  3530. }
  3531. static int cgltf_parse_json_accessors(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  3532. {
  3533. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_accessor), (void**)&out_data->accessors, &out_data->accessors_count);
  3534. if (i < 0)
  3535. {
  3536. return i;
  3537. }
  3538. for (cgltf_size j = 0; j < out_data->accessors_count; ++j)
  3539. {
  3540. i = cgltf_parse_json_accessor(options, tokens, i, json_chunk, &out_data->accessors[j]);
  3541. if (i < 0)
  3542. {
  3543. return i;
  3544. }
  3545. }
  3546. return i;
  3547. }
  3548. static int cgltf_parse_json_materials(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  3549. {
  3550. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_material), (void**)&out_data->materials, &out_data->materials_count);
  3551. if (i < 0)
  3552. {
  3553. return i;
  3554. }
  3555. for (cgltf_size j = 0; j < out_data->materials_count; ++j)
  3556. {
  3557. i = cgltf_parse_json_material(options, tokens, i, json_chunk, &out_data->materials[j]);
  3558. if (i < 0)
  3559. {
  3560. return i;
  3561. }
  3562. }
  3563. return i;
  3564. }
  3565. static int cgltf_parse_json_images(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  3566. {
  3567. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_image), (void**)&out_data->images, &out_data->images_count);
  3568. if (i < 0)
  3569. {
  3570. return i;
  3571. }
  3572. for (cgltf_size j = 0; j < out_data->images_count; ++j)
  3573. {
  3574. i = cgltf_parse_json_image(options, tokens, i, json_chunk, &out_data->images[j]);
  3575. if (i < 0)
  3576. {
  3577. return i;
  3578. }
  3579. }
  3580. return i;
  3581. }
  3582. static int cgltf_parse_json_textures(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  3583. {
  3584. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_texture), (void**)&out_data->textures, &out_data->textures_count);
  3585. if (i < 0)
  3586. {
  3587. return i;
  3588. }
  3589. for (cgltf_size j = 0; j < out_data->textures_count; ++j)
  3590. {
  3591. i = cgltf_parse_json_texture(options, tokens, i, json_chunk, &out_data->textures[j]);
  3592. if (i < 0)
  3593. {
  3594. return i;
  3595. }
  3596. }
  3597. return i;
  3598. }
  3599. static int cgltf_parse_json_samplers(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  3600. {
  3601. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_sampler), (void**)&out_data->samplers, &out_data->samplers_count);
  3602. if (i < 0)
  3603. {
  3604. return i;
  3605. }
  3606. for (cgltf_size j = 0; j < out_data->samplers_count; ++j)
  3607. {
  3608. i = cgltf_parse_json_sampler(options, tokens, i, json_chunk, &out_data->samplers[j]);
  3609. if (i < 0)
  3610. {
  3611. return i;
  3612. }
  3613. }
  3614. return i;
  3615. }
  3616. static int cgltf_parse_json_meshopt_compression(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_meshopt_compression* out_meshopt_compression)
  3617. {
  3618. (void)options;
  3619. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3620. int size = tokens[i].size;
  3621. ++i;
  3622. for (int j = 0; j < size; ++j)
  3623. {
  3624. CGLTF_CHECK_KEY(tokens[i]);
  3625. if (cgltf_json_strcmp(tokens+i, json_chunk, "buffer") == 0)
  3626. {
  3627. ++i;
  3628. out_meshopt_compression->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
  3629. ++i;
  3630. }
  3631. else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
  3632. {
  3633. ++i;
  3634. out_meshopt_compression->offset = cgltf_json_to_int(tokens+i, json_chunk);
  3635. ++i;
  3636. }
  3637. else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
  3638. {
  3639. ++i;
  3640. out_meshopt_compression->size = cgltf_json_to_int(tokens+i, json_chunk);
  3641. ++i;
  3642. }
  3643. else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
  3644. {
  3645. ++i;
  3646. out_meshopt_compression->stride = cgltf_json_to_int(tokens+i, json_chunk);
  3647. ++i;
  3648. }
  3649. else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
  3650. {
  3651. ++i;
  3652. out_meshopt_compression->count = cgltf_json_to_int(tokens+i, json_chunk);
  3653. ++i;
  3654. }
  3655. else if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
  3656. {
  3657. ++i;
  3658. if (cgltf_json_strcmp(tokens+i, json_chunk, "ATTRIBUTES") == 0)
  3659. {
  3660. out_meshopt_compression->mode = cgltf_meshopt_compression_mode_attributes;
  3661. }
  3662. else if (cgltf_json_strcmp(tokens+i, json_chunk, "TRIANGLES") == 0)
  3663. {
  3664. out_meshopt_compression->mode = cgltf_meshopt_compression_mode_triangles;
  3665. }
  3666. else if (cgltf_json_strcmp(tokens+i, json_chunk, "INDICES") == 0)
  3667. {
  3668. out_meshopt_compression->mode = cgltf_meshopt_compression_mode_indices;
  3669. }
  3670. ++i;
  3671. }
  3672. else if (cgltf_json_strcmp(tokens+i, json_chunk, "filter") == 0)
  3673. {
  3674. ++i;
  3675. if (cgltf_json_strcmp(tokens+i, json_chunk, "NONE") == 0)
  3676. {
  3677. out_meshopt_compression->filter = cgltf_meshopt_compression_filter_none;
  3678. }
  3679. else if (cgltf_json_strcmp(tokens+i, json_chunk, "OCTAHEDRAL") == 0)
  3680. {
  3681. out_meshopt_compression->filter = cgltf_meshopt_compression_filter_octahedral;
  3682. }
  3683. else if (cgltf_json_strcmp(tokens+i, json_chunk, "QUATERNION") == 0)
  3684. {
  3685. out_meshopt_compression->filter = cgltf_meshopt_compression_filter_quaternion;
  3686. }
  3687. else if (cgltf_json_strcmp(tokens+i, json_chunk, "EXPONENTIAL") == 0)
  3688. {
  3689. out_meshopt_compression->filter = cgltf_meshopt_compression_filter_exponential;
  3690. }
  3691. ++i;
  3692. }
  3693. else
  3694. {
  3695. i = cgltf_skip_json(tokens, i+1);
  3696. }
  3697. if (i < 0)
  3698. {
  3699. return i;
  3700. }
  3701. }
  3702. return i;
  3703. }
  3704. static int cgltf_parse_json_buffer_view(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_buffer_view* out_buffer_view)
  3705. {
  3706. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3707. int size = tokens[i].size;
  3708. ++i;
  3709. for (int j = 0; j < size; ++j)
  3710. {
  3711. CGLTF_CHECK_KEY(tokens[i]);
  3712. if (cgltf_json_strcmp(tokens+i, json_chunk, "buffer") == 0)
  3713. {
  3714. ++i;
  3715. out_buffer_view->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
  3716. ++i;
  3717. }
  3718. else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
  3719. {
  3720. ++i;
  3721. out_buffer_view->offset =
  3722. cgltf_json_to_int(tokens+i, json_chunk);
  3723. ++i;
  3724. }
  3725. else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
  3726. {
  3727. ++i;
  3728. out_buffer_view->size =
  3729. cgltf_json_to_int(tokens+i, json_chunk);
  3730. ++i;
  3731. }
  3732. else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
  3733. {
  3734. ++i;
  3735. out_buffer_view->stride =
  3736. cgltf_json_to_int(tokens+i, json_chunk);
  3737. ++i;
  3738. }
  3739. else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
  3740. {
  3741. ++i;
  3742. int type = cgltf_json_to_int(tokens+i, json_chunk);
  3743. switch (type)
  3744. {
  3745. case 34962:
  3746. type = cgltf_buffer_view_type_vertices;
  3747. break;
  3748. case 34963:
  3749. type = cgltf_buffer_view_type_indices;
  3750. break;
  3751. default:
  3752. type = cgltf_buffer_view_type_invalid;
  3753. break;
  3754. }
  3755. out_buffer_view->type = (cgltf_buffer_view_type)type;
  3756. ++i;
  3757. }
  3758. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  3759. {
  3760. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_buffer_view->extras);
  3761. }
  3762. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  3763. {
  3764. ++i;
  3765. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3766. if(out_buffer_view->extensions)
  3767. {
  3768. return CGLTF_ERROR_JSON;
  3769. }
  3770. int extensions_size = tokens[i].size;
  3771. out_buffer_view->extensions_count = 0;
  3772. out_buffer_view->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
  3773. if (!out_buffer_view->extensions)
  3774. {
  3775. return CGLTF_ERROR_NOMEM;
  3776. }
  3777. ++i;
  3778. for (int k = 0; k < extensions_size; ++k)
  3779. {
  3780. CGLTF_CHECK_KEY(tokens[i]);
  3781. if (cgltf_json_strcmp(tokens+i, json_chunk, "EXT_meshopt_compression") == 0)
  3782. {
  3783. out_buffer_view->has_meshopt_compression = 1;
  3784. i = cgltf_parse_json_meshopt_compression(options, tokens, i + 1, json_chunk, &out_buffer_view->meshopt_compression);
  3785. }
  3786. else
  3787. {
  3788. i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_buffer_view->extensions[out_buffer_view->extensions_count++]));
  3789. }
  3790. if (i < 0)
  3791. {
  3792. return i;
  3793. }
  3794. }
  3795. }
  3796. else
  3797. {
  3798. i = cgltf_skip_json(tokens, i+1);
  3799. }
  3800. if (i < 0)
  3801. {
  3802. return i;
  3803. }
  3804. }
  3805. return i;
  3806. }
  3807. static int cgltf_parse_json_buffer_views(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  3808. {
  3809. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_buffer_view), (void**)&out_data->buffer_views, &out_data->buffer_views_count);
  3810. if (i < 0)
  3811. {
  3812. return i;
  3813. }
  3814. for (cgltf_size j = 0; j < out_data->buffer_views_count; ++j)
  3815. {
  3816. i = cgltf_parse_json_buffer_view(options, tokens, i, json_chunk, &out_data->buffer_views[j]);
  3817. if (i < 0)
  3818. {
  3819. return i;
  3820. }
  3821. }
  3822. return i;
  3823. }
  3824. static int cgltf_parse_json_buffer(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_buffer* out_buffer)
  3825. {
  3826. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3827. int size = tokens[i].size;
  3828. ++i;
  3829. for (int j = 0; j < size; ++j)
  3830. {
  3831. CGLTF_CHECK_KEY(tokens[i]);
  3832. if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
  3833. {
  3834. ++i;
  3835. out_buffer->size =
  3836. cgltf_json_to_int(tokens+i, json_chunk);
  3837. ++i;
  3838. }
  3839. else if (cgltf_json_strcmp(tokens+i, json_chunk, "uri") == 0)
  3840. {
  3841. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer->uri);
  3842. }
  3843. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  3844. {
  3845. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_buffer->extras);
  3846. }
  3847. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  3848. {
  3849. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_buffer->extensions_count, &out_buffer->extensions);
  3850. }
  3851. else
  3852. {
  3853. i = cgltf_skip_json(tokens, i+1);
  3854. }
  3855. if (i < 0)
  3856. {
  3857. return i;
  3858. }
  3859. }
  3860. return i;
  3861. }
  3862. static int cgltf_parse_json_buffers(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  3863. {
  3864. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_buffer), (void**)&out_data->buffers, &out_data->buffers_count);
  3865. if (i < 0)
  3866. {
  3867. return i;
  3868. }
  3869. for (cgltf_size j = 0; j < out_data->buffers_count; ++j)
  3870. {
  3871. i = cgltf_parse_json_buffer(options, tokens, i, json_chunk, &out_data->buffers[j]);
  3872. if (i < 0)
  3873. {
  3874. return i;
  3875. }
  3876. }
  3877. return i;
  3878. }
  3879. static int cgltf_parse_json_skin(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_skin* out_skin)
  3880. {
  3881. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3882. int size = tokens[i].size;
  3883. ++i;
  3884. for (int j = 0; j < size; ++j)
  3885. {
  3886. CGLTF_CHECK_KEY(tokens[i]);
  3887. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  3888. {
  3889. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_skin->name);
  3890. }
  3891. else if (cgltf_json_strcmp(tokens+i, json_chunk, "joints") == 0)
  3892. {
  3893. i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_skin->joints, &out_skin->joints_count);
  3894. if (i < 0)
  3895. {
  3896. return i;
  3897. }
  3898. for (cgltf_size k = 0; k < out_skin->joints_count; ++k)
  3899. {
  3900. out_skin->joints[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
  3901. ++i;
  3902. }
  3903. }
  3904. else if (cgltf_json_strcmp(tokens+i, json_chunk, "skeleton") == 0)
  3905. {
  3906. ++i;
  3907. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
  3908. out_skin->skeleton = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
  3909. ++i;
  3910. }
  3911. else if (cgltf_json_strcmp(tokens+i, json_chunk, "inverseBindMatrices") == 0)
  3912. {
  3913. ++i;
  3914. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
  3915. out_skin->inverse_bind_matrices = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
  3916. ++i;
  3917. }
  3918. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  3919. {
  3920. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_skin->extras);
  3921. }
  3922. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  3923. {
  3924. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_skin->extensions_count, &out_skin->extensions);
  3925. }
  3926. else
  3927. {
  3928. i = cgltf_skip_json(tokens, i+1);
  3929. }
  3930. if (i < 0)
  3931. {
  3932. return i;
  3933. }
  3934. }
  3935. return i;
  3936. }
  3937. static int cgltf_parse_json_skins(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  3938. {
  3939. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_skin), (void**)&out_data->skins, &out_data->skins_count);
  3940. if (i < 0)
  3941. {
  3942. return i;
  3943. }
  3944. for (cgltf_size j = 0; j < out_data->skins_count; ++j)
  3945. {
  3946. i = cgltf_parse_json_skin(options, tokens, i, json_chunk, &out_data->skins[j]);
  3947. if (i < 0)
  3948. {
  3949. return i;
  3950. }
  3951. }
  3952. return i;
  3953. }
  3954. static int cgltf_parse_json_camera(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_camera* out_camera)
  3955. {
  3956. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3957. int size = tokens[i].size;
  3958. ++i;
  3959. for (int j = 0; j < size; ++j)
  3960. {
  3961. CGLTF_CHECK_KEY(tokens[i]);
  3962. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  3963. {
  3964. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_camera->name);
  3965. }
  3966. else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
  3967. {
  3968. ++i;
  3969. if (cgltf_json_strcmp(tokens + i, json_chunk, "perspective") == 0)
  3970. {
  3971. out_camera->type = cgltf_camera_type_perspective;
  3972. }
  3973. else if (cgltf_json_strcmp(tokens + i, json_chunk, "orthographic") == 0)
  3974. {
  3975. out_camera->type = cgltf_camera_type_orthographic;
  3976. }
  3977. ++i;
  3978. }
  3979. else if (cgltf_json_strcmp(tokens+i, json_chunk, "perspective") == 0)
  3980. {
  3981. ++i;
  3982. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  3983. int data_size = tokens[i].size;
  3984. ++i;
  3985. out_camera->type = cgltf_camera_type_perspective;
  3986. for (int k = 0; k < data_size; ++k)
  3987. {
  3988. CGLTF_CHECK_KEY(tokens[i]);
  3989. if (cgltf_json_strcmp(tokens+i, json_chunk, "aspectRatio") == 0)
  3990. {
  3991. ++i;
  3992. out_camera->data.perspective.aspect_ratio = cgltf_json_to_float(tokens + i, json_chunk);
  3993. ++i;
  3994. }
  3995. else if (cgltf_json_strcmp(tokens+i, json_chunk, "yfov") == 0)
  3996. {
  3997. ++i;
  3998. out_camera->data.perspective.yfov = cgltf_json_to_float(tokens + i, json_chunk);
  3999. ++i;
  4000. }
  4001. else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
  4002. {
  4003. ++i;
  4004. out_camera->data.perspective.zfar = cgltf_json_to_float(tokens + i, json_chunk);
  4005. ++i;
  4006. }
  4007. else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
  4008. {
  4009. ++i;
  4010. out_camera->data.perspective.znear = cgltf_json_to_float(tokens + i, json_chunk);
  4011. ++i;
  4012. }
  4013. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4014. {
  4015. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->data.perspective.extras);
  4016. }
  4017. else
  4018. {
  4019. i = cgltf_skip_json(tokens, i+1);
  4020. }
  4021. if (i < 0)
  4022. {
  4023. return i;
  4024. }
  4025. }
  4026. }
  4027. else if (cgltf_json_strcmp(tokens+i, json_chunk, "orthographic") == 0)
  4028. {
  4029. ++i;
  4030. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4031. int data_size = tokens[i].size;
  4032. ++i;
  4033. out_camera->type = cgltf_camera_type_orthographic;
  4034. for (int k = 0; k < data_size; ++k)
  4035. {
  4036. CGLTF_CHECK_KEY(tokens[i]);
  4037. if (cgltf_json_strcmp(tokens+i, json_chunk, "xmag") == 0)
  4038. {
  4039. ++i;
  4040. out_camera->data.orthographic.xmag = cgltf_json_to_float(tokens + i, json_chunk);
  4041. ++i;
  4042. }
  4043. else if (cgltf_json_strcmp(tokens+i, json_chunk, "ymag") == 0)
  4044. {
  4045. ++i;
  4046. out_camera->data.orthographic.ymag = cgltf_json_to_float(tokens + i, json_chunk);
  4047. ++i;
  4048. }
  4049. else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
  4050. {
  4051. ++i;
  4052. out_camera->data.orthographic.zfar = cgltf_json_to_float(tokens + i, json_chunk);
  4053. ++i;
  4054. }
  4055. else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
  4056. {
  4057. ++i;
  4058. out_camera->data.orthographic.znear = cgltf_json_to_float(tokens + i, json_chunk);
  4059. ++i;
  4060. }
  4061. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4062. {
  4063. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->data.orthographic.extras);
  4064. }
  4065. else
  4066. {
  4067. i = cgltf_skip_json(tokens, i+1);
  4068. }
  4069. if (i < 0)
  4070. {
  4071. return i;
  4072. }
  4073. }
  4074. }
  4075. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4076. {
  4077. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->extras);
  4078. }
  4079. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  4080. {
  4081. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_camera->extensions_count, &out_camera->extensions);
  4082. }
  4083. else
  4084. {
  4085. i = cgltf_skip_json(tokens, i+1);
  4086. }
  4087. if (i < 0)
  4088. {
  4089. return i;
  4090. }
  4091. }
  4092. return i;
  4093. }
  4094. static int cgltf_parse_json_cameras(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  4095. {
  4096. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_camera), (void**)&out_data->cameras, &out_data->cameras_count);
  4097. if (i < 0)
  4098. {
  4099. return i;
  4100. }
  4101. for (cgltf_size j = 0; j < out_data->cameras_count; ++j)
  4102. {
  4103. i = cgltf_parse_json_camera(options, tokens, i, json_chunk, &out_data->cameras[j]);
  4104. if (i < 0)
  4105. {
  4106. return i;
  4107. }
  4108. }
  4109. return i;
  4110. }
  4111. static int cgltf_parse_json_light(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_light* out_light)
  4112. {
  4113. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4114. int size = tokens[i].size;
  4115. ++i;
  4116. for (int j = 0; j < size; ++j)
  4117. {
  4118. CGLTF_CHECK_KEY(tokens[i]);
  4119. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  4120. {
  4121. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_light->name);
  4122. }
  4123. else if (cgltf_json_strcmp(tokens + i, json_chunk, "color") == 0)
  4124. {
  4125. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_light->color, 3);
  4126. }
  4127. else if (cgltf_json_strcmp(tokens + i, json_chunk, "intensity") == 0)
  4128. {
  4129. ++i;
  4130. out_light->intensity = cgltf_json_to_float(tokens + i, json_chunk);
  4131. ++i;
  4132. }
  4133. else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
  4134. {
  4135. ++i;
  4136. if (cgltf_json_strcmp(tokens + i, json_chunk, "directional") == 0)
  4137. {
  4138. out_light->type = cgltf_light_type_directional;
  4139. }
  4140. else if (cgltf_json_strcmp(tokens + i, json_chunk, "point") == 0)
  4141. {
  4142. out_light->type = cgltf_light_type_point;
  4143. }
  4144. else if (cgltf_json_strcmp(tokens + i, json_chunk, "spot") == 0)
  4145. {
  4146. out_light->type = cgltf_light_type_spot;
  4147. }
  4148. ++i;
  4149. }
  4150. else if (cgltf_json_strcmp(tokens + i, json_chunk, "range") == 0)
  4151. {
  4152. ++i;
  4153. out_light->range = cgltf_json_to_float(tokens + i, json_chunk);
  4154. ++i;
  4155. }
  4156. else if (cgltf_json_strcmp(tokens+i, json_chunk, "spot") == 0)
  4157. {
  4158. ++i;
  4159. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4160. int data_size = tokens[i].size;
  4161. ++i;
  4162. for (int k = 0; k < data_size; ++k)
  4163. {
  4164. CGLTF_CHECK_KEY(tokens[i]);
  4165. if (cgltf_json_strcmp(tokens+i, json_chunk, "innerConeAngle") == 0)
  4166. {
  4167. ++i;
  4168. out_light->spot_inner_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
  4169. ++i;
  4170. }
  4171. else if (cgltf_json_strcmp(tokens+i, json_chunk, "outerConeAngle") == 0)
  4172. {
  4173. ++i;
  4174. out_light->spot_outer_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
  4175. ++i;
  4176. }
  4177. else
  4178. {
  4179. i = cgltf_skip_json(tokens, i+1);
  4180. }
  4181. if (i < 0)
  4182. {
  4183. return i;
  4184. }
  4185. }
  4186. }
  4187. else
  4188. {
  4189. i = cgltf_skip_json(tokens, i+1);
  4190. }
  4191. if (i < 0)
  4192. {
  4193. return i;
  4194. }
  4195. }
  4196. return i;
  4197. }
  4198. static int cgltf_parse_json_lights(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  4199. {
  4200. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_light), (void**)&out_data->lights, &out_data->lights_count);
  4201. if (i < 0)
  4202. {
  4203. return i;
  4204. }
  4205. for (cgltf_size j = 0; j < out_data->lights_count; ++j)
  4206. {
  4207. i = cgltf_parse_json_light(options, tokens, i, json_chunk, &out_data->lights[j]);
  4208. if (i < 0)
  4209. {
  4210. return i;
  4211. }
  4212. }
  4213. return i;
  4214. }
  4215. static int cgltf_parse_json_node(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_node* out_node)
  4216. {
  4217. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4218. out_node->rotation[3] = 1.0f;
  4219. out_node->scale[0] = 1.0f;
  4220. out_node->scale[1] = 1.0f;
  4221. out_node->scale[2] = 1.0f;
  4222. out_node->matrix[0] = 1.0f;
  4223. out_node->matrix[5] = 1.0f;
  4224. out_node->matrix[10] = 1.0f;
  4225. out_node->matrix[15] = 1.0f;
  4226. int size = tokens[i].size;
  4227. ++i;
  4228. for (int j = 0; j < size; ++j)
  4229. {
  4230. CGLTF_CHECK_KEY(tokens[i]);
  4231. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  4232. {
  4233. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_node->name);
  4234. }
  4235. else if (cgltf_json_strcmp(tokens+i, json_chunk, "children") == 0)
  4236. {
  4237. i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_node->children, &out_node->children_count);
  4238. if (i < 0)
  4239. {
  4240. return i;
  4241. }
  4242. for (cgltf_size k = 0; k < out_node->children_count; ++k)
  4243. {
  4244. out_node->children[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
  4245. ++i;
  4246. }
  4247. }
  4248. else if (cgltf_json_strcmp(tokens+i, json_chunk, "mesh") == 0)
  4249. {
  4250. ++i;
  4251. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
  4252. out_node->mesh = CGLTF_PTRINDEX(cgltf_mesh, cgltf_json_to_int(tokens + i, json_chunk));
  4253. ++i;
  4254. }
  4255. else if (cgltf_json_strcmp(tokens+i, json_chunk, "skin") == 0)
  4256. {
  4257. ++i;
  4258. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
  4259. out_node->skin = CGLTF_PTRINDEX(cgltf_skin, cgltf_json_to_int(tokens + i, json_chunk));
  4260. ++i;
  4261. }
  4262. else if (cgltf_json_strcmp(tokens+i, json_chunk, "camera") == 0)
  4263. {
  4264. ++i;
  4265. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
  4266. out_node->camera = CGLTF_PTRINDEX(cgltf_camera, cgltf_json_to_int(tokens + i, json_chunk));
  4267. ++i;
  4268. }
  4269. else if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
  4270. {
  4271. out_node->has_translation = 1;
  4272. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->translation, 3);
  4273. }
  4274. else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
  4275. {
  4276. out_node->has_rotation = 1;
  4277. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->rotation, 4);
  4278. }
  4279. else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
  4280. {
  4281. out_node->has_scale = 1;
  4282. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->scale, 3);
  4283. }
  4284. else if (cgltf_json_strcmp(tokens+i, json_chunk, "matrix") == 0)
  4285. {
  4286. out_node->has_matrix = 1;
  4287. i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->matrix, 16);
  4288. }
  4289. else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
  4290. {
  4291. i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_node->weights, &out_node->weights_count);
  4292. if (i < 0)
  4293. {
  4294. return i;
  4295. }
  4296. i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_node->weights, (int)out_node->weights_count);
  4297. }
  4298. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4299. {
  4300. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_node->extras);
  4301. }
  4302. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  4303. {
  4304. ++i;
  4305. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4306. if(out_node->extensions)
  4307. {
  4308. return CGLTF_ERROR_JSON;
  4309. }
  4310. int extensions_size = tokens[i].size;
  4311. out_node->extensions_count= 0;
  4312. out_node->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
  4313. if (!out_node->extensions)
  4314. {
  4315. return CGLTF_ERROR_NOMEM;
  4316. }
  4317. ++i;
  4318. for (int k = 0; k < extensions_size; ++k)
  4319. {
  4320. CGLTF_CHECK_KEY(tokens[i]);
  4321. if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
  4322. {
  4323. ++i;
  4324. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4325. int data_size = tokens[i].size;
  4326. ++i;
  4327. for (int m = 0; m < data_size; ++m)
  4328. {
  4329. CGLTF_CHECK_KEY(tokens[i]);
  4330. if (cgltf_json_strcmp(tokens + i, json_chunk, "light") == 0)
  4331. {
  4332. ++i;
  4333. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
  4334. out_node->light = CGLTF_PTRINDEX(cgltf_light, cgltf_json_to_int(tokens + i, json_chunk));
  4335. ++i;
  4336. }
  4337. else
  4338. {
  4339. i = cgltf_skip_json(tokens, i + 1);
  4340. }
  4341. if (i < 0)
  4342. {
  4343. return i;
  4344. }
  4345. }
  4346. }
  4347. else
  4348. {
  4349. i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_node->extensions[out_node->extensions_count++]));
  4350. }
  4351. if (i < 0)
  4352. {
  4353. return i;
  4354. }
  4355. }
  4356. }
  4357. else
  4358. {
  4359. i = cgltf_skip_json(tokens, i+1);
  4360. }
  4361. if (i < 0)
  4362. {
  4363. return i;
  4364. }
  4365. }
  4366. return i;
  4367. }
  4368. static int cgltf_parse_json_nodes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  4369. {
  4370. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_node), (void**)&out_data->nodes, &out_data->nodes_count);
  4371. if (i < 0)
  4372. {
  4373. return i;
  4374. }
  4375. for (cgltf_size j = 0; j < out_data->nodes_count; ++j)
  4376. {
  4377. i = cgltf_parse_json_node(options, tokens, i, json_chunk, &out_data->nodes[j]);
  4378. if (i < 0)
  4379. {
  4380. return i;
  4381. }
  4382. }
  4383. return i;
  4384. }
  4385. static int cgltf_parse_json_scene(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_scene* out_scene)
  4386. {
  4387. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4388. int size = tokens[i].size;
  4389. ++i;
  4390. for (int j = 0; j < size; ++j)
  4391. {
  4392. CGLTF_CHECK_KEY(tokens[i]);
  4393. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  4394. {
  4395. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_scene->name);
  4396. }
  4397. else if (cgltf_json_strcmp(tokens+i, json_chunk, "nodes") == 0)
  4398. {
  4399. i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_scene->nodes, &out_scene->nodes_count);
  4400. if (i < 0)
  4401. {
  4402. return i;
  4403. }
  4404. for (cgltf_size k = 0; k < out_scene->nodes_count; ++k)
  4405. {
  4406. out_scene->nodes[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
  4407. ++i;
  4408. }
  4409. }
  4410. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4411. {
  4412. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_scene->extras);
  4413. }
  4414. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  4415. {
  4416. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_scene->extensions_count, &out_scene->extensions);
  4417. }
  4418. else
  4419. {
  4420. i = cgltf_skip_json(tokens, i+1);
  4421. }
  4422. if (i < 0)
  4423. {
  4424. return i;
  4425. }
  4426. }
  4427. return i;
  4428. }
  4429. static int cgltf_parse_json_scenes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  4430. {
  4431. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_scene), (void**)&out_data->scenes, &out_data->scenes_count);
  4432. if (i < 0)
  4433. {
  4434. return i;
  4435. }
  4436. for (cgltf_size j = 0; j < out_data->scenes_count; ++j)
  4437. {
  4438. i = cgltf_parse_json_scene(options, tokens, i, json_chunk, &out_data->scenes[j]);
  4439. if (i < 0)
  4440. {
  4441. return i;
  4442. }
  4443. }
  4444. return i;
  4445. }
  4446. static int cgltf_parse_json_animation_sampler(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation_sampler* out_sampler)
  4447. {
  4448. (void)options;
  4449. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4450. int size = tokens[i].size;
  4451. ++i;
  4452. for (int j = 0; j < size; ++j)
  4453. {
  4454. CGLTF_CHECK_KEY(tokens[i]);
  4455. if (cgltf_json_strcmp(tokens+i, json_chunk, "input") == 0)
  4456. {
  4457. ++i;
  4458. out_sampler->input = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
  4459. ++i;
  4460. }
  4461. else if (cgltf_json_strcmp(tokens+i, json_chunk, "output") == 0)
  4462. {
  4463. ++i;
  4464. out_sampler->output = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
  4465. ++i;
  4466. }
  4467. else if (cgltf_json_strcmp(tokens+i, json_chunk, "interpolation") == 0)
  4468. {
  4469. ++i;
  4470. if (cgltf_json_strcmp(tokens + i, json_chunk, "LINEAR") == 0)
  4471. {
  4472. out_sampler->interpolation = cgltf_interpolation_type_linear;
  4473. }
  4474. else if (cgltf_json_strcmp(tokens + i, json_chunk, "STEP") == 0)
  4475. {
  4476. out_sampler->interpolation = cgltf_interpolation_type_step;
  4477. }
  4478. else if (cgltf_json_strcmp(tokens + i, json_chunk, "CUBICSPLINE") == 0)
  4479. {
  4480. out_sampler->interpolation = cgltf_interpolation_type_cubic_spline;
  4481. }
  4482. ++i;
  4483. }
  4484. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4485. {
  4486. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sampler->extras);
  4487. }
  4488. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  4489. {
  4490. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sampler->extensions_count, &out_sampler->extensions);
  4491. }
  4492. else
  4493. {
  4494. i = cgltf_skip_json(tokens, i+1);
  4495. }
  4496. if (i < 0)
  4497. {
  4498. return i;
  4499. }
  4500. }
  4501. return i;
  4502. }
  4503. static int cgltf_parse_json_animation_channel(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation_channel* out_channel)
  4504. {
  4505. (void)options;
  4506. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4507. int size = tokens[i].size;
  4508. ++i;
  4509. for (int j = 0; j < size; ++j)
  4510. {
  4511. CGLTF_CHECK_KEY(tokens[i]);
  4512. if (cgltf_json_strcmp(tokens+i, json_chunk, "sampler") == 0)
  4513. {
  4514. ++i;
  4515. out_channel->sampler = CGLTF_PTRINDEX(cgltf_animation_sampler, cgltf_json_to_int(tokens + i, json_chunk));
  4516. ++i;
  4517. }
  4518. else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
  4519. {
  4520. ++i;
  4521. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4522. int target_size = tokens[i].size;
  4523. ++i;
  4524. for (int k = 0; k < target_size; ++k)
  4525. {
  4526. CGLTF_CHECK_KEY(tokens[i]);
  4527. if (cgltf_json_strcmp(tokens+i, json_chunk, "node") == 0)
  4528. {
  4529. ++i;
  4530. out_channel->target_node = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
  4531. ++i;
  4532. }
  4533. else if (cgltf_json_strcmp(tokens+i, json_chunk, "path") == 0)
  4534. {
  4535. ++i;
  4536. if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
  4537. {
  4538. out_channel->target_path = cgltf_animation_path_type_translation;
  4539. }
  4540. else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
  4541. {
  4542. out_channel->target_path = cgltf_animation_path_type_rotation;
  4543. }
  4544. else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
  4545. {
  4546. out_channel->target_path = cgltf_animation_path_type_scale;
  4547. }
  4548. else if (cgltf_json_strcmp(tokens+i, json_chunk, "weights") == 0)
  4549. {
  4550. out_channel->target_path = cgltf_animation_path_type_weights;
  4551. }
  4552. ++i;
  4553. }
  4554. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4555. {
  4556. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_channel->extras);
  4557. }
  4558. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  4559. {
  4560. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_channel->extensions_count, &out_channel->extensions);
  4561. }
  4562. else
  4563. {
  4564. i = cgltf_skip_json(tokens, i+1);
  4565. }
  4566. if (i < 0)
  4567. {
  4568. return i;
  4569. }
  4570. }
  4571. }
  4572. else
  4573. {
  4574. i = cgltf_skip_json(tokens, i+1);
  4575. }
  4576. if (i < 0)
  4577. {
  4578. return i;
  4579. }
  4580. }
  4581. return i;
  4582. }
  4583. static int cgltf_parse_json_animation(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation* out_animation)
  4584. {
  4585. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4586. int size = tokens[i].size;
  4587. ++i;
  4588. for (int j = 0; j < size; ++j)
  4589. {
  4590. CGLTF_CHECK_KEY(tokens[i]);
  4591. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  4592. {
  4593. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_animation->name);
  4594. }
  4595. else if (cgltf_json_strcmp(tokens+i, json_chunk, "samplers") == 0)
  4596. {
  4597. i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_sampler), (void**)&out_animation->samplers, &out_animation->samplers_count);
  4598. if (i < 0)
  4599. {
  4600. return i;
  4601. }
  4602. for (cgltf_size k = 0; k < out_animation->samplers_count; ++k)
  4603. {
  4604. i = cgltf_parse_json_animation_sampler(options, tokens, i, json_chunk, &out_animation->samplers[k]);
  4605. if (i < 0)
  4606. {
  4607. return i;
  4608. }
  4609. }
  4610. }
  4611. else if (cgltf_json_strcmp(tokens+i, json_chunk, "channels") == 0)
  4612. {
  4613. i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_channel), (void**)&out_animation->channels, &out_animation->channels_count);
  4614. if (i < 0)
  4615. {
  4616. return i;
  4617. }
  4618. for (cgltf_size k = 0; k < out_animation->channels_count; ++k)
  4619. {
  4620. i = cgltf_parse_json_animation_channel(options, tokens, i, json_chunk, &out_animation->channels[k]);
  4621. if (i < 0)
  4622. {
  4623. return i;
  4624. }
  4625. }
  4626. }
  4627. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4628. {
  4629. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_animation->extras);
  4630. }
  4631. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  4632. {
  4633. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_animation->extensions_count, &out_animation->extensions);
  4634. }
  4635. else
  4636. {
  4637. i = cgltf_skip_json(tokens, i+1);
  4638. }
  4639. if (i < 0)
  4640. {
  4641. return i;
  4642. }
  4643. }
  4644. return i;
  4645. }
  4646. static int cgltf_parse_json_animations(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  4647. {
  4648. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_animation), (void**)&out_data->animations, &out_data->animations_count);
  4649. if (i < 0)
  4650. {
  4651. return i;
  4652. }
  4653. for (cgltf_size j = 0; j < out_data->animations_count; ++j)
  4654. {
  4655. i = cgltf_parse_json_animation(options, tokens, i, json_chunk, &out_data->animations[j]);
  4656. if (i < 0)
  4657. {
  4658. return i;
  4659. }
  4660. }
  4661. return i;
  4662. }
  4663. static int cgltf_parse_json_variant(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_material_variant* out_variant)
  4664. {
  4665. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4666. int size = tokens[i].size;
  4667. ++i;
  4668. for (int j = 0; j < size; ++j)
  4669. {
  4670. CGLTF_CHECK_KEY(tokens[i]);
  4671. if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
  4672. {
  4673. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_variant->name);
  4674. }
  4675. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4676. {
  4677. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_variant->extras);
  4678. }
  4679. else
  4680. {
  4681. i = cgltf_skip_json(tokens, i+1);
  4682. }
  4683. if (i < 0)
  4684. {
  4685. return i;
  4686. }
  4687. }
  4688. return i;
  4689. }
  4690. static int cgltf_parse_json_variants(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  4691. {
  4692. i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_material_variant), (void**)&out_data->variants, &out_data->variants_count);
  4693. if (i < 0)
  4694. {
  4695. return i;
  4696. }
  4697. for (cgltf_size j = 0; j < out_data->variants_count; ++j)
  4698. {
  4699. i = cgltf_parse_json_variant(options, tokens, i, json_chunk, &out_data->variants[j]);
  4700. if (i < 0)
  4701. {
  4702. return i;
  4703. }
  4704. }
  4705. return i;
  4706. }
  4707. static int cgltf_parse_json_asset(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_asset* out_asset)
  4708. {
  4709. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4710. int size = tokens[i].size;
  4711. ++i;
  4712. for (int j = 0; j < size; ++j)
  4713. {
  4714. CGLTF_CHECK_KEY(tokens[i]);
  4715. if (cgltf_json_strcmp(tokens+i, json_chunk, "copyright") == 0)
  4716. {
  4717. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->copyright);
  4718. }
  4719. else if (cgltf_json_strcmp(tokens+i, json_chunk, "generator") == 0)
  4720. {
  4721. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->generator);
  4722. }
  4723. else if (cgltf_json_strcmp(tokens+i, json_chunk, "version") == 0)
  4724. {
  4725. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->version);
  4726. }
  4727. else if (cgltf_json_strcmp(tokens+i, json_chunk, "minVersion") == 0)
  4728. {
  4729. i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->min_version);
  4730. }
  4731. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
  4732. {
  4733. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_asset->extras);
  4734. }
  4735. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  4736. {
  4737. i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_asset->extensions_count, &out_asset->extensions);
  4738. }
  4739. else
  4740. {
  4741. i = cgltf_skip_json(tokens, i+1);
  4742. }
  4743. if (i < 0)
  4744. {
  4745. return i;
  4746. }
  4747. }
  4748. if (out_asset->version && CGLTF_ATOF(out_asset->version) < 2)
  4749. {
  4750. return CGLTF_ERROR_LEGACY;
  4751. }
  4752. return i;
  4753. }
  4754. cgltf_size cgltf_num_components(cgltf_type type) {
  4755. switch (type)
  4756. {
  4757. case cgltf_type_vec2:
  4758. return 2;
  4759. case cgltf_type_vec3:
  4760. return 3;
  4761. case cgltf_type_vec4:
  4762. return 4;
  4763. case cgltf_type_mat2:
  4764. return 4;
  4765. case cgltf_type_mat3:
  4766. return 9;
  4767. case cgltf_type_mat4:
  4768. return 16;
  4769. case cgltf_type_invalid:
  4770. case cgltf_type_scalar:
  4771. default:
  4772. return 1;
  4773. }
  4774. }
  4775. static cgltf_size cgltf_component_size(cgltf_component_type component_type) {
  4776. switch (component_type)
  4777. {
  4778. case cgltf_component_type_r_8:
  4779. case cgltf_component_type_r_8u:
  4780. return 1;
  4781. case cgltf_component_type_r_16:
  4782. case cgltf_component_type_r_16u:
  4783. return 2;
  4784. case cgltf_component_type_r_32u:
  4785. case cgltf_component_type_r_32f:
  4786. return 4;
  4787. case cgltf_component_type_invalid:
  4788. default:
  4789. return 0;
  4790. }
  4791. }
  4792. static cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type)
  4793. {
  4794. cgltf_size component_size = cgltf_component_size(component_type);
  4795. if (type == cgltf_type_mat2 && component_size == 1)
  4796. {
  4797. return 8 * component_size;
  4798. }
  4799. else if (type == cgltf_type_mat3 && (component_size == 1 || component_size == 2))
  4800. {
  4801. return 12 * component_size;
  4802. }
  4803. return component_size * cgltf_num_components(type);
  4804. }
  4805. static int cgltf_fixup_pointers(cgltf_data* out_data);
  4806. static int cgltf_parse_json_root(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
  4807. {
  4808. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4809. int size = tokens[i].size;
  4810. ++i;
  4811. for (int j = 0; j < size; ++j)
  4812. {
  4813. CGLTF_CHECK_KEY(tokens[i]);
  4814. if (cgltf_json_strcmp(tokens + i, json_chunk, "asset") == 0)
  4815. {
  4816. i = cgltf_parse_json_asset(options, tokens, i + 1, json_chunk, &out_data->asset);
  4817. }
  4818. else if (cgltf_json_strcmp(tokens + i, json_chunk, "meshes") == 0)
  4819. {
  4820. i = cgltf_parse_json_meshes(options, tokens, i + 1, json_chunk, out_data);
  4821. }
  4822. else if (cgltf_json_strcmp(tokens + i, json_chunk, "accessors") == 0)
  4823. {
  4824. i = cgltf_parse_json_accessors(options, tokens, i + 1, json_chunk, out_data);
  4825. }
  4826. else if (cgltf_json_strcmp(tokens + i, json_chunk, "bufferViews") == 0)
  4827. {
  4828. i = cgltf_parse_json_buffer_views(options, tokens, i + 1, json_chunk, out_data);
  4829. }
  4830. else if (cgltf_json_strcmp(tokens + i, json_chunk, "buffers") == 0)
  4831. {
  4832. i = cgltf_parse_json_buffers(options, tokens, i + 1, json_chunk, out_data);
  4833. }
  4834. else if (cgltf_json_strcmp(tokens + i, json_chunk, "materials") == 0)
  4835. {
  4836. i = cgltf_parse_json_materials(options, tokens, i + 1, json_chunk, out_data);
  4837. }
  4838. else if (cgltf_json_strcmp(tokens + i, json_chunk, "images") == 0)
  4839. {
  4840. i = cgltf_parse_json_images(options, tokens, i + 1, json_chunk, out_data);
  4841. }
  4842. else if (cgltf_json_strcmp(tokens + i, json_chunk, "textures") == 0)
  4843. {
  4844. i = cgltf_parse_json_textures(options, tokens, i + 1, json_chunk, out_data);
  4845. }
  4846. else if (cgltf_json_strcmp(tokens + i, json_chunk, "samplers") == 0)
  4847. {
  4848. i = cgltf_parse_json_samplers(options, tokens, i + 1, json_chunk, out_data);
  4849. }
  4850. else if (cgltf_json_strcmp(tokens + i, json_chunk, "skins") == 0)
  4851. {
  4852. i = cgltf_parse_json_skins(options, tokens, i + 1, json_chunk, out_data);
  4853. }
  4854. else if (cgltf_json_strcmp(tokens + i, json_chunk, "cameras") == 0)
  4855. {
  4856. i = cgltf_parse_json_cameras(options, tokens, i + 1, json_chunk, out_data);
  4857. }
  4858. else if (cgltf_json_strcmp(tokens + i, json_chunk, "nodes") == 0)
  4859. {
  4860. i = cgltf_parse_json_nodes(options, tokens, i + 1, json_chunk, out_data);
  4861. }
  4862. else if (cgltf_json_strcmp(tokens + i, json_chunk, "scenes") == 0)
  4863. {
  4864. i = cgltf_parse_json_scenes(options, tokens, i + 1, json_chunk, out_data);
  4865. }
  4866. else if (cgltf_json_strcmp(tokens + i, json_chunk, "scene") == 0)
  4867. {
  4868. ++i;
  4869. out_data->scene = CGLTF_PTRINDEX(cgltf_scene, cgltf_json_to_int(tokens + i, json_chunk));
  4870. ++i;
  4871. }
  4872. else if (cgltf_json_strcmp(tokens + i, json_chunk, "animations") == 0)
  4873. {
  4874. i = cgltf_parse_json_animations(options, tokens, i + 1, json_chunk, out_data);
  4875. }
  4876. else if (cgltf_json_strcmp(tokens+i, json_chunk, "extras") == 0)
  4877. {
  4878. i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_data->extras);
  4879. }
  4880. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
  4881. {
  4882. ++i;
  4883. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4884. if(out_data->data_extensions)
  4885. {
  4886. return CGLTF_ERROR_JSON;
  4887. }
  4888. int extensions_size = tokens[i].size;
  4889. out_data->data_extensions_count = 0;
  4890. out_data->data_extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
  4891. if (!out_data->data_extensions)
  4892. {
  4893. return CGLTF_ERROR_NOMEM;
  4894. }
  4895. ++i;
  4896. for (int k = 0; k < extensions_size; ++k)
  4897. {
  4898. CGLTF_CHECK_KEY(tokens[i]);
  4899. if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
  4900. {
  4901. ++i;
  4902. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4903. int data_size = tokens[i].size;
  4904. ++i;
  4905. for (int m = 0; m < data_size; ++m)
  4906. {
  4907. CGLTF_CHECK_KEY(tokens[i]);
  4908. if (cgltf_json_strcmp(tokens + i, json_chunk, "lights") == 0)
  4909. {
  4910. i = cgltf_parse_json_lights(options, tokens, i + 1, json_chunk, out_data);
  4911. }
  4912. else
  4913. {
  4914. i = cgltf_skip_json(tokens, i + 1);
  4915. }
  4916. if (i < 0)
  4917. {
  4918. return i;
  4919. }
  4920. }
  4921. }
  4922. else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_variants") == 0)
  4923. {
  4924. ++i;
  4925. CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
  4926. int data_size = tokens[i].size;
  4927. ++i;
  4928. for (int m = 0; m < data_size; ++m)
  4929. {
  4930. CGLTF_CHECK_KEY(tokens[i]);
  4931. if (cgltf_json_strcmp(tokens + i, json_chunk, "variants") == 0)
  4932. {
  4933. i = cgltf_parse_json_variants(options, tokens, i + 1, json_chunk, out_data);
  4934. }
  4935. else
  4936. {
  4937. i = cgltf_skip_json(tokens, i + 1);
  4938. }
  4939. if (i < 0)
  4940. {
  4941. return i;
  4942. }
  4943. }
  4944. }
  4945. else
  4946. {
  4947. i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_data->data_extensions[out_data->data_extensions_count++]));
  4948. }
  4949. if (i < 0)
  4950. {
  4951. return i;
  4952. }
  4953. }
  4954. }
  4955. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensionsUsed") == 0)
  4956. {
  4957. i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_data->extensions_used, &out_data->extensions_used_count);
  4958. }
  4959. else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensionsRequired") == 0)
  4960. {
  4961. i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_data->extensions_required, &out_data->extensions_required_count);
  4962. }
  4963. else
  4964. {
  4965. i = cgltf_skip_json(tokens, i + 1);
  4966. }
  4967. if (i < 0)
  4968. {
  4969. return i;
  4970. }
  4971. }
  4972. return i;
  4973. }
  4974. cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data)
  4975. {
  4976. jsmn_parser parser = { 0, 0, 0 };
  4977. if (options->json_token_count == 0)
  4978. {
  4979. int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, NULL, 0);
  4980. if (token_count <= 0)
  4981. {
  4982. return cgltf_result_invalid_json;
  4983. }
  4984. options->json_token_count = token_count;
  4985. }
  4986. jsmntok_t* tokens = (jsmntok_t*)options->memory.alloc(options->memory.user_data, sizeof(jsmntok_t) * (options->json_token_count + 1));
  4987. if (!tokens)
  4988. {
  4989. return cgltf_result_out_of_memory;
  4990. }
  4991. jsmn_init(&parser);
  4992. int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, tokens, options->json_token_count);
  4993. if (token_count <= 0)
  4994. {
  4995. options->memory.free(options->memory.user_data, tokens);
  4996. return cgltf_result_invalid_json;
  4997. }
  4998. // this makes sure that we always have an UNDEFINED token at the end of the stream
  4999. // for invalid JSON inputs this makes sure we don't perform out of bound reads of token data
  5000. tokens[token_count].type = JSMN_UNDEFINED;
  5001. cgltf_data* data = (cgltf_data*)options->memory.alloc(options->memory.user_data, sizeof(cgltf_data));
  5002. if (!data)
  5003. {
  5004. options->memory.free(options->memory.user_data, tokens);
  5005. return cgltf_result_out_of_memory;
  5006. }
  5007. memset(data, 0, sizeof(cgltf_data));
  5008. data->memory = options->memory;
  5009. data->file = options->file;
  5010. int i = cgltf_parse_json_root(options, tokens, 0, json_chunk, data);
  5011. options->memory.free(options->memory.user_data, tokens);
  5012. if (i < 0)
  5013. {
  5014. cgltf_free(data);
  5015. switch (i)
  5016. {
  5017. case CGLTF_ERROR_NOMEM: return cgltf_result_out_of_memory;
  5018. case CGLTF_ERROR_LEGACY: return cgltf_result_legacy_gltf;
  5019. default: return cgltf_result_invalid_gltf;
  5020. }
  5021. }
  5022. if (cgltf_fixup_pointers(data) < 0)
  5023. {
  5024. cgltf_free(data);
  5025. return cgltf_result_invalid_gltf;
  5026. }
  5027. data->json = (const char*)json_chunk;
  5028. data->json_size = size;
  5029. *out_data = data;
  5030. return cgltf_result_success;
  5031. }
  5032. static int cgltf_fixup_pointers(cgltf_data* data)
  5033. {
  5034. for (cgltf_size i = 0; i < data->meshes_count; ++i)
  5035. {
  5036. for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
  5037. {
  5038. CGLTF_PTRFIXUP(data->meshes[i].primitives[j].indices, data->accessors, data->accessors_count);
  5039. CGLTF_PTRFIXUP(data->meshes[i].primitives[j].material, data->materials, data->materials_count);
  5040. for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
  5041. {
  5042. CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].attributes[k].data, data->accessors, data->accessors_count);
  5043. }
  5044. for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
  5045. {
  5046. for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
  5047. {
  5048. CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].targets[k].attributes[m].data, data->accessors, data->accessors_count);
  5049. }
  5050. }
  5051. if (data->meshes[i].primitives[j].has_draco_mesh_compression)
  5052. {
  5053. CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].draco_mesh_compression.buffer_view, data->buffer_views, data->buffer_views_count);
  5054. for (cgltf_size m = 0; m < data->meshes[i].primitives[j].draco_mesh_compression.attributes_count; ++m)
  5055. {
  5056. CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].draco_mesh_compression.attributes[m].data, data->accessors, data->accessors_count);
  5057. }
  5058. }
  5059. for (cgltf_size k = 0; k < data->meshes[i].primitives[j].mappings_count; ++k)
  5060. {
  5061. CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].mappings[k].material, data->materials, data->materials_count);
  5062. }
  5063. }
  5064. }
  5065. for (cgltf_size i = 0; i < data->accessors_count; ++i)
  5066. {
  5067. CGLTF_PTRFIXUP(data->accessors[i].buffer_view, data->buffer_views, data->buffer_views_count);
  5068. if (data->accessors[i].is_sparse)
  5069. {
  5070. CGLTF_PTRFIXUP_REQ(data->accessors[i].sparse.indices_buffer_view, data->buffer_views, data->buffer_views_count);
  5071. CGLTF_PTRFIXUP_REQ(data->accessors[i].sparse.values_buffer_view, data->buffer_views, data->buffer_views_count);
  5072. }
  5073. if (data->accessors[i].buffer_view)
  5074. {
  5075. data->accessors[i].stride = data->accessors[i].buffer_view->stride;
  5076. }
  5077. if (data->accessors[i].stride == 0)
  5078. {
  5079. data->accessors[i].stride = cgltf_calc_size(data->accessors[i].type, data->accessors[i].component_type);
  5080. }
  5081. }
  5082. for (cgltf_size i = 0; i < data->textures_count; ++i)
  5083. {
  5084. CGLTF_PTRFIXUP(data->textures[i].image, data->images, data->images_count);
  5085. CGLTF_PTRFIXUP(data->textures[i].sampler, data->samplers, data->samplers_count);
  5086. }
  5087. for (cgltf_size i = 0; i < data->images_count; ++i)
  5088. {
  5089. CGLTF_PTRFIXUP(data->images[i].buffer_view, data->buffer_views, data->buffer_views_count);
  5090. }
  5091. for (cgltf_size i = 0; i < data->materials_count; ++i)
  5092. {
  5093. CGLTF_PTRFIXUP(data->materials[i].normal_texture.texture, data->textures, data->textures_count);
  5094. CGLTF_PTRFIXUP(data->materials[i].emissive_texture.texture, data->textures, data->textures_count);
  5095. CGLTF_PTRFIXUP(data->materials[i].occlusion_texture.texture, data->textures, data->textures_count);
  5096. CGLTF_PTRFIXUP(data->materials[i].pbr_metallic_roughness.base_color_texture.texture, data->textures, data->textures_count);
  5097. CGLTF_PTRFIXUP(data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture, data->textures, data->textures_count);
  5098. CGLTF_PTRFIXUP(data->materials[i].pbr_specular_glossiness.diffuse_texture.texture, data->textures, data->textures_count);
  5099. CGLTF_PTRFIXUP(data->materials[i].pbr_specular_glossiness.specular_glossiness_texture.texture, data->textures, data->textures_count);
  5100. CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_texture.texture, data->textures, data->textures_count);
  5101. CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_roughness_texture.texture, data->textures, data->textures_count);
  5102. CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_normal_texture.texture, data->textures, data->textures_count);
  5103. CGLTF_PTRFIXUP(data->materials[i].specular.specular_texture.texture, data->textures, data->textures_count);
  5104. CGLTF_PTRFIXUP(data->materials[i].specular.specular_color_texture.texture, data->textures, data->textures_count);
  5105. CGLTF_PTRFIXUP(data->materials[i].transmission.transmission_texture.texture, data->textures, data->textures_count);
  5106. CGLTF_PTRFIXUP(data->materials[i].volume.thickness_texture.texture, data->textures, data->textures_count);
  5107. CGLTF_PTRFIXUP(data->materials[i].sheen.sheen_color_texture.texture, data->textures, data->textures_count);
  5108. CGLTF_PTRFIXUP(data->materials[i].sheen.sheen_roughness_texture.texture, data->textures, data->textures_count);
  5109. }
  5110. for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
  5111. {
  5112. CGLTF_PTRFIXUP_REQ(data->buffer_views[i].buffer, data->buffers, data->buffers_count);
  5113. if (data->buffer_views[i].has_meshopt_compression)
  5114. {
  5115. CGLTF_PTRFIXUP_REQ(data->buffer_views[i].meshopt_compression.buffer, data->buffers, data->buffers_count);
  5116. }
  5117. }
  5118. for (cgltf_size i = 0; i < data->skins_count; ++i)
  5119. {
  5120. for (cgltf_size j = 0; j < data->skins[i].joints_count; ++j)
  5121. {
  5122. CGLTF_PTRFIXUP_REQ(data->skins[i].joints[j], data->nodes, data->nodes_count);
  5123. }
  5124. CGLTF_PTRFIXUP(data->skins[i].skeleton, data->nodes, data->nodes_count);
  5125. CGLTF_PTRFIXUP(data->skins[i].inverse_bind_matrices, data->accessors, data->accessors_count);
  5126. }
  5127. for (cgltf_size i = 0; i < data->nodes_count; ++i)
  5128. {
  5129. for (cgltf_size j = 0; j < data->nodes[i].children_count; ++j)
  5130. {
  5131. CGLTF_PTRFIXUP_REQ(data->nodes[i].children[j], data->nodes, data->nodes_count);
  5132. if (data->nodes[i].children[j]->parent)
  5133. {
  5134. return CGLTF_ERROR_JSON;
  5135. }
  5136. data->nodes[i].children[j]->parent = &data->nodes[i];
  5137. }
  5138. CGLTF_PTRFIXUP(data->nodes[i].mesh, data->meshes, data->meshes_count);
  5139. CGLTF_PTRFIXUP(data->nodes[i].skin, data->skins, data->skins_count);
  5140. CGLTF_PTRFIXUP(data->nodes[i].camera, data->cameras, data->cameras_count);
  5141. CGLTF_PTRFIXUP(data->nodes[i].light, data->lights, data->lights_count);
  5142. }
  5143. for (cgltf_size i = 0; i < data->scenes_count; ++i)
  5144. {
  5145. for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j)
  5146. {
  5147. CGLTF_PTRFIXUP_REQ(data->scenes[i].nodes[j], data->nodes, data->nodes_count);
  5148. if (data->scenes[i].nodes[j]->parent)
  5149. {
  5150. return CGLTF_ERROR_JSON;
  5151. }
  5152. }
  5153. }
  5154. CGLTF_PTRFIXUP(data->scene, data->scenes, data->scenes_count);
  5155. for (cgltf_size i = 0; i < data->animations_count; ++i)
  5156. {
  5157. for (cgltf_size j = 0; j < data->animations[i].samplers_count; ++j)
  5158. {
  5159. CGLTF_PTRFIXUP_REQ(data->animations[i].samplers[j].input, data->accessors, data->accessors_count);
  5160. CGLTF_PTRFIXUP_REQ(data->animations[i].samplers[j].output, data->accessors, data->accessors_count);
  5161. }
  5162. for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
  5163. {
  5164. CGLTF_PTRFIXUP_REQ(data->animations[i].channels[j].sampler, data->animations[i].samplers, data->animations[i].samplers_count);
  5165. CGLTF_PTRFIXUP(data->animations[i].channels[j].target_node, data->nodes, data->nodes_count);
  5166. }
  5167. }
  5168. return 0;
  5169. }
  5170. /*
  5171. * -- jsmn.c start --
  5172. * Source: https://github.com/zserge/jsmn
  5173. * License: MIT
  5174. *
  5175. * Copyright (c) 2010 Serge A. Zaitsev
  5176. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5177. * of this software and associated documentation files (the "Software"), to deal
  5178. * in the Software without restriction, including without limitation the rights
  5179. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  5180. * copies of the Software, and to permit persons to whom the Software is
  5181. * furnished to do so, subject to the following conditions:
  5182. * The above copyright notice and this permission notice shall be included in
  5183. * all copies or substantial portions of the Software.
  5184. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  5185. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  5186. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  5187. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  5188. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  5189. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  5190. * THE SOFTWARE.
  5191. */
  5192. /**
  5193. * Allocates a fresh unused token from the token pull.
  5194. */
  5195. static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
  5196. jsmntok_t *tokens, size_t num_tokens) {
  5197. jsmntok_t *tok;
  5198. if (parser->toknext >= num_tokens) {
  5199. return NULL;
  5200. }
  5201. tok = &tokens[parser->toknext++];
  5202. tok->start = tok->end = -1;
  5203. tok->size = 0;
  5204. #ifdef JSMN_PARENT_LINKS
  5205. tok->parent = -1;
  5206. #endif
  5207. return tok;
  5208. }
  5209. /**
  5210. * Fills token type and boundaries.
  5211. */
  5212. static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
  5213. int start, int end) {
  5214. token->type = type;
  5215. token->start = start;
  5216. token->end = end;
  5217. token->size = 0;
  5218. }
  5219. /**
  5220. * Fills next available token with JSON primitive.
  5221. */
  5222. static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
  5223. size_t len, jsmntok_t *tokens, size_t num_tokens) {
  5224. jsmntok_t *token;
  5225. int start;
  5226. start = parser->pos;
  5227. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
  5228. switch (js[parser->pos]) {
  5229. #ifndef JSMN_STRICT
  5230. /* In strict mode primitive must be followed by "," or "}" or "]" */
  5231. case ':':
  5232. #endif
  5233. case '\t' : case '\r' : case '\n' : case ' ' :
  5234. case ',' : case ']' : case '}' :
  5235. goto found;
  5236. }
  5237. if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
  5238. parser->pos = start;
  5239. return JSMN_ERROR_INVAL;
  5240. }
  5241. }
  5242. #ifdef JSMN_STRICT
  5243. /* In strict mode primitive must be followed by a comma/object/array */
  5244. parser->pos = start;
  5245. return JSMN_ERROR_PART;
  5246. #endif
  5247. found:
  5248. if (tokens == NULL) {
  5249. parser->pos--;
  5250. return 0;
  5251. }
  5252. token = jsmn_alloc_token(parser, tokens, num_tokens);
  5253. if (token == NULL) {
  5254. parser->pos = start;
  5255. return JSMN_ERROR_NOMEM;
  5256. }
  5257. jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
  5258. #ifdef JSMN_PARENT_LINKS
  5259. token->parent = parser->toksuper;
  5260. #endif
  5261. parser->pos--;
  5262. return 0;
  5263. }
  5264. /**
  5265. * Fills next token with JSON string.
  5266. */
  5267. static int jsmn_parse_string(jsmn_parser *parser, const char *js,
  5268. size_t len, jsmntok_t *tokens, size_t num_tokens) {
  5269. jsmntok_t *token;
  5270. int start = parser->pos;
  5271. parser->pos++;
  5272. /* Skip starting quote */
  5273. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
  5274. char c = js[parser->pos];
  5275. /* Quote: end of string */
  5276. if (c == '\"') {
  5277. if (tokens == NULL) {
  5278. return 0;
  5279. }
  5280. token = jsmn_alloc_token(parser, tokens, num_tokens);
  5281. if (token == NULL) {
  5282. parser->pos = start;
  5283. return JSMN_ERROR_NOMEM;
  5284. }
  5285. jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
  5286. #ifdef JSMN_PARENT_LINKS
  5287. token->parent = parser->toksuper;
  5288. #endif
  5289. return 0;
  5290. }
  5291. /* Backslash: Quoted symbol expected */
  5292. if (c == '\\' && parser->pos + 1 < len) {
  5293. int i;
  5294. parser->pos++;
  5295. switch (js[parser->pos]) {
  5296. /* Allowed escaped symbols */
  5297. case '\"': case '/' : case '\\' : case 'b' :
  5298. case 'f' : case 'r' : case 'n' : case 't' :
  5299. break;
  5300. /* Allows escaped symbol \uXXXX */
  5301. case 'u':
  5302. parser->pos++;
  5303. for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
  5304. /* If it isn't a hex character we have an error */
  5305. if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
  5306. (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
  5307. (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
  5308. parser->pos = start;
  5309. return JSMN_ERROR_INVAL;
  5310. }
  5311. parser->pos++;
  5312. }
  5313. parser->pos--;
  5314. break;
  5315. /* Unexpected symbol */
  5316. default:
  5317. parser->pos = start;
  5318. return JSMN_ERROR_INVAL;
  5319. }
  5320. }
  5321. }
  5322. parser->pos = start;
  5323. return JSMN_ERROR_PART;
  5324. }
  5325. /**
  5326. * Parse JSON string and fill tokens.
  5327. */
  5328. static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
  5329. jsmntok_t *tokens, size_t num_tokens) {
  5330. int r;
  5331. int i;
  5332. jsmntok_t *token;
  5333. int count = parser->toknext;
  5334. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
  5335. char c;
  5336. jsmntype_t type;
  5337. c = js[parser->pos];
  5338. switch (c) {
  5339. case '{': case '[':
  5340. count++;
  5341. if (tokens == NULL) {
  5342. break;
  5343. }
  5344. token = jsmn_alloc_token(parser, tokens, num_tokens);
  5345. if (token == NULL)
  5346. return JSMN_ERROR_NOMEM;
  5347. if (parser->toksuper != -1) {
  5348. tokens[parser->toksuper].size++;
  5349. #ifdef JSMN_PARENT_LINKS
  5350. token->parent = parser->toksuper;
  5351. #endif
  5352. }
  5353. token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
  5354. token->start = parser->pos;
  5355. parser->toksuper = parser->toknext - 1;
  5356. break;
  5357. case '}': case ']':
  5358. if (tokens == NULL)
  5359. break;
  5360. type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
  5361. #ifdef JSMN_PARENT_LINKS
  5362. if (parser->toknext < 1) {
  5363. return JSMN_ERROR_INVAL;
  5364. }
  5365. token = &tokens[parser->toknext - 1];
  5366. for (;;) {
  5367. if (token->start != -1 && token->end == -1) {
  5368. if (token->type != type) {
  5369. return JSMN_ERROR_INVAL;
  5370. }
  5371. token->end = parser->pos + 1;
  5372. parser->toksuper = token->parent;
  5373. break;
  5374. }
  5375. if (token->parent == -1) {
  5376. if(token->type != type || parser->toksuper == -1) {
  5377. return JSMN_ERROR_INVAL;
  5378. }
  5379. break;
  5380. }
  5381. token = &tokens[token->parent];
  5382. }
  5383. #else
  5384. for (i = parser->toknext - 1; i >= 0; i--) {
  5385. token = &tokens[i];
  5386. if (token->start != -1 && token->end == -1) {
  5387. if (token->type != type) {
  5388. return JSMN_ERROR_INVAL;
  5389. }
  5390. parser->toksuper = -1;
  5391. token->end = parser->pos + 1;
  5392. break;
  5393. }
  5394. }
  5395. /* Error if unmatched closing bracket */
  5396. if (i == -1) return JSMN_ERROR_INVAL;
  5397. for (; i >= 0; i--) {
  5398. token = &tokens[i];
  5399. if (token->start != -1 && token->end == -1) {
  5400. parser->toksuper = i;
  5401. break;
  5402. }
  5403. }
  5404. #endif
  5405. break;
  5406. case '\"':
  5407. r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
  5408. if (r < 0) return r;
  5409. count++;
  5410. if (parser->toksuper != -1 && tokens != NULL)
  5411. tokens[parser->toksuper].size++;
  5412. break;
  5413. case '\t' : case '\r' : case '\n' : case ' ':
  5414. break;
  5415. case ':':
  5416. parser->toksuper = parser->toknext - 1;
  5417. break;
  5418. case ',':
  5419. if (tokens != NULL && parser->toksuper != -1 &&
  5420. tokens[parser->toksuper].type != JSMN_ARRAY &&
  5421. tokens[parser->toksuper].type != JSMN_OBJECT) {
  5422. #ifdef JSMN_PARENT_LINKS
  5423. parser->toksuper = tokens[parser->toksuper].parent;
  5424. #else
  5425. for (i = parser->toknext - 1; i >= 0; i--) {
  5426. if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
  5427. if (tokens[i].start != -1 && tokens[i].end == -1) {
  5428. parser->toksuper = i;
  5429. break;
  5430. }
  5431. }
  5432. }
  5433. #endif
  5434. }
  5435. break;
  5436. #ifdef JSMN_STRICT
  5437. /* In strict mode primitives are: numbers and booleans */
  5438. case '-': case '0': case '1' : case '2': case '3' : case '4':
  5439. case '5': case '6': case '7' : case '8': case '9':
  5440. case 't': case 'f': case 'n' :
  5441. /* And they must not be keys of the object */
  5442. if (tokens != NULL && parser->toksuper != -1) {
  5443. jsmntok_t *t = &tokens[parser->toksuper];
  5444. if (t->type == JSMN_OBJECT ||
  5445. (t->type == JSMN_STRING && t->size != 0)) {
  5446. return JSMN_ERROR_INVAL;
  5447. }
  5448. }
  5449. #else
  5450. /* In non-strict mode every unquoted value is a primitive */
  5451. default:
  5452. #endif
  5453. r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
  5454. if (r < 0) return r;
  5455. count++;
  5456. if (parser->toksuper != -1 && tokens != NULL)
  5457. tokens[parser->toksuper].size++;
  5458. break;
  5459. #ifdef JSMN_STRICT
  5460. /* Unexpected char in strict mode */
  5461. default:
  5462. return JSMN_ERROR_INVAL;
  5463. #endif
  5464. }
  5465. }
  5466. if (tokens != NULL) {
  5467. for (i = parser->toknext - 1; i >= 0; i--) {
  5468. /* Unmatched opened object or array */
  5469. if (tokens[i].start != -1 && tokens[i].end == -1) {
  5470. return JSMN_ERROR_PART;
  5471. }
  5472. }
  5473. }
  5474. return count;
  5475. }
  5476. /**
  5477. * Creates a new parser based over a given buffer with an array of tokens
  5478. * available.
  5479. */
  5480. static void jsmn_init(jsmn_parser *parser) {
  5481. parser->pos = 0;
  5482. parser->toknext = 0;
  5483. parser->toksuper = -1;
  5484. }
  5485. /*
  5486. * -- jsmn.c end --
  5487. */
  5488. #endif /* #ifdef CGLTF_IMPLEMENTATION */
  5489. /* cgltf is distributed under MIT license:
  5490. *
  5491. * Copyright (c) 2018 Johannes Kuhlmann
  5492. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5493. * of this software and associated documentation files (the "Software"), to deal
  5494. * in the Software without restriction, including without limitation the rights
  5495. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  5496. * copies of the Software, and to permit persons to whom the Software is
  5497. * furnished to do so, subject to the following conditions:
  5498. * The above copyright notice and this permission notice shall be included in all
  5499. * copies or substantial portions of the Software.
  5500. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  5501. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  5502. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  5503. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  5504. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  5505. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  5506. * SOFTWARE.
  5507. */