cgltf.h 200 KB

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