nodePath.cxx 217 KB

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