cgltf.h 180 KB

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