nodePath.cxx 229 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117
  1. // Filename: nodePath.cxx
  2. // Created by: drose (25Feb02)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "nodePath.h"
  19. #include "nodePathCollection.h"
  20. #include "findApproxPath.h"
  21. #include "findApproxLevelEntry.h"
  22. #include "config_pgraph.h"
  23. #include "colorAttrib.h"
  24. #include "colorScaleAttrib.h"
  25. #include "cullBinAttrib.h"
  26. #include "textureAttrib.h"
  27. #include "texMatrixAttrib.h"
  28. #include "texGenAttrib.h"
  29. #include "materialAttrib.h"
  30. #include "lightAttrib.h"
  31. #include "clipPlaneAttrib.h"
  32. #include "polylightEffect.h"
  33. #include "fogAttrib.h"
  34. #include "renderModeAttrib.h"
  35. #include "cullFaceAttrib.h"
  36. #include "alphaTestAttrib.h"
  37. #include "depthTestAttrib.h"
  38. #include "depthWriteAttrib.h"
  39. #include "shaderAttrib.h"
  40. #include "billboardEffect.h"
  41. #include "compassEffect.h"
  42. #include "showBoundsEffect.h"
  43. #include "transparencyAttrib.h"
  44. #include "antialiasAttrib.h"
  45. #include "texProjectorEffect.h"
  46. #include "texturePool.h"
  47. #include "planeNode.h"
  48. #include "lensNode.h"
  49. #include "materialPool.h"
  50. #include "look_at.h"
  51. #include "plist.h"
  52. #include "boundingSphere.h"
  53. #include "geomNode.h"
  54. #include "sceneGraphAnalyzer.h"
  55. #include "sceneGraphReducer.h"
  56. #include "textureCollection.h"
  57. #include "textureStageCollection.h"
  58. #include "globPattern.h"
  59. #include "shader.h"
  60. #include "shaderInput.h"
  61. #include "config_gobj.h"
  62. #include "bamFile.h"
  63. #include "preparedGraphicsObjects.h"
  64. #include "dcast.h"
  65. // stack seems to overflow on Intel C++ at 7000. If we need more than
  66. // 7000, need to increase stack size.
  67. int NodePath::_max_search_depth = 7000;
  68. TypeHandle NodePath::_type_handle;
  69. ////////////////////////////////////////////////////////////////////
  70. // Function: NodePath::get_num_nodes
  71. // Access: Published
  72. // Description: Returns the number of nodes in the path.
  73. ////////////////////////////////////////////////////////////////////
  74. int NodePath::
  75. get_num_nodes() const {
  76. if (is_empty()) {
  77. return 0;
  78. }
  79. return _head->get_length();
  80. }
  81. ////////////////////////////////////////////////////////////////////
  82. // Function: NodePath::get_node
  83. // Access: Published
  84. // Description: Returns the nth node of the path, where 0 is the
  85. // referenced (bottom) node and get_num_nodes() - 1 is
  86. // the top node. This requires iterating through the
  87. // path.
  88. //
  89. // Also see node(), which is a convenience function to
  90. // return the same thing as get_node(0) (since the
  91. // bottom node is the most important node in the
  92. // NodePath, and is the one most frequently referenced).
  93. ////////////////////////////////////////////////////////////////////
  94. PandaNode *NodePath::
  95. get_node(int index) const {
  96. nassertr(index >= 0 && index < get_num_nodes(), NULL);
  97. NodePathComponent *comp = _head;
  98. while (index > 0) {
  99. // If this assertion fails, the index was out of range; the
  100. // component's length must have been invalid.
  101. nassertr(comp != (NodePathComponent *)NULL, NULL);
  102. comp = comp->get_next();
  103. index--;
  104. }
  105. // If this assertion fails, the index was out of range; the
  106. // component's length must have been invalid.
  107. nassertr(comp != (NodePathComponent *)NULL, NULL);
  108. return comp->get_node();
  109. }
  110. ////////////////////////////////////////////////////////////////////
  111. // Function: NodePath::get_top
  112. // Access: Published
  113. // Description: Returns a singleton NodePath that represents the top
  114. // of the path, or empty NodePath if this path is empty.
  115. ////////////////////////////////////////////////////////////////////
  116. NodePath NodePath::
  117. get_top() const {
  118. if (is_empty()) {
  119. return *this;
  120. }
  121. NodePathComponent *comp = _head;
  122. while (!comp->is_top_node()) {
  123. comp = comp->get_next();
  124. nassertr(comp != (NodePathComponent *)NULL, NULL);
  125. }
  126. NodePath top;
  127. top._head = comp;
  128. return top;
  129. }
  130. ////////////////////////////////////////////////////////////////////
  131. // Function: NodePath::get_children
  132. // Access: Published
  133. // Description: Returns the set of all child nodes of the referenced
  134. // node.
  135. ////////////////////////////////////////////////////////////////////
  136. NodePathCollection NodePath::
  137. get_children() const {
  138. NodePathCollection result;
  139. nassertr_always(!is_empty(), result);
  140. PandaNode *bottom_node = node();
  141. PandaNode::Children cr = bottom_node->get_children();
  142. int num_children = cr.get_num_children();
  143. for (int i = 0; i < num_children; i++) {
  144. NodePath child;
  145. child._head = PandaNode::get_component(_head, cr.get_child(i));
  146. result.add_path(child);
  147. }
  148. return result;
  149. }
  150. ////////////////////////////////////////////////////////////////////
  151. // Function: NodePath::get_stashed_children
  152. // Access: Published
  153. // Description: Returns the set of all child nodes of the referenced
  154. // node that have been stashed. These children are not
  155. // normally visible on the node, and do not appear in
  156. // the list returned by get_children().
  157. ////////////////////////////////////////////////////////////////////
  158. NodePathCollection NodePath::
  159. get_stashed_children() const {
  160. NodePathCollection result;
  161. nassertr_always(!is_empty(), result);
  162. PandaNode *bottom_node = node();
  163. int num_stashed = bottom_node->get_num_stashed();
  164. for (int i = 0; i < num_stashed; i++) {
  165. NodePath stashed;
  166. stashed._head = PandaNode::get_component(_head, bottom_node->get_stashed(i));
  167. result.add_path(stashed);
  168. }
  169. return result;
  170. }
  171. ////////////////////////////////////////////////////////////////////
  172. // Function: NodePath::get_sort
  173. // Access: Published
  174. // Description: Returns the sort value of the referenced node within
  175. // its parent; that is, the sort number passed on the
  176. // last reparenting operation for this node. This will
  177. // control the position of the node within its parent's
  178. // list of children.
  179. ////////////////////////////////////////////////////////////////////
  180. int NodePath::
  181. get_sort() const {
  182. if (!has_parent()) {
  183. return 0;
  184. }
  185. PandaNode *parent = _head->get_next()->get_node();
  186. PandaNode *child = node();
  187. nassertr(parent != (PandaNode *)NULL && child != (PandaNode *)NULL, 0);
  188. int child_index = parent->find_child(child);
  189. if (child_index != -1) {
  190. return parent->get_child_sort(child_index);
  191. }
  192. child_index = parent->find_stashed(child);
  193. if (child_index != -1) {
  194. return parent->get_stashed_sort(child_index);
  195. }
  196. nassertr(false, 0);
  197. return 0;
  198. }
  199. ////////////////////////////////////////////////////////////////////
  200. // Function: NodePath::find
  201. // Access: Published
  202. // Description: Searches for a node below the referenced node that
  203. // matches the indicated string. Returns the shortest
  204. // match found, if any, or an empty NodePath if no match
  205. // can be found.
  206. ////////////////////////////////////////////////////////////////////
  207. NodePath NodePath::
  208. find(const string &path) const {
  209. nassertr_always(!is_empty(), fail());
  210. NodePathCollection col;
  211. find_matches(col, path, 1);
  212. if (col.is_empty()) {
  213. return NodePath::not_found();
  214. }
  215. return col.get_path(0);
  216. }
  217. ////////////////////////////////////////////////////////////////////
  218. // Function: NodePath::find_path_to
  219. // Access: Published
  220. // Description: Searches for the indicated node below this node and
  221. // returns the shortest NodePath that connects them.
  222. ////////////////////////////////////////////////////////////////////
  223. NodePath NodePath::
  224. find_path_to(PandaNode *node) const {
  225. nassertr_always(!is_empty(), fail());
  226. nassertr(node != (PandaNode *)NULL, fail());
  227. NodePathCollection col;
  228. FindApproxPath approx_path;
  229. approx_path.add_match_many(0);
  230. approx_path.add_match_pointer(node, 0);
  231. find_matches(col, approx_path, 1);
  232. if (col.is_empty()) {
  233. return NodePath::not_found();
  234. }
  235. return col.get_path(0);
  236. }
  237. ////////////////////////////////////////////////////////////////////
  238. // Function: NodePath::find_all_matches
  239. // Access: Published
  240. // Description: Returns the complete set of all NodePaths that begin
  241. // with this NodePath and can be extended by
  242. // path. The shortest paths will be listed
  243. // first.
  244. ////////////////////////////////////////////////////////////////////
  245. NodePathCollection NodePath::
  246. find_all_matches(const string &path) const {
  247. NodePathCollection col;
  248. nassertr_always(!is_empty(), col);
  249. nassertr(verify_complete(), col);
  250. find_matches(col, path, -1);
  251. return col;
  252. }
  253. ////////////////////////////////////////////////////////////////////
  254. // Function: NodePath::find_all_paths_to
  255. // Access: Published
  256. // Description: Returns the set of all NodePaths that extend from
  257. // this NodePath down to the indicated node. The
  258. // shortest paths will be listed first.
  259. ////////////////////////////////////////////////////////////////////
  260. NodePathCollection NodePath::
  261. find_all_paths_to(PandaNode *node) const {
  262. NodePathCollection col;
  263. nassertr_always(!is_empty(), col);
  264. nassertr(verify_complete(), col);
  265. nassertr(node != (PandaNode *)NULL, col);
  266. FindApproxPath approx_path;
  267. approx_path.add_match_many(0);
  268. approx_path.add_match_pointer(node, 0);
  269. find_matches(col, approx_path, -1);
  270. return col;
  271. }
  272. ////////////////////////////////////////////////////////////////////
  273. // Function: NodePath::reparent_to
  274. // Access: Published
  275. // Description: Removes the referenced node of the NodePath from its
  276. // current parent and attaches it to the referenced node
  277. // of the indicated NodePath.
  278. //
  279. // If the destination NodePath is empty, this is the
  280. // same thing as detach_node().
  281. ////////////////////////////////////////////////////////////////////
  282. void NodePath::
  283. reparent_to(const NodePath &other, int sort) {
  284. nassertv(verify_complete());
  285. nassertv(other.verify_complete());
  286. nassertv_always(!is_empty());
  287. nassertv(other._error_type == ET_ok);
  288. // Reparenting implicitly resents the delta vector.
  289. node()->reset_prev_transform();
  290. bool reparented = PandaNode::reparent(other._head, _head, sort, false);
  291. nassertv(reparented);
  292. }
  293. ////////////////////////////////////////////////////////////////////
  294. // Function: NodePath::wrt_reparent_to
  295. // Access: Published
  296. // Description: This functions identically to reparent_to(), except
  297. // the transform on this node is also adjusted so that
  298. // the node remains in the same place in world
  299. // coordinates, even if it is reparented into a
  300. // different coordinate system.
  301. ////////////////////////////////////////////////////////////////////
  302. void NodePath::
  303. wrt_reparent_to(const NodePath &other, int sort) {
  304. nassertv(verify_complete());
  305. nassertv(other.verify_complete());
  306. nassertv_always(!is_empty());
  307. nassertv(other._error_type == ET_ok);
  308. if (get_transform() == get_prev_transform()) {
  309. set_transform(get_transform(other));
  310. node()->reset_prev_transform();
  311. } else {
  312. set_transform(get_transform(other));
  313. set_prev_transform(get_prev_transform(other));
  314. }
  315. reparent_to(other, sort);
  316. }
  317. ////////////////////////////////////////////////////////////////////
  318. // Function: NodePath::instance_to
  319. // Access: Published
  320. // Description: Adds the referenced node of the NodePath as a child
  321. // of the referenced node of the indicated other
  322. // NodePath. Any other parent-child relations of the
  323. // node are unchanged; in particular, the node is not
  324. // removed from its existing parent, if any.
  325. //
  326. // If the node already had an existing parent, this
  327. // method will create a new instance of the node within
  328. // the scene graph.
  329. //
  330. // This does not change the NodePath itself, but does
  331. // return a new NodePath that reflects the new instance
  332. // node.
  333. //
  334. // If the destination NodePath is empty, this creates a
  335. // new instance which is not yet parented to any node.
  336. // A new instance of this sort cannot easily be
  337. // differentiated from other similar instances, but it
  338. // is nevertheless a different instance and it will
  339. // return a different get_id() value.
  340. ////////////////////////////////////////////////////////////////////
  341. NodePath NodePath::
  342. instance_to(const NodePath &other, int sort) const {
  343. nassertr(verify_complete(), NodePath::fail());
  344. nassertr(other.verify_complete(), NodePath::fail());
  345. nassertr_always(!is_empty(), NodePath::fail());
  346. nassertr(other._error_type == ET_ok, NodePath::fail());
  347. NodePath new_instance;
  348. // First, we'll attach to NULL, to guarantee we get a brand new
  349. // instance.
  350. new_instance._head = PandaNode::attach(NULL, node(), sort);
  351. // Now, we'll reparent the new instance to the target node.
  352. bool reparented = PandaNode::reparent(other._head, new_instance._head,
  353. sort, false);
  354. nassertr(reparented, new_instance);
  355. // instance_to() doesn't reset the velocity delta, unlike most of
  356. // the other reparenting operations. The reasoning is that
  357. // instance_to() is not necessarily a reparenting operation, since
  358. // it doesn't change the original instance.
  359. return new_instance;
  360. }
  361. ////////////////////////////////////////////////////////////////////
  362. // Function: NodePath::instance_under_node
  363. // Access: Published
  364. // Description: Behaves like instance_to(), but implicitly creates a
  365. // new node to instance the geometry under, and returns a
  366. // NodePath to that new node. This allows the
  367. // programmer to set a unique state and/or transform on
  368. // this instance.
  369. ////////////////////////////////////////////////////////////////////
  370. NodePath NodePath::
  371. instance_under_node(const NodePath &other, const string &name, int sort) const {
  372. NodePath new_node = other.attach_new_node(name, sort);
  373. NodePath instance = instance_to(new_node);
  374. if (instance.is_empty()) {
  375. new_node.remove_node();
  376. return instance;
  377. }
  378. return new_node;
  379. }
  380. ////////////////////////////////////////////////////////////////////
  381. // Function: NodePath::copy_to
  382. // Access: Published
  383. // Description: Functions like instance_to(), except a deep
  384. // copy is made of the referenced node and all of its
  385. // descendents, which is then parented to the indicated
  386. // node. A NodePath to the newly created copy is
  387. // returned.
  388. ////////////////////////////////////////////////////////////////////
  389. NodePath NodePath::
  390. copy_to(const NodePath &other, int sort) const {
  391. nassertr(verify_complete(), fail());
  392. nassertr(other.verify_complete(), fail());
  393. nassertr_always(!is_empty(), fail());
  394. nassertr(other._error_type == ET_ok, fail());
  395. PandaNode *source_node = node();
  396. PT(PandaNode) copy_node = source_node->copy_subgraph();
  397. nassertr(copy_node != (PandaNode *)NULL, fail());
  398. copy_node->reset_prev_transform();
  399. return other.attach_new_node(copy_node, sort);
  400. }
  401. ////////////////////////////////////////////////////////////////////
  402. // Function: NodePath::attach_new_node
  403. // Access: Published
  404. // Description: Attaches a new node, with or without existing
  405. // parents, to the scene graph below the referenced node
  406. // of this NodePath. This is the preferred way to add
  407. // nodes to the graph.
  408. //
  409. // If the node was already a child of the parent, this
  410. // returns a NodePath to the existing child.
  411. //
  412. // This does *not* automatically extend the current
  413. // NodePath to reflect the attachment; however, a
  414. // NodePath that does reflect this extension is
  415. // returned.
  416. ////////////////////////////////////////////////////////////////////
  417. NodePath NodePath::
  418. attach_new_node(PandaNode *node, int sort) const {
  419. nassertr(verify_complete(), NodePath::fail());
  420. nassertr(_error_type == ET_ok, NodePath::fail());
  421. nassertr(node != (PandaNode *)NULL, NodePath::fail());
  422. NodePath new_path(*this);
  423. new_path._head = PandaNode::attach(_head, node, sort);
  424. return new_path;
  425. }
  426. ////////////////////////////////////////////////////////////////////
  427. // Function: NodePath::remove_node
  428. // Access: Published
  429. // Description: Disconnects the referenced node from the scene graph.
  430. // This will also delete the node if there are no other
  431. // pointers to it.
  432. //
  433. // Normally, this should be called only when you are
  434. // really done with the node. If you want to remove a
  435. // node from the scene graph but keep it around for
  436. // later, you should probably use detach_node() instead.
  437. //
  438. // In practice, the only difference between
  439. // remove_node() and detach_node() is that remove_node()
  440. // also resets the NodePath to empty, which will cause
  441. // the node to be deleted immediately if there are no
  442. // other references. On the other hand, detach_node()
  443. // leaves the NodePath referencing the node, which will
  444. // keep at least one reference to the node for as long
  445. // as the NodePath exists.
  446. ////////////////////////////////////////////////////////////////////
  447. void NodePath::
  448. remove_node() {
  449. nassertv(_error_type != ET_not_found);
  450. // If we have no parents, remove_node() is just a do-nothing
  451. // operation; if we have no nodes, maybe we were already removed.
  452. // In either case, quietly do nothing except to ensure the
  453. // NodePath is clear.
  454. if (!is_empty() && !is_singleton()) {
  455. node()->reset_prev_transform();
  456. PandaNode::detach(_head);
  457. }
  458. if (is_empty() || _head->has_key()) {
  459. // Preserve the key we had on the node before we removed it.
  460. int key = get_key();
  461. (*this) = NodePath::removed();
  462. _backup_key = key;
  463. } else {
  464. // We didn't have a key; just clear the NodePath.
  465. (*this) = NodePath::removed();
  466. }
  467. }
  468. ////////////////////////////////////////////////////////////////////
  469. // Function: NodePath::detach_node
  470. // Access: Published
  471. // Description: Disconnects the referenced node from its parent, but
  472. // does not immediately delete it. The NodePath retains
  473. // a pointer to the node, and becomes a singleton
  474. // NodePath.
  475. //
  476. // This should be called to detach a node from the scene
  477. // graph, with the option of reattaching it later to the
  478. // same parent or to a different parent.
  479. //
  480. // In practice, the only difference between
  481. // remove_node() and detach_node() is that remove_node()
  482. // also resets the NodePath to empty, which will cause
  483. // the node to be deleted immediately if there are no
  484. // other references. On the other hand, detach_node()
  485. // leaves the NodePath referencing the node, which will
  486. // keep at least one reference to the node for as long
  487. // as the NodePath exists.
  488. ////////////////////////////////////////////////////////////////////
  489. void NodePath::
  490. detach_node() {
  491. nassertv(_error_type != ET_not_found);
  492. if (!is_empty() && !is_singleton()) {
  493. node()->reset_prev_transform();
  494. PandaNode::detach(_head);
  495. }
  496. }
  497. ////////////////////////////////////////////////////////////////////
  498. // Function: NodePath::output
  499. // Access: Published
  500. // Description: Writes a sensible description of the NodePath to the
  501. // indicated output stream.
  502. ////////////////////////////////////////////////////////////////////
  503. void NodePath::
  504. output(ostream &out) const {
  505. switch (_error_type) {
  506. case ET_not_found:
  507. out << "**not found**";
  508. return;
  509. case ET_removed:
  510. out << "**removed**";
  511. return;
  512. case ET_fail:
  513. out << "**error**";
  514. return;
  515. default:
  516. break;
  517. }
  518. if (_head == (NodePathComponent *)NULL) {
  519. out << "(empty)";
  520. } else {
  521. _head->output(out);
  522. }
  523. }
  524. ////////////////////////////////////////////////////////////////////
  525. // Function: NodePath::get_state
  526. // Access: Published
  527. // Description: Returns the state changes that must be made to
  528. // transition to the render state of this node from the
  529. // render state of the other node.
  530. ////////////////////////////////////////////////////////////////////
  531. CPT(RenderState) NodePath::
  532. get_state(const NodePath &other) const {
  533. nassertr(_error_type == ET_ok && other._error_type == ET_ok, RenderState::make_empty());
  534. if (other.is_empty()) {
  535. return get_net_state();
  536. }
  537. if (is_empty()) {
  538. return other.get_net_state()->invert_compose(RenderState::make_empty());
  539. }
  540. nassertr(verify_complete(), RenderState::make_empty());
  541. nassertr(other.verify_complete(), RenderState::make_empty());
  542. int a_count, b_count;
  543. if (find_common_ancestor(*this, other, a_count, b_count) == (NodePathComponent *)NULL) {
  544. if (allow_unrelated_wrt) {
  545. pgraph_cat.debug()
  546. << *this << " is not related to " << other << "\n";
  547. } else {
  548. pgraph_cat.error()
  549. << *this << " is not related to " << other << "\n";
  550. nassertr(false, RenderState::make_empty());
  551. }
  552. }
  553. CPT(RenderState) a_state = r_get_partial_state(_head, a_count);
  554. CPT(RenderState) b_state = r_get_partial_state(other._head, b_count);
  555. return b_state->invert_compose(a_state);
  556. }
  557. ////////////////////////////////////////////////////////////////////
  558. // Function: NodePath::set_state
  559. // Access: Published
  560. // Description: Sets the state object on this node, relative to
  561. // the other node. This computes a new state object
  562. // that will have the indicated value when seen from the
  563. // other node.
  564. ////////////////////////////////////////////////////////////////////
  565. void NodePath::
  566. set_state(const NodePath &other, const RenderState *state) {
  567. nassertv(_error_type == ET_ok && other._error_type == ET_ok);
  568. nassertv_always(!is_empty());
  569. // First, we perform a wrt to the parent, to get the conversion.
  570. CPT(RenderState) rel_state;
  571. if (has_parent()) {
  572. rel_state = other.get_state(get_parent());
  573. } else {
  574. rel_state = other.get_state(NodePath());
  575. }
  576. CPT(RenderState) new_state = rel_state->compose(state);
  577. set_state(new_state);
  578. }
  579. ////////////////////////////////////////////////////////////////////
  580. // Function: NodePath::get_transform
  581. // Access: Published
  582. // Description: Returns the relative transform to this node from the
  583. // other node; i.e. the transformation of this node
  584. // as seen from the other node.
  585. ////////////////////////////////////////////////////////////////////
  586. CPT(TransformState) NodePath::
  587. get_transform(const NodePath &other) const {
  588. nassertr(_error_type == ET_ok && other._error_type == ET_ok, TransformState::make_identity());
  589. if (other.is_empty()) {
  590. return get_net_transform();
  591. }
  592. if (is_empty()) {
  593. return other.get_net_transform()->invert_compose(TransformState::make_identity());
  594. }
  595. nassertr(verify_complete(), TransformState::make_identity());
  596. nassertr(other.verify_complete(), TransformState::make_identity());
  597. int a_count, b_count;
  598. if (find_common_ancestor(*this, other, a_count, b_count) == (NodePathComponent *)NULL) {
  599. if (allow_unrelated_wrt) {
  600. if (pgraph_cat.is_debug()) {
  601. pgraph_cat.debug()
  602. << *this << " is not related to " << other << "\n";
  603. }
  604. } else {
  605. pgraph_cat.error()
  606. << *this << " is not related to " << other << "\n";
  607. nassertr(false, TransformState::make_identity());
  608. }
  609. }
  610. CPT(TransformState) a_transform, b_transform;
  611. a_transform = r_get_partial_transform(_head, a_count);
  612. if (a_transform != (TransformState *)NULL) {
  613. b_transform = r_get_partial_transform(other._head, b_count);
  614. }
  615. if (b_transform == (TransformState *)NULL) {
  616. // If either path involved a node with a net_transform
  617. // RenderEffect applied, we have to go all the way up to the root
  618. // to get the right answer.
  619. a_transform = r_get_net_transform(_head);
  620. b_transform = r_get_net_transform(other._head);
  621. }
  622. return b_transform->invert_compose(a_transform);
  623. }
  624. ////////////////////////////////////////////////////////////////////
  625. // Function: NodePath::set_transform
  626. // Access: Published
  627. // Description: Sets the transform object on this node, relative to
  628. // the other node. This computes a new transform object
  629. // that will have the indicated value when seen from the
  630. // other node.
  631. ////////////////////////////////////////////////////////////////////
  632. void NodePath::
  633. set_transform(const NodePath &other, const TransformState *transform) {
  634. nassertv(_error_type == ET_ok && other._error_type == ET_ok);
  635. nassertv_always(!is_empty());
  636. // First, we perform a wrt to the parent, to get the conversion.
  637. CPT(TransformState) rel_trans;
  638. if (has_parent()) {
  639. rel_trans = other.get_transform(get_parent());
  640. } else {
  641. rel_trans = other.get_transform(NodePath());
  642. }
  643. CPT(TransformState) new_trans = rel_trans->compose(transform);
  644. set_transform(new_trans);
  645. }
  646. ////////////////////////////////////////////////////////////////////
  647. // Function: NodePath::get_prev_transform
  648. // Access: Published
  649. // Description: Returns the relative "previous" transform to this
  650. // node from the other node; i.e. the position of this
  651. // node in the previous frame, as seen by the other node
  652. // in the previous frame.
  653. ////////////////////////////////////////////////////////////////////
  654. CPT(TransformState) NodePath::
  655. get_prev_transform(const NodePath &other) const {
  656. nassertr(_error_type == ET_ok && other._error_type == ET_ok, TransformState::make_identity());
  657. if (other.is_empty()) {
  658. return get_net_prev_transform();
  659. }
  660. if (is_empty()) {
  661. return other.get_net_prev_transform()->invert_compose(TransformState::make_identity());
  662. }
  663. nassertr(verify_complete(), TransformState::make_identity());
  664. nassertr(other.verify_complete(), TransformState::make_identity());
  665. int a_count, b_count;
  666. if (find_common_ancestor(*this, other, a_count, b_count) == (NodePathComponent *)NULL) {
  667. if (allow_unrelated_wrt) {
  668. pgraph_cat.debug()
  669. << *this << " is not related to " << other << "\n";
  670. } else {
  671. pgraph_cat.error()
  672. << *this << " is not related to " << other << "\n";
  673. nassertr(false, TransformState::make_identity());
  674. }
  675. }
  676. CPT(TransformState) a_prev_transform = r_get_partial_prev_transform(_head, a_count);
  677. CPT(TransformState) b_prev_transform = r_get_partial_prev_transform(other._head, b_count);
  678. return b_prev_transform->invert_compose(a_prev_transform);
  679. }
  680. ////////////////////////////////////////////////////////////////////
  681. // Function: NodePath::set_prev_transform
  682. // Access: Published
  683. // Description: Sets the "previous" transform object on this node,
  684. // relative to the other node. This computes a new
  685. // transform object that will have the indicated value
  686. // when seen from the other node.
  687. ////////////////////////////////////////////////////////////////////
  688. void NodePath::
  689. set_prev_transform(const NodePath &other, const TransformState *transform) {
  690. nassertv(_error_type == ET_ok && other._error_type == ET_ok);
  691. nassertv_always(!is_empty());
  692. // First, we perform a wrt to the parent, to get the conversion.
  693. CPT(TransformState) rel_trans;
  694. if (has_parent()) {
  695. rel_trans = other.get_prev_transform(get_parent());
  696. } else {
  697. rel_trans = other.get_prev_transform(NodePath());
  698. }
  699. CPT(TransformState) new_trans = rel_trans->compose(transform);
  700. set_prev_transform(new_trans);
  701. }
  702. ////////////////////////////////////////////////////////////////////
  703. // Function: NodePath::set_pos
  704. // Access: Published
  705. // Description: Sets the translation component of the transform,
  706. // leaving rotation and scale untouched. This also
  707. // resets the node's "previous" position, so that the
  708. // collision system will see the node as having suddenly
  709. // appeared in the new position, without passing any
  710. // points in between.
  711. // See Also: NodePath::set_fluid_pos
  712. ////////////////////////////////////////////////////////////////////
  713. void NodePath::
  714. set_pos(const LVecBase3f &pos) {
  715. nassertv_always(!is_empty());
  716. set_transform(get_transform()->set_pos(pos));
  717. node()->reset_prev_transform();
  718. }
  719. void NodePath::
  720. set_x(float x) {
  721. nassertv_always(!is_empty());
  722. LPoint3f pos = get_pos();
  723. pos[0] = x;
  724. set_pos(pos);
  725. }
  726. void NodePath::
  727. set_y(float y) {
  728. nassertv_always(!is_empty());
  729. LPoint3f pos = get_pos();
  730. pos[1] = y;
  731. set_pos(pos);
  732. }
  733. void NodePath::
  734. set_z(float z) {
  735. nassertv_always(!is_empty());
  736. LPoint3f pos = get_pos();
  737. pos[2] = z;
  738. set_pos(pos);
  739. }
  740. ////////////////////////////////////////////////////////////////////
  741. // Function: NodePath::set_fluid_pos
  742. // Access: Published
  743. // Description: Sets the translation component, without changing the
  744. // "previous" position, so that the collision system
  745. // will see the node as moving fluidly from its previous
  746. // position to its new position.
  747. // See Also: NodePath::set_pos
  748. ////////////////////////////////////////////////////////////////////
  749. void NodePath::
  750. set_fluid_pos(const LVecBase3f &pos) {
  751. nassertv_always(!is_empty());
  752. set_transform(get_transform()->set_pos(pos));
  753. }
  754. void NodePath::
  755. set_fluid_x(float x) {
  756. nassertv_always(!is_empty());
  757. LPoint3f pos = get_pos();
  758. pos[0] = x;
  759. set_fluid_pos(pos);
  760. }
  761. void NodePath::
  762. set_fluid_y(float y) {
  763. nassertv_always(!is_empty());
  764. LPoint3f pos = get_pos();
  765. pos[1] = y;
  766. set_fluid_pos(pos);
  767. }
  768. void NodePath::
  769. set_fluid_z(float z) {
  770. nassertv_always(!is_empty());
  771. LPoint3f pos = get_pos();
  772. pos[2] = z;
  773. set_fluid_pos(pos);
  774. }
  775. ////////////////////////////////////////////////////////////////////
  776. // Function: NodePath::get_pos
  777. // Access: Published
  778. // Description: Retrieves the translation component of the transform.
  779. ////////////////////////////////////////////////////////////////////
  780. LPoint3f NodePath::
  781. get_pos() const {
  782. nassertr_always(!is_empty(), LPoint3f(0.0f, 0.0f, 0.0f));
  783. return get_transform()->get_pos();
  784. }
  785. ////////////////////////////////////////////////////////////////////
  786. // Function: NodePath::get_pos_delta
  787. // Access: Published
  788. // Description: Returns the delta vector from this node's position in
  789. // the previous frame (according to
  790. // set_prev_transform(), typically set via the use of
  791. // set_fluid_pos()) and its position in the current
  792. // frame. This is the vector used to determine
  793. // collisions. Generally, if the node was last
  794. // repositioned via set_pos(), the delta will be zero;
  795. // if it was adjusted via set_fluid_pos(), the delta
  796. // will represent the change from the previous frame's
  797. // position.
  798. ////////////////////////////////////////////////////////////////////
  799. LVector3f NodePath::
  800. get_pos_delta() const {
  801. nassertr_always(!is_empty(), LPoint3f(0.0f, 0.0f, 0.0f));
  802. return get_transform()->get_pos() - get_prev_transform()->get_pos();
  803. }
  804. ////////////////////////////////////////////////////////////////////
  805. // Function: NodePath::set_hpr
  806. // Access: Published
  807. // Description: Sets the rotation component of the transform,
  808. // leaving translation and scale untouched.
  809. ////////////////////////////////////////////////////////////////////
  810. void NodePath::
  811. set_hpr(const LVecBase3f &hpr) {
  812. nassertv_always(!is_empty());
  813. CPT(TransformState) transform = get_transform();
  814. nassertv(transform->has_hpr());
  815. set_transform(transform->set_hpr(hpr));
  816. }
  817. void NodePath::
  818. set_h(float h) {
  819. nassertv_always(!is_empty());
  820. CPT(TransformState) transform = get_transform();
  821. nassertv(transform->has_hpr());
  822. LVecBase3f hpr = transform->get_hpr();
  823. hpr[0] = h;
  824. set_transform(transform->set_hpr(hpr));
  825. }
  826. void NodePath::
  827. set_p(float p) {
  828. nassertv_always(!is_empty());
  829. CPT(TransformState) transform = get_transform();
  830. nassertv(transform->has_hpr());
  831. LVecBase3f hpr = transform->get_hpr();
  832. hpr[1] = p;
  833. set_transform(transform->set_hpr(hpr));
  834. }
  835. void NodePath::
  836. set_r(float r) {
  837. nassertv_always(!is_empty());
  838. CPT(TransformState) transform = get_transform();
  839. nassertv(transform->has_hpr());
  840. LVecBase3f hpr = transform->get_hpr();
  841. hpr[2] = r;
  842. set_transform(transform->set_hpr(hpr));
  843. }
  844. ////////////////////////////////////////////////////////////////////
  845. // Function: NodePath::get_hpr
  846. // Access: Published
  847. // Description: Retrieves the rotation component of the transform.
  848. ////////////////////////////////////////////////////////////////////
  849. LVecBase3f NodePath::
  850. get_hpr() const {
  851. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  852. CPT(TransformState) transform = get_transform();
  853. nassertr(transform->has_hpr(), LVecBase3f(0.0f, 0.0f, 0.0f));
  854. return transform->get_hpr();
  855. }
  856. ////////////////////////////////////////////////////////////////////
  857. // Function: NodePath::set_quat
  858. // Access: Published
  859. // Description: Sets the rotation component of the transform,
  860. // leaving translation and scale untouched.
  861. ////////////////////////////////////////////////////////////////////
  862. void NodePath::
  863. set_quat(const LQuaternionf &quat) {
  864. nassertv_always(!is_empty());
  865. CPT(TransformState) transform = get_transform();
  866. set_transform(transform->set_quat(quat));
  867. }
  868. ////////////////////////////////////////////////////////////////////
  869. // Function: NodePath::get_quat
  870. // Access: Published
  871. // Description: Retrieves the rotation component of the transform.
  872. ////////////////////////////////////////////////////////////////////
  873. LQuaternionf NodePath::
  874. get_quat() const {
  875. nassertr_always(!is_empty(), LQuaternionf::ident_quat());
  876. CPT(TransformState) transform = get_transform();
  877. return transform->get_quat();
  878. }
  879. ////////////////////////////////////////////////////////////////////
  880. // Function: NodePath::set_scale
  881. // Access: Published
  882. // Description: Sets the scale component of the transform,
  883. // leaving translation and rotation untouched.
  884. ////////////////////////////////////////////////////////////////////
  885. void NodePath::
  886. set_scale(const LVecBase3f &scale) {
  887. nassertv_always(!is_empty());
  888. CPT(TransformState) transform = get_transform();
  889. set_transform(transform->set_scale(scale));
  890. }
  891. void NodePath::
  892. set_sx(float sx) {
  893. nassertv_always(!is_empty());
  894. CPT(TransformState) transform = get_transform();
  895. LVecBase3f scale = transform->get_scale();
  896. scale[0] = sx;
  897. set_transform(transform->set_scale(scale));
  898. }
  899. void NodePath::
  900. set_sy(float sy) {
  901. nassertv_always(!is_empty());
  902. CPT(TransformState) transform = get_transform();
  903. LVecBase3f scale = transform->get_scale();
  904. scale[1] = sy;
  905. set_transform(transform->set_scale(scale));
  906. }
  907. void NodePath::
  908. set_sz(float sz) {
  909. nassertv_always(!is_empty());
  910. CPT(TransformState) transform = get_transform();
  911. LVecBase3f scale = transform->get_scale();
  912. scale[2] = sz;
  913. set_transform(transform->set_scale(scale));
  914. }
  915. ////////////////////////////////////////////////////////////////////
  916. // Function: NodePath::get_scale
  917. // Access: Published
  918. // Description: Retrieves the scale component of the transform.
  919. ////////////////////////////////////////////////////////////////////
  920. LVecBase3f NodePath::
  921. get_scale() const {
  922. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  923. CPT(TransformState) transform = get_transform();
  924. return transform->get_scale();
  925. }
  926. ////////////////////////////////////////////////////////////////////
  927. // Function: NodePath::set_shear
  928. // Access: Published
  929. // Description: Sets the shear component of the transform,
  930. // leaving translation and rotation untouched.
  931. ////////////////////////////////////////////////////////////////////
  932. void NodePath::
  933. set_shear(const LVecBase3f &shear) {
  934. nassertv_always(!is_empty());
  935. CPT(TransformState) transform = get_transform();
  936. set_transform(transform->set_shear(shear));
  937. }
  938. void NodePath::
  939. set_shxy(float shxy) {
  940. nassertv_always(!is_empty());
  941. CPT(TransformState) transform = get_transform();
  942. LVecBase3f shear = transform->get_shear();
  943. shear[0] = shxy;
  944. set_transform(transform->set_shear(shear));
  945. }
  946. void NodePath::
  947. set_shxz(float shxz) {
  948. nassertv_always(!is_empty());
  949. CPT(TransformState) transform = get_transform();
  950. LVecBase3f shear = transform->get_shear();
  951. shear[1] = shxz;
  952. set_transform(transform->set_shear(shear));
  953. }
  954. void NodePath::
  955. set_shyz(float shyz) {
  956. nassertv_always(!is_empty());
  957. CPT(TransformState) transform = get_transform();
  958. LVecBase3f shear = transform->get_shear();
  959. shear[2] = shyz;
  960. set_transform(transform->set_shear(shear));
  961. }
  962. ////////////////////////////////////////////////////////////////////
  963. // Function: NodePath::get_shear
  964. // Access: Published
  965. // Description: Retrieves the shear component of the transform.
  966. ////////////////////////////////////////////////////////////////////
  967. LVecBase3f NodePath::
  968. get_shear() const {
  969. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  970. CPT(TransformState) transform = get_transform();
  971. return transform->get_shear();
  972. }
  973. ////////////////////////////////////////////////////////////////////
  974. // Function: NodePath::set_pos_hpr
  975. // Access: Published
  976. // Description: Sets the translation and rotation component of the
  977. // transform, leaving scale untouched.
  978. ////////////////////////////////////////////////////////////////////
  979. void NodePath::
  980. set_pos_hpr(const LVecBase3f &pos, const LVecBase3f &hpr) {
  981. nassertv_always(!is_empty());
  982. CPT(TransformState) transform = get_transform();
  983. transform = TransformState::make_pos_hpr_scale_shear
  984. (pos, hpr, transform->get_scale(), transform->get_shear());
  985. set_transform(transform);
  986. node()->reset_prev_transform();
  987. }
  988. ////////////////////////////////////////////////////////////////////
  989. // Function: NodePath::set_pos_quat
  990. // Access: Published
  991. // Description: Sets the translation and rotation component of the
  992. // transform, leaving scale untouched.
  993. ////////////////////////////////////////////////////////////////////
  994. void NodePath::
  995. set_pos_quat(const LVecBase3f &pos, const LQuaternionf &quat) {
  996. nassertv_always(!is_empty());
  997. CPT(TransformState) transform = get_transform();
  998. transform = TransformState::make_pos_quat_scale_shear
  999. (pos, quat, transform->get_scale(), transform->get_shear());
  1000. set_transform(transform);
  1001. node()->reset_prev_transform();
  1002. }
  1003. ////////////////////////////////////////////////////////////////////
  1004. // Function: NodePath::set_hpr_scale
  1005. // Access: Published
  1006. // Description: Sets the rotation and scale components of the
  1007. // transform, leaving translation untouched.
  1008. ////////////////////////////////////////////////////////////////////
  1009. void NodePath::
  1010. set_hpr_scale(const LVecBase3f &hpr, const LVecBase3f &scale) {
  1011. nassertv_always(!is_empty());
  1012. CPT(TransformState) transform = get_transform();
  1013. transform = TransformState::make_pos_hpr_scale_shear
  1014. (transform->get_pos(), hpr, scale, transform->get_shear());
  1015. set_transform(transform);
  1016. }
  1017. ////////////////////////////////////////////////////////////////////
  1018. // Function: NodePath::set_quat_scale
  1019. // Access: Published
  1020. // Description: Sets the rotation and scale components of the
  1021. // transform, leaving translation untouched.
  1022. ////////////////////////////////////////////////////////////////////
  1023. void NodePath::
  1024. set_quat_scale(const LQuaternionf &quat, const LVecBase3f &scale) {
  1025. nassertv_always(!is_empty());
  1026. CPT(TransformState) transform = get_transform();
  1027. transform = TransformState::make_pos_quat_scale_shear
  1028. (transform->get_pos(), quat, scale, transform->get_shear());
  1029. set_transform(transform);
  1030. }
  1031. ////////////////////////////////////////////////////////////////////
  1032. // Function: NodePath::set_pos_hpr_scale
  1033. // Access: Published
  1034. // Description: Replaces the translation, rotation, and scale
  1035. // components, implicitly setting shear to 0.
  1036. ////////////////////////////////////////////////////////////////////
  1037. void NodePath::
  1038. set_pos_hpr_scale(const LVecBase3f &pos, const LVecBase3f &hpr,
  1039. const LVecBase3f &scale) {
  1040. nassertv_always(!is_empty());
  1041. set_transform(TransformState::make_pos_hpr_scale
  1042. (pos, hpr, scale));
  1043. node()->reset_prev_transform();
  1044. }
  1045. ////////////////////////////////////////////////////////////////////
  1046. // Function: NodePath::set_pos_quat_scale
  1047. // Access: Published
  1048. // Description: Replaces the translation, rotation, and scale
  1049. // components, implicitly setting shear to 0.
  1050. ////////////////////////////////////////////////////////////////////
  1051. void NodePath::
  1052. set_pos_quat_scale(const LVecBase3f &pos, const LQuaternionf &quat,
  1053. const LVecBase3f &scale) {
  1054. nassertv_always(!is_empty());
  1055. set_transform(TransformState::make_pos_quat_scale
  1056. (pos, quat, scale));
  1057. node()->reset_prev_transform();
  1058. }
  1059. ////////////////////////////////////////////////////////////////////
  1060. // Function: NodePath::set_pos_hpr_scale_shear
  1061. // Access: Published
  1062. // Description: Completely replaces the transform with new
  1063. // translation, rotation, scale, and shear components.
  1064. ////////////////////////////////////////////////////////////////////
  1065. void NodePath::
  1066. set_pos_hpr_scale_shear(const LVecBase3f &pos, const LVecBase3f &hpr,
  1067. const LVecBase3f &scale, const LVecBase3f &shear) {
  1068. nassertv_always(!is_empty());
  1069. set_transform(TransformState::make_pos_hpr_scale_shear
  1070. (pos, hpr, scale, shear));
  1071. node()->reset_prev_transform();
  1072. }
  1073. ////////////////////////////////////////////////////////////////////
  1074. // Function: NodePath::set_pos_quat_scale_shear
  1075. // Access: Published
  1076. // Description: Completely replaces the transform with new
  1077. // translation, rotation, scale, and shear components.
  1078. ////////////////////////////////////////////////////////////////////
  1079. void NodePath::
  1080. set_pos_quat_scale_shear(const LVecBase3f &pos, const LQuaternionf &quat,
  1081. const LVecBase3f &scale, const LVecBase3f &shear) {
  1082. nassertv_always(!is_empty());
  1083. set_transform(TransformState::make_pos_quat_scale_shear
  1084. (pos, quat, scale, shear));
  1085. node()->reset_prev_transform();
  1086. }
  1087. ////////////////////////////////////////////////////////////////////
  1088. // Function: NodePath::set_mat
  1089. // Access: Published
  1090. // Description: Directly sets an arbitrary 4x4 transform matrix.
  1091. ////////////////////////////////////////////////////////////////////
  1092. void NodePath::
  1093. set_mat(const LMatrix4f &mat) {
  1094. nassertv_always(!is_empty());
  1095. set_transform(TransformState::make_mat(mat));
  1096. node()->reset_prev_transform();
  1097. }
  1098. ////////////////////////////////////////////////////////////////////
  1099. // Function: NodePath::look_at
  1100. // Access: Published
  1101. // Description: Sets the hpr on this NodePath so that it
  1102. // rotates to face the indicated point in space.
  1103. ////////////////////////////////////////////////////////////////////
  1104. void NodePath::
  1105. look_at(const LPoint3f &point, const LVector3f &up) {
  1106. nassertv_always(!is_empty());
  1107. LPoint3f pos = get_pos();
  1108. LQuaternionf quat;
  1109. ::look_at(quat, point - pos, up);
  1110. set_quat(quat);
  1111. }
  1112. ////////////////////////////////////////////////////////////////////
  1113. // Function: NodePath::heads_up
  1114. // Access: Published
  1115. // Description: Behaves like look_at(), but with a strong preference
  1116. // to keeping the up vector oriented in the indicated
  1117. // "up" direction.
  1118. ////////////////////////////////////////////////////////////////////
  1119. void NodePath::
  1120. heads_up(const LPoint3f &point, const LVector3f &up) {
  1121. nassertv_always(!is_empty());
  1122. LPoint3f pos = get_pos();
  1123. LQuaternionf quat;
  1124. ::heads_up(quat, point - pos, up);
  1125. set_quat(quat);
  1126. }
  1127. ////////////////////////////////////////////////////////////////////
  1128. // Function: NodePath::set_pos
  1129. // Access: Published
  1130. // Description: Sets the translation component of the transform,
  1131. // relative to the other node.
  1132. ////////////////////////////////////////////////////////////////////
  1133. void NodePath::
  1134. set_pos(const NodePath &other, const LVecBase3f &pos) {
  1135. nassertv_always(!is_empty());
  1136. CPT(TransformState) rel_transform = get_transform(other);
  1137. CPT(TransformState) orig_transform = get_transform();
  1138. if (orig_transform->has_components()) {
  1139. // If we had a componentwise transform before we started, we
  1140. // should be careful to preserve the other three components. We
  1141. // wouldn't need to do this, except for the possibility of
  1142. // numerical error or decompose ambiguity.
  1143. const LVecBase3f &orig_hpr = orig_transform->get_hpr();
  1144. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1145. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1146. set_transform(other, rel_transform->set_pos(pos));
  1147. set_pos_hpr_scale_shear(get_transform()->get_pos(), orig_hpr, orig_scale, orig_shear);
  1148. } else {
  1149. // If we didn't have a componentwise transform already, never
  1150. // mind.
  1151. set_transform(other, rel_transform->set_pos(pos));
  1152. }
  1153. node()->reset_prev_transform();
  1154. }
  1155. void NodePath::
  1156. set_x(const NodePath &other, float x) {
  1157. nassertv_always(!is_empty());
  1158. LPoint3f pos = get_pos(other);
  1159. pos[0] = x;
  1160. set_pos(other, pos);
  1161. }
  1162. void NodePath::
  1163. set_y(const NodePath &other, float y) {
  1164. nassertv_always(!is_empty());
  1165. LPoint3f pos = get_pos(other);
  1166. pos[1] = y;
  1167. set_pos(other, pos);
  1168. }
  1169. void NodePath::
  1170. set_z(const NodePath &other, float z) {
  1171. nassertv_always(!is_empty());
  1172. LPoint3f pos = get_pos(other);
  1173. pos[2] = z;
  1174. set_pos(other, pos);
  1175. }
  1176. ////////////////////////////////////////////////////////////////////
  1177. // Function: NodePath::set_fluid_pos
  1178. // Access: Published
  1179. // Description: Sets the translation component of the transform,
  1180. // relative to the other node.
  1181. ////////////////////////////////////////////////////////////////////
  1182. void NodePath::
  1183. set_fluid_pos(const NodePath &other, const LVecBase3f &pos) {
  1184. nassertv_always(!is_empty());
  1185. CPT(TransformState) rel_transform = get_transform(other);
  1186. CPT(TransformState) orig_transform = get_transform();
  1187. if (orig_transform->has_components()) {
  1188. // If we had a componentwise transform before we started, we
  1189. // should be careful to preserve the other three components. We
  1190. // wouldn't need to do this, except for the possibility of
  1191. // numerical error or decompose ambiguity.
  1192. const LVecBase3f &orig_hpr = orig_transform->get_hpr();
  1193. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1194. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1195. // Use the relative set_transform() to compute the relative pos, and
  1196. // then reset all of the other components back to the way they were.
  1197. set_transform(other, rel_transform->set_pos(pos));
  1198. set_transform(TransformState::make_pos_hpr_scale_shear
  1199. (get_transform()->get_pos(), orig_hpr, orig_scale, orig_shear));
  1200. } else {
  1201. // If we didn't have a componentwise transform already, never
  1202. // mind.
  1203. set_transform(other, rel_transform->set_pos(pos));
  1204. }
  1205. }
  1206. void NodePath::
  1207. set_fluid_x(const NodePath &other, float x) {
  1208. nassertv_always(!is_empty());
  1209. LPoint3f pos = get_pos(other);
  1210. pos[0] = x;
  1211. set_fluid_pos(other, pos);
  1212. }
  1213. void NodePath::
  1214. set_fluid_y(const NodePath &other, float y) {
  1215. nassertv_always(!is_empty());
  1216. LPoint3f pos = get_pos(other);
  1217. pos[1] = y;
  1218. set_fluid_pos(other, pos);
  1219. }
  1220. void NodePath::
  1221. set_fluid_z(const NodePath &other, float z) {
  1222. nassertv_always(!is_empty());
  1223. LPoint3f pos = get_pos(other);
  1224. pos[2] = z;
  1225. set_fluid_pos(other, pos);
  1226. }
  1227. ////////////////////////////////////////////////////////////////////
  1228. // Function: NodePath::get_pos
  1229. // Access: Published
  1230. // Description: Returns the relative position of the referenced node
  1231. // as seen from the other node.
  1232. ////////////////////////////////////////////////////////////////////
  1233. LPoint3f NodePath::
  1234. get_pos(const NodePath &other) const {
  1235. nassertr_always(!is_empty(), LPoint3f(0.0f, 0.0f, 0.0f));
  1236. return get_transform(other)->get_pos();
  1237. }
  1238. ////////////////////////////////////////////////////////////////////
  1239. // Function: NodePath::get_pos_delta
  1240. // Access: Published
  1241. // Description: Returns the delta vector from this node's position in
  1242. // the previous frame (according to
  1243. // set_prev_transform(), typically set via the use of
  1244. // set_fluid_pos()) and its position in the current
  1245. // frame, as seen in the indicated node's coordinate
  1246. // space. This is the vector used to determine
  1247. // collisions. Generally, if the node was last
  1248. // repositioned via set_pos(), the delta will be zero;
  1249. // if it was adjusted via set_fluid_pos(), the delta
  1250. // will represent the change from the previous frame's
  1251. // position.
  1252. ////////////////////////////////////////////////////////////////////
  1253. LVector3f NodePath::
  1254. get_pos_delta(const NodePath &other) const {
  1255. nassertr_always(!is_empty(), LPoint3f(0.0f, 0.0f, 0.0f));
  1256. return get_transform(other)->get_pos() - get_prev_transform(other)->get_pos();
  1257. }
  1258. ////////////////////////////////////////////////////////////////////
  1259. // Function: NodePath::set_hpr
  1260. // Access: Published
  1261. // Description: Sets the rotation component of the transform,
  1262. // relative to the other node.
  1263. ////////////////////////////////////////////////////////////////////
  1264. void NodePath::
  1265. set_hpr(const NodePath &other, const LVecBase3f &hpr) {
  1266. nassertv_always(!is_empty());
  1267. CPT(TransformState) rel_transform = get_transform(other);
  1268. nassertv(rel_transform->has_hpr());
  1269. CPT(TransformState) orig_transform = get_transform();
  1270. if (orig_transform->has_components()) {
  1271. // If we had a componentwise transform before we started, we
  1272. // should be careful to preserve the other three components. We
  1273. // wouldn't need to do this, except for the possibility of
  1274. // numerical error or decompose ambiguity.
  1275. const LVecBase3f &orig_pos = orig_transform->get_pos();
  1276. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1277. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1278. set_transform(other, rel_transform->set_hpr(hpr));
  1279. const TransformState *new_transform = get_transform();
  1280. if (new_transform->has_components()) {
  1281. set_transform(TransformState::make_pos_hpr_scale_shear
  1282. (orig_pos, new_transform->get_hpr(), orig_scale, orig_shear));
  1283. }
  1284. } else {
  1285. // If we didn't have a componentwise transform already, never
  1286. // mind.
  1287. set_transform(other, rel_transform->set_hpr(hpr));
  1288. }
  1289. }
  1290. void NodePath::
  1291. set_h(const NodePath &other, float h) {
  1292. nassertv_always(!is_empty());
  1293. LVecBase3f hpr = get_hpr(other);
  1294. hpr[0] = h;
  1295. set_hpr(other, hpr);
  1296. }
  1297. void NodePath::
  1298. set_p(const NodePath &other, float p) {
  1299. nassertv_always(!is_empty());
  1300. LVecBase3f hpr = get_hpr(other);
  1301. hpr[1] = p;
  1302. set_hpr(other, hpr);
  1303. }
  1304. void NodePath::
  1305. set_r(const NodePath &other, float r) {
  1306. nassertv_always(!is_empty());
  1307. LVecBase3f hpr = get_hpr(other);
  1308. hpr[2] = r;
  1309. set_hpr(other, hpr);
  1310. }
  1311. ////////////////////////////////////////////////////////////////////
  1312. // Function: NodePath::get_hpr
  1313. // Access: Published
  1314. // Description: Returns the relative orientation of the bottom node
  1315. // as seen from the other node.
  1316. ////////////////////////////////////////////////////////////////////
  1317. LVecBase3f NodePath::
  1318. get_hpr(const NodePath &other) const {
  1319. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  1320. CPT(TransformState) transform = get_transform(other);
  1321. nassertr(transform->has_hpr(), LVecBase3f(0.0f, 0.0f, 0.0f));
  1322. return transform->get_hpr();
  1323. }
  1324. ////////////////////////////////////////////////////////////////////
  1325. // Function: NodePath::set_quat
  1326. // Access: Published
  1327. // Description: Sets the rotation component of the transform,
  1328. // relative to the other node.
  1329. ////////////////////////////////////////////////////////////////////
  1330. void NodePath::
  1331. set_quat(const NodePath &other, const LQuaternionf &quat) {
  1332. nassertv_always(!is_empty());
  1333. CPT(TransformState) rel_transform = get_transform(other);
  1334. CPT(TransformState) orig_transform = get_transform();
  1335. if (orig_transform->has_components()) {
  1336. // If we had a componentwise transform before we started, we
  1337. // should be careful to preserve the other three components. We
  1338. // wouldn't need to do this, except for the possibility of
  1339. // numerical error or decompose ambiguity.
  1340. const LVecBase3f &orig_pos = orig_transform->get_pos();
  1341. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1342. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1343. set_transform(other, rel_transform->set_quat(quat));
  1344. const TransformState *new_transform = get_transform();
  1345. if (new_transform->has_components()) {
  1346. set_transform(TransformState::make_pos_quat_scale_shear
  1347. (orig_pos, new_transform->get_quat(), orig_scale, orig_shear));
  1348. }
  1349. } else {
  1350. // If we didn't have a componentwise transform already, never
  1351. // mind.
  1352. set_transform(other, rel_transform->set_quat(quat));
  1353. }
  1354. }
  1355. ////////////////////////////////////////////////////////////////////
  1356. // Function: NodePath::get_quat
  1357. // Access: Published
  1358. // Description: Returns the relative orientation of the bottom node
  1359. // as seen from the other node.
  1360. ////////////////////////////////////////////////////////////////////
  1361. LQuaternionf NodePath::
  1362. get_quat(const NodePath &other) const {
  1363. nassertr_always(!is_empty(), LQuaternionf::ident_quat());
  1364. CPT(TransformState) transform = get_transform(other);
  1365. return transform->get_quat();
  1366. }
  1367. ////////////////////////////////////////////////////////////////////
  1368. // Function: NodePath::set_scale
  1369. // Access: Published
  1370. // Description: Sets the scale component of the transform,
  1371. // relative to the other node.
  1372. ////////////////////////////////////////////////////////////////////
  1373. void NodePath::
  1374. set_scale(const NodePath &other, const LVecBase3f &scale) {
  1375. nassertv_always(!is_empty());
  1376. CPT(TransformState) rel_transform = get_transform(other);
  1377. CPT(TransformState) orig_transform = get_transform();
  1378. if (orig_transform->has_components()) {
  1379. // If we had a componentwise transform before we started, we
  1380. // should be careful to preserve the other three components. We
  1381. // wouldn't need to do this, except for the possibility of
  1382. // numerical error or decompose ambiguity.
  1383. const LVecBase3f &orig_pos = orig_transform->get_pos();
  1384. const LVecBase3f &orig_hpr = orig_transform->get_hpr();
  1385. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1386. set_transform(other, rel_transform->set_scale(scale));
  1387. const TransformState *new_transform = get_transform();
  1388. if (new_transform->has_components()) {
  1389. set_transform(TransformState::make_pos_hpr_scale_shear
  1390. (orig_pos, orig_hpr, new_transform->get_scale(), orig_shear));
  1391. }
  1392. } else {
  1393. // If we didn't have a componentwise transform already, never
  1394. // mind.
  1395. set_transform(other, rel_transform->set_scale(scale));
  1396. }
  1397. }
  1398. void NodePath::
  1399. set_sx(const NodePath &other, float sx) {
  1400. nassertv_always(!is_empty());
  1401. LVecBase3f scale = get_scale(other);
  1402. scale[0] = sx;
  1403. set_scale(other, scale);
  1404. }
  1405. void NodePath::
  1406. set_sy(const NodePath &other, float sy) {
  1407. nassertv_always(!is_empty());
  1408. LVecBase3f scale = get_scale(other);
  1409. scale[1] = sy;
  1410. set_scale(other, scale);
  1411. }
  1412. void NodePath::
  1413. set_sz(const NodePath &other, float sz) {
  1414. nassertv_always(!is_empty());
  1415. LVecBase3f scale = get_scale(other);
  1416. scale[2] = sz;
  1417. set_scale(other, scale);
  1418. }
  1419. ////////////////////////////////////////////////////////////////////
  1420. // Function: NodePath::get_scale
  1421. // Access: Published
  1422. // Description: Returns the relative scale of the bottom node
  1423. // as seen from the other node.
  1424. ////////////////////////////////////////////////////////////////////
  1425. LVecBase3f NodePath::
  1426. get_scale(const NodePath &other) const {
  1427. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  1428. CPT(TransformState) transform = get_transform(other);
  1429. return transform->get_scale();
  1430. }
  1431. ////////////////////////////////////////////////////////////////////
  1432. // Function: NodePath::set_shear
  1433. // Access: Published
  1434. // Description: Sets the shear component of the transform,
  1435. // relative to the other node.
  1436. ////////////////////////////////////////////////////////////////////
  1437. void NodePath::
  1438. set_shear(const NodePath &other, const LVecBase3f &shear) {
  1439. nassertv_always(!is_empty());
  1440. CPT(TransformState) rel_transform = get_transform(other);
  1441. CPT(TransformState) orig_transform = get_transform();
  1442. if (orig_transform->has_components()) {
  1443. // If we had a componentwise transform before we started, we
  1444. // should be careful to preserve the other three components. We
  1445. // wouldn't need to do this, except for the possibility of
  1446. // numerical error or decompose ambiguity.
  1447. const LVecBase3f &orig_pos = orig_transform->get_pos();
  1448. const LVecBase3f &orig_hpr = orig_transform->get_hpr();
  1449. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1450. set_transform(other, rel_transform->set_shear(shear));
  1451. const TransformState *new_transform = get_transform();
  1452. if (new_transform->has_components()) {
  1453. set_transform(TransformState::make_pos_hpr_scale_shear
  1454. (orig_pos, orig_hpr, orig_scale, new_transform->get_shear()));
  1455. }
  1456. } else {
  1457. // If we didn't have a componentwise transform already, never
  1458. // mind.
  1459. set_transform(other, rel_transform->set_shear(shear));
  1460. }
  1461. }
  1462. void NodePath::
  1463. set_shxy(const NodePath &other, float shxy) {
  1464. nassertv_always(!is_empty());
  1465. LVecBase3f shear = get_shear(other);
  1466. shear[0] = shxy;
  1467. set_shear(other, shear);
  1468. }
  1469. void NodePath::
  1470. set_shxz(const NodePath &other, float shxz) {
  1471. nassertv_always(!is_empty());
  1472. LVecBase3f shear = get_shear(other);
  1473. shear[1] = shxz;
  1474. set_shear(other, shear);
  1475. }
  1476. void NodePath::
  1477. set_shyz(const NodePath &other, float shyz) {
  1478. nassertv_always(!is_empty());
  1479. LVecBase3f shear = get_shear(other);
  1480. shear[2] = shyz;
  1481. set_shear(other, shear);
  1482. }
  1483. ////////////////////////////////////////////////////////////////////
  1484. // Function: NodePath::get_shear
  1485. // Access: Published
  1486. // Description: Returns the relative shear of the bottom node
  1487. // as seen from the other node.
  1488. ////////////////////////////////////////////////////////////////////
  1489. LVecBase3f NodePath::
  1490. get_shear(const NodePath &other) const {
  1491. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  1492. CPT(TransformState) transform = get_transform(other);
  1493. return transform->get_shear();
  1494. }
  1495. ////////////////////////////////////////////////////////////////////
  1496. // Function: NodePath::set_pos_hpr
  1497. // Access: Published
  1498. // Description: Sets the translation and rotation component of the
  1499. // transform, relative to the other node.
  1500. ////////////////////////////////////////////////////////////////////
  1501. void NodePath::
  1502. set_pos_hpr(const NodePath &other, const LVecBase3f &pos,
  1503. const LVecBase3f &hpr) {
  1504. nassertv_always(!is_empty());
  1505. CPT(TransformState) rel_transform = get_transform(other);
  1506. CPT(TransformState) orig_transform = get_transform();
  1507. if (orig_transform->has_components()) {
  1508. // If we had a componentwise transform before we started, we
  1509. // should be careful to preserve the other two components. We
  1510. // wouldn't need to do this, except for the possibility of
  1511. // numerical error or decompose ambiguity.
  1512. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1513. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1514. set_transform(other, TransformState::make_pos_hpr_scale_shear
  1515. (pos, hpr, rel_transform->get_scale(), rel_transform->get_shear()));
  1516. const TransformState *new_transform = get_transform();
  1517. if (new_transform->has_components()) {
  1518. set_pos_hpr_scale_shear(new_transform->get_pos(), new_transform->get_hpr(),
  1519. orig_scale, orig_shear);
  1520. }
  1521. } else {
  1522. // If we didn't have a componentwise transform already, never
  1523. // mind.
  1524. set_transform(other, TransformState::make_pos_hpr_scale_shear
  1525. (pos, hpr, rel_transform->get_scale(), rel_transform->get_shear()));
  1526. node()->reset_prev_transform();
  1527. }
  1528. }
  1529. ////////////////////////////////////////////////////////////////////
  1530. // Function: NodePath::set_pos_quat
  1531. // Access: Published
  1532. // Description: Sets the translation and rotation component of the
  1533. // transform, relative to the other node.
  1534. ////////////////////////////////////////////////////////////////////
  1535. void NodePath::
  1536. set_pos_quat(const NodePath &other, const LVecBase3f &pos,
  1537. const LQuaternionf &quat) {
  1538. nassertv_always(!is_empty());
  1539. CPT(TransformState) rel_transform = get_transform(other);
  1540. CPT(TransformState) orig_transform = get_transform();
  1541. if (orig_transform->has_components()) {
  1542. // If we had a componentwise transform before we started, we
  1543. // should be careful to preserve the other two components. We
  1544. // wouldn't need to do this, except for the possibility of
  1545. // numerical error or decompose ambiguity.
  1546. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1547. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1548. set_transform(other, TransformState::make_pos_quat_scale_shear
  1549. (pos, quat, rel_transform->get_scale(), rel_transform->get_shear()));
  1550. const TransformState *new_transform = get_transform();
  1551. if (new_transform->has_components()) {
  1552. set_pos_quat_scale_shear(new_transform->get_pos(), new_transform->get_quat(),
  1553. orig_scale, orig_shear);
  1554. }
  1555. } else {
  1556. // If we didn't have a componentwise transform already, never
  1557. // mind.
  1558. set_transform(other, TransformState::make_pos_quat_scale_shear
  1559. (pos, quat, rel_transform->get_scale(), rel_transform->get_shear()));
  1560. node()->reset_prev_transform();
  1561. }
  1562. }
  1563. ////////////////////////////////////////////////////////////////////
  1564. // Function: NodePath::set_hpr_scale
  1565. // Access: Published
  1566. // Description: Sets the rotation and scale components of the
  1567. // transform, leaving translation untouched. This, or
  1568. // set_pos_hpr_scale, is the preferred way to update a
  1569. // transform when both hpr and scale are to be changed.
  1570. ////////////////////////////////////////////////////////////////////
  1571. void NodePath::
  1572. set_hpr_scale(const NodePath &other, const LVecBase3f &hpr, const LVecBase3f &scale) {
  1573. // We don't bother trying very hard to preserve pos across this
  1574. // operation, unlike the work we do above to preserve hpr or scale,
  1575. // since it generally doesn't matter that much if pos is off by a
  1576. // few thousandths.
  1577. nassertv_always(!is_empty());
  1578. CPT(TransformState) transform = get_transform(other);
  1579. transform = TransformState::make_pos_hpr_scale_shear
  1580. (transform->get_pos(), hpr, scale, transform->get_shear());
  1581. set_transform(other, transform);
  1582. }
  1583. ////////////////////////////////////////////////////////////////////
  1584. // Function: NodePath::set_quat_scale
  1585. // Access: Published
  1586. // Description: Sets the rotation and scale components of the
  1587. // transform, leaving translation untouched. This, or
  1588. // set_pos_quat_scale, is the preferred way to update a
  1589. // transform when both quat and scale are to be changed.
  1590. ////////////////////////////////////////////////////////////////////
  1591. void NodePath::
  1592. set_quat_scale(const NodePath &other, const LQuaternionf &quat,
  1593. const LVecBase3f &scale) {
  1594. // We don't bother trying very hard to preserve pos across this
  1595. // operation, unlike the work we do above to preserve quat or scale,
  1596. // since it generally doesn't matter that much if pos is off by a
  1597. // few thousandths.
  1598. nassertv_always(!is_empty());
  1599. CPT(TransformState) transform = get_transform(other);
  1600. transform = TransformState::make_pos_quat_scale_shear
  1601. (transform->get_pos(), quat, scale, transform->get_shear());
  1602. set_transform(other, transform);
  1603. }
  1604. ////////////////////////////////////////////////////////////////////
  1605. // Function: NodePath::set_pos_hpr_scale
  1606. // Access: Published
  1607. // Description: Completely replaces the transform with new
  1608. // translation, rotation, and scale components, relative
  1609. // to the other node, implicitly setting shear to 0.
  1610. ////////////////////////////////////////////////////////////////////
  1611. void NodePath::
  1612. set_pos_hpr_scale(const NodePath &other,
  1613. const LVecBase3f &pos, const LVecBase3f &hpr,
  1614. const LVecBase3f &scale) {
  1615. nassertv_always(!is_empty());
  1616. set_transform(other, TransformState::make_pos_hpr_scale
  1617. (pos, hpr, scale));
  1618. node()->reset_prev_transform();
  1619. }
  1620. ////////////////////////////////////////////////////////////////////
  1621. // Function: NodePath::set_pos_quat_scale
  1622. // Access: Published
  1623. // Description: Completely replaces the transform with new
  1624. // translation, rotation, and scale components, relative
  1625. // to the other node, implicitly setting shear to 0.
  1626. ////////////////////////////////////////////////////////////////////
  1627. void NodePath::
  1628. set_pos_quat_scale(const NodePath &other,
  1629. const LVecBase3f &pos, const LQuaternionf &quat,
  1630. const LVecBase3f &scale) {
  1631. nassertv_always(!is_empty());
  1632. set_transform(other, TransformState::make_pos_quat_scale
  1633. (pos, quat, scale));
  1634. node()->reset_prev_transform();
  1635. }
  1636. ////////////////////////////////////////////////////////////////////
  1637. // Function: NodePath::set_pos_hpr_scale_shear
  1638. // Access: Published
  1639. // Description: Completely replaces the transform with new
  1640. // translation, rotation, scale, and shear components,
  1641. // relative to the other node.
  1642. ////////////////////////////////////////////////////////////////////
  1643. void NodePath::
  1644. set_pos_hpr_scale_shear(const NodePath &other,
  1645. const LVecBase3f &pos, const LVecBase3f &hpr,
  1646. const LVecBase3f &scale, const LVecBase3f &shear) {
  1647. nassertv_always(!is_empty());
  1648. set_transform(other, TransformState::make_pos_hpr_scale_shear
  1649. (pos, hpr, scale, shear));
  1650. node()->reset_prev_transform();
  1651. }
  1652. ////////////////////////////////////////////////////////////////////
  1653. // Function: NodePath::set_pos_quat_scale_shear
  1654. // Access: Published
  1655. // Description: Completely replaces the transform with new
  1656. // translation, rotation, scale, and shear components,
  1657. // relative to the other node.
  1658. ////////////////////////////////////////////////////////////////////
  1659. void NodePath::
  1660. set_pos_quat_scale_shear(const NodePath &other,
  1661. const LVecBase3f &pos, const LQuaternionf &quat,
  1662. const LVecBase3f &scale, const LVecBase3f &shear) {
  1663. nassertv_always(!is_empty());
  1664. set_transform(other, TransformState::make_pos_quat_scale_shear
  1665. (pos, quat, scale, shear));
  1666. node()->reset_prev_transform();
  1667. }
  1668. ////////////////////////////////////////////////////////////////////
  1669. // Function: NodePath::get_mat
  1670. // Access: Published
  1671. // Description: Returns the matrix that describes the coordinate
  1672. // space of the bottom node, relative to the other
  1673. // path's bottom node's coordinate space.
  1674. ////////////////////////////////////////////////////////////////////
  1675. LMatrix4f NodePath::
  1676. get_mat(const NodePath &other) const {
  1677. CPT(TransformState) transform = get_transform(other);
  1678. // We can't safely return a reference to the matrix, because we
  1679. // can't assume the transform won't go away when the function
  1680. // returns. If the transform was partially modified by, say, a
  1681. // CompassEffect, it won't be stored in the cache, and thus we might
  1682. // have the only reference to it.
  1683. return transform->get_mat();
  1684. }
  1685. ////////////////////////////////////////////////////////////////////
  1686. // Function: NodePath::set_mat
  1687. // Access: Published
  1688. // Description: Converts the indicated matrix from the other's
  1689. // coordinate space to the local coordinate space, and
  1690. // applies it to the node.
  1691. ////////////////////////////////////////////////////////////////////
  1692. void NodePath::
  1693. set_mat(const NodePath &other, const LMatrix4f &mat) {
  1694. nassertv_always(!is_empty());
  1695. set_transform(other, TransformState::make_mat(mat));
  1696. node()->reset_prev_transform();
  1697. }
  1698. ////////////////////////////////////////////////////////////////////
  1699. // Function: NodePath::get_relative_point
  1700. // Access: Published
  1701. // Description: Given that the indicated point is in the coordinate
  1702. // system of the other node, returns the same point in
  1703. // this node's coordinate system.
  1704. ////////////////////////////////////////////////////////////////////
  1705. LPoint3f NodePath::
  1706. get_relative_point(const NodePath &other, const LVecBase3f &point) const {
  1707. CPT(TransformState) transform = other.get_transform(*this);
  1708. LPoint3f rel_point = LPoint3f(point) * transform->get_mat();
  1709. return rel_point;
  1710. }
  1711. ////////////////////////////////////////////////////////////////////
  1712. // Function: NodePath::get_relative_vector
  1713. // Access: Published
  1714. // Description: Given that the indicated vector is in the coordinate
  1715. // system of the other node, returns the same vector in
  1716. // this node's coordinate system.
  1717. ////////////////////////////////////////////////////////////////////
  1718. LVector3f NodePath::
  1719. get_relative_vector(const NodePath &other, const LVecBase3f &vec) const {
  1720. CPT(TransformState) transform = other.get_transform(*this);
  1721. LVector3f rel_vector = LVector3f(vec) * transform->get_mat();
  1722. return rel_vector;
  1723. }
  1724. ////////////////////////////////////////////////////////////////////
  1725. // Function: NodePath::look_at
  1726. // Access: Published
  1727. // Description: Sets the transform on this NodePath so that it
  1728. // rotates to face the indicated point in space, which
  1729. // is relative to the other NodePath.
  1730. ////////////////////////////////////////////////////////////////////
  1731. void NodePath::
  1732. look_at(const NodePath &other, const LPoint3f &point, const LVector3f &up) {
  1733. nassertv_always(!is_empty());
  1734. CPT(TransformState) transform = other.get_transform(get_parent());
  1735. LPoint3f rel_point = point * transform->get_mat();
  1736. LPoint3f pos = get_pos();
  1737. LQuaternionf quat;
  1738. ::look_at(quat, rel_point - pos, up);
  1739. set_quat(quat);
  1740. }
  1741. ////////////////////////////////////////////////////////////////////
  1742. // Function: NodePath::heads_up
  1743. // Access: Published
  1744. // Description: Behaves like look_at(), but with a strong preference
  1745. // to keeping the up vector oriented in the indicated
  1746. // "up" direction.
  1747. ////////////////////////////////////////////////////////////////////
  1748. void NodePath::
  1749. heads_up(const NodePath &other, const LPoint3f &point, const LVector3f &up) {
  1750. nassertv_always(!is_empty());
  1751. CPT(TransformState) transform = other.get_transform(get_parent());
  1752. LPoint3f rel_point = point * transform->get_mat();
  1753. LPoint3f pos = get_pos();
  1754. LQuaternionf quat;
  1755. ::heads_up(quat, rel_point - pos, up);
  1756. set_quat(quat);
  1757. }
  1758. ////////////////////////////////////////////////////////////////////
  1759. // Function: NodePath::set_color
  1760. // Access: Published
  1761. // Description: Applies a scene-graph color to the referenced node.
  1762. // This color will apply to all geometry at this level
  1763. // and below (that does not specify a new color or a
  1764. // set_color_off()).
  1765. ////////////////////////////////////////////////////////////////////
  1766. void NodePath::
  1767. set_color(float r, float g, float b, float a,
  1768. int priority) {
  1769. set_color(Colorf(r, g, b, a), priority);
  1770. }
  1771. ////////////////////////////////////////////////////////////////////
  1772. // Function: NodePath::set_color
  1773. // Access: Published
  1774. // Description: Applies a scene-graph color to the referenced node.
  1775. // This color will apply to all geometry at this level
  1776. // and below (that does not specify a new color or a
  1777. // set_color_off()).
  1778. ////////////////////////////////////////////////////////////////////
  1779. void NodePath::
  1780. set_color(const Colorf &color, int priority) {
  1781. nassertv_always(!is_empty());
  1782. node()->set_attrib(ColorAttrib::make_flat(color), priority);
  1783. }
  1784. ////////////////////////////////////////////////////////////////////
  1785. // Function: NodePath::set_color_off
  1786. // Access: Published
  1787. // Description: Sets the geometry at this level and below to render
  1788. // using the geometry color. This is normally the
  1789. // default, but it may be useful to use this to
  1790. // contradict set_color() at a higher node level (or,
  1791. // with a priority, to override a set_color() at a lower
  1792. // level).
  1793. ////////////////////////////////////////////////////////////////////
  1794. void NodePath::
  1795. set_color_off(int priority) {
  1796. nassertv_always(!is_empty());
  1797. node()->set_attrib(ColorAttrib::make_vertex(), priority);
  1798. }
  1799. ////////////////////////////////////////////////////////////////////
  1800. // Function: NodePath::clear_color
  1801. // Access: Published
  1802. // Description: Completely removes any color adjustment from the node.
  1803. // This allows the natural color of the geometry, or
  1804. // whatever color transitions might be otherwise
  1805. // affecting the geometry, to show instead.
  1806. ////////////////////////////////////////////////////////////////////
  1807. void NodePath::
  1808. clear_color() {
  1809. nassertv_always(!is_empty());
  1810. node()->clear_attrib(ColorAttrib::get_class_type());
  1811. }
  1812. ////////////////////////////////////////////////////////////////////
  1813. // Function: NodePath::has_color
  1814. // Access: Published
  1815. // Description: Returns true if a color has been applied to the given
  1816. // node, false otherwise.
  1817. ////////////////////////////////////////////////////////////////////
  1818. bool NodePath::
  1819. has_color() const {
  1820. nassertr_always(!is_empty(), false);
  1821. return node()->has_attrib(ColorAttrib::get_class_type());
  1822. }
  1823. ////////////////////////////////////////////////////////////////////
  1824. // Function: NodePath::get_color
  1825. // Access: Published
  1826. // Description: Returns the color that has been assigned to the node,
  1827. // or black if no color has been assigned.
  1828. ////////////////////////////////////////////////////////////////////
  1829. Colorf NodePath::
  1830. get_color() const {
  1831. nassertr_always(!is_empty(), false);
  1832. const RenderAttrib *attrib =
  1833. node()->get_attrib(ColorAttrib::get_class_type());
  1834. if (attrib != (const RenderAttrib *)NULL) {
  1835. const ColorAttrib *ca = DCAST(ColorAttrib, attrib);
  1836. if (ca->get_color_type() == ColorAttrib::T_flat) {
  1837. return ca->get_color();
  1838. }
  1839. }
  1840. pgraph_cat.warning()
  1841. << "get_color() called on " << *this << " which has no color set.\n";
  1842. return Colorf(1.0f, 1.0f, 1.0f, 1.0f);
  1843. }
  1844. ////////////////////////////////////////////////////////////////////
  1845. // Function: NodePath::has_color_scale
  1846. // Access: Published
  1847. // Description: Returns true if a color scale has been applied
  1848. // to the referenced node, false otherwise. It is still
  1849. // possible that color at this node might have been
  1850. // scaled by an ancestor node.
  1851. ////////////////////////////////////////////////////////////////////
  1852. bool NodePath::
  1853. has_color_scale() const {
  1854. nassertr_always(!is_empty(), false);
  1855. return node()->has_attrib(ColorScaleAttrib::get_class_type());
  1856. }
  1857. ////////////////////////////////////////////////////////////////////
  1858. // Function: NodePath::clear_color_scale
  1859. // Access: Published
  1860. // Description: Completely removes any color scale from the
  1861. // referenced node. This is preferable to simply
  1862. // setting the color scale to identity, as it also
  1863. // removes the overhead associated with having a color
  1864. // scale at all.
  1865. ////////////////////////////////////////////////////////////////////
  1866. void NodePath::
  1867. clear_color_scale() {
  1868. nassertv_always(!is_empty());
  1869. node()->clear_attrib(ColorScaleAttrib::get_class_type());
  1870. }
  1871. ////////////////////////////////////////////////////////////////////
  1872. // Function: NodePath::set_color_scale
  1873. // Access: Published
  1874. // Description: Sets the color scale component of the transform,
  1875. // leaving translation and rotation untouched.
  1876. ////////////////////////////////////////////////////////////////////
  1877. void NodePath::
  1878. set_color_scale(const LVecBase4f &scale, int priority) {
  1879. nassertv_always(!is_empty());
  1880. const RenderAttrib *attrib =
  1881. node()->get_attrib(ColorScaleAttrib::get_class_type());
  1882. if (attrib != (const RenderAttrib *)NULL) {
  1883. priority = max(priority,
  1884. node()->get_state()->get_override(ColorScaleAttrib::get_class_type()));
  1885. const ColorScaleAttrib *csa = DCAST(ColorScaleAttrib, attrib);
  1886. // Modify the existing ColorScaleAttrib to add the indicated
  1887. // colorScale.
  1888. node()->set_attrib(csa->set_scale(scale), priority);
  1889. } else {
  1890. // Create a new ColorScaleAttrib for this node.
  1891. node()->set_attrib(ColorScaleAttrib::make(scale), priority);
  1892. }
  1893. }
  1894. ////////////////////////////////////////////////////////////////////
  1895. // Function: NodePath::set_color_scale_off
  1896. // Access: Published
  1897. // Description: Disables any color scale attribute inherited from
  1898. // above. This is not the same thing as
  1899. // clear_color_scale(), which undoes any previous
  1900. // set_color_scale() operation on this node; rather,
  1901. // this actively disables any set_color_scale() that
  1902. // might be inherited from a parent node. This also
  1903. // disables set_alpha_scale() at the same time.
  1904. //
  1905. // It is legal to specify a new color scale on the same
  1906. // node with a subsequent call to set_color_scale() or
  1907. // set_alpha_scale(); this new scale will apply to lower
  1908. // geometry.
  1909. ////////////////////////////////////////////////////////////////////
  1910. void NodePath::
  1911. set_color_scale_off(int priority) {
  1912. nassertv_always(!is_empty());
  1913. node()->set_attrib(ColorScaleAttrib::make_off(), priority);
  1914. }
  1915. ////////////////////////////////////////////////////////////////////
  1916. // Function: NodePath::set_alpha_scale
  1917. // Access: Published
  1918. // Description: Sets the alpha scale component of the transform
  1919. // without (much) affecting the color scale. Note that
  1920. // any priority specified will also apply to the color
  1921. // scale.
  1922. ////////////////////////////////////////////////////////////////////
  1923. void NodePath::
  1924. set_alpha_scale(float scale, int priority) {
  1925. nassertv_always(!is_empty());
  1926. const RenderAttrib *attrib =
  1927. node()->get_attrib(ColorScaleAttrib::get_class_type());
  1928. if (attrib != (const RenderAttrib *)NULL) {
  1929. priority = max(priority,
  1930. node()->get_state()->get_override(ColorScaleAttrib::get_class_type()));
  1931. const ColorScaleAttrib *csa = DCAST(ColorScaleAttrib, attrib);
  1932. // Modify the existing ColorScaleAttrib to add the indicated
  1933. // colorScale.
  1934. const LVecBase4f &sc = csa->get_scale();
  1935. node()->set_attrib(csa->set_scale(LVecBase4f(sc[0], sc[1], sc[2], scale)), priority);
  1936. } else {
  1937. // Create a new ColorScaleAttrib for this node.
  1938. node()->set_attrib(ColorScaleAttrib::make(LVecBase4f(1.0f, 1.0f, 1.0f, scale)), priority);
  1939. }
  1940. }
  1941. ////////////////////////////////////////////////////////////////////
  1942. // Function: NodePath::set_all_color_scale
  1943. // Access: Published
  1944. // Description: Scales all the color components of the object by the
  1945. // same amount, darkening the object, without (much)
  1946. // affecting alpha. Note that any priority specified
  1947. // will also apply to the alpha scale.
  1948. ////////////////////////////////////////////////////////////////////
  1949. void NodePath::
  1950. set_all_color_scale(float scale, int priority) {
  1951. nassertv_always(!is_empty());
  1952. const RenderAttrib *attrib =
  1953. node()->get_attrib(ColorScaleAttrib::get_class_type());
  1954. if (attrib != (const RenderAttrib *)NULL) {
  1955. priority = max(priority,
  1956. node()->get_state()->get_override(ColorScaleAttrib::get_class_type()));
  1957. const ColorScaleAttrib *csa = DCAST(ColorScaleAttrib, attrib);
  1958. // Modify the existing ColorScaleAttrib to add the indicated
  1959. // colorScale.
  1960. const LVecBase4f &sc = csa->get_scale();
  1961. node()->set_attrib(csa->set_scale(LVecBase4f(scale, scale, scale, sc[3])), priority);
  1962. } else {
  1963. // Create a new ColorScaleAttrib for this node.
  1964. node()->set_attrib(ColorScaleAttrib::make(LVecBase4f(scale, scale, scale, 1.0f)), priority);
  1965. }
  1966. }
  1967. ////////////////////////////////////////////////////////////////////
  1968. // Function: NodePath::get_color_scale
  1969. // Access: Published
  1970. // Description: Returns the complete color scale vector that has been
  1971. // applied to this node via a previous call to
  1972. // set_color_scale() and/or set_alpha_scale(), or all
  1973. // 1's (identity) if no scale has been applied to this
  1974. // particular node.
  1975. ////////////////////////////////////////////////////////////////////
  1976. const LVecBase4f &NodePath::
  1977. get_color_scale() const {
  1978. static const LVecBase4f ident_scale(1.0f, 1.0f, 1.0f, 1.0f);
  1979. nassertr_always(!is_empty(), ident_scale);
  1980. const RenderAttrib *attrib =
  1981. node()->get_attrib(ColorScaleAttrib::get_class_type());
  1982. if (attrib != (const RenderAttrib *)NULL) {
  1983. const ColorScaleAttrib *csa = DCAST(ColorScaleAttrib, attrib);
  1984. return csa->get_scale();
  1985. }
  1986. return ident_scale;
  1987. }
  1988. ////////////////////////////////////////////////////////////////////
  1989. // Function: NodePath::set_light
  1990. // Access: Published
  1991. // Description: Adds the indicated Light or PolylightNode to the list
  1992. // of lights that illuminate geometry at this node and
  1993. // below. The light itself should be parented into the
  1994. // scene graph elsewhere, to represent the light's
  1995. // position in space; but until set_light() is called it
  1996. // will illuminate no geometry.
  1997. ////////////////////////////////////////////////////////////////////
  1998. void NodePath::
  1999. set_light(const NodePath &light, int priority) {
  2000. nassertv_always(!is_empty());
  2001. if (!light.is_empty()) {
  2002. Light *light_obj = light.node()->as_light();
  2003. if (light_obj != (Light *)NULL) {
  2004. // It's an actual Light object.
  2005. const RenderAttrib *attrib =
  2006. node()->get_attrib(LightAttrib::get_class_type());
  2007. if (attrib != (const RenderAttrib *)NULL) {
  2008. priority = max(priority,
  2009. node()->get_state()->get_override(LightAttrib::get_class_type()));
  2010. const LightAttrib *la = DCAST(LightAttrib, attrib);
  2011. // Modify the existing LightAttrib to add the indicated
  2012. // light.
  2013. node()->set_attrib(la->add_on_light(light), priority);
  2014. } else {
  2015. // Create a new LightAttrib for this node.
  2016. CPT(LightAttrib) la = DCAST(LightAttrib, LightAttrib::make());
  2017. node()->set_attrib(la->add_on_light(light), priority);
  2018. }
  2019. return;
  2020. } else if (light.node()->is_of_type(PolylightNode::get_class_type())) {
  2021. // It's a Polylight object.
  2022. if (priority != 0) {
  2023. // PolylightEffects can't have a priority, since they're just
  2024. // an effect to be applied immediately.
  2025. pgraph_cat.warning()
  2026. << "Ignoring priority on set_light(" << light << ")\n";
  2027. }
  2028. const RenderEffect *effect =
  2029. node()->get_effect(PolylightEffect::get_class_type());
  2030. if (effect != (const RenderEffect *)NULL) {
  2031. const PolylightEffect *ple = DCAST(PolylightEffect, effect);
  2032. // Modify the existing PolylightEffect to add the indicated
  2033. // light.
  2034. node()->set_effect(ple->add_light(light));
  2035. } else {
  2036. // Create a new PolylightEffect for this node.
  2037. CPT(PolylightEffect) ple = DCAST(PolylightEffect, PolylightEffect::make());
  2038. node()->set_effect(ple->add_light(light));
  2039. }
  2040. return;
  2041. }
  2042. }
  2043. nassert_raise("Not a Light object.");
  2044. }
  2045. ////////////////////////////////////////////////////////////////////
  2046. // Function: NodePath::set_light_off
  2047. // Access: Published
  2048. // Description: Sets the geometry at this level and below to render
  2049. // using no lights at all. This is different
  2050. // from not specifying a light; rather, this
  2051. // specifically contradicts set_light() at a higher
  2052. // node level (or, with a priority, overrides a
  2053. // set_light() at a lower level).
  2054. //
  2055. // If no lights are in effect on a particular piece of
  2056. // geometry, that geometry is rendered with lighting
  2057. // disabled.
  2058. ////////////////////////////////////////////////////////////////////
  2059. void NodePath::
  2060. set_light_off(int priority) {
  2061. nassertv_always(!is_empty());
  2062. node()->set_attrib(LightAttrib::make_all_off(), priority);
  2063. node()->clear_effect(PolylightEffect::get_class_type());
  2064. }
  2065. ////////////////////////////////////////////////////////////////////
  2066. // Function: NodePath::set_light_off
  2067. // Access: Published
  2068. // Description: Sets the geometry at this level and below to render
  2069. // without using the indicated Light. This is different
  2070. // from not specifying the Light; rather, this
  2071. // specifically contradicts set_light() at a higher node
  2072. // level (or, with a priority, overrides a set_light()
  2073. // at a lower level).
  2074. //
  2075. // This interface does not support PolylightNodes, which
  2076. // cannot be turned off at a lower level.
  2077. ////////////////////////////////////////////////////////////////////
  2078. void NodePath::
  2079. set_light_off(const NodePath &light, int priority) {
  2080. nassertv_always(!is_empty());
  2081. if (!light.is_empty()) {
  2082. Light *light_obj = light.node()->as_light();
  2083. if (light_obj != (Light *)NULL) {
  2084. const RenderAttrib *attrib =
  2085. node()->get_attrib(LightAttrib::get_class_type());
  2086. if (attrib != (const RenderAttrib *)NULL) {
  2087. priority = max(priority,
  2088. node()->get_state()->get_override(LightAttrib::get_class_type()));
  2089. const LightAttrib *la = DCAST(LightAttrib, attrib);
  2090. // Modify the existing LightAttrib to add the indicated light
  2091. // to the "off" list. This also, incidentally, removes it from
  2092. // the "on" list if it is there.
  2093. node()->set_attrib(la->add_off_light(light), priority);
  2094. } else {
  2095. // Create a new LightAttrib for this node that turns off the
  2096. // indicated light.
  2097. CPT(LightAttrib) la = DCAST(LightAttrib, LightAttrib::make());
  2098. node()->set_attrib(la->add_off_light(light), priority);
  2099. }
  2100. return;
  2101. }
  2102. }
  2103. nassert_raise("Not a Light object.");
  2104. }
  2105. ////////////////////////////////////////////////////////////////////
  2106. // Function: NodePath::clear_light
  2107. // Access: Published
  2108. // Description: Completely removes any lighting operations that may
  2109. // have been set via set_light() or set_light_off()
  2110. // from this particular node.
  2111. ////////////////////////////////////////////////////////////////////
  2112. void NodePath::
  2113. clear_light() {
  2114. nassertv_always(!is_empty());
  2115. node()->clear_attrib(LightAttrib::get_class_type());
  2116. node()->clear_effect(PolylightEffect::get_class_type());
  2117. }
  2118. ////////////////////////////////////////////////////////////////////
  2119. // Function: NodePath::clear_light
  2120. // Access: Published
  2121. // Description: Removes any reference to the indicated Light or
  2122. // PolylightNode from the NodePath.
  2123. ////////////////////////////////////////////////////////////////////
  2124. void NodePath::
  2125. clear_light(const NodePath &light) {
  2126. nassertv_always(!is_empty());
  2127. if (!light.is_empty()) {
  2128. Light *light_obj = light.node()->as_light();
  2129. if (light_obj != (Light *)NULL) {
  2130. const RenderAttrib *attrib =
  2131. node()->get_attrib(LightAttrib::get_class_type());
  2132. if (attrib != (const RenderAttrib *)NULL) {
  2133. CPT(LightAttrib) la = DCAST(LightAttrib, attrib);
  2134. la = DCAST(LightAttrib, la->remove_on_light(light));
  2135. la = DCAST(LightAttrib, la->remove_off_light(light));
  2136. if (la->is_identity()) {
  2137. node()->clear_attrib(LightAttrib::get_class_type());
  2138. } else {
  2139. int priority = node()->get_state()->get_override(LightAttrib::get_class_type());
  2140. node()->set_attrib(la, priority);
  2141. }
  2142. }
  2143. return;
  2144. } else if (light.node()->is_of_type(PolylightNode::get_class_type())) {
  2145. const RenderEffect *effect =
  2146. node()->get_effect(PolylightEffect::get_class_type());
  2147. if (effect != (const RenderEffect *)NULL) {
  2148. CPT(PolylightEffect) ple = DCAST(PolylightEffect, effect);
  2149. ple = DCAST(PolylightEffect, ple->remove_light(light));
  2150. node()->set_effect(ple);
  2151. }
  2152. return;
  2153. }
  2154. }
  2155. nassert_raise("Not a Light object.");
  2156. }
  2157. ////////////////////////////////////////////////////////////////////
  2158. // Function: NodePath::has_light
  2159. // Access: Published
  2160. // Description: Returns true if the indicated Light or PolylightNode
  2161. // has been specifically enabled on this particular
  2162. // node. This means that someone called set_light() on
  2163. // this node with the indicated light.
  2164. ////////////////////////////////////////////////////////////////////
  2165. bool NodePath::
  2166. has_light(const NodePath &light) const {
  2167. nassertr_always(!is_empty(), false);
  2168. if (!light.is_empty()) {
  2169. Light *light_obj = light.node()->as_light();
  2170. if (light_obj != (Light *)NULL) {
  2171. const RenderAttrib *attrib =
  2172. node()->get_attrib(LightAttrib::get_class_type());
  2173. if (attrib != (const RenderAttrib *)NULL) {
  2174. const LightAttrib *la = DCAST(LightAttrib, attrib);
  2175. return la->has_on_light(light);
  2176. }
  2177. return false;
  2178. } else if (light.node()->is_of_type(PolylightNode::get_class_type())) {
  2179. const RenderEffect *effect =
  2180. node()->get_effect(PolylightEffect::get_class_type());
  2181. if (effect != (const RenderEffect *)NULL) {
  2182. const PolylightEffect *ple = DCAST(PolylightEffect, effect);
  2183. return ple->has_light(light);
  2184. }
  2185. return false;
  2186. }
  2187. }
  2188. nassert_raise("Not a Light object.");
  2189. return false;
  2190. }
  2191. ////////////////////////////////////////////////////////////////////
  2192. // Function: NodePath::has_light_off
  2193. // Access: Published
  2194. // Description: Returns true if all Lights have been specifically
  2195. // disabled on this particular node. This means that
  2196. // someone called set_light_off() on this node with no
  2197. // parameters.
  2198. ////////////////////////////////////////////////////////////////////
  2199. bool NodePath::
  2200. has_light_off() const {
  2201. nassertr_always(!is_empty(), false);
  2202. const RenderAttrib *attrib =
  2203. node()->get_attrib(LightAttrib::get_class_type());
  2204. if (attrib != (const RenderAttrib *)NULL) {
  2205. const LightAttrib *la = DCAST(LightAttrib, attrib);
  2206. return la->has_all_off();
  2207. }
  2208. return false;
  2209. }
  2210. ////////////////////////////////////////////////////////////////////
  2211. // Function: NodePath::has_light_off
  2212. // Access: Published
  2213. // Description: Returns true if the indicated Light has been
  2214. // specifically disabled on this particular node. This
  2215. // means that someone called set_light_off() on this
  2216. // node with the indicated light.
  2217. //
  2218. // This interface does not support PolylightNodes, which
  2219. // cannot be turned off at a lower level.
  2220. ////////////////////////////////////////////////////////////////////
  2221. bool NodePath::
  2222. has_light_off(const NodePath &light) const {
  2223. nassertr_always(!is_empty(), false);
  2224. if (!light.is_empty()) {
  2225. Light *light_obj = light.node()->as_light();
  2226. if (light_obj != (Light *)NULL) {
  2227. const RenderAttrib *attrib =
  2228. node()->get_attrib(LightAttrib::get_class_type());
  2229. if (attrib != (const RenderAttrib *)NULL) {
  2230. const LightAttrib *la = DCAST(LightAttrib, attrib);
  2231. return la->has_off_light(light);
  2232. }
  2233. }
  2234. }
  2235. nassert_raise("Not a Light object.");
  2236. return false;
  2237. }
  2238. ////////////////////////////////////////////////////////////////////
  2239. // Function: NodePath::set_clip_plane
  2240. // Access: Published
  2241. // Description: Adds the indicated clipping plane to the list of
  2242. // planes that apply to geometry at this node and below.
  2243. // The clipping plane itself, a PlaneNode, should be
  2244. // parented into the scene graph elsewhere, to represent
  2245. // the plane's position in space; but until
  2246. // set_clip_plane() is called it will clip no geometry.
  2247. ////////////////////////////////////////////////////////////////////
  2248. void NodePath::
  2249. set_clip_plane(const NodePath &clip_plane, int priority) {
  2250. nassertv_always(!is_empty());
  2251. if (!clip_plane.is_empty() && clip_plane.node()->is_of_type(PlaneNode::get_class_type())) {
  2252. const RenderAttrib *attrib =
  2253. node()->get_attrib(ClipPlaneAttrib::get_class_type());
  2254. if (attrib != (const RenderAttrib *)NULL) {
  2255. priority = max(priority,
  2256. node()->get_state()->get_override(ClipPlaneAttrib::get_class_type()));
  2257. const ClipPlaneAttrib *la = DCAST(ClipPlaneAttrib, attrib);
  2258. // Modify the existing ClipPlaneAttrib to add the indicated
  2259. // clip_plane.
  2260. node()->set_attrib(la->add_on_plane(clip_plane), priority);
  2261. } else {
  2262. // Create a new ClipPlaneAttrib for this node.
  2263. CPT(ClipPlaneAttrib) la = DCAST(ClipPlaneAttrib, ClipPlaneAttrib::make());
  2264. node()->set_attrib(la->add_on_plane(clip_plane), priority);
  2265. }
  2266. return;
  2267. }
  2268. nassert_raise("Not a PlaneNode object.");
  2269. }
  2270. ////////////////////////////////////////////////////////////////////
  2271. // Function: NodePath::set_clip_plane_off
  2272. // Access: Published
  2273. // Description: Sets the geometry at this level and below to render
  2274. // using no clip_planes at all. This is different
  2275. // from not specifying a clip_plane; rather, this
  2276. // specifically contradicts set_clip_plane() at a higher
  2277. // node level (or, with a priority, overrides a
  2278. // set_clip_plane() at a lower level).
  2279. //
  2280. // If no clip_planes are in effect on a particular piece
  2281. // of geometry, that geometry is rendered without being
  2282. // clipped (other than by the viewing frustum).
  2283. ////////////////////////////////////////////////////////////////////
  2284. void NodePath::
  2285. set_clip_plane_off(int priority) {
  2286. nassertv_always(!is_empty());
  2287. node()->set_attrib(ClipPlaneAttrib::make_all_off(), priority);
  2288. }
  2289. ////////////////////////////////////////////////////////////////////
  2290. // Function: NodePath::set_clip_plane_off
  2291. // Access: Published
  2292. // Description: Sets the geometry at this level and below to render
  2293. // without being clipped by the indicated PlaneNode.
  2294. // This is different from not specifying the PlaneNode;
  2295. // rather, this specifically contradicts
  2296. // set_clip_plane() at a higher node level (or, with a
  2297. // priority, overrides a set_clip_plane() at a lower
  2298. // level).
  2299. ////////////////////////////////////////////////////////////////////
  2300. void NodePath::
  2301. set_clip_plane_off(const NodePath &clip_plane, int priority) {
  2302. nassertv_always(!is_empty());
  2303. if (!clip_plane.is_empty() && clip_plane.node()->is_of_type(PlaneNode::get_class_type())) {
  2304. const RenderAttrib *attrib =
  2305. node()->get_attrib(ClipPlaneAttrib::get_class_type());
  2306. if (attrib != (const RenderAttrib *)NULL) {
  2307. priority = max(priority,
  2308. node()->get_state()->get_override(ClipPlaneAttrib::get_class_type()));
  2309. const ClipPlaneAttrib *la = DCAST(ClipPlaneAttrib, attrib);
  2310. // Modify the existing ClipPlaneAttrib to add the indicated clip_plane
  2311. // to the "off" list. This also, incidentally, removes it from
  2312. // the "on" list if it is there.
  2313. node()->set_attrib(la->add_off_plane(clip_plane), priority);
  2314. } else {
  2315. // Create a new ClipPlaneAttrib for this node that turns off the
  2316. // indicated clip_plane.
  2317. CPT(ClipPlaneAttrib) la = DCAST(ClipPlaneAttrib, ClipPlaneAttrib::make());
  2318. node()->set_attrib(la->add_off_plane(clip_plane), priority);
  2319. }
  2320. return;
  2321. }
  2322. nassert_raise("Not a PlaneNode object.");
  2323. }
  2324. ////////////////////////////////////////////////////////////////////
  2325. // Function: NodePath::clear_clip_plane
  2326. // Access: Published
  2327. // Description: Completely removes any clip planes that may have been
  2328. // set via set_clip_plane() or set_clip_plane_off() from
  2329. // this particular node.
  2330. ////////////////////////////////////////////////////////////////////
  2331. void NodePath::
  2332. clear_clip_plane() {
  2333. nassertv_always(!is_empty());
  2334. node()->clear_attrib(ClipPlaneAttrib::get_class_type());
  2335. }
  2336. ////////////////////////////////////////////////////////////////////
  2337. // Function: NodePath::clear_clip_plane
  2338. // Access: Published
  2339. // Description: Removes any reference to the indicated clipping plane
  2340. // from the NodePath.
  2341. ////////////////////////////////////////////////////////////////////
  2342. void NodePath::
  2343. clear_clip_plane(const NodePath &clip_plane) {
  2344. nassertv_always(!is_empty());
  2345. if (!clip_plane.is_empty() && clip_plane.node()->is_of_type(PlaneNode::get_class_type())) {
  2346. const RenderAttrib *attrib =
  2347. node()->get_attrib(ClipPlaneAttrib::get_class_type());
  2348. if (attrib != (const RenderAttrib *)NULL) {
  2349. CPT(ClipPlaneAttrib) la = DCAST(ClipPlaneAttrib, attrib);
  2350. la = DCAST(ClipPlaneAttrib, la->remove_on_plane(clip_plane));
  2351. la = DCAST(ClipPlaneAttrib, la->remove_off_plane(clip_plane));
  2352. if (la->is_identity()) {
  2353. node()->clear_attrib(ClipPlaneAttrib::get_class_type());
  2354. } else {
  2355. int priority = node()->get_state()->get_override(ClipPlaneAttrib::get_class_type());
  2356. node()->set_attrib(la, priority);
  2357. }
  2358. }
  2359. return;
  2360. }
  2361. nassert_raise("Not a PlaneNode object.");
  2362. }
  2363. ////////////////////////////////////////////////////////////////////
  2364. // Function: NodePath::has_clip_plane
  2365. // Access: Published
  2366. // Description: Returns true if the indicated clipping plane has been
  2367. // specifically applied to this particular node. This
  2368. // means that someone called set_clip_plane() on this
  2369. // node with the indicated clip_plane.
  2370. ////////////////////////////////////////////////////////////////////
  2371. bool NodePath::
  2372. has_clip_plane(const NodePath &clip_plane) const {
  2373. nassertr_always(!is_empty(), false);
  2374. if (!clip_plane.is_empty() && clip_plane.node()->is_of_type(PlaneNode::get_class_type())) {
  2375. const RenderAttrib *attrib =
  2376. node()->get_attrib(ClipPlaneAttrib::get_class_type());
  2377. if (attrib != (const RenderAttrib *)NULL) {
  2378. const ClipPlaneAttrib *la = DCAST(ClipPlaneAttrib, attrib);
  2379. return la->has_on_plane(clip_plane);
  2380. }
  2381. return false;
  2382. }
  2383. nassert_raise("Not a PlaneNode object.");
  2384. return false;
  2385. }
  2386. ////////////////////////////////////////////////////////////////////
  2387. // Function: NodePath::has_clip_plane_off
  2388. // Access: Published
  2389. // Description: Returns true if all clipping planes have been
  2390. // specifically disabled on this particular node. This
  2391. // means that someone called set_clip_plane_off() on
  2392. // this node with no parameters.
  2393. ////////////////////////////////////////////////////////////////////
  2394. bool NodePath::
  2395. has_clip_plane_off() const {
  2396. nassertr_always(!is_empty(), false);
  2397. const RenderAttrib *attrib =
  2398. node()->get_attrib(ClipPlaneAttrib::get_class_type());
  2399. if (attrib != (const RenderAttrib *)NULL) {
  2400. const ClipPlaneAttrib *la = DCAST(ClipPlaneAttrib, attrib);
  2401. return la->has_all_off();
  2402. }
  2403. return false;
  2404. }
  2405. ////////////////////////////////////////////////////////////////////
  2406. // Function: NodePath::has_clip_plane_off
  2407. // Access: Published
  2408. // Description: Returns true if the indicated clipping plane has been
  2409. // specifically disabled on this particular node. This
  2410. // means that someone called set_clip_plane_off() on
  2411. // this node with the indicated clip_plane.
  2412. ////////////////////////////////////////////////////////////////////
  2413. bool NodePath::
  2414. has_clip_plane_off(const NodePath &clip_plane) const {
  2415. nassertr_always(!is_empty(), false);
  2416. if (!clip_plane.is_empty() && clip_plane.node()->is_of_type(PlaneNode::get_class_type())) {
  2417. const RenderAttrib *attrib =
  2418. node()->get_attrib(ClipPlaneAttrib::get_class_type());
  2419. if (attrib != (const RenderAttrib *)NULL) {
  2420. const ClipPlaneAttrib *la = DCAST(ClipPlaneAttrib, attrib);
  2421. return la->has_off_plane(clip_plane);
  2422. }
  2423. }
  2424. nassert_raise("Not a PlaneNode object.");
  2425. return false;
  2426. }
  2427. ////////////////////////////////////////////////////////////////////
  2428. // Function: NodePath::set_bin
  2429. // Access: Published
  2430. // Description: Assigns the geometry at this level and below to the
  2431. // named rendering bin. It is the user's responsibility
  2432. // to ensure that such a bin already exists, either via
  2433. // the cull-bin Configrc variable, or by explicitly
  2434. // creating a GeomBin of the appropriate type at
  2435. // runtime.
  2436. //
  2437. // There are two default bins created when Panda is
  2438. // started: "default" and "fixed". Normally, all
  2439. // geometry is assigned to "default" unless specified
  2440. // otherwise. This bin renders opaque geometry in
  2441. // state-sorted order, followed by transparent geometry
  2442. // sorted back-to-front. If any geometry is assigned to
  2443. // "fixed", this will be rendered following all the
  2444. // geometry in "default", in the order specified by
  2445. // draw_order for each piece of geometry so assigned.
  2446. //
  2447. // The draw_order parameter is meaningful only for
  2448. // GeomBinFixed type bins, e.g. "fixed". Other kinds of
  2449. // bins ignore it.
  2450. ////////////////////////////////////////////////////////////////////
  2451. void NodePath::
  2452. set_bin(const string &bin_name, int draw_order, int priority) {
  2453. nassertv_always(!is_empty());
  2454. node()->set_attrib(CullBinAttrib::make(bin_name, draw_order), priority);
  2455. }
  2456. ////////////////////////////////////////////////////////////////////
  2457. // Function: NodePath::clear_bin
  2458. // Access: Published
  2459. // Description: Completely removes any bin adjustment that may have
  2460. // been set via set_bin() from this particular node.
  2461. ////////////////////////////////////////////////////////////////////
  2462. void NodePath::
  2463. clear_bin() {
  2464. nassertv_always(!is_empty());
  2465. node()->clear_attrib(CullBinAttrib::get_class_type());
  2466. }
  2467. ////////////////////////////////////////////////////////////////////
  2468. // Function: NodePath::has_bin
  2469. // Access: Published
  2470. // Description: Returns true if the node has been assigned to the a
  2471. // particular rendering bin via set_bin(), false
  2472. // otherwise.
  2473. ////////////////////////////////////////////////////////////////////
  2474. bool NodePath::
  2475. has_bin() const {
  2476. nassertr_always(!is_empty(), false);
  2477. return node()->has_attrib(CullBinAttrib::get_class_type());
  2478. }
  2479. ////////////////////////////////////////////////////////////////////
  2480. // Function: NodePath::get_bin_name
  2481. // Access: Published
  2482. // Description: Returns the name of the bin that this particular node
  2483. // was assigned to via set_bin(), or the empty string if
  2484. // no bin was assigned. See set_bin() and has_bin().
  2485. ////////////////////////////////////////////////////////////////////
  2486. string NodePath::
  2487. get_bin_name() const {
  2488. nassertr_always(!is_empty(), string());
  2489. const RenderAttrib *attrib =
  2490. node()->get_attrib(CullBinAttrib::get_class_type());
  2491. if (attrib != (const RenderAttrib *)NULL) {
  2492. const CullBinAttrib *ba = DCAST(CullBinAttrib, attrib);
  2493. return ba->get_bin_name();
  2494. }
  2495. return string();
  2496. }
  2497. ////////////////////////////////////////////////////////////////////
  2498. // Function: NodePath::get_bin_draw_order
  2499. // Access: Published
  2500. // Description: Returns the drawing order associated with the bin
  2501. // that this particular node was assigned to via
  2502. // set_bin(), or 0 if no bin was assigned. See
  2503. // set_bin() and has_bin().
  2504. ////////////////////////////////////////////////////////////////////
  2505. int NodePath::
  2506. get_bin_draw_order() const {
  2507. nassertr_always(!is_empty(), false);
  2508. const RenderAttrib *attrib =
  2509. node()->get_attrib(CullBinAttrib::get_class_type());
  2510. if (attrib != (const RenderAttrib *)NULL) {
  2511. const CullBinAttrib *ba = DCAST(CullBinAttrib, attrib);
  2512. return ba->get_draw_order();
  2513. }
  2514. return 0;
  2515. }
  2516. ////////////////////////////////////////////////////////////////////
  2517. // Function: NodePath::set_texture
  2518. // Access: Published
  2519. // Description: Adds the indicated texture to the list of textures
  2520. // that will be rendered on the default texture stage.
  2521. //
  2522. // This is the deprecated single-texture variant of this
  2523. // method; it is now superceded by set_texture() that
  2524. // accepts a stage and texture. However, this method
  2525. // may be used in the presence of multitexture if you
  2526. // just want to adjust the default stage.
  2527. ////////////////////////////////////////////////////////////////////
  2528. void NodePath::
  2529. set_texture(Texture *tex, int priority) {
  2530. nassertv_always(!is_empty());
  2531. PT(TextureStage) stage = TextureStage::get_default();
  2532. set_texture(stage, tex, priority);
  2533. }
  2534. ////////////////////////////////////////////////////////////////////
  2535. // Function: NodePath::set_texture
  2536. // Access: Published
  2537. // Description: Adds the indicated texture to the list of textures
  2538. // that will be rendered on the indicated multitexture
  2539. // stage. If there are multiple texture stages
  2540. // specified (possibly on multiple different nodes at
  2541. // different levels), they will all be applied to
  2542. // geometry together, according to the stage
  2543. // specification set up in the TextureStage object.
  2544. ////////////////////////////////////////////////////////////////////
  2545. void NodePath::
  2546. set_texture(TextureStage *stage, Texture *tex, int priority) {
  2547. nassertv_always(!is_empty());
  2548. const RenderAttrib *attrib =
  2549. node()->get_attrib(TextureAttrib::get_class_type());
  2550. if (attrib != (const RenderAttrib *)NULL) {
  2551. priority = max(priority,
  2552. node()->get_state()->get_override(TextureAttrib::get_class_type()));
  2553. const TextureAttrib *tsa = DCAST(TextureAttrib, attrib);
  2554. // Modify the existing TextureAttrib to add the indicated
  2555. // texture.
  2556. node()->set_attrib(tsa->add_on_stage(stage, tex), priority);
  2557. } else {
  2558. // Create a new TextureAttrib for this node.
  2559. CPT(TextureAttrib) tsa = DCAST(TextureAttrib, TextureAttrib::make());
  2560. node()->set_attrib(tsa->add_on_stage(stage, tex), priority);
  2561. }
  2562. }
  2563. ////////////////////////////////////////////////////////////////////
  2564. // Function: NodePath::set_texture_off
  2565. // Access: Published
  2566. // Description: Sets the geometry at this level and below to render
  2567. // using no texture, on any stage. This is different
  2568. // from not specifying a texture; rather, this
  2569. // specifically contradicts set_texture() at a higher
  2570. // node level (or, with a priority, overrides a
  2571. // set_texture() at a lower level).
  2572. ////////////////////////////////////////////////////////////////////
  2573. void NodePath::
  2574. set_texture_off(int priority) {
  2575. nassertv_always(!is_empty());
  2576. node()->set_attrib(TextureAttrib::make_all_off(), priority);
  2577. }
  2578. ////////////////////////////////////////////////////////////////////
  2579. // Function: NodePath::set_texture_off
  2580. // Access: Published
  2581. // Description: Sets the geometry at this level and below to render
  2582. // using no texture, on the indicated stage. This is
  2583. // different from not specifying a texture; rather, this
  2584. // specifically contradicts set_texture() at a higher
  2585. // node level (or, with a priority, overrides a
  2586. // set_texture() at a lower level).
  2587. ////////////////////////////////////////////////////////////////////
  2588. void NodePath::
  2589. set_texture_off(TextureStage *stage, int priority) {
  2590. nassertv_always(!is_empty());
  2591. const RenderAttrib *attrib =
  2592. node()->get_attrib(TextureAttrib::get_class_type());
  2593. if (attrib != (const RenderAttrib *)NULL) {
  2594. priority = max(priority,
  2595. node()->get_state()->get_override(TextureAttrib::get_class_type()));
  2596. const TextureAttrib *tsa = DCAST(TextureAttrib, attrib);
  2597. // Modify the existing TextureAttrib to add the indicated texture
  2598. // to the "off" list. This also, incidentally, removes it from
  2599. // the "on" list if it is there.
  2600. node()->set_attrib(tsa->add_off_stage(stage), priority);
  2601. } else {
  2602. // Create a new TextureAttrib for this node that turns off the
  2603. // indicated stage.
  2604. CPT(TextureAttrib) tsa = DCAST(TextureAttrib, TextureAttrib::make());
  2605. node()->set_attrib(tsa->add_off_stage(stage), priority);
  2606. }
  2607. }
  2608. ////////////////////////////////////////////////////////////////////
  2609. // Function: NodePath::clear_texture
  2610. // Access: Published
  2611. // Description: Completely removes any texture adjustment that may
  2612. // have been set via set_texture() or set_texture_off()
  2613. // from this particular node. This allows whatever
  2614. // textures might be otherwise affecting the geometry to
  2615. // show instead.
  2616. ////////////////////////////////////////////////////////////////////
  2617. void NodePath::
  2618. clear_texture() {
  2619. nassertv_always(!is_empty());
  2620. node()->clear_attrib(TextureAttrib::get_class_type());
  2621. }
  2622. ////////////////////////////////////////////////////////////////////
  2623. // Function: NodePath::clear_texture
  2624. // Access: Published
  2625. // Description: Removes any reference to the indicated texture stage
  2626. // from the NodePath.
  2627. ////////////////////////////////////////////////////////////////////
  2628. void NodePath::
  2629. clear_texture(TextureStage *stage) {
  2630. nassertv_always(!is_empty());
  2631. const RenderAttrib *attrib =
  2632. node()->get_attrib(TextureAttrib::get_class_type());
  2633. if (attrib != (const RenderAttrib *)NULL) {
  2634. CPT(TextureAttrib) tsa = DCAST(TextureAttrib, attrib);
  2635. tsa = DCAST(TextureAttrib, tsa->remove_on_stage(stage));
  2636. tsa = DCAST(TextureAttrib, tsa->remove_off_stage(stage));
  2637. if (tsa->is_identity()) {
  2638. node()->clear_attrib(TextureAttrib::get_class_type());
  2639. } else {
  2640. int priority = node()->get_state()->get_override(TextureAttrib::get_class_type());
  2641. node()->set_attrib(tsa, priority);
  2642. }
  2643. }
  2644. }
  2645. ////////////////////////////////////////////////////////////////////
  2646. // Function: NodePath::has_texture
  2647. // Access: Published
  2648. // Description: Returns true if a texture has been applied to this
  2649. // particular node via set_texture(), false otherwise.
  2650. // This is not the same thing as asking whether the
  2651. // geometry at this node will be rendered with
  2652. // texturing, as there may be a texture in effect from a
  2653. // higher or lower level.
  2654. ////////////////////////////////////////////////////////////////////
  2655. bool NodePath::
  2656. has_texture() const {
  2657. return get_texture() != (Texture *)NULL;
  2658. }
  2659. ////////////////////////////////////////////////////////////////////
  2660. // Function: NodePath::has_texture
  2661. // Access: Published
  2662. // Description: Returns true if texturing has been specifically
  2663. // enabled on this particular node for the indicated
  2664. // stage. This means that someone called
  2665. // set_texture() on this node with the indicated stage
  2666. // name, or the stage_name is the default stage_name,
  2667. // and someone called set_texture() on this node.
  2668. ////////////////////////////////////////////////////////////////////
  2669. bool NodePath::
  2670. has_texture(TextureStage *stage) const {
  2671. nassertr_always(!is_empty(), false);
  2672. const RenderAttrib *attrib =
  2673. node()->get_attrib(TextureAttrib::get_class_type());
  2674. if (attrib != (const RenderAttrib *)NULL) {
  2675. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  2676. return ta->has_on_stage(stage);
  2677. }
  2678. return false;
  2679. }
  2680. ////////////////////////////////////////////////////////////////////
  2681. // Function: NodePath::has_texture_off
  2682. // Access: Published
  2683. // Description: Returns true if texturing has been specifically
  2684. // disabled on this particular node via
  2685. // set_texture_off(), false otherwise. This is not the
  2686. // same thing as asking whether the geometry at this
  2687. // node will be rendered untextured, as there may be a
  2688. // texture in effect from a higher or lower level.
  2689. ////////////////////////////////////////////////////////////////////
  2690. bool NodePath::
  2691. has_texture_off() const {
  2692. nassertr_always(!is_empty(), false);
  2693. const RenderAttrib *attrib =
  2694. node()->get_attrib(TextureAttrib::get_class_type());
  2695. if (attrib != (const RenderAttrib *)NULL) {
  2696. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  2697. return ta->has_all_off();
  2698. }
  2699. return false;
  2700. }
  2701. ////////////////////////////////////////////////////////////////////
  2702. // Function: NodePath::has_texture_off
  2703. // Access: Published
  2704. // Description: Returns true if texturing has been specifically
  2705. // disabled on this particular node for the indicated
  2706. // stage. This means that someone called
  2707. // set_texture_off() on this node with the indicated
  2708. // stage name, or that someone called set_texture_off()
  2709. // on this node to remove all stages.
  2710. ////////////////////////////////////////////////////////////////////
  2711. bool NodePath::
  2712. has_texture_off(TextureStage *stage) const {
  2713. nassertr_always(!is_empty(), false);
  2714. const RenderAttrib *attrib =
  2715. node()->get_attrib(TextureAttrib::get_class_type());
  2716. if (attrib != (const RenderAttrib *)NULL) {
  2717. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  2718. return ta->has_off_stage(stage);
  2719. }
  2720. return false;
  2721. }
  2722. ////////////////////////////////////////////////////////////////////
  2723. // Function: NodePath::get_texture
  2724. // Access: Published
  2725. // Description: Returns the base-level texture that has been set on
  2726. // this particular node, or NULL if no texture has been
  2727. // set. This is not necessarily the texture that will
  2728. // be applied to the geometry at or below this level, as
  2729. // another texture at a higher or lower level may
  2730. // override.
  2731. //
  2732. // See also find_texture().
  2733. ////////////////////////////////////////////////////////////////////
  2734. Texture *NodePath::
  2735. get_texture() const {
  2736. nassertr_always(!is_empty(), NULL);
  2737. const RenderAttrib *attrib =
  2738. node()->get_attrib(TextureAttrib::get_class_type());
  2739. if (attrib != (const RenderAttrib *)NULL) {
  2740. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  2741. return ta->get_texture();
  2742. }
  2743. return NULL;
  2744. }
  2745. ////////////////////////////////////////////////////////////////////
  2746. // Function: NodePath::get_texture
  2747. // Access: Published
  2748. // Description: Returns the texture that has been set on the
  2749. // indicated stage for this particular node, or NULL if
  2750. // no texture has been set for this stage.
  2751. ////////////////////////////////////////////////////////////////////
  2752. Texture *NodePath::
  2753. get_texture(TextureStage *stage) const {
  2754. nassertr_always(!is_empty(), NULL);
  2755. const RenderAttrib *attrib =
  2756. node()->get_attrib(TextureAttrib::get_class_type());
  2757. if (attrib != (const RenderAttrib *)NULL) {
  2758. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  2759. return ta->get_on_texture(stage);
  2760. }
  2761. return NULL;
  2762. }
  2763. ////////////////////////////////////////////////////////////////////
  2764. // Function: NodePath::set_shader
  2765. // Access: Published
  2766. // Description:
  2767. ////////////////////////////////////////////////////////////////////
  2768. void NodePath::
  2769. set_shader(Shader *sha, int priority) {
  2770. nassertv_always(!is_empty());
  2771. const RenderAttrib *attrib =
  2772. node()->get_attrib(ShaderAttrib::get_class_type());
  2773. if (attrib != (const RenderAttrib *)NULL) {
  2774. priority = max(priority,
  2775. node()->get_state()->get_override(ShaderAttrib::get_class_type()));
  2776. const ShaderAttrib *sa = DCAST(ShaderAttrib, attrib);
  2777. node()->set_attrib(sa->set_shader(sha, priority));
  2778. } else {
  2779. // Create a new ShaderAttrib for this node.
  2780. CPT(ShaderAttrib) sa = DCAST(ShaderAttrib, ShaderAttrib::make());
  2781. node()->set_attrib(sa->set_shader(sha, priority));
  2782. }
  2783. }
  2784. ////////////////////////////////////////////////////////////////////
  2785. // Function: NodePath::set_shader_off
  2786. // Access: Published
  2787. // Description:
  2788. ////////////////////////////////////////////////////////////////////
  2789. void NodePath::
  2790. set_shader_off(int priority) {
  2791. set_shader(NULL, priority);
  2792. }
  2793. ////////////////////////////////////////////////////////////////////
  2794. // Function: NodePath::clear_shader
  2795. // Access: Published
  2796. // Description:
  2797. ////////////////////////////////////////////////////////////////////
  2798. void NodePath::
  2799. clear_shader() {
  2800. nassertv_always(!is_empty());
  2801. const RenderAttrib *attrib =
  2802. node()->get_attrib(ShaderAttrib::get_class_type());
  2803. if (attrib != (const RenderAttrib *)NULL) {
  2804. const ShaderAttrib *sa = DCAST(ShaderAttrib, attrib);
  2805. node()->set_attrib(sa->clear_shader());
  2806. }
  2807. }
  2808. ////////////////////////////////////////////////////////////////////
  2809. // Function: NodePath::get_shader
  2810. // Access: Published
  2811. // Description:
  2812. ////////////////////////////////////////////////////////////////////
  2813. const Shader *NodePath::
  2814. get_shader() const {
  2815. nassertr_always(!is_empty(), NULL);
  2816. const RenderAttrib *attrib =
  2817. node()->get_attrib(ShaderAttrib::get_class_type());
  2818. if (attrib != (const RenderAttrib *)NULL) {
  2819. const ShaderAttrib *sa = DCAST(ShaderAttrib, attrib);
  2820. return sa->get_shader();
  2821. }
  2822. return NULL;
  2823. }
  2824. ////////////////////////////////////////////////////////////////////
  2825. // Function: NodePath::set_shader_input
  2826. // Access: Published
  2827. // Description:
  2828. ////////////////////////////////////////////////////////////////////
  2829. void NodePath::
  2830. set_shader_input(const ShaderInput *inp) {
  2831. nassertv_always(!is_empty());
  2832. const RenderAttrib *attrib =
  2833. node()->get_attrib(ShaderAttrib::get_class_type());
  2834. if (attrib != (const RenderAttrib *)NULL) {
  2835. const ShaderAttrib *sa = DCAST(ShaderAttrib, attrib);
  2836. node()->set_attrib(sa->set_shader_input(inp));
  2837. } else {
  2838. // Create a new ShaderAttrib for this node.
  2839. CPT(ShaderAttrib) sa = DCAST(ShaderAttrib, ShaderAttrib::make());
  2840. node()->set_attrib(sa->set_shader_input(inp));
  2841. }
  2842. }
  2843. ////////////////////////////////////////////////////////////////////
  2844. // Function: NodePath::get_shader_input
  2845. // Access: Published
  2846. // Description:
  2847. ////////////////////////////////////////////////////////////////////
  2848. const ShaderInput *NodePath::
  2849. get_shader_input(InternalName *id) const {
  2850. nassertr_always(!is_empty(), NULL);
  2851. const RenderAttrib *attrib =
  2852. node()->get_attrib(ShaderAttrib::get_class_type());
  2853. if (attrib != (const RenderAttrib *)NULL) {
  2854. const ShaderAttrib *sa = DCAST(ShaderAttrib, attrib);
  2855. return sa->get_shader_input(id);
  2856. }
  2857. return NULL;
  2858. }
  2859. ////////////////////////////////////////////////////////////////////
  2860. // Function: NodePath::clear_shader_input
  2861. // Access: Published
  2862. // Description:
  2863. ////////////////////////////////////////////////////////////////////
  2864. void NodePath::
  2865. clear_shader_input(InternalName *id) {
  2866. nassertv_always(!is_empty());
  2867. const RenderAttrib *attrib =
  2868. node()->get_attrib(ShaderAttrib::get_class_type());
  2869. if (attrib != (const RenderAttrib *)NULL) {
  2870. const ShaderAttrib *sa = DCAST(ShaderAttrib, attrib);
  2871. node()->set_attrib(sa->clear_shader_input(id));
  2872. }
  2873. }
  2874. ////////////////////////////////////////////////////////////////////
  2875. // Function: NodePath::set_shader_input
  2876. // Access: Published
  2877. // Description:
  2878. ////////////////////////////////////////////////////////////////////
  2879. void NodePath::
  2880. set_shader_input(InternalName *id, Texture *tex, int priority) {
  2881. set_shader_input(new ShaderInput(id,tex,priority));
  2882. }
  2883. ////////////////////////////////////////////////////////////////////
  2884. // Function: NodePath::set_shader_input
  2885. // Access: Published
  2886. // Description:
  2887. ////////////////////////////////////////////////////////////////////
  2888. void NodePath::
  2889. set_shader_input(InternalName *id, const NodePath &np, int priority) {
  2890. set_shader_input(new ShaderInput(id,np,priority));
  2891. }
  2892. ////////////////////////////////////////////////////////////////////
  2893. // Function: NodePath::set_shader_input
  2894. // Access: Published
  2895. // Description:
  2896. ////////////////////////////////////////////////////////////////////
  2897. void NodePath::
  2898. set_shader_input(InternalName *id, const LVector4f &v, int priority) {
  2899. set_shader_input(new ShaderInput(id,v,priority));
  2900. }
  2901. ////////////////////////////////////////////////////////////////////
  2902. // Function: NodePath::set_shader_input
  2903. // Access: Published
  2904. // Description:
  2905. ////////////////////////////////////////////////////////////////////
  2906. void NodePath::
  2907. set_shader_input(InternalName *id, double n1, double n2, double n3, double n4, int priority) {
  2908. set_shader_input(new ShaderInput(id,LVector4f(n1,n2,n3,n4),priority));
  2909. }
  2910. ////////////////////////////////////////////////////////////////////
  2911. // Function: NodePath::set_shader_input
  2912. // Access: Published
  2913. // Description:
  2914. ////////////////////////////////////////////////////////////////////
  2915. void NodePath::
  2916. set_shader_input(const string &id, Texture *tex, int priority) {
  2917. set_shader_input(new ShaderInput(InternalName::make(id),tex,priority));
  2918. }
  2919. ////////////////////////////////////////////////////////////////////
  2920. // Function: NodePath::set_shader_input
  2921. // Access: Published
  2922. // Description:
  2923. ////////////////////////////////////////////////////////////////////
  2924. void NodePath::
  2925. set_shader_input(const string &id, const NodePath &np, int priority) {
  2926. set_shader_input(new ShaderInput(InternalName::make(id),np,priority));
  2927. }
  2928. ////////////////////////////////////////////////////////////////////
  2929. // Function: NodePath::set_shader_input
  2930. // Access: Published
  2931. // Description:
  2932. ////////////////////////////////////////////////////////////////////
  2933. void NodePath::
  2934. set_shader_input(const string &id, const LVector4f &v, int priority) {
  2935. set_shader_input(new ShaderInput(InternalName::make(id),v,priority));
  2936. }
  2937. ////////////////////////////////////////////////////////////////////
  2938. // Function: NodePath::set_shader_input
  2939. // Access: Published
  2940. // Description:
  2941. ////////////////////////////////////////////////////////////////////
  2942. void NodePath::
  2943. set_shader_input(const string &id, double n1, double n2, double n3, double n4, int priority) {
  2944. set_shader_input(new ShaderInput(InternalName::make(id),LVector4f(n1,n2,n3,n4),priority));
  2945. }
  2946. ////////////////////////////////////////////////////////////////////
  2947. // Function: NodePath::get_shader_input
  2948. // Access: Published
  2949. // Description:
  2950. ////////////////////////////////////////////////////////////////////
  2951. const ShaderInput *NodePath::
  2952. get_shader_input(const string &id) const {
  2953. return get_shader_input(InternalName::make(id));
  2954. }
  2955. ////////////////////////////////////////////////////////////////////
  2956. // Function: NodePath::clear_shader_input
  2957. // Access: Published
  2958. // Description:
  2959. ////////////////////////////////////////////////////////////////////
  2960. void NodePath::
  2961. clear_shader_input(const string &id) {
  2962. clear_shader_input(InternalName::make(id));
  2963. }
  2964. ////////////////////////////////////////////////////////////////////
  2965. // Function: NodePath::set_tex_transform
  2966. // Access: Published
  2967. // Description: Sets the texture matrix on the current node to the
  2968. // indicated transform for the given stage.
  2969. ////////////////////////////////////////////////////////////////////
  2970. void NodePath::
  2971. set_tex_transform(TextureStage *stage, const TransformState *transform) {
  2972. nassertv_always(!is_empty());
  2973. const RenderAttrib *attrib =
  2974. node()->get_attrib(TexMatrixAttrib::get_class_type());
  2975. if (attrib != (const RenderAttrib *)NULL) {
  2976. const TexMatrixAttrib *tma = DCAST(TexMatrixAttrib, attrib);
  2977. // Modify the existing TexMatrixAttrib to add the indicated
  2978. // stage.
  2979. node()->set_attrib(tma->add_stage(stage, transform));
  2980. } else {
  2981. // Create a new TexMatrixAttrib for this node.
  2982. node()->set_attrib(TexMatrixAttrib::make(stage, transform));
  2983. }
  2984. }
  2985. ////////////////////////////////////////////////////////////////////
  2986. // Function: NodePath::clear_tex_transform
  2987. // Access: Published
  2988. // Description: Removes all texture matrices from the current node.
  2989. ////////////////////////////////////////////////////////////////////
  2990. void NodePath::
  2991. clear_tex_transform() {
  2992. nassertv_always(!is_empty());
  2993. node()->clear_attrib(TexMatrixAttrib::get_class_type());
  2994. }
  2995. ////////////////////////////////////////////////////////////////////
  2996. // Function: NodePath::clear_tex_transform
  2997. // Access: Published
  2998. // Description: Removes the texture matrix on the current node for
  2999. // the given stage.
  3000. ////////////////////////////////////////////////////////////////////
  3001. void NodePath::
  3002. clear_tex_transform(TextureStage *stage) {
  3003. nassertv_always(!is_empty());
  3004. const RenderAttrib *attrib =
  3005. node()->get_attrib(TexMatrixAttrib::get_class_type());
  3006. if (attrib != (const RenderAttrib *)NULL) {
  3007. CPT(TexMatrixAttrib) tma = DCAST(TexMatrixAttrib, attrib);
  3008. tma = DCAST(TexMatrixAttrib, tma->remove_stage(stage));
  3009. if (tma->is_empty()) {
  3010. node()->clear_attrib(TexMatrixAttrib::get_class_type());
  3011. } else {
  3012. node()->set_attrib(tma);
  3013. }
  3014. }
  3015. }
  3016. ////////////////////////////////////////////////////////////////////
  3017. // Function: NodePath::has_tex_transform
  3018. // Access: Published
  3019. // Description: Returns true if there is an explicit texture matrix
  3020. // on the current node for the given stage.
  3021. ////////////////////////////////////////////////////////////////////
  3022. bool NodePath::
  3023. has_tex_transform(TextureStage *stage) const {
  3024. nassertr_always(!is_empty(), false);
  3025. const RenderAttrib *attrib =
  3026. node()->get_attrib(TexMatrixAttrib::get_class_type());
  3027. if (attrib != (const RenderAttrib *)NULL) {
  3028. const TexMatrixAttrib *tma = DCAST(TexMatrixAttrib, attrib);
  3029. return tma->has_stage(stage);
  3030. }
  3031. return false;
  3032. }
  3033. ////////////////////////////////////////////////////////////////////
  3034. // Function: NodePath::get_tex_transform
  3035. // Access: Published
  3036. // Description: Returns the texture matrix on the current node for the
  3037. // given stage, or identity transform if there is no
  3038. // explicit transform set for the given stage.
  3039. ////////////////////////////////////////////////////////////////////
  3040. CPT(TransformState) NodePath::
  3041. get_tex_transform(TextureStage *stage) const {
  3042. nassertr_always(!is_empty(), NULL);
  3043. const RenderAttrib *attrib =
  3044. node()->get_attrib(TexMatrixAttrib::get_class_type());
  3045. if (attrib != (const RenderAttrib *)NULL) {
  3046. const TexMatrixAttrib *tma = DCAST(TexMatrixAttrib, attrib);
  3047. return tma->get_transform(stage);
  3048. }
  3049. return TransformState::make_identity();
  3050. }
  3051. ////////////////////////////////////////////////////////////////////
  3052. // Function: NodePath::set_tex_transform
  3053. // Access: Published
  3054. // Description: Sets the texture matrix on the current node to the
  3055. // indicated transform for the given stage.
  3056. ////////////////////////////////////////////////////////////////////
  3057. void NodePath::
  3058. set_tex_transform(const NodePath &other, TextureStage *stage, const TransformState *transform) {
  3059. nassertv(_error_type == ET_ok && other._error_type == ET_ok);
  3060. nassertv_always(!is_empty());
  3061. CPT(RenderState) state = get_state(other);
  3062. const RenderAttrib *attrib =
  3063. state->get_attrib(TexMatrixAttrib::get_class_type());
  3064. if (attrib != (const RenderAttrib *)NULL) {
  3065. const TexMatrixAttrib *tma = DCAST(TexMatrixAttrib, attrib);
  3066. // Modify the existing TexMatrixAttrib to add the indicated
  3067. // stage.
  3068. state = state->add_attrib(tma->add_stage(stage, transform));
  3069. } else {
  3070. // Create a new TexMatrixAttrib for this node.
  3071. state = state->add_attrib(TexMatrixAttrib::make(stage, transform));
  3072. }
  3073. // Now compose that with our parent's state.
  3074. CPT(RenderState) rel_state;
  3075. if (has_parent()) {
  3076. rel_state = other.get_state(get_parent());
  3077. } else {
  3078. rel_state = other.get_state(NodePath());
  3079. }
  3080. CPT(RenderState) new_state = rel_state->compose(state);
  3081. // And apply only the TexMatrixAttrib to the current node, leaving
  3082. // the others unchanged.
  3083. node()->set_attrib(new_state->get_attrib(TexMatrixAttrib::get_class_type()));
  3084. }
  3085. ////////////////////////////////////////////////////////////////////
  3086. // Function: NodePath::get_tex_transform
  3087. // Access: Published
  3088. // Description: Returns the texture matrix on the current node for the
  3089. // given stage, relative to the other node.
  3090. ////////////////////////////////////////////////////////////////////
  3091. CPT(TransformState) NodePath::
  3092. get_tex_transform(const NodePath &other, TextureStage *stage) const {
  3093. nassertr(_error_type == ET_ok && other._error_type == ET_ok, TransformState::make_identity());
  3094. CPT(RenderState) state = get_state(other);
  3095. const RenderAttrib *attrib =
  3096. state->get_attrib(TexMatrixAttrib::get_class_type());
  3097. if (attrib != (const RenderAttrib *)NULL) {
  3098. const TexMatrixAttrib *tma = DCAST(TexMatrixAttrib, attrib);
  3099. return tma->get_transform(stage);
  3100. }
  3101. return TransformState::make_identity();
  3102. }
  3103. ////////////////////////////////////////////////////////////////////
  3104. // Function: NodePath::set_tex_gen
  3105. // Access: Published
  3106. // Description: Enables automatic texture coordinate generation for
  3107. // the indicated texture stage.
  3108. ////////////////////////////////////////////////////////////////////
  3109. void NodePath::
  3110. set_tex_gen(TextureStage *stage, RenderAttrib::TexGenMode mode, int priority) {
  3111. nassertv_always(!is_empty());
  3112. const RenderAttrib *attrib =
  3113. node()->get_attrib(TexGenAttrib::get_class_type());
  3114. CPT(TexGenAttrib) tga;
  3115. if (attrib != (const RenderAttrib *)NULL) {
  3116. priority = max(priority,
  3117. node()->get_state()->get_override(TextureAttrib::get_class_type()));
  3118. tga = DCAST(TexGenAttrib, attrib);
  3119. } else {
  3120. tga = DCAST(TexGenAttrib, TexGenAttrib::make());
  3121. }
  3122. node()->set_attrib(tga->add_stage(stage, mode), priority);
  3123. }
  3124. ////////////////////////////////////////////////////////////////////
  3125. // Function: NodePath::set_tex_gen
  3126. // Access: Published
  3127. // Description: Enables automatic texture coordinate generation for
  3128. // the indicated texture stage. This version of this
  3129. // method is useful when setting M_light_vector, which
  3130. // requires the name of the texture coordinate set that
  3131. // supplies the tangent and binormal, as well as the
  3132. // specific light to generate coordinates for.
  3133. ////////////////////////////////////////////////////////////////////
  3134. void NodePath::
  3135. set_tex_gen(TextureStage *stage, RenderAttrib::TexGenMode mode,
  3136. const string &source_name, const NodePath &light, int priority) {
  3137. nassertv_always(!is_empty());
  3138. const RenderAttrib *attrib =
  3139. node()->get_attrib(TexGenAttrib::get_class_type());
  3140. CPT(TexGenAttrib) tga;
  3141. if (attrib != (const RenderAttrib *)NULL) {
  3142. priority = max(priority,
  3143. node()->get_state()->get_override(TextureAttrib::get_class_type()));
  3144. tga = DCAST(TexGenAttrib, attrib);
  3145. } else {
  3146. tga = DCAST(TexGenAttrib, TexGenAttrib::make());
  3147. }
  3148. node()->set_attrib(tga->add_stage(stage, mode, source_name, light), priority);
  3149. }
  3150. ////////////////////////////////////////////////////////////////////
  3151. // Function: NodePath::clear_tex_gen
  3152. // Access: Published
  3153. // Description: Removes the texture coordinate generation mode from
  3154. // all texture stages on this node.
  3155. ////////////////////////////////////////////////////////////////////
  3156. void NodePath::
  3157. clear_tex_gen() {
  3158. nassertv_always(!is_empty());
  3159. node()->clear_attrib(TexGenAttrib::get_class_type());
  3160. }
  3161. ////////////////////////////////////////////////////////////////////
  3162. // Function: NodePath::clear_tex_gen
  3163. // Access: Published
  3164. // Description: Disables automatic texture coordinate generation for
  3165. // the indicated texture stage.
  3166. ////////////////////////////////////////////////////////////////////
  3167. void NodePath::
  3168. clear_tex_gen(TextureStage *stage) {
  3169. nassertv_always(!is_empty());
  3170. const RenderAttrib *attrib =
  3171. node()->get_attrib(TexGenAttrib::get_class_type());
  3172. if (attrib != (const RenderAttrib *)NULL) {
  3173. CPT(TexGenAttrib) tga = DCAST(TexGenAttrib, attrib);
  3174. tga = DCAST(TexGenAttrib, tga->remove_stage(stage));
  3175. if (tga->is_empty()) {
  3176. node()->clear_attrib(TexGenAttrib::get_class_type());
  3177. } else {
  3178. node()->set_attrib(tga);
  3179. }
  3180. }
  3181. }
  3182. ////////////////////////////////////////////////////////////////////
  3183. // Function: NodePath::has_tex_gen
  3184. // Access: Published
  3185. // Description: Returns true if there is a mode for automatic texture
  3186. // coordinate generation on the current node for the
  3187. // given stage.
  3188. ////////////////////////////////////////////////////////////////////
  3189. bool NodePath::
  3190. has_tex_gen(TextureStage *stage) const {
  3191. nassertr_always(!is_empty(), false);
  3192. const RenderAttrib *attrib =
  3193. node()->get_attrib(TexGenAttrib::get_class_type());
  3194. if (attrib != (const RenderAttrib *)NULL) {
  3195. const TexGenAttrib *tga = DCAST(TexGenAttrib, attrib);
  3196. return tga->has_stage(stage);
  3197. }
  3198. return false;
  3199. }
  3200. ////////////////////////////////////////////////////////////////////
  3201. // Function: NodePath::get_tex_gen
  3202. // Access: Published
  3203. // Description: Returns the texture coordinate generation mode for
  3204. // the given stage, or M_off if there is no explicit
  3205. // mode set for the given stage.
  3206. ////////////////////////////////////////////////////////////////////
  3207. RenderAttrib::TexGenMode NodePath::
  3208. get_tex_gen(TextureStage *stage) const {
  3209. nassertr_always(!is_empty(), TexGenAttrib::M_off);
  3210. const RenderAttrib *attrib =
  3211. node()->get_attrib(TexGenAttrib::get_class_type());
  3212. if (attrib != (const RenderAttrib *)NULL) {
  3213. const TexGenAttrib *tga = DCAST(TexGenAttrib, attrib);
  3214. return tga->get_mode(stage);
  3215. }
  3216. return TexGenAttrib::M_off;
  3217. }
  3218. ////////////////////////////////////////////////////////////////////
  3219. // Function: NodePath::get_tex_gen_light
  3220. // Access: Published
  3221. // Description: Returns the particular Light set for the indicated
  3222. // texgen mode's texture stage, or empty NodePath if no
  3223. // light is set. This is only meaningful if the texgen
  3224. // mode (returned by get_tex_gen()) is M_light_vector.
  3225. ////////////////////////////////////////////////////////////////////
  3226. NodePath NodePath::
  3227. get_tex_gen_light(TextureStage *stage) const {
  3228. nassertr_always(!is_empty(), NodePath::fail());
  3229. const RenderAttrib *attrib =
  3230. node()->get_attrib(TexGenAttrib::get_class_type());
  3231. if (attrib != (const RenderAttrib *)NULL) {
  3232. const TexGenAttrib *tga = DCAST(TexGenAttrib, attrib);
  3233. return tga->get_light(stage);
  3234. }
  3235. return NodePath();
  3236. }
  3237. ////////////////////////////////////////////////////////////////////
  3238. // Function: NodePath::set_tex_projector
  3239. // Access: Published
  3240. // Description: Establishes a TexProjectorEffect on this node, which
  3241. // can be used to establish projective texturing (but
  3242. // see also the NodePath::project_texture() convenience
  3243. // function), or it can be used to bind this node's
  3244. // texture transform to particular node's position in
  3245. // space, allowing a LerpInterval (for instance) to
  3246. // adjust this node's texture coordinates.
  3247. ////////////////////////////////////////////////////////////////////
  3248. void NodePath::
  3249. set_tex_projector(TextureStage *stage, const NodePath &from, const NodePath &to) {
  3250. nassertv_always(!is_empty());
  3251. const RenderEffect *effect =
  3252. node()->get_effect(TexProjectorEffect::get_class_type());
  3253. CPT(TexProjectorEffect) tpe;
  3254. if (effect != (const RenderEffect *)NULL) {
  3255. tpe = DCAST(TexProjectorEffect, effect);
  3256. } else {
  3257. tpe = DCAST(TexProjectorEffect, TexProjectorEffect::make());
  3258. }
  3259. node()->set_effect(tpe->add_stage(stage, from, to));
  3260. }
  3261. ////////////////////////////////////////////////////////////////////
  3262. // Function: NodePath::clear_tex_projector
  3263. // Access: Published
  3264. // Description: Removes the TexProjectorEffect for the indicated
  3265. // stage from this node.
  3266. ////////////////////////////////////////////////////////////////////
  3267. void NodePath::
  3268. clear_tex_projector(TextureStage *stage) {
  3269. nassertv_always(!is_empty());
  3270. const RenderEffect *effect =
  3271. node()->get_effect(TexProjectorEffect::get_class_type());
  3272. if (effect != (const RenderEffect *)NULL) {
  3273. CPT(TexProjectorEffect) tpe = DCAST(TexProjectorEffect, effect);
  3274. tpe = DCAST(TexProjectorEffect, tpe->remove_stage(stage));
  3275. if (tpe->is_empty()) {
  3276. node()->clear_effect(TexProjectorEffect::get_class_type());
  3277. } else {
  3278. node()->set_effect(tpe);
  3279. }
  3280. }
  3281. }
  3282. ////////////////////////////////////////////////////////////////////
  3283. // Function: NodePath::clear_tex_projector
  3284. // Access: Published
  3285. // Description: Removes the TexProjectorEffect for all stages from
  3286. // this node.
  3287. ////////////////////////////////////////////////////////////////////
  3288. void NodePath::
  3289. clear_tex_projector() {
  3290. nassertv_always(!is_empty());
  3291. node()->clear_effect(TexProjectorEffect::get_class_type());
  3292. }
  3293. ////////////////////////////////////////////////////////////////////
  3294. // Function: NodePath::has_tex_projector
  3295. // Access: Published
  3296. // Description: Returns true if this node has a TexProjectorEffect
  3297. // for the indicated stage, false otherwise.
  3298. ////////////////////////////////////////////////////////////////////
  3299. bool NodePath::
  3300. has_tex_projector(TextureStage *stage) const {
  3301. nassertr_always(!is_empty(), false);
  3302. const RenderEffect *effect =
  3303. node()->get_effect(TexProjectorEffect::get_class_type());
  3304. if (effect != (const RenderEffect *)NULL) {
  3305. const TexProjectorEffect *tpe = DCAST(TexProjectorEffect, effect);
  3306. return tpe->has_stage(stage);
  3307. }
  3308. return false;
  3309. }
  3310. ////////////////////////////////////////////////////////////////////
  3311. // Function: NodePath::get_tex_projector_from
  3312. // Access: Published
  3313. // Description: Returns the "from" node associated with the
  3314. // TexProjectorEffect on the indicated stage. The
  3315. // relative transform between the "from" and the "to"
  3316. // nodes is automatically applied to the texture
  3317. // transform each frame.
  3318. ////////////////////////////////////////////////////////////////////
  3319. NodePath NodePath::
  3320. get_tex_projector_from(TextureStage *stage) const {
  3321. nassertr_always(!is_empty(), NodePath::fail());
  3322. const RenderEffect *effect =
  3323. node()->get_effect(TexProjectorEffect::get_class_type());
  3324. if (effect != (const RenderEffect *)NULL) {
  3325. const TexProjectorEffect *tpe = DCAST(TexProjectorEffect, effect);
  3326. return tpe->get_from(stage);
  3327. }
  3328. return NodePath::not_found();
  3329. }
  3330. ////////////////////////////////////////////////////////////////////
  3331. // Function: NodePath::get_tex_projector_to
  3332. // Access: Published
  3333. // Description: Returns the "to" node associated with the
  3334. // TexProjectorEffect on the indicated stage. The
  3335. // relative transform between the "from" and the "to"
  3336. // nodes is automatically applied to the texture
  3337. // transform each frame.
  3338. ////////////////////////////////////////////////////////////////////
  3339. NodePath NodePath::
  3340. get_tex_projector_to(TextureStage *stage) const {
  3341. nassertr_always(!is_empty(), NodePath::fail());
  3342. const RenderEffect *effect =
  3343. node()->get_effect(TexProjectorEffect::get_class_type());
  3344. if (effect != (const RenderEffect *)NULL) {
  3345. const TexProjectorEffect *tpe = DCAST(TexProjectorEffect, effect);
  3346. return tpe->get_to(stage);
  3347. }
  3348. return NodePath::not_found();
  3349. }
  3350. ////////////////////////////////////////////////////////////////////
  3351. // Function: NodePath::project_texture
  3352. // Access: Published
  3353. // Description: A convenience function to enable projective texturing
  3354. // at this node level and below, using the indicated
  3355. // NodePath (which should contain a LensNode) as the
  3356. // projector.
  3357. ////////////////////////////////////////////////////////////////////
  3358. void NodePath::
  3359. project_texture(TextureStage *stage, Texture *tex, const NodePath &projector) {
  3360. nassertv(!projector.is_empty() && projector.node()->is_of_type(LensNode::get_class_type()));
  3361. set_texture(stage, tex);
  3362. set_tex_gen(stage, TexGenAttrib::M_world_position);
  3363. set_tex_projector(stage, NodePath(), projector);
  3364. }
  3365. ////////////////////////////////////////////////////////////////////
  3366. // Function: NodePath::set_normal_map
  3367. // Access: Published
  3368. // Description: A convenience function to set up a normal map on this
  3369. // geometry. This uses the single highest-priority
  3370. // light on the object only. It also requires
  3371. // multitexture, and consumes at least two texture
  3372. // stages, in addition to what may already be in use.
  3373. //
  3374. // The normal_map parameter is the texture that contains
  3375. // the normal map information (with a 3-d delta vector
  3376. // encoded into the r,g,b of each texel). texcoord_name is
  3377. // the name of the texture coordinate set that contains
  3378. // the tangent and binormal we wish to use. If
  3379. // preserve_color is true, then one additional texture
  3380. // stage is consumed to blend in the geometry's original
  3381. // vertex color.
  3382. //
  3383. // Only one normal map may be in effect through this
  3384. // interface at any given time.
  3385. ////////////////////////////////////////////////////////////////////
  3386. void NodePath::
  3387. set_normal_map(Texture *normal_map, const string &texcoord_name,
  3388. bool preserve_color) {
  3389. clear_normal_map();
  3390. // First, we apply the normal map itself, to the bottom layer.
  3391. PT(TextureStage) normal_map_ts = new TextureStage("__normal_map");
  3392. normal_map_ts->set_texcoord_name(texcoord_name);
  3393. normal_map_ts->set_sort(-20);
  3394. normal_map_ts->set_mode(TextureStage::M_replace);
  3395. set_texture(normal_map_ts, normal_map);
  3396. // Then, we apply a normalization map, to normalize, per-pixel, the
  3397. // vector to the light.
  3398. PT(Texture) normalization_map = TexturePool::get_normalization_cube_map(32);
  3399. PT(TextureStage) normalization_map_ts = new TextureStage("__normalization_map");
  3400. normalization_map_ts->set_combine_rgb
  3401. (TextureStage::CM_dot3_rgb,
  3402. TextureStage::CS_texture, TextureStage::CO_src_color,
  3403. TextureStage::CS_previous, TextureStage::CO_src_color);
  3404. normalization_map_ts->set_texcoord_name("light_vector");
  3405. normalization_map_ts->set_sort(-15);
  3406. set_texture(normalization_map_ts, normalization_map);
  3407. // Finally, we enable M_light_vector texture coordinate generation.
  3408. set_tex_gen(normalization_map_ts, TexGenAttrib::M_light_vector,
  3409. texcoord_name, NodePath());
  3410. if (preserve_color) {
  3411. // One more stage to get back the original color.
  3412. PT(TextureStage) orig_color_ts = new TextureStage("__orig_color");
  3413. orig_color_ts->set_combine_rgb
  3414. (TextureStage::CM_modulate,
  3415. TextureStage::CS_primary_color, TextureStage::CO_src_color,
  3416. TextureStage::CS_previous, TextureStage::CO_src_color);
  3417. set_texture(orig_color_ts, normal_map);
  3418. }
  3419. }
  3420. ////////////////////////////////////////////////////////////////////
  3421. // Function: NodePath::clear_normal_map
  3422. // Access: Published
  3423. // Description: Undoes the effect of a previous call to
  3424. // set_normal_map().
  3425. ////////////////////////////////////////////////////////////////////
  3426. void NodePath::
  3427. clear_normal_map() {
  3428. // Scan through the TextureStages, and if we find any whose name
  3429. // matches one of the stages that would have been left by
  3430. // set_normal_map(), remove it from the state.
  3431. CPT(RenderAttrib) attrib =
  3432. get_state()->get_attrib(TextureAttrib::get_class_type());
  3433. if (attrib != (const RenderAttrib *)NULL) {
  3434. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  3435. for (int i = 0; i < ta->get_num_on_stages(); i++) {
  3436. TextureStage *stage = ta->get_on_stage(i);
  3437. if (stage->get_name() == "__normal_map") {
  3438. clear_texture(stage);
  3439. } else if (stage->get_name() == "__normalization_map") {
  3440. clear_texture(stage);
  3441. clear_tex_gen(stage);
  3442. } else if (stage->get_name() == "__orig_color") {
  3443. clear_texture(stage);
  3444. }
  3445. }
  3446. }
  3447. }
  3448. ////////////////////////////////////////////////////////////////////
  3449. // Function: NodePath::find_texture
  3450. // Access: Published
  3451. // Description: Returns the first texture found applied to geometry
  3452. // at this node or below that matches the indicated name
  3453. // (which may contain wildcards). Returns the texture
  3454. // if it is found, or NULL if it is not.
  3455. ////////////////////////////////////////////////////////////////////
  3456. Texture *NodePath::
  3457. find_texture(const string &name) const {
  3458. GlobPattern glob(name);
  3459. return r_find_texture(node(), get_net_state(), glob);
  3460. }
  3461. ////////////////////////////////////////////////////////////////////
  3462. // Function: NodePath::find_texture
  3463. // Access: Published
  3464. // Description: Returns the first texture found applied to geometry
  3465. // at this node or below that is assigned to the
  3466. // indicated texture stage. Returns the texture if it
  3467. // is found, or NULL if it is not.
  3468. ////////////////////////////////////////////////////////////////////
  3469. Texture *NodePath::
  3470. find_texture(TextureStage *stage) const {
  3471. return r_find_texture(node(), stage);
  3472. }
  3473. ////////////////////////////////////////////////////////////////////
  3474. // Function: NodePath::find_all_textures
  3475. // Access: Published
  3476. // Description: Returns a list of a textures applied to geometry at
  3477. // this node and below.
  3478. ////////////////////////////////////////////////////////////////////
  3479. TextureCollection NodePath::
  3480. find_all_textures() const {
  3481. Textures textures;
  3482. r_find_all_textures(node(), get_net_state(), textures);
  3483. TextureCollection tc;
  3484. Textures::iterator ti;
  3485. for (ti = textures.begin(); ti != textures.end(); ++ti) {
  3486. tc.add_texture(*ti);
  3487. }
  3488. return tc;
  3489. }
  3490. ////////////////////////////////////////////////////////////////////
  3491. // Function: NodePath::find_all_textures
  3492. // Access: Published
  3493. // Description: Returns a list of a textures applied to geometry at
  3494. // this node and below that match the indicated name
  3495. // (which may contain wildcard characters).
  3496. ////////////////////////////////////////////////////////////////////
  3497. TextureCollection NodePath::
  3498. find_all_textures(const string &name) const {
  3499. Textures textures;
  3500. r_find_all_textures(node(), get_net_state(), textures);
  3501. GlobPattern glob(name);
  3502. TextureCollection tc;
  3503. Textures::iterator ti;
  3504. for (ti = textures.begin(); ti != textures.end(); ++ti) {
  3505. Texture *texture = (*ti);
  3506. if (glob.matches(texture->get_name())) {
  3507. tc.add_texture(texture);
  3508. }
  3509. }
  3510. return tc;
  3511. }
  3512. ////////////////////////////////////////////////////////////////////
  3513. // Function: NodePath::find_all_textures
  3514. // Access: Published
  3515. // Description: Returns a list of a textures on geometry at
  3516. // this node and below that are assigned to the
  3517. // indicated texture stage.
  3518. ////////////////////////////////////////////////////////////////////
  3519. TextureCollection NodePath::
  3520. find_all_textures(TextureStage *stage) const {
  3521. Textures textures;
  3522. r_find_all_textures(node(), stage, textures);
  3523. TextureCollection tc;
  3524. Textures::iterator ti;
  3525. for (ti = textures.begin(); ti != textures.end(); ++ti) {
  3526. Texture *texture = (*ti);
  3527. tc.add_texture(texture);
  3528. }
  3529. return tc;
  3530. }
  3531. ////////////////////////////////////////////////////////////////////
  3532. // Function: NodePath::find_texture_stage
  3533. // Access: Published
  3534. // Description: Returns the first TextureStage found applied to
  3535. // geometry at this node or below that matches the
  3536. // indicated name (which may contain wildcards).
  3537. // Returns the TextureStage if it is found, or NULL if
  3538. // it is not.
  3539. ////////////////////////////////////////////////////////////////////
  3540. TextureStage *NodePath::
  3541. find_texture_stage(const string &name) const {
  3542. GlobPattern glob(name);
  3543. return r_find_texture_stage(node(), get_net_state(), glob);
  3544. }
  3545. ////////////////////////////////////////////////////////////////////
  3546. // Function: NodePath::find_all_texture_stages
  3547. // Access: Published
  3548. // Description: Returns a list of a TextureStages applied to geometry
  3549. // at this node and below.
  3550. ////////////////////////////////////////////////////////////////////
  3551. TextureStageCollection NodePath::
  3552. find_all_texture_stages() const {
  3553. TextureStages texture_stages;
  3554. r_find_all_texture_stages(node(), get_net_state(), texture_stages);
  3555. TextureStageCollection tc;
  3556. TextureStages::iterator ti;
  3557. for (ti = texture_stages.begin(); ti != texture_stages.end(); ++ti) {
  3558. tc.add_texture_stage(*ti);
  3559. }
  3560. return tc;
  3561. }
  3562. ////////////////////////////////////////////////////////////////////
  3563. // Function: NodePath::unify_texture_stages
  3564. // Access: Published
  3565. // Description: Searches through all TextureStages at this node and
  3566. // below. Any TextureStages that share the same name as
  3567. // the indicated TextureStage object are replaced with
  3568. // this object, thus ensuring that all geometry at this
  3569. // node and below with a particular TextureStage name is
  3570. // using the same TextureStage object.
  3571. ////////////////////////////////////////////////////////////////////
  3572. void NodePath::
  3573. unify_texture_stages(TextureStage *stage) {
  3574. r_unify_texture_stages(node(), stage);
  3575. }
  3576. ////////////////////////////////////////////////////////////////////
  3577. // Function: NodePath::find_all_texture_stages
  3578. // Access: Published
  3579. // Description: Returns a list of a TextureStages applied to geometry
  3580. // at this node and below that match the indicated name
  3581. // (which may contain wildcard characters).
  3582. ////////////////////////////////////////////////////////////////////
  3583. TextureStageCollection NodePath::
  3584. find_all_texture_stages(const string &name) const {
  3585. TextureStages texture_stages;
  3586. r_find_all_texture_stages(node(), get_net_state(), texture_stages);
  3587. GlobPattern glob(name);
  3588. TextureStageCollection tc;
  3589. TextureStages::iterator ti;
  3590. for (ti = texture_stages.begin(); ti != texture_stages.end(); ++ti) {
  3591. TextureStage *texture_stage = (*ti);
  3592. if (glob.matches(texture_stage->get_name())) {
  3593. tc.add_texture_stage(texture_stage);
  3594. }
  3595. }
  3596. return tc;
  3597. }
  3598. ////////////////////////////////////////////////////////////////////
  3599. // Function: NodePath::set_material
  3600. // Access: Published
  3601. // Description: Sets the geometry at this level and below to render
  3602. // using the indicated material.
  3603. //
  3604. // Previously, this operation made a copy of the
  3605. // material structure, but nowadays it assigns the
  3606. // pointer directly.
  3607. ////////////////////////////////////////////////////////////////////
  3608. void NodePath::
  3609. set_material(Material *mat, int priority) {
  3610. nassertv_always(!is_empty());
  3611. nassertv(mat != NULL);
  3612. node()->set_attrib(MaterialAttrib::make(mat), priority);
  3613. }
  3614. ////////////////////////////////////////////////////////////////////
  3615. // Function: NodePath::set_material_off
  3616. // Access: Published
  3617. // Description: Sets the geometry at this level and below to render
  3618. // using no material. This is normally the default, but
  3619. // it may be useful to use this to contradict
  3620. // set_material() at a higher node level (or, with a
  3621. // priority, to override a set_material() at a lower
  3622. // level).
  3623. ////////////////////////////////////////////////////////////////////
  3624. void NodePath::
  3625. set_material_off(int priority) {
  3626. nassertv_always(!is_empty());
  3627. node()->set_attrib(MaterialAttrib::make_off(), priority);
  3628. }
  3629. ////////////////////////////////////////////////////////////////////
  3630. // Function: NodePath::clear_material
  3631. // Access: Published
  3632. // Description: Completely removes any material adjustment that may
  3633. // have been set via set_material() from this particular
  3634. // node.
  3635. ////////////////////////////////////////////////////////////////////
  3636. void NodePath::
  3637. clear_material() {
  3638. nassertv_always(!is_empty());
  3639. node()->clear_attrib(MaterialAttrib::get_class_type());
  3640. }
  3641. ////////////////////////////////////////////////////////////////////
  3642. // Function: NodePath::has_material
  3643. // Access: Published
  3644. // Description: Returns true if a material has been applied to this
  3645. // particular node via set_material(), false otherwise.
  3646. ////////////////////////////////////////////////////////////////////
  3647. bool NodePath::
  3648. has_material() const {
  3649. nassertr_always(!is_empty(), false);
  3650. const RenderAttrib *attrib =
  3651. node()->get_attrib(MaterialAttrib::get_class_type());
  3652. if (attrib != (const RenderAttrib *)NULL) {
  3653. const MaterialAttrib *ma = DCAST(MaterialAttrib, attrib);
  3654. return !ma->is_off();
  3655. }
  3656. return false;
  3657. }
  3658. ////////////////////////////////////////////////////////////////////
  3659. // Function: NodePath::get_material
  3660. // Access: Published
  3661. // Description: Returns the material that has been set on this
  3662. // particular node, or NULL if no material has been set.
  3663. // This is not necessarily the material that will be
  3664. // applied to the geometry at or below this level, as
  3665. // another material at a higher or lower level may
  3666. // override.
  3667. //
  3668. // This function returns a copy of the given material,
  3669. // to allow changes, if desired. Once changes are made,
  3670. // they should be reapplied via set_material().
  3671. ////////////////////////////////////////////////////////////////////
  3672. PT(Material) NodePath::
  3673. get_material() const {
  3674. nassertr_always(!is_empty(), NULL);
  3675. const RenderAttrib *attrib =
  3676. node()->get_attrib(MaterialAttrib::get_class_type());
  3677. if (attrib != (const RenderAttrib *)NULL) {
  3678. const MaterialAttrib *ma = DCAST(MaterialAttrib, attrib);
  3679. return new Material(*ma->get_material());
  3680. }
  3681. return NULL;
  3682. }
  3683. ////////////////////////////////////////////////////////////////////
  3684. // Function: NodePath::set_fog
  3685. // Access: Published
  3686. // Description: Sets the geometry at this level and below to render
  3687. // using the indicated fog.
  3688. ////////////////////////////////////////////////////////////////////
  3689. void NodePath::
  3690. set_fog(Fog *fog, int priority) {
  3691. nassertv_always(!is_empty());
  3692. node()->set_attrib(FogAttrib::make(fog), priority);
  3693. }
  3694. ////////////////////////////////////////////////////////////////////
  3695. // Function: NodePath::set_fog_off
  3696. // Access: Published
  3697. // Description: Sets the geometry at this level and below to render
  3698. // using no fog. This is normally the default, but
  3699. // it may be useful to use this to contradict
  3700. // set_fog() at a higher node level (or, with a
  3701. // priority, to override a set_fog() at a lower
  3702. // level).
  3703. ////////////////////////////////////////////////////////////////////
  3704. void NodePath::
  3705. set_fog_off(int priority) {
  3706. nassertv_always(!is_empty());
  3707. node()->set_attrib(FogAttrib::make_off(), priority);
  3708. }
  3709. ////////////////////////////////////////////////////////////////////
  3710. // Function: NodePath::clear_fog
  3711. // Access: Published
  3712. // Description: Completely removes any fog adjustment that may
  3713. // have been set via set_fog() or set_fog_off()
  3714. // from this particular node. This allows whatever
  3715. // fogs might be otherwise affecting the geometry to
  3716. // show instead.
  3717. ////////////////////////////////////////////////////////////////////
  3718. void NodePath::
  3719. clear_fog() {
  3720. nassertv_always(!is_empty());
  3721. node()->clear_attrib(FogAttrib::get_class_type());
  3722. }
  3723. ////////////////////////////////////////////////////////////////////
  3724. // Function: NodePath::has_fog
  3725. // Access: Published
  3726. // Description: Returns true if a fog has been applied to this
  3727. // particular node via set_fog(), false otherwise.
  3728. // This is not the same thing as asking whether the
  3729. // geometry at this node will be rendered with
  3730. // fog, as there may be a fog in effect from a higher or
  3731. // lower level.
  3732. ////////////////////////////////////////////////////////////////////
  3733. bool NodePath::
  3734. has_fog() const {
  3735. nassertr_always(!is_empty(), false);
  3736. const RenderAttrib *attrib =
  3737. node()->get_attrib(FogAttrib::get_class_type());
  3738. if (attrib != (const RenderAttrib *)NULL) {
  3739. const FogAttrib *fa = DCAST(FogAttrib, attrib);
  3740. return !fa->is_off();
  3741. }
  3742. return false;
  3743. }
  3744. ////////////////////////////////////////////////////////////////////
  3745. // Function: NodePath::has_fog_off
  3746. // Access: Published
  3747. // Description: Returns true if a fog has been specifically
  3748. // disabled on this particular node via
  3749. // set_fog_off(), false otherwise. This is not the
  3750. // same thing as asking whether the geometry at this
  3751. // node will be rendered unfogged, as there may be a
  3752. // fog in effect from a higher or lower level.
  3753. ////////////////////////////////////////////////////////////////////
  3754. bool NodePath::
  3755. has_fog_off() const {
  3756. nassertr_always(!is_empty(), false);
  3757. const RenderAttrib *attrib =
  3758. node()->get_attrib(FogAttrib::get_class_type());
  3759. if (attrib != (const RenderAttrib *)NULL) {
  3760. const FogAttrib *fa = DCAST(FogAttrib, attrib);
  3761. return fa->is_off();
  3762. }
  3763. return false;
  3764. }
  3765. ////////////////////////////////////////////////////////////////////
  3766. // Function: NodePath::get_fog
  3767. // Access: Published
  3768. // Description: Returns the fog that has been set on this
  3769. // particular node, or NULL if no fog has been set.
  3770. // This is not necessarily the fog that will be
  3771. // applied to the geometry at or below this level, as
  3772. // another fog at a higher or lower level may
  3773. // override.
  3774. ////////////////////////////////////////////////////////////////////
  3775. Fog *NodePath::
  3776. get_fog() const {
  3777. nassertr_always(!is_empty(), NULL);
  3778. const RenderAttrib *attrib =
  3779. node()->get_attrib(FogAttrib::get_class_type());
  3780. if (attrib != (const RenderAttrib *)NULL) {
  3781. const FogAttrib *fa = DCAST(FogAttrib, attrib);
  3782. return fa->get_fog();
  3783. }
  3784. return NULL;
  3785. }
  3786. ////////////////////////////////////////////////////////////////////
  3787. // Function: NodePath::set_render_mode_wireframe
  3788. // Access: Published
  3789. // Description: Sets up the geometry at this level and below (unless
  3790. // overridden) to render in wireframe mode.
  3791. ////////////////////////////////////////////////////////////////////
  3792. void NodePath::
  3793. set_render_mode_wireframe(int priority) {
  3794. nassertv_always(!is_empty());
  3795. node()->set_attrib(RenderModeAttrib::make(RenderModeAttrib::M_wireframe), priority);
  3796. }
  3797. ////////////////////////////////////////////////////////////////////
  3798. // Function: NodePath::set_render_mode_filled
  3799. // Access: Published
  3800. // Description: Sets up the geometry at this level and below (unless
  3801. // overridden) to render in filled (i.e. not wireframe)
  3802. // mode.
  3803. ////////////////////////////////////////////////////////////////////
  3804. void NodePath::
  3805. set_render_mode_filled(int priority) {
  3806. nassertv_always(!is_empty());
  3807. node()->set_attrib(RenderModeAttrib::make(RenderModeAttrib::M_filled), priority);
  3808. }
  3809. ////////////////////////////////////////////////////////////////////
  3810. // Function: NodePath::set_render_mode
  3811. // Access: Published
  3812. // Description: Sets up the geometry at this level and below (unless
  3813. // overridden) to render in the specified mode and with
  3814. // the indicated line and/or point thickness.
  3815. ////////////////////////////////////////////////////////////////////
  3816. void NodePath::
  3817. set_render_mode(RenderModeAttrib::Mode mode, float thickness, int priority) {
  3818. nassertv_always(!is_empty());
  3819. node()->set_attrib(RenderModeAttrib::make(mode, thickness), priority);
  3820. }
  3821. ////////////////////////////////////////////////////////////////////
  3822. // Function: NodePath::clear_render_mode
  3823. // Access: Published
  3824. // Description: Completely removes any render mode adjustment that
  3825. // may have been set on this node via
  3826. // set_render_mode_wireframe() or
  3827. // set_render_mode_filled().
  3828. ////////////////////////////////////////////////////////////////////
  3829. void NodePath::
  3830. clear_render_mode() {
  3831. nassertv_always(!is_empty());
  3832. node()->clear_attrib(RenderModeAttrib::get_class_type());
  3833. }
  3834. ////////////////////////////////////////////////////////////////////
  3835. // Function: NodePath::has_render_mode
  3836. // Access: Published
  3837. // Description: Returns true if a render mode has been explicitly set
  3838. // on this particular node via set_render_mode() (or
  3839. // set_render_mode_wireframe() or
  3840. // set_render_mode_filled()), false otherwise.
  3841. ////////////////////////////////////////////////////////////////////
  3842. bool NodePath::
  3843. has_render_mode() const {
  3844. nassertr_always(!is_empty(), false);
  3845. return node()->has_attrib(RenderModeAttrib::get_class_type());
  3846. }
  3847. ////////////////////////////////////////////////////////////////////
  3848. // Function: NodePath::get_render_mode
  3849. // Access: Published
  3850. // Description: Returns the render mode that has been specifically
  3851. // set on this node via set_render_mode(), or
  3852. // M_unchanged if nothing has been set.
  3853. ////////////////////////////////////////////////////////////////////
  3854. RenderModeAttrib::Mode NodePath::
  3855. get_render_mode() const {
  3856. nassertr_always(!is_empty(), RenderModeAttrib::M_unchanged);
  3857. const RenderAttrib *attrib =
  3858. node()->get_attrib(RenderModeAttrib::get_class_type());
  3859. if (attrib != (const RenderAttrib *)NULL) {
  3860. const RenderModeAttrib *ta = DCAST(RenderModeAttrib, attrib);
  3861. return ta->get_mode();
  3862. }
  3863. return RenderModeAttrib::M_unchanged;
  3864. }
  3865. ////////////////////////////////////////////////////////////////////
  3866. // Function: NodePath::get_render_mode_thickness
  3867. // Access: Published
  3868. // Description: Returns the render mode thickness that has been
  3869. // specifically set on this node via set_render_mode(),
  3870. // or 0.0 if nothing has been set.
  3871. ////////////////////////////////////////////////////////////////////
  3872. float NodePath::
  3873. get_render_mode_thickness() const {
  3874. nassertr_always(!is_empty(), 0.0f);
  3875. const RenderAttrib *attrib =
  3876. node()->get_attrib(RenderModeAttrib::get_class_type());
  3877. if (attrib != (const RenderAttrib *)NULL) {
  3878. const RenderModeAttrib *ta = DCAST(RenderModeAttrib, attrib);
  3879. return ta->get_thickness();
  3880. }
  3881. return 0.0f;
  3882. }
  3883. ////////////////////////////////////////////////////////////////////
  3884. // Function: NodePath::set_two_sided
  3885. // Access: Published
  3886. // Description: Specifically sets or disables two-sided rendering
  3887. // mode on this particular node. If no other nodes
  3888. // override, this will cause backfacing polygons to be
  3889. // drawn (in two-sided mode, true) or culled (in
  3890. // one-sided mode, false).
  3891. ////////////////////////////////////////////////////////////////////
  3892. void NodePath::
  3893. set_two_sided(bool two_sided, int priority) {
  3894. nassertv_always(!is_empty());
  3895. CullFaceAttrib::Mode mode =
  3896. two_sided ?
  3897. CullFaceAttrib::M_cull_none :
  3898. CullFaceAttrib::M_cull_clockwise;
  3899. node()->set_attrib(CullFaceAttrib::make(mode), priority);
  3900. }
  3901. ////////////////////////////////////////////////////////////////////
  3902. // Function: NodePath::clear_two_sided
  3903. // Access: Published
  3904. // Description: Completely removes any two-sided adjustment that
  3905. // may have been set on this node via set_two_sided().
  3906. // The geometry at this level and below will
  3907. // subsequently be rendered either two-sided or
  3908. // one-sided, according to whatever other nodes may have
  3909. // had set_two_sided() on it, or according to the
  3910. // initial state otherwise.
  3911. ////////////////////////////////////////////////////////////////////
  3912. void NodePath::
  3913. clear_two_sided() {
  3914. nassertv_always(!is_empty());
  3915. node()->clear_attrib(CullFaceAttrib::get_class_type());
  3916. }
  3917. ////////////////////////////////////////////////////////////////////
  3918. // Function: NodePath::has_two_sided
  3919. // Access: Published
  3920. // Description: Returns true if a two-sided adjustment has been
  3921. // explicitly set on this particular node via
  3922. // set_two_sided(). If this returns true, then
  3923. // get_two_sided() may be called to determine which has
  3924. // been set.
  3925. ////////////////////////////////////////////////////////////////////
  3926. bool NodePath::
  3927. has_two_sided() const {
  3928. nassertr_always(!is_empty(), false);
  3929. return node()->has_attrib(CullFaceAttrib::get_class_type());
  3930. }
  3931. ////////////////////////////////////////////////////////////////////
  3932. // Function: NodePath::get_two_sided
  3933. // Access: Published
  3934. // Description: Returns true if two-sided rendering has been
  3935. // specifically set on this node via set_two_sided(), or
  3936. // false if one-sided rendering has been specifically
  3937. // set, or if nothing has been specifically set. See
  3938. // also has_two_sided(). This does not necessarily
  3939. // imply that the geometry will or will not be rendered
  3940. // two-sided, as there may be other nodes that override.
  3941. ////////////////////////////////////////////////////////////////////
  3942. bool NodePath::
  3943. get_two_sided() const {
  3944. nassertr_always(!is_empty(), false);
  3945. const RenderAttrib *attrib =
  3946. node()->get_attrib(CullFaceAttrib::get_class_type());
  3947. if (attrib != (const RenderAttrib *)NULL) {
  3948. const CullFaceAttrib *cfa = DCAST(CullFaceAttrib, attrib);
  3949. return (cfa->get_actual_mode() == CullFaceAttrib::M_cull_none);
  3950. }
  3951. return false;
  3952. }
  3953. ////////////////////////////////////////////////////////////////////
  3954. // Function: NodePath::set_depth_test
  3955. // Access: Published
  3956. // Description: Specifically sets or disables the testing of the
  3957. // depth buffer on this particular node. This is
  3958. // normally on in the 3-d scene graph and off in the 2-d
  3959. // scene graph; it should be on for rendering most 3-d
  3960. // objects properly.
  3961. ////////////////////////////////////////////////////////////////////
  3962. void NodePath::
  3963. set_depth_test(bool depth_test, int priority) {
  3964. nassertv_always(!is_empty());
  3965. DepthTestAttrib::PandaCompareFunc mode =
  3966. depth_test ?
  3967. DepthTestAttrib::M_less :
  3968. DepthTestAttrib::M_none;
  3969. node()->set_attrib(DepthTestAttrib::make(mode), priority);
  3970. }
  3971. ////////////////////////////////////////////////////////////////////
  3972. // Function: NodePath::clear_depth_test
  3973. // Access: Published
  3974. // Description: Completely removes any depth-test adjustment that
  3975. // may have been set on this node via set_depth_test().
  3976. ////////////////////////////////////////////////////////////////////
  3977. void NodePath::
  3978. clear_depth_test() {
  3979. nassertv_always(!is_empty());
  3980. node()->clear_attrib(DepthTestAttrib::get_class_type());
  3981. }
  3982. ////////////////////////////////////////////////////////////////////
  3983. // Function: NodePath::has_depth_test
  3984. // Access: Published
  3985. // Description: Returns true if a depth-test adjustment has been
  3986. // explicitly set on this particular node via
  3987. // set_depth_test(). If this returns true, then
  3988. // get_depth_test() may be called to determine which has
  3989. // been set.
  3990. ////////////////////////////////////////////////////////////////////
  3991. bool NodePath::
  3992. has_depth_test() const {
  3993. nassertr_always(!is_empty(), false);
  3994. return node()->has_attrib(DepthTestAttrib::get_class_type());
  3995. }
  3996. ////////////////////////////////////////////////////////////////////
  3997. // Function: NodePath::get_depth_test
  3998. // Access: Published
  3999. // Description: Returns true if depth-test rendering has been
  4000. // specifically set on this node via set_depth_test(), or
  4001. // false if depth-test rendering has been specifically
  4002. // disabled, or if nothing has been specifically set. See
  4003. // also has_depth_test().
  4004. ////////////////////////////////////////////////////////////////////
  4005. bool NodePath::
  4006. get_depth_test() const {
  4007. nassertr_always(!is_empty(), false);
  4008. const RenderAttrib *attrib =
  4009. node()->get_attrib(DepthTestAttrib::get_class_type());
  4010. if (attrib != (const RenderAttrib *)NULL) {
  4011. const DepthTestAttrib *dta = DCAST(DepthTestAttrib, attrib);
  4012. return (dta->get_mode() != DepthTestAttrib::M_none);
  4013. }
  4014. return false;
  4015. }
  4016. ////////////////////////////////////////////////////////////////////
  4017. // Function: NodePath::set_depth_write
  4018. // Access: Published
  4019. // Description: Specifically sets or disables the writing to the
  4020. // depth buffer on this particular node. This is
  4021. // normally on in the 3-d scene graph and off in the 2-d
  4022. // scene graph; it should be on for rendering most 3-d
  4023. // objects properly.
  4024. ////////////////////////////////////////////////////////////////////
  4025. void NodePath::
  4026. set_depth_write(bool depth_write, int priority) {
  4027. nassertv_always(!is_empty());
  4028. DepthWriteAttrib::Mode mode =
  4029. depth_write ?
  4030. DepthWriteAttrib::M_on :
  4031. DepthWriteAttrib::M_off;
  4032. node()->set_attrib(DepthWriteAttrib::make(mode), priority);
  4033. }
  4034. ////////////////////////////////////////////////////////////////////
  4035. // Function: NodePath::clear_depth_write
  4036. // Access: Published
  4037. // Description: Completely removes any depth-write adjustment that
  4038. // may have been set on this node via set_depth_write().
  4039. ////////////////////////////////////////////////////////////////////
  4040. void NodePath::
  4041. clear_depth_write() {
  4042. nassertv_always(!is_empty());
  4043. node()->clear_attrib(DepthWriteAttrib::get_class_type());
  4044. }
  4045. ////////////////////////////////////////////////////////////////////
  4046. // Function: NodePath::has_depth_write
  4047. // Access: Published
  4048. // Description: Returns true if a depth-write adjustment has been
  4049. // explicitly set on this particular node via
  4050. // set_depth_write(). If this returns true, then
  4051. // get_depth_write() may be called to determine which has
  4052. // been set.
  4053. ////////////////////////////////////////////////////////////////////
  4054. bool NodePath::
  4055. has_depth_write() const {
  4056. nassertr_always(!is_empty(), false);
  4057. return node()->has_attrib(DepthWriteAttrib::get_class_type());
  4058. }
  4059. ////////////////////////////////////////////////////////////////////
  4060. // Function: NodePath::get_depth_write
  4061. // Access: Published
  4062. // Description: Returns true if depth-write rendering has been
  4063. // specifically set on this node via set_depth_write(), or
  4064. // false if depth-write rendering has been specifically
  4065. // disabled, or if nothing has been specifically set. See
  4066. // also has_depth_write().
  4067. ////////////////////////////////////////////////////////////////////
  4068. bool NodePath::
  4069. get_depth_write() const {
  4070. nassertr_always(!is_empty(), false);
  4071. const RenderAttrib *attrib =
  4072. node()->get_attrib(DepthWriteAttrib::get_class_type());
  4073. if (attrib != (const RenderAttrib *)NULL) {
  4074. const DepthWriteAttrib *dta = DCAST(DepthWriteAttrib, attrib);
  4075. return (dta->get_mode() != DepthWriteAttrib::M_off);
  4076. }
  4077. return false;
  4078. }
  4079. ////////////////////////////////////////////////////////////////////
  4080. // Function: NodePath::do_billboard_axis
  4081. // Access: Published
  4082. // Description: Performs a billboard-type rotate to the indicated
  4083. // camera node, one time only, and leaves the object
  4084. // rotated. This is similar in principle to heads_up().
  4085. ////////////////////////////////////////////////////////////////////
  4086. void NodePath::
  4087. do_billboard_axis(const NodePath &camera, float offset) {
  4088. nassertv_always(!is_empty());
  4089. CPT(TransformState) transform = camera.get_transform(get_parent());
  4090. const LMatrix4f &rel_mat = transform->get_mat();
  4091. LVector3f up = LVector3f::up();
  4092. LVector3f rel_pos = -rel_mat.get_row3(3);
  4093. LQuaternionf quat;
  4094. ::heads_up(quat, rel_pos, up);
  4095. set_quat(quat);
  4096. // Also slide the geometry towards the camera according to the
  4097. // offset factor.
  4098. if (offset != 0.0f) {
  4099. LVector3f translate = rel_mat.get_row3(3);
  4100. translate.normalize();
  4101. translate *= offset;
  4102. set_pos(translate);
  4103. }
  4104. }
  4105. ////////////////////////////////////////////////////////////////////
  4106. // Function: NodePath::do_billboard_point_eye
  4107. // Access: Published
  4108. // Description: Performs a billboard-type rotate to the indicated
  4109. // camera node, one time only, and leaves the object
  4110. // rotated. This is similar in principle to look_at(),
  4111. // although the point_eye billboard effect cannot be
  4112. // achieved using the ordinary look_at() call.
  4113. ////////////////////////////////////////////////////////////////////
  4114. void NodePath::
  4115. do_billboard_point_eye(const NodePath &camera, float offset) {
  4116. nassertv_always(!is_empty());
  4117. CPT(TransformState) transform = camera.get_transform(get_parent());
  4118. const LMatrix4f &rel_mat = transform->get_mat();
  4119. LVector3f up = LVector3f::up() * rel_mat;
  4120. LVector3f rel_pos = LVector3f::forward() * rel_mat;
  4121. LQuaternionf quat;
  4122. ::look_at(quat, rel_pos, up);
  4123. set_quat(quat);
  4124. // Also slide the geometry towards the camera according to the
  4125. // offset factor.
  4126. if (offset != 0.0f) {
  4127. LVector3f translate = rel_mat.get_row3(3);
  4128. translate.normalize();
  4129. translate *= offset;
  4130. set_pos(translate);
  4131. }
  4132. }
  4133. ////////////////////////////////////////////////////////////////////
  4134. // Function: NodePath::do_billboard_point_world
  4135. // Access: Published
  4136. // Description: Performs a billboard-type rotate to the indicated
  4137. // camera node, one time only, and leaves the object
  4138. // rotated. This is similar in principle to look_at().
  4139. ////////////////////////////////////////////////////////////////////
  4140. void NodePath::
  4141. do_billboard_point_world(const NodePath &camera, float offset) {
  4142. nassertv_always(!is_empty());
  4143. CPT(TransformState) transform = camera.get_transform(get_parent());
  4144. const LMatrix4f &rel_mat = transform->get_mat();
  4145. LVector3f up = LVector3f::up();
  4146. LVector3f rel_pos = -rel_mat.get_row3(3);
  4147. LQuaternionf quat;
  4148. ::look_at(quat, rel_pos, up);
  4149. set_quat(quat);
  4150. // Also slide the geometry towards the camera according to the
  4151. // offset factor.
  4152. if (offset != 0.0f) {
  4153. LVector3f translate = rel_mat.get_row3(3);
  4154. translate.normalize();
  4155. translate *= offset;
  4156. set_pos(translate);
  4157. }
  4158. }
  4159. ////////////////////////////////////////////////////////////////////
  4160. // Function: NodePath::set_billboard_axis
  4161. // Access: Published
  4162. // Description: Puts a billboard transition on the node such that it
  4163. // will rotate in two dimensions around the up axis,
  4164. // towards a specified "camera" instead of to the
  4165. // viewing camera.
  4166. ////////////////////////////////////////////////////////////////////
  4167. void NodePath::
  4168. set_billboard_axis(const NodePath &camera, float offset) {
  4169. nassertv_always(!is_empty());
  4170. CPT(RenderEffect) billboard = BillboardEffect::make
  4171. (LVector3f::up(), false, true,
  4172. offset, camera, LPoint3f(0.0f, 0.0f, 0.0f));
  4173. node()->set_effect(billboard);
  4174. }
  4175. ////////////////////////////////////////////////////////////////////
  4176. // Function: NodePath::set_billboard_point_eye
  4177. // Access: Published
  4178. // Description: Puts a billboard transition on the node such that it
  4179. // will rotate in three dimensions about the origin,
  4180. // keeping its up vector oriented to the top of the
  4181. // camera, towards a specified "camera" instead of to
  4182. // the viewing camera.
  4183. ////////////////////////////////////////////////////////////////////
  4184. void NodePath::
  4185. set_billboard_point_eye(const NodePath &camera, float offset) {
  4186. nassertv_always(!is_empty());
  4187. CPT(RenderEffect) billboard = BillboardEffect::make
  4188. (LVector3f::up(), true, false,
  4189. offset, camera, LPoint3f(0.0f, 0.0f, 0.0f));
  4190. node()->set_effect(billboard);
  4191. }
  4192. ////////////////////////////////////////////////////////////////////
  4193. // Function: NodePath::set_billboard_point_world
  4194. // Access: Published
  4195. // Description: Puts a billboard transition on the node such that it
  4196. // will rotate in three dimensions about the origin,
  4197. // keeping its up vector oriented to the sky, towards a
  4198. // specified "camera" instead of to the viewing camera.
  4199. ////////////////////////////////////////////////////////////////////
  4200. void NodePath::
  4201. set_billboard_point_world(const NodePath &camera, float offset) {
  4202. nassertv_always(!is_empty());
  4203. CPT(RenderEffect) billboard = BillboardEffect::make
  4204. (LVector3f::up(), false, false,
  4205. offset, camera, LPoint3f(0.0f, 0.0f, 0.0f));
  4206. node()->set_effect(billboard);
  4207. }
  4208. ////////////////////////////////////////////////////////////////////
  4209. // Function: NodePath::clear_billboard
  4210. // Access: Published
  4211. // Description: Removes any billboard effect from the node.
  4212. ////////////////////////////////////////////////////////////////////
  4213. void NodePath::
  4214. clear_billboard() {
  4215. nassertv_always(!is_empty());
  4216. node()->clear_effect(BillboardEffect::get_class_type());
  4217. }
  4218. ////////////////////////////////////////////////////////////////////
  4219. // Function: NodePath::has_billboard
  4220. // Access: Published
  4221. // Description: Returns true if there is any billboard effect on
  4222. // the node.
  4223. ////////////////////////////////////////////////////////////////////
  4224. bool NodePath::
  4225. has_billboard() const {
  4226. nassertr_always(!is_empty(), false);
  4227. return node()->has_effect(BillboardEffect::get_class_type());
  4228. }
  4229. ////////////////////////////////////////////////////////////////////
  4230. // Function: NodePath::set_compass
  4231. // Access: Published
  4232. // Description: Puts a compass effect on the node, so that it will
  4233. // retain a fixed rotation relative to the reference
  4234. // node (or render if the reference node is empty)
  4235. // regardless of the transforms above it.
  4236. ////////////////////////////////////////////////////////////////////
  4237. void NodePath::
  4238. set_compass(const NodePath &reference) {
  4239. nassertv_always(!is_empty());
  4240. node()->set_effect(CompassEffect::make(reference));
  4241. }
  4242. ////////////////////////////////////////////////////////////////////
  4243. // Function: NodePath::clear_compass
  4244. // Access: Published
  4245. // Description: Removes any compass effect from the node.
  4246. ////////////////////////////////////////////////////////////////////
  4247. void NodePath::
  4248. clear_compass() {
  4249. nassertv_always(!is_empty());
  4250. node()->clear_effect(CompassEffect::get_class_type());
  4251. }
  4252. ////////////////////////////////////////////////////////////////////
  4253. // Function: NodePath::has_compass
  4254. // Access: Published
  4255. // Description: Returns true if there is any compass effect on
  4256. // the node.
  4257. ////////////////////////////////////////////////////////////////////
  4258. bool NodePath::
  4259. has_compass() const {
  4260. nassertr_always(!is_empty(), false);
  4261. return node()->has_effect(CompassEffect::get_class_type());
  4262. }
  4263. ////////////////////////////////////////////////////////////////////
  4264. // Function: NodePath::set_transparency
  4265. // Access: Published
  4266. // Description: Specifically sets or disables transparent rendering
  4267. // mode on this particular node. If no other nodes
  4268. // override, this will cause items with a non-1 value
  4269. // for alpha color to be rendered partially transparent.
  4270. ////////////////////////////////////////////////////////////////////
  4271. void NodePath::
  4272. set_transparency(TransparencyAttrib::Mode mode, int priority) {
  4273. nassertv_always(!is_empty());
  4274. node()->set_attrib(TransparencyAttrib::make(mode), priority);
  4275. }
  4276. ////////////////////////////////////////////////////////////////////
  4277. // Function: NodePath::clear_transparency
  4278. // Access: Published
  4279. // Description: Completely removes any transparency adjustment that
  4280. // may have been set on this node via set_transparency().
  4281. // The geometry at this level and below will
  4282. // subsequently be rendered either transparent or not,
  4283. // to whatever other nodes may have had
  4284. // set_transparency() on them.
  4285. ////////////////////////////////////////////////////////////////////
  4286. void NodePath::
  4287. clear_transparency() {
  4288. nassertv_always(!is_empty());
  4289. node()->clear_attrib(TransparencyAttrib::get_class_type());
  4290. }
  4291. ////////////////////////////////////////////////////////////////////
  4292. // Function: NodePath::has_transparency
  4293. // Access: Published
  4294. // Description: Returns true if a transparent-rendering adjustment
  4295. // has been explicitly set on this particular node via
  4296. // set_transparency(). If this returns true, then
  4297. // get_transparency() may be called to determine whether
  4298. // transparency has been explicitly enabled or
  4299. // explicitly disabled for this node.
  4300. ////////////////////////////////////////////////////////////////////
  4301. bool NodePath::
  4302. has_transparency() const {
  4303. nassertr_always(!is_empty(), false);
  4304. return node()->has_attrib(TransparencyAttrib::get_class_type());
  4305. }
  4306. ////////////////////////////////////////////////////////////////////
  4307. // Function: NodePath::get_transparency
  4308. // Access: Published
  4309. // Description: Returns the transparent rendering that has been
  4310. // specifically set on this node via set_transparency(), or
  4311. // M_none if nontransparent rendering has been specifically
  4312. // set, or if nothing has been specifically set. See
  4313. // also has_transparency(). This does not necessarily
  4314. // imply that the geometry will or will not be rendered
  4315. // transparent, as there may be other nodes that override.
  4316. ////////////////////////////////////////////////////////////////////
  4317. TransparencyAttrib::Mode NodePath::
  4318. get_transparency() const {
  4319. nassertr_always(!is_empty(), TransparencyAttrib::M_none);
  4320. const RenderAttrib *attrib =
  4321. node()->get_attrib(TransparencyAttrib::get_class_type());
  4322. if (attrib != (const RenderAttrib *)NULL) {
  4323. const TransparencyAttrib *ta = DCAST(TransparencyAttrib, attrib);
  4324. return ta->get_mode();
  4325. }
  4326. return TransparencyAttrib::M_none;
  4327. }
  4328. ////////////////////////////////////////////////////////////////////
  4329. // Function: NodePath::set_antialias
  4330. // Access: Published
  4331. // Description: Specifies the antialiasing type that should be
  4332. // applied at this node and below. See AntialiasAttrib.
  4333. ////////////////////////////////////////////////////////////////////
  4334. void NodePath::
  4335. set_antialias(unsigned short mode, int priority) {
  4336. nassertv_always(!is_empty());
  4337. node()->set_attrib(AntialiasAttrib::make(mode), priority);
  4338. }
  4339. ////////////////////////////////////////////////////////////////////
  4340. // Function: NodePath::clear_antialias
  4341. // Access: Published
  4342. // Description: Completely removes any antialias setting that
  4343. // may have been set on this node via set_antialias().
  4344. ////////////////////////////////////////////////////////////////////
  4345. void NodePath::
  4346. clear_antialias() {
  4347. nassertv_always(!is_empty());
  4348. node()->clear_attrib(AntialiasAttrib::get_class_type());
  4349. }
  4350. ////////////////////////////////////////////////////////////////////
  4351. // Function: NodePath::has_antialias
  4352. // Access: Published
  4353. // Description: Returns true if an antialias setting has been
  4354. // explicitly mode on this particular node via
  4355. // set_antialias(). If this returns true, then
  4356. // get_antialias() may be called to determine what the
  4357. // setting was.
  4358. ////////////////////////////////////////////////////////////////////
  4359. bool NodePath::
  4360. has_antialias() const {
  4361. nassertr_always(!is_empty(), false);
  4362. return node()->has_attrib(AntialiasAttrib::get_class_type());
  4363. }
  4364. ////////////////////////////////////////////////////////////////////
  4365. // Function: NodePath::get_antialias
  4366. // Access: Published
  4367. // Description: Returns the antialias setting that has been
  4368. // specifically set on this node via set_antialias(), or
  4369. // M_none if no setting has been made.
  4370. ////////////////////////////////////////////////////////////////////
  4371. unsigned short NodePath::
  4372. get_antialias() const {
  4373. nassertr_always(!is_empty(), AntialiasAttrib::M_none);
  4374. const RenderAttrib *attrib =
  4375. node()->get_attrib(AntialiasAttrib::get_class_type());
  4376. if (attrib != (const RenderAttrib *)NULL) {
  4377. const AntialiasAttrib *ta = DCAST(AntialiasAttrib, attrib);
  4378. return ta->get_mode();
  4379. }
  4380. return AntialiasAttrib::M_none;
  4381. }
  4382. ////////////////////////////////////////////////////////////////////
  4383. // Function: NodePath::get_hidden_ancestor
  4384. // Access: Published
  4385. // Description: Returns the NodePath at or above the referenced node
  4386. // that is hidden to the indicated camera(s), or an
  4387. // empty NodePath if no ancestor of the referenced node
  4388. // is hidden (and the node should be visible).
  4389. ////////////////////////////////////////////////////////////////////
  4390. NodePath NodePath::
  4391. get_hidden_ancestor(DrawMask camera_mask) const {
  4392. NodePathComponent *comp;
  4393. for (comp = _head;
  4394. comp != (NodePathComponent *)NULL;
  4395. comp = comp->get_next()) {
  4396. PandaNode *node = comp->get_node();
  4397. if ((node->get_draw_mask() & camera_mask).is_zero()) {
  4398. NodePath result;
  4399. result._head = comp;
  4400. return result;
  4401. }
  4402. }
  4403. return not_found();
  4404. }
  4405. ////////////////////////////////////////////////////////////////////
  4406. // Function: NodePath::stash
  4407. // Access: Published
  4408. // Description: Removes the referenced node (and the entire subgraph
  4409. // below this node) from the scene graph in any normal
  4410. // sense. The node will no longer be visible and is not
  4411. // tested for collisions; furthermore, no normal scene
  4412. // graph traversal will visit the node. The node's
  4413. // bounding volume no longer contributes to its parent's
  4414. // bounding volume.
  4415. //
  4416. // A stashed node cannot be located by a normal find()
  4417. // operation (although a special find string can still
  4418. // retrieve it).
  4419. ////////////////////////////////////////////////////////////////////
  4420. void NodePath::
  4421. stash(int sort) {
  4422. nassertv_always(!is_singleton() && !is_empty());
  4423. nassertv(verify_complete());
  4424. bool reparented = PandaNode::reparent(_head->get_next(), _head, sort, true);
  4425. nassertv(reparented);
  4426. }
  4427. ////////////////////////////////////////////////////////////////////
  4428. // Function: NodePath::unstash
  4429. // Access: Published
  4430. // Description: Undoes the effect of a previous stash() on this
  4431. // node: makes the referenced node (and the entire
  4432. // subgraph below this node) once again part of the
  4433. // scene graph.
  4434. ////////////////////////////////////////////////////////////////////
  4435. void NodePath::
  4436. unstash(int sort) {
  4437. nassertv_always(!is_singleton() && !is_empty());
  4438. nassertv(verify_complete());
  4439. bool reparented = PandaNode::reparent(_head->get_next(), _head, sort, false);
  4440. nassertv(reparented);
  4441. }
  4442. ////////////////////////////////////////////////////////////////////
  4443. // Function: NodePath::unstash_all
  4444. // Access: Published
  4445. // Description: Unstashes this node and all stashed child nodes.
  4446. ////////////////////////////////////////////////////////////////////
  4447. void NodePath::
  4448. unstash_all() {
  4449. NodePathCollection stashed_descendents = find_all_matches("**/@@*");
  4450. stashed_descendents.unstash();
  4451. unstash();
  4452. }
  4453. ////////////////////////////////////////////////////////////////////
  4454. // Function: NodePath::get_stashed_ancestor
  4455. // Access: Published
  4456. // Description: Returns the NodePath at or above the referenced node
  4457. // that is stashed, or an empty NodePath if no ancestor
  4458. // of the referenced node is stashed (and the node should
  4459. // be visible).
  4460. ////////////////////////////////////////////////////////////////////
  4461. NodePath NodePath::
  4462. get_stashed_ancestor() const {
  4463. NodePathComponent *comp = _head;
  4464. if (comp != (NodePathComponent *)NULL) {
  4465. NodePathComponent *next = comp->get_next();
  4466. while (next != (NodePathComponent *)NULL) {
  4467. PandaNode *node = comp->get_node();
  4468. PandaNode *parent_node = next->get_node();
  4469. if (parent_node->find_stashed(node) >= 0) {
  4470. NodePath result;
  4471. result._head = comp;
  4472. return result;
  4473. }
  4474. comp = next;
  4475. next = next->get_next();
  4476. }
  4477. }
  4478. return not_found();
  4479. }
  4480. ////////////////////////////////////////////////////////////////////
  4481. // Function: NodePath::verify_complete
  4482. // Access: Published
  4483. // Description: Returns true if all of the nodes described in the
  4484. // NodePath are connected, or false otherwise.
  4485. ////////////////////////////////////////////////////////////////////
  4486. bool NodePath::
  4487. verify_complete() const {
  4488. if (is_empty()) {
  4489. return true;
  4490. }
  4491. const NodePathComponent *comp = _head;
  4492. nassertr(comp != (const NodePathComponent *)NULL, false);
  4493. PandaNode *node = comp->get_node();
  4494. nassertr(node != (const PandaNode *)NULL, false);
  4495. int length = comp->get_length();
  4496. comp = comp->get_next();
  4497. length--;
  4498. while (comp != (const NodePathComponent *)NULL) {
  4499. PandaNode *next_node = comp->get_node();
  4500. nassertr(next_node != (const PandaNode *)NULL, false);
  4501. if (node->find_parent(next_node) < 0) {
  4502. pgraph_cat.warning()
  4503. << *this << " is incomplete; " << *node << " is not a child of "
  4504. << *next_node << "\n";
  4505. return false;
  4506. }
  4507. if (comp->get_length() != length) {
  4508. pgraph_cat.warning()
  4509. << *this << " is incomplete; length at " << *next_node
  4510. << " indicates " << comp->get_length() << " while length at "
  4511. << *node << " indicates " << length << "\n";
  4512. return false;
  4513. }
  4514. node = next_node;
  4515. comp = comp->get_next();
  4516. length--;
  4517. }
  4518. return true;
  4519. }
  4520. ////////////////////////////////////////////////////////////////////
  4521. // Function: NodePath::prepare_scene
  4522. // Access: Published
  4523. // Description: Walks through the scene graph beginning at the bottom
  4524. // node, and does whatever initialization is required to
  4525. // render the scene properly with the indicated GSG. It
  4526. // is not strictly necessary to call this, since the GSG
  4527. // will initialize itself when the scene is rendered,
  4528. // but this may take some of the overhead away from that
  4529. // process.
  4530. //
  4531. // In particular, this will ensure that textures within
  4532. // the scene are loaded in texture memory, and display
  4533. // lists are built up from static geometry.
  4534. ////////////////////////////////////////////////////////////////////
  4535. void NodePath::
  4536. prepare_scene(GraphicsStateGuardianBase *gsg) {
  4537. nassertv_always(!is_empty());
  4538. PreparedGraphicsObjects *prepared_objects = gsg->get_prepared_objects();
  4539. CPT(RenderState) net_state = get_net_state();
  4540. r_prepare_scene(node(), net_state, prepared_objects);
  4541. }
  4542. ////////////////////////////////////////////////////////////////////
  4543. // Function: NodePath::show_bounds
  4544. // Access: Published
  4545. // Description: Causes the bounding volume of the bottom node and all
  4546. // of its descendants (that is, the bounding volume
  4547. // associated with the the bottom arc) to be rendered,
  4548. // if possible. The rendering method is less than
  4549. // optimal; this is intended primarily for debugging.
  4550. ////////////////////////////////////////////////////////////////////
  4551. void NodePath::
  4552. show_bounds() {
  4553. nassertv_always(!is_empty());
  4554. node()->set_effect(ShowBoundsEffect::make(false));
  4555. }
  4556. ////////////////////////////////////////////////////////////////////
  4557. // Function: NodePath::show_tight_bounds
  4558. // Access: Published
  4559. // Description: Similar to show_bounds(), this draws a bounding box
  4560. // representing the "tight" bounds of this node and all
  4561. // of its descendants. The bounding box is recomputed
  4562. // every frame by reexamining all of the vertices; this
  4563. // is far from efficient, but this is intended for
  4564. // debugging.
  4565. ////////////////////////////////////////////////////////////////////
  4566. void NodePath::
  4567. show_tight_bounds() {
  4568. nassertv_always(!is_empty());
  4569. node()->set_effect(ShowBoundsEffect::make(true));
  4570. }
  4571. ////////////////////////////////////////////////////////////////////
  4572. // Function: NodePath::hide_bounds
  4573. // Access: Published
  4574. // Description: Stops the rendering of the bounding volume begun with
  4575. // show_bounds().
  4576. ////////////////////////////////////////////////////////////////////
  4577. void NodePath::
  4578. hide_bounds() {
  4579. nassertv_always(!is_empty());
  4580. node()->clear_effect(ShowBoundsEffect::get_class_type());
  4581. }
  4582. ////////////////////////////////////////////////////////////////////
  4583. // Function: NodePath::get_bounds
  4584. // Access: Published
  4585. // Description: Returns a newly-allocated bounding volume containing
  4586. // the bottom node and all of its descendants. This is
  4587. // the bounding volume on the bottom arc, converted to
  4588. // the local coordinate space of the node.
  4589. ////////////////////////////////////////////////////////////////////
  4590. PT(BoundingVolume) NodePath::
  4591. get_bounds() const {
  4592. nassertr_always(!is_empty(), new BoundingSphere);
  4593. return node()->get_bound().make_copy();
  4594. }
  4595. ////////////////////////////////////////////////////////////////////
  4596. // Function: NodePath::force_recompute_bounds
  4597. // Access: Published
  4598. // Description: Forces the recomputing of all the bounding volumes at
  4599. // every node in the subgraph beginning at this node and
  4600. // below.
  4601. //
  4602. // This should not normally need to be called, since the
  4603. // bounding volumes are supposed to be recomputed
  4604. // automatically when necessary. It may be useful when
  4605. // debugging, to verify that the bounding volumes have
  4606. // not become inadvertently stale; it may also be useful
  4607. // to force animated characters to update their bounding
  4608. // volumes (which does not presently happen
  4609. // automatically).
  4610. ////////////////////////////////////////////////////////////////////
  4611. void NodePath::
  4612. force_recompute_bounds() {
  4613. nassertv_always(!is_empty());
  4614. r_force_recompute_bounds(node());
  4615. }
  4616. ////////////////////////////////////////////////////////////////////
  4617. // Function: NodePath::write_bounds
  4618. // Access: Published
  4619. // Description: Writes a description of the bounding volume
  4620. // containing the bottom node and all of its descendants
  4621. // to the indicated output stream.
  4622. ////////////////////////////////////////////////////////////////////
  4623. void NodePath::
  4624. write_bounds(ostream &out) const {
  4625. get_bounds()->write(out);
  4626. }
  4627. ////////////////////////////////////////////////////////////////////
  4628. // Function: NodePath::calc_tight_bounds
  4629. // Access: Published
  4630. // Description: Calculates the minimum and maximum vertices of all
  4631. // Geoms at this NodePath's bottom node and below. This
  4632. // is a tight bounding box; it will generally be tighter
  4633. // than the bounding volume returned by get_bounds()
  4634. // (but it is more expensive to compute).
  4635. //
  4636. // The return value is true if any points are within the
  4637. // bounding volume, or false if none are.
  4638. ////////////////////////////////////////////////////////////////////
  4639. bool NodePath::
  4640. calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point) {
  4641. min_point.set(0.0f, 0.0f, 0.0f);
  4642. max_point.set(0.0f, 0.0f, 0.0f);
  4643. nassertr_always(!is_empty(), false);
  4644. bool found_any = false;
  4645. node()->calc_tight_bounds(min_point, max_point, found_any,
  4646. TransformState::make_identity());
  4647. return found_any;
  4648. }
  4649. ////////////////////////////////////////////////////////////////////
  4650. // Function: NodePath::analyze
  4651. // Access: Published
  4652. // Description: Analyzes the geometry below this node and reports the
  4653. // number of vertices, triangles, etc. This is the same
  4654. // information reported by the bam-info program.
  4655. ////////////////////////////////////////////////////////////////////
  4656. void NodePath::
  4657. analyze() const {
  4658. nassertv_always(!is_empty());
  4659. SceneGraphAnalyzer sga;
  4660. sga.add_node(node());
  4661. sga.write(nout);
  4662. }
  4663. ////////////////////////////////////////////////////////////////////
  4664. // Function: NodePath::flatten_light
  4665. // Access: Published
  4666. // Description: Lightly flattens out the hierarchy below this node by
  4667. // applying transforms, colors, and texture matrices
  4668. // from the arcs onto the vertices, but does not remove
  4669. // any nodes.
  4670. //
  4671. // This can result in improved rendering performance
  4672. // because there will be fewer transforms in the
  4673. // resulting scene graph, but the number of nodes will
  4674. // remain the same.
  4675. //
  4676. // Particularly, any NodePaths that reference nodes
  4677. // within this hierarchy will not be damaged. However,
  4678. // since this operation will remove transforms from the
  4679. // scene graph, it may be dangerous to apply to arcs
  4680. // where you expect to dynamically modify the transform,
  4681. // or where you expect the geometry to remain in a
  4682. // particular local coordinate system.
  4683. //
  4684. // The return value is always 0, since flatten_light
  4685. // does not remove any arcs.
  4686. ////////////////////////////////////////////////////////////////////
  4687. int NodePath::
  4688. flatten_light() {
  4689. nassertr_always(!is_empty(), 0);
  4690. SceneGraphReducer gr;
  4691. gr.apply_attribs(node());
  4692. return 0;
  4693. }
  4694. ////////////////////////////////////////////////////////////////////
  4695. // Function: NodePath::flatten_medium
  4696. // Access: Published
  4697. // Description: A more thorough flattening than flatten_light(), this
  4698. // first applies all the transforms, colors, and texture
  4699. // matrices from the arcs onto the vertices, and then
  4700. // removes unneeded grouping nodes--nodes that have
  4701. // exactly one child, for instance, but have no special
  4702. // properties in themselves.
  4703. //
  4704. // This results in improved perforamance over
  4705. // flatten_light() because the number of nodes in the
  4706. // scene graph is reduced.
  4707. //
  4708. // If max_children is specified, it represents the
  4709. // maximum number of children a node is allowed to have
  4710. // and still be flattened. Normally, this is 1; we
  4711. // don't typically want to flatten a node that has
  4712. // multiple children. However, sometimes this may be
  4713. // desirable; set this parameter to control the limit.
  4714. // If this is set to -1, there is no limit.
  4715. //
  4716. // The return value is the number of arcs removed.
  4717. ////////////////////////////////////////////////////////////////////
  4718. int NodePath::
  4719. flatten_medium() {
  4720. nassertr_always(!is_empty(), 0);
  4721. SceneGraphReducer gr;
  4722. gr.apply_attribs(node());
  4723. int num_removed = gr.flatten(node(), 0);
  4724. return num_removed;
  4725. }
  4726. ////////////////////////////////////////////////////////////////////
  4727. // Function: NodePath::flatten_strong
  4728. // Access: Published
  4729. // Description: The strongest possible flattening. This first
  4730. // applies all of the transforms to the vertices, as in
  4731. // flatten_medium(), but then it will combine sibling
  4732. // nodes together when possible, in addition to removing
  4733. // unnecessary parent-child nodes. This can result in
  4734. // substantially fewer nodes, but any nicely-grouped
  4735. // hierachical bounding volumes may be lost.
  4736. //
  4737. // It is generally a good idea to apply this kind of
  4738. // flattening only to nodes that will be culled largely
  4739. // as a single unit, like a car. Applying this to an
  4740. // entire scene may result in overall poorer performance
  4741. // because of less-effective culling.
  4742. ////////////////////////////////////////////////////////////////////
  4743. int NodePath::
  4744. flatten_strong() {
  4745. nassertr_always(!is_empty(), 0);
  4746. SceneGraphReducer gr;
  4747. gr.apply_attribs(node());
  4748. int num_removed = gr.flatten(node(), ~0);
  4749. return num_removed;
  4750. }
  4751. ////////////////////////////////////////////////////////////////////
  4752. // Function: NodePath::find_net_tag
  4753. // Access: Published
  4754. // Description: Returns the lowest ancestor of this node that
  4755. // contains a tag definition with the indicated key, if
  4756. // any, or an empty NodePath if no ancestor of this node
  4757. // contains this tag definition. See set_tag().
  4758. ////////////////////////////////////////////////////////////////////
  4759. NodePath NodePath::
  4760. find_net_tag(const string &key) const {
  4761. if (is_empty()) {
  4762. return NodePath::not_found();
  4763. }
  4764. if (has_tag(key)) {
  4765. return *this;
  4766. }
  4767. return get_parent().find_net_tag(key);
  4768. }
  4769. #ifdef HAVE_PYTHON
  4770. ////////////////////////////////////////////////////////////////////
  4771. // Function: NodePath::find_net_python_tag
  4772. // Access: Published
  4773. // Description: Returns the lowest ancestor of this node that
  4774. // contains a tag definition with the indicated key, if
  4775. // any, or an empty NodePath if no ancestor of this node
  4776. // contains this tag definition. See set_python_tag().
  4777. ////////////////////////////////////////////////////////////////////
  4778. NodePath NodePath::
  4779. find_net_python_tag(const string &key) const {
  4780. if (is_empty()) {
  4781. return NodePath::not_found();
  4782. }
  4783. if (has_python_tag(key)) {
  4784. return *this;
  4785. }
  4786. return get_parent().find_net_python_tag(key);
  4787. }
  4788. #endif // HAVE_PYTHON
  4789. ////////////////////////////////////////////////////////////////////
  4790. // Function: NodePath::write_bam_file
  4791. // Access: Published
  4792. // Description: Writes the contents of this node and below out to a
  4793. // bam file with the indicated filename. This file may
  4794. // then be read in again, as is, at some later point.
  4795. // Returns true if successful, false on some kind of
  4796. // error.
  4797. ////////////////////////////////////////////////////////////////////
  4798. bool NodePath::
  4799. write_bam_file(const string &filename) const {
  4800. nassertr_always(!is_empty(), false);
  4801. BamFile bam_file;
  4802. bool okflag = false;
  4803. if (bam_file.open_write(filename)) {
  4804. if (bam_file.write_object(node())) {
  4805. okflag = true;
  4806. }
  4807. bam_file.close();
  4808. }
  4809. return okflag;
  4810. }
  4811. ////////////////////////////////////////////////////////////////////
  4812. // Function: NodePath::find_common_ancestor
  4813. // Access: Private, Static
  4814. // Description: Walks up from both NodePaths to find the first node
  4815. // that both have in common, if any. Fills a_count and
  4816. // b_count with the number of nodes below the common
  4817. // node in each path.
  4818. //
  4819. // The return value is the NodePathComponent of the node
  4820. // they have in common, or NULL if they have nothing in
  4821. // common.
  4822. ////////////////////////////////////////////////////////////////////
  4823. NodePathComponent *NodePath::
  4824. find_common_ancestor(const NodePath &a, const NodePath &b,
  4825. int &a_count, int &b_count) {
  4826. nassertr(!a.is_empty() && !b.is_empty(), NULL);
  4827. NodePathComponent *ac = a._head;
  4828. NodePathComponent *bc = b._head;
  4829. a_count = 0;
  4830. b_count = 0;
  4831. // Shorten up the longer one until they are the same length.
  4832. while (ac->get_length() > bc->get_length()) {
  4833. nassertr(ac != (NodePathComponent *)NULL, NULL);
  4834. ac = ac->get_next();
  4835. a_count++;
  4836. }
  4837. while (bc->get_length() > ac->get_length()) {
  4838. nassertr(bc != (NodePathComponent *)NULL, NULL);
  4839. bc = bc->get_next();
  4840. b_count++;
  4841. }
  4842. // Now shorten them both up until we reach the same component.
  4843. while (ac != bc) {
  4844. // These shouldn't go to NULL unless they both go there together.
  4845. nassertr(ac != (NodePathComponent *)NULL, NULL);
  4846. nassertr(bc != (NodePathComponent *)NULL, NULL);
  4847. ac = ac->get_next();
  4848. a_count++;
  4849. bc = bc->get_next();
  4850. b_count++;
  4851. }
  4852. return ac;
  4853. }
  4854. ////////////////////////////////////////////////////////////////////
  4855. // Function: NodePath::r_get_net_state
  4856. // Access: Private
  4857. // Description: Recursively determines the net state changes to the
  4858. // indicated component node from the root of the graph.
  4859. ////////////////////////////////////////////////////////////////////
  4860. CPT(RenderState) NodePath::
  4861. r_get_net_state(NodePathComponent *comp) const {
  4862. if (comp == (NodePathComponent *)NULL) {
  4863. return RenderState::make_empty();
  4864. } else {
  4865. CPT(RenderState) state = comp->get_node()->get_state();
  4866. return r_get_net_state(comp->get_next())->compose(state);
  4867. }
  4868. }
  4869. ////////////////////////////////////////////////////////////////////
  4870. // Function: NodePath::r_get_partial_state
  4871. // Access: Private
  4872. // Description: Recursively determines the net state changes to the
  4873. // indicated component node from the nth node above it.
  4874. // If n exceeds the length of the path, this returns the
  4875. // net transform from the root of the graph.
  4876. ////////////////////////////////////////////////////////////////////
  4877. CPT(RenderState) NodePath::
  4878. r_get_partial_state(NodePathComponent *comp, int n) const {
  4879. if (n == 0 || comp == (NodePathComponent *)NULL) {
  4880. return RenderState::make_empty();
  4881. } else {
  4882. CPT(RenderState) state = comp->get_node()->get_state();
  4883. return r_get_partial_state(comp->get_next(), n - 1)->compose(state);
  4884. }
  4885. }
  4886. ////////////////////////////////////////////////////////////////////
  4887. // Function: NodePath::r_get_net_transform
  4888. // Access: Private
  4889. // Description: Recursively determines the net transform to the
  4890. // indicated component node from the root of the graph.
  4891. ////////////////////////////////////////////////////////////////////
  4892. CPT(TransformState) NodePath::
  4893. r_get_net_transform(NodePathComponent *comp) const {
  4894. if (comp == (NodePathComponent *)NULL) {
  4895. return TransformState::make_identity();
  4896. } else {
  4897. CPT(TransformState) net_transform = r_get_net_transform(comp->get_next());
  4898. CPT(TransformState) transform = comp->get_node()->get_transform();
  4899. CPT(RenderEffects) effects = comp->get_node()->get_effects();
  4900. if (effects->has_adjust_transform()) {
  4901. effects->adjust_transform(net_transform, transform);
  4902. }
  4903. return net_transform->compose(transform);
  4904. }
  4905. }
  4906. ////////////////////////////////////////////////////////////////////
  4907. // Function: NodePath::r_get_partial_transform
  4908. // Access: Private
  4909. // Description: Recursively determines the net transform to the
  4910. // indicated component node from the nth node above it.
  4911. // If n exceeds the length of the path, this returns the
  4912. // net transform from the root of the graph.
  4913. //
  4914. // If any node in the path had a net_transform effect
  4915. // applied, returns NULL--in this case the partial
  4916. // transform cannot be easily determined.
  4917. ////////////////////////////////////////////////////////////////////
  4918. CPT(TransformState) NodePath::
  4919. r_get_partial_transform(NodePathComponent *comp, int n) const {
  4920. if (n == 0 || comp == (NodePathComponent *)NULL) {
  4921. return TransformState::make_identity();
  4922. } else {
  4923. if (comp->get_node()->get_effects()->has_adjust_transform()) {
  4924. return NULL;
  4925. }
  4926. CPT(TransformState) transform = comp->get_node()->get_transform();
  4927. CPT(TransformState) partial = r_get_partial_transform(comp->get_next(), n - 1);
  4928. if (partial == (const TransformState *)NULL) {
  4929. return NULL;
  4930. }
  4931. return partial->compose(transform);
  4932. }
  4933. }
  4934. ////////////////////////////////////////////////////////////////////
  4935. // Function: NodePath::r_get_net_prev_transform
  4936. // Access: Private
  4937. // Description: Recursively determines the net "previous" transform
  4938. // to the indicated component node from the root of the
  4939. // graph.
  4940. ////////////////////////////////////////////////////////////////////
  4941. CPT(TransformState) NodePath::
  4942. r_get_net_prev_transform(NodePathComponent *comp) const {
  4943. if (comp == (NodePathComponent *)NULL) {
  4944. return TransformState::make_identity();
  4945. } else {
  4946. CPT(TransformState) transform = comp->get_node()->get_prev_transform();
  4947. return r_get_net_prev_transform(comp->get_next())->compose(transform);
  4948. }
  4949. }
  4950. ////////////////////////////////////////////////////////////////////
  4951. // Function: NodePath::r_get_partial_prev_transform
  4952. // Access: Private
  4953. // Description: Recursively determines the net "previous" transform
  4954. // to the indicated component node from the nth node
  4955. // above it. If n exceeds the length of the path, this
  4956. // returns the net previous transform from the root of
  4957. // the graph.
  4958. ////////////////////////////////////////////////////////////////////
  4959. CPT(TransformState) NodePath::
  4960. r_get_partial_prev_transform(NodePathComponent *comp, int n) const {
  4961. if (n == 0 || comp == (NodePathComponent *)NULL) {
  4962. return TransformState::make_identity();
  4963. } else {
  4964. CPT(TransformState) transform = comp->get_node()->get_prev_transform();
  4965. return r_get_partial_prev_transform(comp->get_next(), n - 1)->compose(transform);
  4966. }
  4967. }
  4968. ////////////////////////////////////////////////////////////////////
  4969. // Function: NodePath::find_matches
  4970. // Access: Private
  4971. // Description: Finds up to max_matches matches against the given
  4972. // path string from this node and deeper. The
  4973. // max_matches count indicates the maximum number of
  4974. // matches to return, or -1 not to limit the number
  4975. // returned.
  4976. ////////////////////////////////////////////////////////////////////
  4977. void NodePath::
  4978. find_matches(NodePathCollection &result, const string &path,
  4979. int max_matches) const {
  4980. if (is_empty()) {
  4981. pgraph_cat.warning()
  4982. << "Attempt to extend an empty NodePath by '" << path
  4983. << "'.\n";
  4984. return;
  4985. }
  4986. FindApproxPath approx_path;
  4987. if (approx_path.add_string(path)) {
  4988. find_matches(result, approx_path, max_matches);
  4989. }
  4990. }
  4991. ////////////////////////////////////////////////////////////////////
  4992. // Function: NodePath::find_matches
  4993. // Access: Private
  4994. // Description: Finds up to max_matches matches against the given
  4995. // approx_path from this node and deeper. The
  4996. // max_matches count indicates the maximum number of
  4997. // matches to return, or -1 not to limit the number
  4998. // returned.
  4999. ////////////////////////////////////////////////////////////////////
  5000. void NodePath::
  5001. find_matches(NodePathCollection &result, FindApproxPath &approx_path,
  5002. int max_matches) const {
  5003. if (is_empty()) {
  5004. pgraph_cat.warning()
  5005. << "Attempt to extend an empty NodePath by: " << approx_path << ".\n";
  5006. return;
  5007. }
  5008. // We start with just one entry on the level.
  5009. FindApproxLevelEntry *level =
  5010. new FindApproxLevelEntry(WorkingNodePath(*this), approx_path);
  5011. nassertv(level->_node_path.is_valid());
  5012. find_matches(result, level, max_matches);
  5013. }
  5014. ////////////////////////////////////////////////////////////////////
  5015. // Function: NodePath::find_matches
  5016. // Access: Private
  5017. // Description: The fundamental implementation of find_matches(),
  5018. // given a starting level (a linked list of
  5019. // FindApproxLevelEntry objects).
  5020. ////////////////////////////////////////////////////////////////////
  5021. void NodePath::
  5022. find_matches(NodePathCollection &result, FindApproxLevelEntry *level,
  5023. int max_matches) const {
  5024. int num_levels_remaining = _max_search_depth;
  5025. FindApproxLevelEntry *deleted_entries = NULL;
  5026. while (num_levels_remaining > 0 && level != NULL) {
  5027. if (pgraph_cat.is_spam()) {
  5028. pgraph_cat.spam()
  5029. << "find_matches pass: " << result << ", "
  5030. << max_matches << ", " << num_levels_remaining << "\n";
  5031. level->write_level(pgraph_cat.spam(false), 4);
  5032. }
  5033. num_levels_remaining--;
  5034. FindApproxLevelEntry *next_level = NULL;
  5035. // For each node in the current level, build up the set of possible
  5036. // matches in the next level.
  5037. FindApproxLevelEntry *entry = level;
  5038. while (entry != (FindApproxLevelEntry *)NULL) {
  5039. if (entry->consider_node(result, next_level, max_matches, 0)) {
  5040. // If we found the requisite number of matches, we can stop.
  5041. // Delete all remaining entries and return immediately.
  5042. while (entry != (FindApproxLevelEntry *)NULL) {
  5043. FindApproxLevelEntry *next = entry->_next;
  5044. delete entry;
  5045. entry = next;
  5046. }
  5047. while (next_level != (FindApproxLevelEntry *)NULL) {
  5048. FindApproxLevelEntry *next = next_level->_next;
  5049. delete next_level;
  5050. next_level = next;
  5051. }
  5052. while (deleted_entries != (FindApproxLevelEntry *)NULL) {
  5053. FindApproxLevelEntry *next = deleted_entries->_next;
  5054. delete deleted_entries;
  5055. deleted_entries = next;
  5056. }
  5057. return;
  5058. }
  5059. // Move the entry to the delete chain so we can delete it before
  5060. // we return from this method. (We can't delete it immediately,
  5061. // because there might be WorkingNodePaths in the next_level
  5062. // that reference the WorkingNodePath object within the entry.)
  5063. FindApproxLevelEntry *next = entry->_next;
  5064. entry->_next = deleted_entries;
  5065. deleted_entries = entry;
  5066. entry = next;
  5067. }
  5068. // Make sure the remaining entries from this level are added to
  5069. // the delete chain.
  5070. while (entry != (FindApproxLevelEntry *)NULL) {
  5071. FindApproxLevelEntry *next = entry->_next;
  5072. entry->_next = deleted_entries;
  5073. deleted_entries = entry;
  5074. entry = next;
  5075. }
  5076. level = next_level;
  5077. }
  5078. // Now it's safe to delete all entries on the delete chain.
  5079. while (deleted_entries != (FindApproxLevelEntry *)NULL) {
  5080. FindApproxLevelEntry *next = deleted_entries->_next;
  5081. delete deleted_entries;
  5082. deleted_entries = next;
  5083. }
  5084. }
  5085. ////////////////////////////////////////////////////////////////////
  5086. // Function: NodePath::r_adjust_all_priorities
  5087. // Access: Private
  5088. // Description: The recursive implementation of
  5089. // adjust_all_priorities(). This walks through the
  5090. // subgraph defined by the indicated node and below.
  5091. ////////////////////////////////////////////////////////////////////
  5092. void NodePath::
  5093. r_adjust_all_priorities(PandaNode *node, int adjustment) {
  5094. node->set_state(node->get_state()->adjust_all_priorities(adjustment));
  5095. if (node->is_geom_node()) {
  5096. GeomNode *gnode;
  5097. DCAST_INTO_V(gnode, node);
  5098. int num_geoms = gnode->get_num_geoms();
  5099. for (int i = 0; i < num_geoms; i++) {
  5100. gnode->set_geom_state(i, gnode->get_geom_state(i)->adjust_all_priorities(adjustment));
  5101. }
  5102. }
  5103. PandaNode::Children cr = node->get_children();
  5104. int num_children = cr.get_num_children();
  5105. for (int i = 0; i < num_children; i++) {
  5106. r_adjust_all_priorities(cr.get_child(i), adjustment);
  5107. }
  5108. }
  5109. ////////////////////////////////////////////////////////////////////
  5110. // Function: NodePath::r_force_recompute_bounds
  5111. // Access: Private
  5112. // Description:
  5113. ////////////////////////////////////////////////////////////////////
  5114. void NodePath::
  5115. r_force_recompute_bounds(PandaNode *node) {
  5116. if (node->is_geom_node()) {
  5117. GeomNode *gnode;
  5118. DCAST_INTO_V(gnode, node);
  5119. int num_geoms = gnode->get_num_geoms();
  5120. for (int i = 0; i < num_geoms; i++) {
  5121. const Geom *geom = gnode->get_geom(i);
  5122. // It's ok to cast away the const modifier on this Geom pointer,
  5123. // since marking the bounding volume stale doesn't really change
  5124. // the Geom in any substantial way.
  5125. ((Geom *)geom)->mark_bound_stale();
  5126. }
  5127. }
  5128. node->mark_bound_stale();
  5129. // Now consider children.
  5130. PandaNode::Children cr = node->get_children();
  5131. int num_children = cr.get_num_children();
  5132. for (int i = 0; i < num_children; i++) {
  5133. r_force_recompute_bounds(cr.get_child(i));
  5134. }
  5135. }
  5136. ////////////////////////////////////////////////////////////////////
  5137. // Function: NodePath::r_set_collide_mask
  5138. // Access: Private
  5139. // Description: Recursively applies the indicated collide mask to the
  5140. // nodes at and below this node.
  5141. ////////////////////////////////////////////////////////////////////
  5142. void NodePath::
  5143. r_set_collide_mask(PandaNode *node,
  5144. CollideMask and_mask, CollideMask or_mask,
  5145. TypeHandle node_type) {
  5146. if (node->is_of_type(node_type)) {
  5147. CollideMask into_collide_mask = node->get_into_collide_mask();
  5148. into_collide_mask = (into_collide_mask & and_mask) | or_mask;
  5149. node->set_into_collide_mask(into_collide_mask);
  5150. }
  5151. PandaNode::Children cr = node->get_children();
  5152. int num_children = cr.get_num_children();
  5153. for (int i = 0; i < num_children; i++) {
  5154. r_set_collide_mask(cr.get_child(i), and_mask, or_mask, node_type);
  5155. }
  5156. }
  5157. ////////////////////////////////////////////////////////////////////
  5158. // Function: NodePath::r_find_texture
  5159. // Access: Private
  5160. // Description:
  5161. ////////////////////////////////////////////////////////////////////
  5162. Texture * NodePath::
  5163. r_find_texture(PandaNode *node, const RenderState *state,
  5164. const GlobPattern &glob) const {
  5165. if (node->is_geom_node()) {
  5166. GeomNode *gnode;
  5167. DCAST_INTO_R(gnode, node, NULL);
  5168. int num_geoms = gnode->get_num_geoms();
  5169. for (int i = 0; i < num_geoms; i++) {
  5170. CPT(RenderState) geom_state =
  5171. state->compose(gnode->get_geom_state(i));
  5172. // Look for a TextureAttrib on the state.
  5173. const RenderAttrib *attrib =
  5174. geom_state->get_attrib(TextureAttrib::get_class_type());
  5175. if (attrib != (const RenderAttrib *)NULL) {
  5176. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5177. for (int i = 0; i < ta->get_num_on_stages(); i++) {
  5178. Texture *texture = ta->get_on_texture(ta->get_on_stage(i));
  5179. if (texture != (Texture *)NULL) {
  5180. if (glob.matches(texture->get_name())) {
  5181. return texture;
  5182. }
  5183. }
  5184. }
  5185. }
  5186. }
  5187. }
  5188. // Now consider children.
  5189. PandaNode::Children cr = node->get_children();
  5190. int num_children = cr.get_num_children();
  5191. for (int i = 0; i < num_children; i++) {
  5192. PandaNode *child = cr.get_child(i);
  5193. CPT(RenderState) next_state = state->compose(child->get_state());
  5194. Texture *result = r_find_texture(child, next_state, glob);
  5195. if (result != (Texture *)NULL) {
  5196. return result;
  5197. }
  5198. }
  5199. return NULL;
  5200. }
  5201. ////////////////////////////////////////////////////////////////////
  5202. // Function: NodePath::r_find_all_textures
  5203. // Access: Private
  5204. // Description:
  5205. ////////////////////////////////////////////////////////////////////
  5206. void NodePath::
  5207. r_find_all_textures(PandaNode *node, const RenderState *state,
  5208. NodePath::Textures &textures) const {
  5209. if (node->is_geom_node()) {
  5210. GeomNode *gnode;
  5211. DCAST_INTO_V(gnode, node);
  5212. int num_geoms = gnode->get_num_geoms();
  5213. for (int i = 0; i < num_geoms; i++) {
  5214. CPT(RenderState) geom_state =
  5215. state->compose(gnode->get_geom_state(i));
  5216. // Look for a TextureAttrib on the state.
  5217. const RenderAttrib *attrib =
  5218. geom_state->get_attrib(TextureAttrib::get_class_type());
  5219. if (attrib != (const RenderAttrib *)NULL) {
  5220. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5221. for (int i = 0; i < ta->get_num_on_stages(); i++) {
  5222. Texture *texture = ta->get_on_texture(ta->get_on_stage(i));
  5223. if (texture != (Texture *)NULL) {
  5224. textures.insert(texture);
  5225. }
  5226. }
  5227. }
  5228. }
  5229. }
  5230. // Now consider children.
  5231. PandaNode::Children cr = node->get_children();
  5232. int num_children = cr.get_num_children();
  5233. for (int i = 0; i < num_children; i++) {
  5234. PandaNode *child = cr.get_child(i);
  5235. CPT(RenderState) next_state = state->compose(child->get_state());
  5236. r_find_all_textures(child, next_state, textures);
  5237. }
  5238. }
  5239. ////////////////////////////////////////////////////////////////////
  5240. // Function: NodePath::r_find_texture
  5241. // Access: Private
  5242. // Description:
  5243. ////////////////////////////////////////////////////////////////////
  5244. Texture * NodePath::
  5245. r_find_texture(PandaNode *node, TextureStage *stage) const {
  5246. // Look for a TextureAttrib on the node.
  5247. const RenderAttrib *attrib =
  5248. node->get_attrib(TextureAttrib::get_class_type());
  5249. if (attrib != (const RenderAttrib *)NULL) {
  5250. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5251. if (ta->has_on_stage(stage)) {
  5252. return ta->get_on_texture(stage);
  5253. }
  5254. }
  5255. if (node->is_geom_node()) {
  5256. GeomNode *gnode;
  5257. DCAST_INTO_R(gnode, node, NULL);
  5258. int num_geoms = gnode->get_num_geoms();
  5259. for (int i = 0; i < num_geoms; i++) {
  5260. CPT(RenderState) geom_state = gnode->get_geom_state(i);
  5261. // Look for a TextureAttrib on the state.
  5262. const RenderAttrib *attrib =
  5263. geom_state->get_attrib(TextureAttrib::get_class_type());
  5264. if (attrib != (const RenderAttrib *)NULL) {
  5265. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5266. if (ta->has_on_stage(stage)) {
  5267. return ta->get_on_texture(stage);
  5268. }
  5269. }
  5270. }
  5271. }
  5272. // Now consider children.
  5273. PandaNode::Children cr = node->get_children();
  5274. int num_children = cr.get_num_children();
  5275. for (int i = 0; i < num_children; i++) {
  5276. PandaNode *child = cr.get_child(i);
  5277. Texture *result = r_find_texture(child, stage);
  5278. if (result != (Texture *)NULL) {
  5279. return result;
  5280. }
  5281. }
  5282. return NULL;
  5283. }
  5284. ////////////////////////////////////////////////////////////////////
  5285. // Function: NodePath::r_find_all_textures
  5286. // Access: Private
  5287. // Description:
  5288. ////////////////////////////////////////////////////////////////////
  5289. void NodePath::
  5290. r_find_all_textures(PandaNode *node, TextureStage *stage,
  5291. NodePath::Textures &textures) const {
  5292. // Look for a TextureAttrib on the node.
  5293. const RenderAttrib *attrib =
  5294. node->get_attrib(TextureAttrib::get_class_type());
  5295. if (attrib != (const RenderAttrib *)NULL) {
  5296. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5297. if (ta->has_on_stage(stage)) {
  5298. textures.insert(ta->get_on_texture(stage));
  5299. }
  5300. }
  5301. if (node->is_geom_node()) {
  5302. GeomNode *gnode;
  5303. DCAST_INTO_V(gnode, node);
  5304. int num_geoms = gnode->get_num_geoms();
  5305. for (int i = 0; i < num_geoms; i++) {
  5306. CPT(RenderState) geom_state = gnode->get_geom_state(i);
  5307. // Look for a TextureAttrib on the state.
  5308. const RenderAttrib *attrib =
  5309. geom_state->get_attrib(TextureAttrib::get_class_type());
  5310. if (attrib != (const RenderAttrib *)NULL) {
  5311. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5312. if (ta->has_on_stage(stage)) {
  5313. textures.insert(ta->get_on_texture(stage));
  5314. }
  5315. }
  5316. }
  5317. }
  5318. // Now consider children.
  5319. PandaNode::Children cr = node->get_children();
  5320. int num_children = cr.get_num_children();
  5321. for (int i = 0; i < num_children; i++) {
  5322. PandaNode *child = cr.get_child(i);
  5323. r_find_all_textures(child, stage, textures);
  5324. }
  5325. }
  5326. ////////////////////////////////////////////////////////////////////
  5327. // Function: NodePath::r_find_texture_stage
  5328. // Access: Private
  5329. // Description:
  5330. ////////////////////////////////////////////////////////////////////
  5331. TextureStage * NodePath::
  5332. r_find_texture_stage(PandaNode *node, const RenderState *state,
  5333. const GlobPattern &glob) const {
  5334. if (node->is_geom_node()) {
  5335. GeomNode *gnode;
  5336. DCAST_INTO_R(gnode, node, NULL);
  5337. int num_geoms = gnode->get_num_geoms();
  5338. for (int i = 0; i < num_geoms; i++) {
  5339. CPT(RenderState) geom_state =
  5340. state->compose(gnode->get_geom_state(i));
  5341. // Look for a TextureAttrib on the state.
  5342. const RenderAttrib *attrib =
  5343. geom_state->get_attrib(TextureAttrib::get_class_type());
  5344. if (attrib != (const RenderAttrib *)NULL) {
  5345. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5346. for (int i = 0; i < ta->get_num_on_stages(); i++) {
  5347. TextureStage *texture_stage = ta->get_on_stage(i);
  5348. if (texture_stage != (TextureStage *)NULL) {
  5349. if (glob.matches(texture_stage->get_name())) {
  5350. return texture_stage;
  5351. }
  5352. }
  5353. }
  5354. }
  5355. }
  5356. }
  5357. // Now consider children.
  5358. PandaNode::Children cr = node->get_children();
  5359. int num_children = cr.get_num_children();
  5360. for (int i = 0; i < num_children; i++) {
  5361. PandaNode *child = cr.get_child(i);
  5362. CPT(RenderState) next_state = state->compose(child->get_state());
  5363. TextureStage *result = r_find_texture_stage(child, next_state, glob);
  5364. if (result != (TextureStage *)NULL) {
  5365. return result;
  5366. }
  5367. }
  5368. return NULL;
  5369. }
  5370. ////////////////////////////////////////////////////////////////////
  5371. // Function: NodePath::r_find_all_texture_stages
  5372. // Access: Private
  5373. // Description:
  5374. ////////////////////////////////////////////////////////////////////
  5375. void NodePath::
  5376. r_find_all_texture_stages(PandaNode *node, const RenderState *state,
  5377. NodePath::TextureStages &texture_stages) const {
  5378. if (node->is_geom_node()) {
  5379. GeomNode *gnode;
  5380. DCAST_INTO_V(gnode, node);
  5381. int num_geoms = gnode->get_num_geoms();
  5382. for (int i = 0; i < num_geoms; i++) {
  5383. CPT(RenderState) geom_state =
  5384. state->compose(gnode->get_geom_state(i));
  5385. // Look for a TextureAttrib on the state.
  5386. const RenderAttrib *attrib =
  5387. geom_state->get_attrib(TextureAttrib::get_class_type());
  5388. if (attrib != (const RenderAttrib *)NULL) {
  5389. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5390. for (int i = 0; i < ta->get_num_on_stages(); i++) {
  5391. TextureStage *texture_stage = ta->get_on_stage(i);
  5392. if (texture_stage != (TextureStage *)NULL) {
  5393. texture_stages.insert(texture_stage);
  5394. }
  5395. }
  5396. }
  5397. }
  5398. }
  5399. // Now consider children.
  5400. PandaNode::Children cr = node->get_children();
  5401. int num_children = cr.get_num_children();
  5402. for (int i = 0; i < num_children; i++) {
  5403. PandaNode *child = cr.get_child(i);
  5404. CPT(RenderState) next_state = state->compose(child->get_state());
  5405. r_find_all_texture_stages(child, next_state, texture_stages);
  5406. }
  5407. }
  5408. ////////////////////////////////////////////////////////////////////
  5409. // Function: NodePath::r_unify_texture_stages
  5410. // Access: Private
  5411. // Description:
  5412. ////////////////////////////////////////////////////////////////////
  5413. void NodePath::
  5414. r_unify_texture_stages(PandaNode *node, TextureStage *stage) {
  5415. // Look for a TextureAttrib on the state.
  5416. const RenderAttrib *attrib =
  5417. node->get_attrib(TextureAttrib::get_class_type());
  5418. if (attrib != (const RenderAttrib *)NULL) {
  5419. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5420. CPT(RenderAttrib) new_attrib = ta->unify_texture_stages(stage);
  5421. if (new_attrib != ta) {
  5422. node->set_attrib(new_attrib);
  5423. }
  5424. }
  5425. if (node->is_geom_node()) {
  5426. GeomNode *gnode;
  5427. DCAST_INTO_V(gnode, node);
  5428. int num_geoms = gnode->get_num_geoms();
  5429. for (int i = 0; i < num_geoms; i++) {
  5430. CPT(RenderState) state = gnode->get_geom_state(i);
  5431. // Look for a TextureAttrib on the state.
  5432. const RenderAttrib *attrib =
  5433. state->get_attrib(TextureAttrib::get_class_type());
  5434. if (attrib != (const RenderAttrib *)NULL) {
  5435. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  5436. CPT(RenderAttrib) new_attrib = ta->unify_texture_stages(stage);
  5437. if (new_attrib != ta) {
  5438. CPT(RenderState) new_state = state->add_attrib(new_attrib);
  5439. gnode->set_geom_state(i, new_state);
  5440. }
  5441. }
  5442. }
  5443. }
  5444. // Now consider children.
  5445. PandaNode::Children cr = node->get_children();
  5446. int num_children = cr.get_num_children();
  5447. for (int i = 0; i < num_children; i++) {
  5448. PandaNode *child = cr.get_child(i);
  5449. r_unify_texture_stages(child, stage);
  5450. }
  5451. }
  5452. ////////////////////////////////////////////////////////////////////
  5453. // Function: NodePath::r_prepare_scene
  5454. // Access: Private
  5455. // Description: The recursive implementation of prepare_scene.
  5456. ////////////////////////////////////////////////////////////////////
  5457. void NodePath::
  5458. r_prepare_scene(PandaNode *node, const RenderState *state,
  5459. PreparedGraphicsObjects *prepared_objects) {
  5460. if (node->is_geom_node()) {
  5461. GeomNode *gnode;
  5462. DCAST_INTO_V(gnode, node);
  5463. /*
  5464. Not implemented yet in pgraph. Maybe we don't need this anyway.
  5465. if (retained_mode) {
  5466. gnode->prepare(gsg);
  5467. }
  5468. */
  5469. int num_geoms = gnode->get_num_geoms();
  5470. for (int i = 0; i < num_geoms; i++) {
  5471. CPT(RenderState) geom_state = state->compose(gnode->get_geom_state(i));
  5472. const RenderAttrib *attrib =
  5473. geom_state->get_attrib(TextureAttrib::get_class_type());
  5474. if (attrib != (const RenderAttrib *)NULL) {
  5475. const TextureAttrib *ta;
  5476. DCAST_INTO_V(ta, attrib);
  5477. Texture *texture = ta->get_texture();
  5478. if (texture != (Texture *)NULL) {
  5479. texture->prepare(prepared_objects);
  5480. }
  5481. }
  5482. }
  5483. }
  5484. int num_children = node->get_num_children();
  5485. for (int i = 0; i < num_children; i++) {
  5486. PandaNode *child = node->get_child(i);
  5487. CPT(RenderState) child_state = state->compose(child->get_state());
  5488. r_prepare_scene(child, child_state, prepared_objects);
  5489. }
  5490. }