pandaNode.cxx 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file pandaNode.cxx
  10. * @author drose
  11. * @date 2002-02-20
  12. */
  13. #include "pandaNode.h"
  14. #include "config_pgraph.h"
  15. #include "nodePathComponent.h"
  16. #include "bamReader.h"
  17. #include "bamWriter.h"
  18. #include "indent.h"
  19. #include "geometricBoundingVolume.h"
  20. #include "sceneGraphReducer.h"
  21. #include "accumulatedAttribs.h"
  22. #include "clipPlaneAttrib.h"
  23. #include "boundingSphere.h"
  24. #include "boundingBox.h"
  25. #include "pStatTimer.h"
  26. #include "config_mathutil.h"
  27. #include "lightReMutexHolder.h"
  28. #include "graphicsStateGuardianBase.h"
  29. using std::ostream;
  30. using std::ostringstream;
  31. using std::string;
  32. // This category is just temporary for debugging convenience.
  33. NotifyCategoryDecl(drawmask, EXPCL_PANDA_PGRAPH, EXPTP_PANDA_PGRAPH);
  34. NotifyCategoryDef(drawmask, "");
  35. TypeHandle PandaNode::BamReaderAuxDataDown::_type_handle;
  36. PandaNode::SceneRootFunc *PandaNode::_scene_root_func;
  37. PandaNodeChain PandaNode::_dirty_prev_transforms("_dirty_prev_transforms");
  38. DrawMask PandaNode::_overall_bit = DrawMask::bit(31);
  39. PStatCollector PandaNode::_reset_prev_pcollector("App:Collisions:Reset");
  40. PStatCollector PandaNode::_update_bounds_pcollector("*:Bounds");
  41. TypeHandle PandaNode::_type_handle;
  42. TypeHandle PandaNode::CData::_type_handle;
  43. TypeHandle PandaNodePipelineReader::_type_handle;
  44. /*
  45. * There are two different interfaces here for making and breaking parent-
  46. * child connections: the fundamental PandaNode interface, via add_child() and
  47. * remove_child() (and related functions), and the NodePath support interface,
  48. * via attach(), detach(), and reparent(). They both do essentially the same
  49. * thing, but with slightly different inputs. The PandaNode interfaces try to
  50. * guess which NodePaths should be updated as a result of the scene graph
  51. * change, while the NodePath interfaces already know. The NodePath support
  52. * interface functions are strictly called from within the NodePath class, and
  53. * are used to implement NodePath::reparent_to() and NodePath::remove_node(),
  54. * etc. The fundamental interface, on the other hand, is intended to be
  55. * called directly by the user. The fundamental interface has a slightly
  56. * lower overhead because it does not need to create a NodePathComponent chain
  57. * where one does not already exist; however, the NodePath support interface
  58. * is more useful when the NodePath already does exist, because it ensures
  59. * that the particular NodePath calling it is kept appropriately up-to-date.
  60. */
  61. /**
  62. *
  63. */
  64. PandaNode::
  65. PandaNode(const string &name) :
  66. Namable(name),
  67. _paths_lock("PandaNode::_paths_lock"),
  68. _dirty_prev_transform(false)
  69. {
  70. if (pgraph_cat.is_debug()) {
  71. pgraph_cat.debug()
  72. << "Constructing " << (void *)this << ", " << get_name() << "\n";
  73. }
  74. #ifdef DO_MEMORY_USAGE
  75. MemoryUsage::update_type(this, this);
  76. #endif
  77. }
  78. /**
  79. *
  80. */
  81. PandaNode::
  82. ~PandaNode() {
  83. if (pgraph_cat.is_debug()) {
  84. pgraph_cat.debug()
  85. << "Destructing " << (void *)this << ", " << get_name() << "\n";
  86. }
  87. if (_dirty_prev_transform) {
  88. // Need to have this held before we grab any other locks.
  89. LightMutexHolder holder(_dirty_prev_transforms._lock);
  90. do_clear_dirty_prev_transform();
  91. }
  92. // We shouldn't have any parents left by the time we destruct, or there's a
  93. // refcount fault somewhere.
  94. // Actually, that's not necessarily true anymore, since we might be updating
  95. // a node dynamically via the bam reader, which doesn't necessarily keep
  96. // related pairs of nodes in sync with each other.
  97. /*
  98. #ifndef NDEBUG
  99. {
  100. CDReader cdata(_cycler);
  101. nassertv(cdata->get_up()->empty());
  102. }
  103. #endif // NDEBUG
  104. */
  105. remove_all_children();
  106. }
  107. /**
  108. * Do not call the copy constructor directly; instead, use make_copy() or
  109. * copy_subgraph() to make a copy of a node.
  110. */
  111. PandaNode::
  112. PandaNode(const PandaNode &copy) :
  113. TypedWritableReferenceCount(copy),
  114. Namable(copy),
  115. _paths_lock("PandaNode::_paths_lock"),
  116. _dirty_prev_transform(false),
  117. _python_tag_data(copy._python_tag_data),
  118. _unexpected_change_flags(0)
  119. {
  120. if (pgraph_cat.is_debug()) {
  121. pgraph_cat.debug()
  122. << "Copying " << (void *)this << ", " << get_name() << "\n";
  123. }
  124. #ifdef DO_MEMORY_USAGE
  125. MemoryUsage::update_type(this, this);
  126. #endif
  127. // Need to have this held before we grab any other locks.
  128. LightMutexHolder holder(_dirty_prev_transforms._lock);
  129. // Copy the other node's state.
  130. {
  131. CDReader copy_cdata(copy._cycler);
  132. CDWriter cdata(_cycler, true);
  133. cdata->_state = copy_cdata->_state;
  134. cdata->_transform = copy_cdata->_transform;
  135. cdata->_prev_transform = copy_cdata->_prev_transform;
  136. if (cdata->_transform != cdata->_prev_transform) {
  137. do_set_dirty_prev_transform();
  138. }
  139. cdata->_effects = copy_cdata->_effects;
  140. cdata->_tag_data = copy_cdata->_tag_data;
  141. cdata->_draw_control_mask = copy_cdata->_draw_control_mask;
  142. cdata->_draw_show_mask = copy_cdata->_draw_show_mask;
  143. cdata->_into_collide_mask = copy_cdata->_into_collide_mask;
  144. cdata->_bounds_type = copy_cdata->_bounds_type;
  145. cdata->_user_bounds = copy_cdata->_user_bounds;
  146. cdata->_internal_bounds = nullptr;
  147. cdata->_internal_bounds_computed = UpdateSeq::initial();
  148. cdata->_internal_bounds_mark = UpdateSeq::initial();
  149. ++cdata->_internal_bounds_mark;
  150. cdata->_final_bounds = copy_cdata->_final_bounds;
  151. cdata->_fancy_bits = copy_cdata->_fancy_bits;
  152. }
  153. }
  154. /**
  155. * This is similar to make_copy(), but it makes a copy for the specific
  156. * purpose of flatten. Typically, this will be a new PandaNode with a new
  157. * pointer, but all of the internal data will always be shared with the
  158. * original; whereas the new node returned by make_copy() might not share the
  159. * internal data.
  160. */
  161. PandaNode *PandaNode::
  162. dupe_for_flatten() const {
  163. return make_copy();
  164. }
  165. /**
  166. * Returns true if it is generally safe to flatten out this particular kind of
  167. * PandaNode by duplicating instances (by calling dupe_for_flatten()), false
  168. * otherwise (for instance, a Camera cannot be safely flattened, because the
  169. * Camera pointer itself is meaningful).
  170. */
  171. bool PandaNode::
  172. safe_to_flatten() const {
  173. return true;
  174. }
  175. /**
  176. * Returns true if it is generally safe to transform this particular kind of
  177. * PandaNode by calling the xform() method, false otherwise.
  178. */
  179. bool PandaNode::
  180. safe_to_transform() const {
  181. return true;
  182. }
  183. /**
  184. * Returns true if it is safe to automatically adjust the transform on this
  185. * kind of node. Usually, this is only a bad idea if the user expects to find
  186. * a particular transform on the node.
  187. *
  188. * ModelNodes with the preserve_transform flag set are presently the only
  189. * kinds of nodes that should not have their transform even adjusted.
  190. */
  191. bool PandaNode::
  192. safe_to_modify_transform() const {
  193. return true;
  194. }
  195. /**
  196. * Returns true if it is generally safe to combine this particular kind of
  197. * PandaNode with other kinds of PandaNodes of compatible type, adding
  198. * children or whatever. For instance, an LODNode should not be combined with
  199. * any other PandaNode, because its set of children is meaningful.
  200. */
  201. bool PandaNode::
  202. safe_to_combine() const {
  203. return true;
  204. }
  205. /**
  206. * Returns true if it is generally safe to combine the children of this
  207. * PandaNode with each other. For instance, an LODNode's children should not
  208. * be combined with each other, because the set of children is meaningful.
  209. */
  210. bool PandaNode::
  211. safe_to_combine_children() const {
  212. return true;
  213. }
  214. /**
  215. * Returns true if a flatten operation may safely continue past this node, or
  216. * false if nodes below this node may not be molested.
  217. */
  218. bool PandaNode::
  219. safe_to_flatten_below() const {
  220. return true;
  221. }
  222. /**
  223. * Returns true if the node's name has extrinsic meaning and must be preserved
  224. * across a flatten operation, false otherwise.
  225. */
  226. bool PandaNode::
  227. preserve_name() const {
  228. return false;
  229. }
  230. /**
  231. * Returns the union of all attributes from SceneGraphReducer::AttribTypes
  232. * that may not safely be applied to the vertices of this node. If this is
  233. * nonzero, these attributes must be dropped at this node as a state change.
  234. *
  235. * This is a generalization of safe_to_transform().
  236. */
  237. int PandaNode::
  238. get_unsafe_to_apply_attribs() const {
  239. return 0;
  240. }
  241. /**
  242. * Applies whatever attributes are specified in the AccumulatedAttribs object
  243. * (and by the attrib_types bitmask) to the vertices on this node, if
  244. * appropriate. If this node uses geom arrays like a GeomNode, the supplied
  245. * GeomTransformer may be used to unify shared arrays across multiple
  246. * different nodes.
  247. *
  248. * This is a generalization of xform().
  249. */
  250. void PandaNode::
  251. apply_attribs_to_vertices(const AccumulatedAttribs &attribs, int attrib_types,
  252. GeomTransformer &transformer) {
  253. if ((attrib_types & SceneGraphReducer::TT_transform) != 0) {
  254. const LMatrix4 &mat = attribs._transform->get_mat();
  255. xform(mat);
  256. Thread *current_thread = Thread::get_current_thread();
  257. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  258. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  259. cdata->_effects = cdata->_effects->xform(mat);
  260. cdata->set_fancy_bit(FB_effects, !cdata->_effects->is_empty());
  261. }
  262. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  263. }
  264. mark_bam_modified();
  265. }
  266. /**
  267. * Transforms the contents of this PandaNode by the indicated matrix, if it
  268. * means anything to do so. For most kinds of PandaNodes, this does nothing.
  269. */
  270. void PandaNode::
  271. xform(const LMatrix4 &) {
  272. }
  273. /**
  274. * Collapses this PandaNode with the other PandaNode, if possible, and returns
  275. * a pointer to the combined PandaNode, or NULL if the two PandaNodes cannot
  276. * safely be combined.
  277. *
  278. * The return value may be this, other, or a new PandaNode altogether.
  279. *
  280. * This function is called from GraphReducer::flatten(), and need not deal
  281. * with children; its job is just to decide whether to collapse the two
  282. * PandaNodes and what the collapsed PandaNode should look like.
  283. */
  284. PandaNode *PandaNode::
  285. combine_with(PandaNode *other) {
  286. // An unadorned PandaNode always combines with any other PandaNodes by
  287. // yielding completely. However, if we are actually some fancy PandaNode
  288. // type that derives from PandaNode but didn't redefine this function, we
  289. // should refuse to combine.
  290. if (is_exact_type(get_class_type())) {
  291. // No, we're an ordinary PandaNode.
  292. return other;
  293. } else if (other->is_exact_type(get_class_type())) {
  294. // We're not an ordinary PandaNode, but the other one is.
  295. return this;
  296. }
  297. // We're something other than an ordinary PandaNode. Don't combine.
  298. return nullptr;
  299. }
  300. /**
  301. * This is used to support NodePath::calc_tight_bounds(). It is not intended
  302. * to be called directly, and it has nothing to do with the normal Panda
  303. * bounding-volume computation.
  304. *
  305. * If the node contains any geometry, this updates min_point and max_point to
  306. * enclose its bounding box. found_any is to be set true if the node has any
  307. * geometry at all, or left alone if it has none. This method may be called
  308. * over several nodes, so it may enter with min_point, max_point, and
  309. * found_any already set.
  310. *
  311. * This function is recursive, and the return value is the transform after it
  312. * has been modified by this node's transform.
  313. */
  314. CPT(TransformState) PandaNode::
  315. calc_tight_bounds(LPoint3 &min_point, LPoint3 &max_point, bool &found_any,
  316. const TransformState *transform, Thread *current_thread) const {
  317. CPT(TransformState) next_transform = transform->compose(get_transform());
  318. Children cr = get_children(current_thread);
  319. int num_children = cr.get_num_children();
  320. for (int i = 0; i < num_children; i++) {
  321. cr.get_child(i)->calc_tight_bounds(min_point, max_point,
  322. found_any, next_transform,
  323. current_thread);
  324. }
  325. return next_transform;
  326. }
  327. /**
  328. * This function will be called during the cull traversal to perform any
  329. * additional operations that should be performed at cull time. This may
  330. * include additional manipulation of render state or additional
  331. * visible/invisible decisions, or any other arbitrary operation.
  332. *
  333. * Note that this function will *not* be called unless set_cull_callback() is
  334. * called in the constructor of the derived class. It is necessary to call
  335. * set_cull_callback() to indicated that we require cull_callback() to be
  336. * called.
  337. *
  338. * By the time this function is called, the node has already passed the
  339. * bounding-volume test for the viewing frustum, and the node's transform and
  340. * state have already been applied to the indicated CullTraverserData object.
  341. *
  342. * The return value is true if this node should be visible, or false if it
  343. * should be culled.
  344. */
  345. bool PandaNode::
  346. cull_callback(CullTraverser *, CullTraverserData &) {
  347. return true;
  348. }
  349. /**
  350. * Should be overridden by derived classes to return true if this kind of node
  351. * has some restrictions on the set of children that should be rendered. Node
  352. * with this property include LODNodes, SwitchNodes, and SequenceNodes.
  353. *
  354. * If this function returns true, get_first_visible_child() and
  355. * get_next_visible_child() will be called to walk through the list of
  356. * children during cull, instead of iterating through the entire list. This
  357. * method is called after cull_callback(), so cull_callback() may be
  358. * responsible for the decisions as to which children are visible at the
  359. * moment.
  360. */
  361. bool PandaNode::
  362. has_selective_visibility() const {
  363. return false;
  364. }
  365. /**
  366. * Returns the index number of the first visible child of this node, or a
  367. * number >= get_num_children() if there are no visible children of this node.
  368. * This is called during the cull traversal, but only if
  369. * has_selective_visibility() has already returned true. See
  370. * has_selective_visibility().
  371. */
  372. int PandaNode::
  373. get_first_visible_child() const {
  374. return 0;
  375. }
  376. /**
  377. * Returns the index number of the next visible child of this node following
  378. * the indicated child, or a number >= get_num_children() if there are no more
  379. * visible children of this node. See has_selective_visibility() and
  380. * get_first_visible_child().
  381. */
  382. int PandaNode::
  383. get_next_visible_child(int n) const {
  384. return n + 1;
  385. }
  386. /**
  387. * Should be overridden by derived classes to return true if this kind of node
  388. * has the special property that just one of its children is visible at any
  389. * given time, and furthermore that the particular visible child can be
  390. * determined without reference to any external information (such as a
  391. * camera). At present, only SequenceNodes and SwitchNodes fall into this
  392. * category.
  393. *
  394. * If this function returns true, get_visible_child() can be called to return
  395. * the index of the currently-visible child.
  396. */
  397. bool PandaNode::
  398. has_single_child_visibility() const {
  399. return false;
  400. }
  401. /**
  402. * Returns the index number of the currently visible child of this node. This
  403. * is only meaningful if has_single_child_visibility() has returned true.
  404. */
  405. int PandaNode::
  406. get_visible_child() const {
  407. return 0;
  408. }
  409. /**
  410. * Returns true if there is some value to visiting this particular node during
  411. * the cull traversal for any camera, false otherwise. This will be used to
  412. * optimize the result of get_net_draw_show_mask(), so that any subtrees that
  413. * contain only nodes for which is_renderable() is false need not be visited.
  414. */
  415. bool PandaNode::
  416. is_renderable() const {
  417. return false;
  418. }
  419. /**
  420. * Adds the node's contents to the CullResult we are building up during the
  421. * cull traversal, so that it will be drawn at render time. For most nodes
  422. * other than GeomNodes, this is a do-nothing operation.
  423. */
  424. void PandaNode::
  425. add_for_draw(CullTraverser *, CullTraverserData &) {
  426. }
  427. /**
  428. * Returns a newly-allocated PandaNode that is a shallow copy of this one. It
  429. * will be a different pointer, but its internal data may or may not be shared
  430. * with that of the original PandaNode. No children will be copied.
  431. */
  432. PandaNode *PandaNode::
  433. make_copy() const {
  434. return new PandaNode(*this);
  435. }
  436. /**
  437. * Allocates and returns a complete copy of this PandaNode and the entire
  438. * scene graph rooted at this PandaNode. Some data may still be shared from
  439. * the original (e.g. vertex index tables), but nothing that will impede
  440. * normal use of the PandaNode.
  441. */
  442. PT(PandaNode) PandaNode::
  443. copy_subgraph(Thread *current_thread) const {
  444. InstanceMap inst_map;
  445. return r_copy_subgraph(inst_map, current_thread);
  446. }
  447. /**
  448. * Returns the number of nodes at and below this level.
  449. */
  450. int PandaNode::
  451. count_num_descendants() const {
  452. int count = 1;
  453. Children children = get_children();
  454. int num_children = children.get_num_children();
  455. for (int i = 0; i < num_children; ++i) {
  456. PandaNode *child = children.get_child(i);
  457. count += child->count_num_descendants();
  458. }
  459. return count;
  460. }
  461. /**
  462. * Adds a new child to the node. The child is added in the relative position
  463. * indicated by sort; if all children have the same sort index, the child is
  464. * added at the end.
  465. *
  466. * If the same child is added to a node more than once, the previous instance
  467. * is first removed.
  468. */
  469. void PandaNode::
  470. add_child(PandaNode *child_node, int sort, Thread *current_thread) {
  471. nassertv(child_node != nullptr);
  472. if (!verify_child_no_cycles(child_node)) {
  473. // Whoops, adding this child node would introduce a cycle in the scene
  474. // graph.
  475. return;
  476. }
  477. // Ensure the child_node is not deleted while we do this.
  478. PT(PandaNode) keep_child = child_node;
  479. remove_child(child_node);
  480. // Apply this operation to the current stage as well as to all upstream
  481. // stages.
  482. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  483. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  484. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage, current_thread);
  485. cdata->modify_down()->insert(DownConnection(child_node, sort));
  486. cdata_child->modify_up()->insert(UpConnection(this));
  487. }
  488. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  489. OPEN_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler, current_thread) {
  490. new_connection(this, child_node, pipeline_stage, current_thread);
  491. }
  492. CLOSE_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler);
  493. force_bounds_stale();
  494. children_changed();
  495. child_node->parents_changed();
  496. mark_bam_modified();
  497. child_node->mark_bam_modified();
  498. }
  499. /**
  500. * Removes the nth child from the node.
  501. */
  502. void PandaNode::
  503. remove_child(int child_index, Thread *current_thread) {
  504. int pipeline_stage = current_thread->get_pipeline_stage();
  505. nassertv(pipeline_stage == 0);
  506. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  507. PT(Down) down = cdata->modify_down();
  508. nassertv(child_index >= 0 && child_index < (int)down->size());
  509. PT(PandaNode) child_node = (*down)[child_index].get_child();
  510. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage,
  511. current_thread);
  512. PT(Up) up = cdata_child->modify_up();
  513. down->erase(down->begin() + child_index);
  514. int num_erased = up->erase(UpConnection(this));
  515. nassertv(num_erased == 1);
  516. sever_connection(this, child_node, pipeline_stage, current_thread);
  517. force_bounds_stale(pipeline_stage, current_thread);
  518. children_changed();
  519. child_node->parents_changed();
  520. mark_bam_modified();
  521. child_node->mark_bam_modified();
  522. }
  523. /**
  524. * Removes the indicated child from the node. Returns true if the child was
  525. * removed, false if it was not already a child of the node. This will also
  526. * successfully remove the child if it had been stashed.
  527. */
  528. bool PandaNode::
  529. remove_child(PandaNode *child_node, Thread *current_thread) {
  530. nassertr(child_node != nullptr, false);
  531. // Make sure the child node is not destructed during the execution of this
  532. // method.
  533. PT(PandaNode) keep_child = child_node;
  534. // We have to do this for each upstream pipeline stage.
  535. bool any_removed = false;
  536. OPEN_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler, current_thread) {
  537. if (stage_remove_child(child_node, pipeline_stage, current_thread)) {
  538. any_removed = true;
  539. sever_connection(this, child_node, pipeline_stage, current_thread);
  540. force_bounds_stale(pipeline_stage, current_thread);
  541. }
  542. }
  543. CLOSE_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler);
  544. if (any_removed) {
  545. // Call callback hooks.
  546. children_changed();
  547. child_node->parents_changed();
  548. }
  549. return any_removed;
  550. }
  551. /**
  552. * Searches for the orig_child node in the node's list of children, and
  553. * replaces it with the new_child instead. Returns true if the replacement is
  554. * made, or false if the node is not a child or if there is some other
  555. * problem.
  556. */
  557. bool PandaNode::
  558. replace_child(PandaNode *orig_child, PandaNode *new_child,
  559. Thread *current_thread) {
  560. nassertr(orig_child != nullptr, false);
  561. nassertr(new_child != nullptr, false);
  562. if (orig_child == new_child) {
  563. // Trivial no-op.
  564. return true;
  565. }
  566. if (!verify_child_no_cycles(new_child)) {
  567. // Whoops, adding this child node would introduce a cycle in the scene
  568. // graph.
  569. return false;
  570. }
  571. // Make sure the orig_child node is not destructed during the execution of
  572. // this method.
  573. PT(PandaNode) keep_orig_child = orig_child;
  574. // We have to do this for each upstream pipeline stage.
  575. bool any_replaced = false;
  576. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  577. if (stage_replace_child(orig_child, new_child, pipeline_stage, current_thread)) {
  578. any_replaced = true;
  579. }
  580. }
  581. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  582. if (any_replaced) {
  583. children_changed();
  584. orig_child->parents_changed();
  585. new_child->parents_changed();
  586. }
  587. return any_replaced;
  588. }
  589. /**
  590. * Stashes the indicated child node. This removes the child from the list of
  591. * active children and puts it on a special list of stashed children. This
  592. * child node no longer contributes to the bounding volume of the PandaNode,
  593. * and is not visited in normal traversals. It is invisible and uncollidable.
  594. * The child may later be restored by calling unstash_child().
  595. *
  596. * This can only be called from the top pipeline stage (i.e. from App).
  597. */
  598. void PandaNode::
  599. stash_child(int child_index, Thread *current_thread) {
  600. int pipeline_stage = current_thread->get_pipeline_stage();
  601. nassertv(pipeline_stage == 0);
  602. nassertv(child_index >= 0 && child_index < get_num_children());
  603. // Save a reference count for ourselves.
  604. PT(PandaNode) self = this;
  605. PT(PandaNode) child_node = get_child(child_index);
  606. int sort = get_child_sort(child_index);
  607. remove_child(child_index);
  608. {
  609. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  610. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage, current_thread);
  611. cdata->modify_stashed()->insert(DownConnection(child_node, sort));
  612. cdata_child->modify_up()->insert(UpConnection(this));
  613. }
  614. new_connection(this, child_node, pipeline_stage, current_thread);
  615. force_bounds_stale(pipeline_stage, current_thread);
  616. children_changed();
  617. child_node->parents_changed();
  618. mark_bam_modified();
  619. child_node->mark_bam_modified();
  620. }
  621. /**
  622. * Returns the indicated stashed node to normal child status. This removes
  623. * the child from the list of stashed children and puts it on the normal list
  624. * of active children. This child node once again contributes to the bounding
  625. * volume of the PandaNode, and will be visited in normal traversals. It is
  626. * visible and collidable.
  627. *
  628. * This can only be called from the top pipeline stage (i.e. from App).
  629. */
  630. void PandaNode::
  631. unstash_child(int stashed_index, Thread *current_thread) {
  632. int pipeline_stage = current_thread->get_pipeline_stage();
  633. nassertv(pipeline_stage == 0);
  634. nassertv(stashed_index >= 0 && stashed_index < get_num_stashed());
  635. // Save a reference count for ourselves. I don't think this should be
  636. // necessary, but there are occasional crashes in stash() during furniture
  637. // moving mode. Perhaps this will eliminate those crashes.
  638. PT(PandaNode) self = this;
  639. PT(PandaNode) child_node = get_stashed(stashed_index);
  640. int sort = get_stashed_sort(stashed_index);
  641. remove_stashed(stashed_index);
  642. {
  643. CDWriter cdata(_cycler);
  644. CDWriter cdata_child(child_node->_cycler);
  645. cdata->modify_down()->insert(DownConnection(child_node, sort));
  646. cdata_child->modify_up()->insert(UpConnection(this));
  647. }
  648. new_connection(this, child_node, pipeline_stage, current_thread);
  649. force_bounds_stale();
  650. children_changed();
  651. child_node->parents_changed();
  652. mark_bam_modified();
  653. child_node->mark_bam_modified();
  654. }
  655. /**
  656. * Adds a new child to the node, directly as a stashed child. The child is
  657. * not added in the normal sense, but will be revealed if unstash_child() is
  658. * called on it later.
  659. *
  660. * If the same child is added to a node more than once, the previous instance
  661. * is first removed.
  662. *
  663. * This can only be called from the top pipeline stage (i.e. from App).
  664. */
  665. void PandaNode::
  666. add_stashed(PandaNode *child_node, int sort, Thread *current_thread) {
  667. int pipeline_stage = current_thread->get_pipeline_stage();
  668. nassertv(pipeline_stage == 0);
  669. if (!verify_child_no_cycles(child_node)) {
  670. // Whoops, adding this child node would introduce a cycle in the scene
  671. // graph.
  672. return;
  673. }
  674. // Ensure the child_node is not deleted while we do this.
  675. PT(PandaNode) keep_child = child_node;
  676. remove_child(child_node);
  677. {
  678. CDWriter cdata(_cycler);
  679. CDWriter cdata_child(child_node->_cycler);
  680. cdata->modify_stashed()->insert(DownConnection(child_node, sort));
  681. cdata_child->modify_up()->insert(UpConnection(this));
  682. }
  683. new_connection(this, child_node, pipeline_stage, current_thread);
  684. // Call callback hooks.
  685. children_changed();
  686. child_node->parents_changed();
  687. mark_bam_modified();
  688. child_node->mark_bam_modified();
  689. }
  690. /**
  691. * Removes the nth stashed child from the node.
  692. */
  693. void PandaNode::
  694. remove_stashed(int child_index, Thread *current_thread) {
  695. int pipeline_stage = current_thread->get_pipeline_stage();
  696. nassertv(pipeline_stage == 0);
  697. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  698. Down &stashed = *cdata->modify_stashed();
  699. nassertv(child_index >= 0 && child_index < (int)stashed.size());
  700. PT(PandaNode) child_node = stashed[child_index].get_child();
  701. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage, current_thread);
  702. stashed.erase(stashed.begin() + child_index);
  703. int num_erased = cdata_child->modify_up()->erase(UpConnection(this));
  704. nassertv(num_erased == 1);
  705. sever_connection(this, child_node, pipeline_stage, current_thread);
  706. force_bounds_stale(pipeline_stage, current_thread);
  707. children_changed();
  708. child_node->parents_changed();
  709. mark_bam_modified();
  710. child_node->mark_bam_modified();
  711. }
  712. /**
  713. * Removes all the children from the node at once, including stashed children.
  714. *
  715. * This can only be called from the top pipeline stage (i.e. from App).
  716. */
  717. void PandaNode::
  718. remove_all_children(Thread *current_thread) {
  719. // We have to do this for each upstream pipeline stage.
  720. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  721. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  722. PT(Down) down = cdata->modify_down();
  723. Down::iterator di;
  724. for (di = down->begin(); di != down->end(); ++di) {
  725. PT(PandaNode) child_node = (*di).get_child();
  726. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage,
  727. current_thread);
  728. cdata_child->modify_up()->erase(UpConnection(this));
  729. sever_connection(this, child_node, pipeline_stage, current_thread);
  730. child_node->parents_changed();
  731. child_node->mark_bam_modified();
  732. }
  733. down->clear();
  734. Down &stashed = *cdata->modify_stashed();
  735. for (di = stashed.begin(); di != stashed.end(); ++di) {
  736. PT(PandaNode) child_node = (*di).get_child();
  737. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage,
  738. current_thread);
  739. cdata_child->modify_up()->erase(UpConnection(this));
  740. sever_connection(this, child_node, pipeline_stage, current_thread);
  741. child_node->parents_changed();
  742. child_node->mark_bam_modified();
  743. }
  744. stashed.clear();
  745. }
  746. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  747. force_bounds_stale();
  748. children_changed();
  749. mark_bam_modified();
  750. }
  751. /**
  752. * Moves all the children from the other node onto this node.
  753. *
  754. * Any NodePaths to child nodes of the other node are truncated, rather than
  755. * moved to the new parent.
  756. */
  757. void PandaNode::
  758. steal_children(PandaNode *other, Thread *current_thread) {
  759. if (other == this) {
  760. // Trivial.
  761. return;
  762. }
  763. // We do this through the high-level interface for convenience. This could
  764. // begin to be a problem if we have a node with hundreds of children to
  765. // copy; this could break down the ov_set.insert() method, which is an
  766. // O(n^2) operation. If this happens, we should rewrite this to do a
  767. // simpler add_child() operation that involves push_back() instead of
  768. // insert(), and then sort the down list at the end.
  769. int num_children = other->get_num_children();
  770. int i;
  771. for (i = 0; i < num_children; i++) {
  772. PandaNode *child_node = other->get_child(i);
  773. int sort = other->get_child_sort(i);
  774. add_child(child_node, sort, current_thread);
  775. }
  776. int num_stashed = other->get_num_stashed();
  777. for (i = 0; i < num_stashed; i++) {
  778. PandaNode *child_node = other->get_stashed(i);
  779. int sort = other->get_stashed_sort(i);
  780. add_stashed(child_node, sort, current_thread);
  781. }
  782. other->remove_all_children(current_thread);
  783. }
  784. /**
  785. * Makes another instance of all the children of the other node, copying them
  786. * to this node.
  787. */
  788. void PandaNode::
  789. copy_children(PandaNode *other, Thread *current_thread) {
  790. if (other == this) {
  791. // Trivial.
  792. return;
  793. }
  794. Children children = other->get_children(current_thread);
  795. Stashed stashed = other->get_stashed(current_thread);
  796. int num_children = children.get_num_children();
  797. int i;
  798. for (i = 0; i < num_children; i++) {
  799. PandaNode *child_node = children.get_child(i);
  800. int sort = children.get_child_sort(i);
  801. add_child(child_node, sort, current_thread);
  802. }
  803. int num_stashed = stashed.get_num_stashed();
  804. for (i = 0; i < num_stashed; i++) {
  805. PandaNode *child_node = stashed.get_stashed(i);
  806. int sort = stashed.get_stashed_sort(i);
  807. add_stashed(child_node, sort, current_thread);
  808. }
  809. }
  810. /**
  811. * Adds the indicated render attribute to the scene graph on this node. This
  812. * attribute will now apply to this node and everything below. If there was
  813. * already an attribute of the same type, it is replaced.
  814. */
  815. void PandaNode::
  816. set_attrib(const RenderAttrib *attrib, int override) {
  817. // Apply this operation to the current stage as well as to all upstream
  818. // stages.
  819. bool any_changed = false;
  820. Thread *current_thread = Thread::get_current_thread();
  821. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  822. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  823. CPT(RenderState) new_state = cdata->_state->set_attrib(attrib, override);
  824. if (cdata->_state != new_state) {
  825. cdata->_state = new_state;
  826. cdata->set_fancy_bit(FB_state, true);
  827. any_changed = true;
  828. }
  829. }
  830. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  831. // Maybe we changed a ClipPlaneAttrib.
  832. if (any_changed) {
  833. mark_bounds_stale(current_thread);
  834. state_changed();
  835. mark_bam_modified();
  836. }
  837. }
  838. /**
  839. * Removes the render attribute of the given type from this node. This node,
  840. * and the subgraph below, will now inherit the indicated render attribute
  841. * from the nodes above this one.
  842. */
  843. void PandaNode::
  844. clear_attrib(int slot) {
  845. bool any_changed = false;
  846. Thread *current_thread = Thread::get_current_thread();
  847. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  848. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  849. CPT(RenderState) new_state = cdata->_state->remove_attrib(slot);
  850. if (cdata->_state != new_state) {
  851. cdata->_state = new_state;
  852. cdata->set_fancy_bit(FB_state, !new_state->is_empty());
  853. any_changed = true;
  854. }
  855. }
  856. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  857. // We mark the bounds stale when the state changes, in case we have changed
  858. // a ClipPlaneAttrib.
  859. if (any_changed) {
  860. mark_bounds_stale(current_thread);
  861. state_changed();
  862. mark_bam_modified();
  863. }
  864. }
  865. /**
  866. * Adds the indicated render effect to the scene graph on this node. If there
  867. * was already an effect of the same type, it is replaced.
  868. */
  869. void PandaNode::
  870. set_effect(const RenderEffect *effect) {
  871. // Apply this operation to the current stage as well as to all upstream
  872. // stages.
  873. Thread *current_thread = Thread::get_current_thread();
  874. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  875. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  876. cdata->_effects = cdata->_effects->add_effect(effect);
  877. cdata->set_fancy_bit(FB_effects, true);
  878. }
  879. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  880. mark_bam_modified();
  881. }
  882. /**
  883. * Removes the render effect of the given type from this node.
  884. */
  885. void PandaNode::
  886. clear_effect(TypeHandle type) {
  887. Thread *current_thread = Thread::get_current_thread();
  888. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  889. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  890. cdata->_effects = cdata->_effects->remove_effect(type);
  891. cdata->set_fancy_bit(FB_effects, !cdata->_effects->is_empty());
  892. }
  893. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  894. mark_bam_modified();
  895. }
  896. /**
  897. * Sets the complete RenderState that will be applied to all nodes at this
  898. * level and below. (The actual state that will be applied to lower nodes is
  899. * based on the composition of RenderStates from above this node as well).
  900. * This completely replaces whatever has been set on this node via repeated
  901. * calls to set_attrib().
  902. */
  903. void PandaNode::
  904. set_state(const RenderState *state, Thread *current_thread) {
  905. // Apply this operation to the current stage as well as to all upstream
  906. // stages.
  907. bool any_changed = false;
  908. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  909. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  910. if (cdata->_state != state) {
  911. cdata->_state = state;
  912. cdata->set_fancy_bit(FB_state, !state->is_empty());
  913. any_changed = true;
  914. }
  915. }
  916. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  917. // Maybe we have changed a ClipPlaneAttrib.
  918. if (any_changed) {
  919. mark_bounds_stale(current_thread);
  920. state_changed();
  921. mark_bam_modified();
  922. }
  923. }
  924. /**
  925. * Sets the complete RenderEffects that will be applied this node. This
  926. * completely replaces whatever has been set on this node via repeated calls
  927. * to set_attrib().
  928. */
  929. void PandaNode::
  930. set_effects(const RenderEffects *effects, Thread *current_thread) {
  931. // Apply this operation to the current stage as well as to all upstream
  932. // stages.
  933. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  934. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  935. cdata->_effects = effects;
  936. cdata->set_fancy_bit(FB_effects, !effects->is_empty());
  937. }
  938. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  939. mark_bam_modified();
  940. }
  941. /**
  942. * Sets the transform that will be applied to this node and below. This
  943. * defines a new coordinate space at this point in the scene graph and below.
  944. */
  945. void PandaNode::
  946. set_transform(const TransformState *transform, Thread *current_thread) {
  947. nassertv(!transform->is_invalid());
  948. // Need to have this held before we grab any other locks.
  949. LightMutexHolder holder(_dirty_prev_transforms._lock);
  950. // Apply this operation to the current stage as well as to all upstream
  951. // stages.
  952. bool any_changed = false;
  953. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  954. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  955. if (cdata->_transform != transform) {
  956. cdata->_transform = transform;
  957. cdata->set_fancy_bit(FB_transform, !transform->is_identity());
  958. any_changed = true;
  959. if (pipeline_stage == 0) {
  960. if (cdata->_transform != cdata->_prev_transform) {
  961. do_set_dirty_prev_transform();
  962. }
  963. }
  964. }
  965. }
  966. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  967. if (any_changed) {
  968. mark_bounds_stale(current_thread);
  969. transform_changed();
  970. mark_bam_modified();
  971. }
  972. }
  973. /**
  974. * Sets the transform that represents this node's "previous" position, one
  975. * frame ago, for the purposes of detecting motion for accurate collision
  976. * calculations.
  977. */
  978. void PandaNode::
  979. set_prev_transform(const TransformState *transform, Thread *current_thread) {
  980. nassertv(!transform->is_invalid());
  981. // Need to have this held before we grab any other locks.
  982. LightMutexHolder holder(_dirty_prev_transforms._lock);
  983. // Apply this operation to the current stage as well as to all upstream
  984. // stages.
  985. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  986. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  987. cdata->_prev_transform = transform;
  988. if (pipeline_stage == 0) {
  989. if (cdata->_transform != cdata->_prev_transform) {
  990. do_set_dirty_prev_transform();
  991. } else {
  992. do_clear_dirty_prev_transform();
  993. }
  994. }
  995. }
  996. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  997. mark_bam_modified();
  998. }
  999. /**
  1000. * Resets the transform that represents this node's "previous" position to the
  1001. * same as the current transform. This is not the same thing as clearing it
  1002. * to identity.
  1003. */
  1004. void PandaNode::
  1005. reset_prev_transform(Thread *current_thread) {
  1006. // Need to have this held before we grab any other locks.
  1007. LightMutexHolder holder(_dirty_prev_transforms._lock);
  1008. do_clear_dirty_prev_transform();
  1009. // Apply this operation to the current stage as well as to all upstream
  1010. // stages.
  1011. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1012. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  1013. cdata->_prev_transform = cdata->_transform;
  1014. }
  1015. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1016. mark_bam_modified();
  1017. }
  1018. /**
  1019. * Visits all nodes in the world with the _dirty_prev_transform flag--that is,
  1020. * all nodes whose _prev_transform is different from the _transform in
  1021. * pipeline stage 0--and resets the _prev_transform to be the same as
  1022. * _transform.
  1023. */
  1024. void PandaNode::
  1025. reset_all_prev_transform(Thread *current_thread) {
  1026. nassertv(current_thread->get_pipeline_stage() == 0);
  1027. PStatTimer timer(_reset_prev_pcollector, current_thread);
  1028. LightMutexHolder holder(_dirty_prev_transforms._lock);
  1029. LinkedListNode *list_node = _dirty_prev_transforms._next;
  1030. while (list_node != &_dirty_prev_transforms) {
  1031. PandaNode *panda_node = (PandaNode *)list_node;
  1032. nassertv(panda_node->_dirty_prev_transform);
  1033. panda_node->_dirty_prev_transform = false;
  1034. CDStageWriter cdata(panda_node->_cycler, 0, current_thread);
  1035. cdata->_prev_transform = cdata->_transform;
  1036. list_node = panda_node->_next;
  1037. #ifndef NDEBUG
  1038. panda_node->_prev = nullptr;
  1039. panda_node->_next = nullptr;
  1040. #endif // NDEBUG
  1041. panda_node->mark_bam_modified();
  1042. }
  1043. _dirty_prev_transforms._prev = &_dirty_prev_transforms;
  1044. _dirty_prev_transforms._next = &_dirty_prev_transforms;
  1045. }
  1046. /**
  1047. * Associates a user-defined value with a user-defined key which is stored on
  1048. * the node. This value has no meaning to Panda; but it is stored
  1049. * indefinitely on the node until it is requested again.
  1050. *
  1051. * Each unique key stores a different string value. There is no effective
  1052. * limit on the number of different keys that may be stored or on the length
  1053. * of any one key's value.
  1054. */
  1055. void PandaNode::
  1056. set_tag(const string &key, const string &value, Thread *current_thread) {
  1057. // Apply this operation to the current stage as well as to all upstream
  1058. // stages.
  1059. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1060. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  1061. cdata->_tag_data.store(key, value);
  1062. cdata->set_fancy_bit(FB_tag, true);
  1063. }
  1064. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1065. mark_bam_modified();
  1066. }
  1067. /**
  1068. * Removes the value defined for this key on this particular node. After a
  1069. * call to clear_tag(), has_tag() will return false for the indicated key.
  1070. */
  1071. void PandaNode::
  1072. clear_tag(const string &key, Thread *current_thread) {
  1073. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1074. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  1075. cdata->_tag_data.remove(key);
  1076. cdata->set_fancy_bit(FB_tag, !cdata->_tag_data.is_empty());
  1077. }
  1078. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1079. mark_bam_modified();
  1080. }
  1081. /**
  1082. * Copies all of the tags stored on the other node onto this node. If a
  1083. * particular tag exists on both nodes, the contents of this node's value is
  1084. * replaced by that of the other.
  1085. */
  1086. void PandaNode::
  1087. copy_tags(PandaNode *other) {
  1088. if (other == this) {
  1089. // Trivial.
  1090. return;
  1091. }
  1092. // Apply this operation to the current stage as well as to all upstream
  1093. // stages.
  1094. Thread *current_thread = Thread::get_current_thread();
  1095. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1096. CDStageWriter cdataw(_cycler, pipeline_stage, current_thread);
  1097. CDStageReader cdatar(other->_cycler, pipeline_stage, current_thread);
  1098. for (size_t n = 0; n < cdatar->_tag_data.size(); ++n) {
  1099. cdataw->_tag_data.store(cdatar->_tag_data.get_key(n), cdatar->_tag_data.get_data(n));
  1100. }
  1101. cdataw->set_fancy_bit(FB_tag, !cdataw->_tag_data.is_empty());
  1102. }
  1103. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1104. // It's okay to copy the tags by pointer, because get_python_tags does a
  1105. // copy-on-write.
  1106. _python_tag_data = other->_python_tag_data;
  1107. mark_bam_modified();
  1108. }
  1109. /**
  1110. * Writes a list of all the tag keys assigned to the node to the indicated
  1111. * stream. Writes one instance of the separator following each key (but does
  1112. * not write a terminal separator). The value associated with each key is not
  1113. * written.
  1114. *
  1115. * This is mainly for the benefit of the realtime user, to see the list of all
  1116. * of the associated tag keys.
  1117. */
  1118. void PandaNode::
  1119. list_tags(ostream &out, const string &separator) const {
  1120. CDReader cdata(_cycler);
  1121. for (size_t n = 0; n < cdata->_tag_data.size(); ++n) {
  1122. if (n > 0) {
  1123. out << separator;
  1124. }
  1125. out << cdata->_tag_data.get_key(n);
  1126. }
  1127. // We used to list the Python tags here. That's a bit awkward, though,
  1128. // since that means calling up into Python code to print the keys. If
  1129. // someone finds it useful, we can implement it in an extension method.
  1130. }
  1131. /**
  1132. * Fills the given vector up with the list of tags on this PandaNode.
  1133. *
  1134. * It is the user's responsibility to ensure that the keys vector is empty
  1135. * before making this call; otherwise, the new keys will be appended to it.
  1136. */
  1137. void PandaNode::
  1138. get_tag_keys(vector_string &keys) const {
  1139. CDReader cdata(_cycler);
  1140. for (size_t n = 0; n < cdata->_tag_data.size(); ++n) {
  1141. keys.push_back(cdata->_tag_data.get_key(n));
  1142. }
  1143. }
  1144. /**
  1145. * Returns a number less than 0, 0, or greater than 0, to indicate the
  1146. * similarity of tags between this node and the other one. If this returns 0,
  1147. * the tags are identical. If it returns other than 0, then the tags are
  1148. * different; and the nodes may be sorted into a consistent (but arbitrary)
  1149. * ordering based on this number.
  1150. */
  1151. int PandaNode::
  1152. compare_tags(const PandaNode *other) const {
  1153. CDReader cdata(_cycler);
  1154. CDReader cdata_other(other->_cycler);
  1155. const TagData &a_data = cdata->_tag_data;
  1156. const TagData &b_data = cdata_other->_tag_data;
  1157. size_t ai = 0;
  1158. size_t bi = 0;
  1159. while (ai < a_data.size() && bi < b_data.size()) {
  1160. int cmp = strcmp(a_data.get_key(ai).c_str(), b_data.get_key(bi).c_str());
  1161. if (cmp != 0) {
  1162. return cmp;
  1163. }
  1164. cmp = strcmp(a_data.get_key(ai).c_str(), b_data.get_key(bi).c_str());
  1165. if (cmp != 0) {
  1166. return cmp;
  1167. }
  1168. ++ai;
  1169. ++bi;
  1170. }
  1171. if (ai < a_data.size()) {
  1172. // list A is longer.
  1173. return 1;
  1174. }
  1175. if (bi < b_data.size()) {
  1176. // list B is longer.
  1177. return -1;
  1178. }
  1179. // We compare these by pointer, since it's problematic to call up into
  1180. // Python from arbitrary C++ code.
  1181. if (_python_tag_data != other->_python_tag_data) {
  1182. return (_python_tag_data < other->_python_tag_data) ? -1 : 1;
  1183. }
  1184. return 0;
  1185. }
  1186. /**
  1187. * Copies the TransformState, RenderState, RenderEffects, tags, Python tags,
  1188. * and the show/hide state from the other node onto this one. Typically this
  1189. * is used to prepare a node to replace another node in the scene graph (also
  1190. * see replace_node()).
  1191. */
  1192. void PandaNode::
  1193. copy_all_properties(PandaNode *other) {
  1194. if (other == this) {
  1195. // Trivial.
  1196. return;
  1197. }
  1198. // Need to have this held before we grab any other locks.
  1199. LightMutexHolder holder(_dirty_prev_transforms._lock);
  1200. bool any_transform_changed = false;
  1201. bool any_state_changed = false;
  1202. bool any_draw_mask_changed = false;
  1203. Thread *current_thread = Thread::get_current_thread();
  1204. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1205. CDStageWriter cdataw(_cycler, pipeline_stage, current_thread);
  1206. CDStageReader cdatar(other->_cycler, pipeline_stage, current_thread);
  1207. if (cdataw->_transform != cdatar->_transform) {
  1208. any_transform_changed = true;
  1209. }
  1210. if (cdataw->_state != cdatar->_state) {
  1211. any_state_changed = true;
  1212. }
  1213. if (cdataw->_draw_control_mask != cdatar->_draw_control_mask ||
  1214. cdataw->_draw_show_mask != cdatar->_draw_show_mask) {
  1215. any_draw_mask_changed = true;
  1216. }
  1217. cdataw->_transform = cdatar->_transform;
  1218. cdataw->_prev_transform = cdatar->_prev_transform;
  1219. cdataw->_state = cdatar->_state;
  1220. cdataw->_effects = cdatar->_effects;
  1221. cdataw->_draw_control_mask = cdatar->_draw_control_mask;
  1222. cdataw->_draw_show_mask = cdatar->_draw_show_mask;
  1223. // The collide mask becomes the union of the two masks. This is important
  1224. // to preserve properties such as the default GeomNode bitmask.
  1225. cdataw->_into_collide_mask |= cdatar->_into_collide_mask;
  1226. for (size_t n = 0; n < cdatar->_tag_data.size(); ++n) {
  1227. cdataw->_tag_data.store(cdatar->_tag_data.get_key(n), cdatar->_tag_data.get_data(n));
  1228. }
  1229. static const int change_bits = (FB_transform | FB_state | FB_effects |
  1230. FB_tag | FB_draw_mask);
  1231. cdataw->_fancy_bits =
  1232. (cdataw->_fancy_bits & ~change_bits) |
  1233. (cdatar->_fancy_bits & change_bits);
  1234. if (pipeline_stage == 0) {
  1235. if (cdataw->_transform != cdataw->_prev_transform) {
  1236. do_set_dirty_prev_transform();
  1237. }
  1238. }
  1239. }
  1240. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1241. // It's okay to copy the tags by pointer, because get_python_tags does a
  1242. // copy-on-write.
  1243. _python_tag_data = other->_python_tag_data;
  1244. if (any_transform_changed || any_state_changed || any_draw_mask_changed) {
  1245. mark_bounds_stale(current_thread);
  1246. if (any_transform_changed) {
  1247. transform_changed();
  1248. }
  1249. if (any_state_changed) {
  1250. state_changed();
  1251. }
  1252. if (any_draw_mask_changed) {
  1253. draw_mask_changed();
  1254. }
  1255. mark_bam_modified();
  1256. }
  1257. }
  1258. /**
  1259. * Inserts this node into the scene graph in place of the other one, and
  1260. * removes the other node. All scene graph attributes (TransformState,
  1261. * RenderState, etc.) are copied to this node.
  1262. *
  1263. * All children are moved to this node, and removed from the old node. The
  1264. * new node is left in the same place in the old node's parent's list of
  1265. * children.
  1266. *
  1267. * Even NodePaths that reference the old node are updated in-place to
  1268. * reference the new node instead.
  1269. *
  1270. * This method is intended to be used to replace a node of a given type in the
  1271. * scene graph with a node of a different type.
  1272. */
  1273. void PandaNode::
  1274. replace_node(PandaNode *other) {
  1275. // nassertv(Thread::get_current_pipeline_stage() == 0);
  1276. if (other == this) {
  1277. // Trivial.
  1278. return;
  1279. }
  1280. // Make sure the other node is not destructed during the execution of this
  1281. // method.
  1282. PT(PandaNode) keep_other = other;
  1283. // Get all the important scene graph properties.
  1284. copy_all_properties(other);
  1285. // Fix up the NodePaths.
  1286. {
  1287. LightReMutexHolder holder1(other->_paths_lock);
  1288. LightReMutexHolder holder2(_paths_lock);
  1289. Paths::iterator pi;
  1290. for (pi = other->_paths.begin(); pi != other->_paths.end(); ++pi) {
  1291. (*pi)->_node = this;
  1292. _paths.insert(*pi);
  1293. }
  1294. other->_paths.clear();
  1295. }
  1296. // Get the children.
  1297. steal_children(other);
  1298. // Switch the parents.
  1299. Thread *current_thread = Thread::get_current_thread();
  1300. Parents other_parents = other->get_parents();
  1301. for (size_t i = 0; i < other_parents.get_num_parents(); ++i) {
  1302. PandaNode *parent = other_parents.get_parent(i);
  1303. if (find_parent(parent) != -1) {
  1304. // This node was already a child of this parent; don't change it.
  1305. parent->remove_child(other);
  1306. } else {
  1307. // This node was not yet a child of this parent; now it is.
  1308. parent->replace_child(other, this, current_thread);
  1309. }
  1310. }
  1311. }
  1312. /**
  1313. * Sets one or more of the PandaNode::UnexpectedChange bits on, indicating
  1314. * that the corresponding property should not change again on this node. Once
  1315. * one of these bits has been set, if the property changes, an assertion
  1316. * failure will be raised, which is designed to assist the developer in
  1317. * identifying the troublesome code that modified the property unexpectedly.
  1318. *
  1319. * The input parameter is the union of bits that are to be set. To clear
  1320. * these bits later, use clear_unexpected_change().
  1321. *
  1322. * Since this is a developer debugging tool only, this function does nothing
  1323. * in a production (NDEBUG) build.
  1324. */
  1325. void PandaNode::
  1326. set_unexpected_change(unsigned int flags) {
  1327. #ifndef NDEBUG
  1328. _unexpected_change_flags |= flags;
  1329. #endif // !NDEBUG
  1330. }
  1331. /**
  1332. * Returns nonzero if any of the bits in the input parameter are set on this
  1333. * node, or zero if none of them are set. More specifically, this returns the
  1334. * particular set of bits (masked by the input parameter) that have been set
  1335. * on this node. See set_unexpected_change().
  1336. *
  1337. * Since this is a developer debugging tool only, this function always returns
  1338. * zero in a production (NDEBUG) build.
  1339. */
  1340. unsigned int PandaNode::
  1341. get_unexpected_change(unsigned int flags) const {
  1342. #ifndef NDEBUG
  1343. return _unexpected_change_flags & flags;
  1344. #else
  1345. return 0;
  1346. #endif // !NDEBUG
  1347. }
  1348. /**
  1349. * Sets one or more of the PandaNode::UnexpectedChange bits off, indicating
  1350. * that the corresponding property may once again change on this node. See
  1351. * set_unexpected_change().
  1352. *
  1353. * The input parameter is the union of bits that are to be cleared.
  1354. *
  1355. * Since this is a developer debugging tool only, this function does nothing
  1356. * in a production (NDEBUG) build.
  1357. */
  1358. void PandaNode::
  1359. clear_unexpected_change(unsigned int flags) {
  1360. #ifndef NDEBUG
  1361. _unexpected_change_flags &= ~flags;
  1362. #endif // !NDEBUG
  1363. }
  1364. /**
  1365. * Adjusts the hide/show bits of this particular node.
  1366. *
  1367. * These three parameters can be used to adjust the _draw_control_mask and
  1368. * _draw_show_mask independently, which work together to provide per-camera
  1369. * visibility for the node and its descendents.
  1370. *
  1371. * _draw_control_mask indicates the bits in _draw_show_mask that are
  1372. * significant. Each different bit corresponds to a different camera (and
  1373. * these bits are assigned via Camera::set_camera_mask()).
  1374. *
  1375. * Where _draw_control_mask has a 1 bit, a 1 bit in _draw_show_mask indicates
  1376. * the node is visible to that camera, and a 0 bit indicates the node is
  1377. * hidden to that camera. Where _draw_control_mask is 0, the node is hidden
  1378. * only if a parent node is hidden.
  1379. *
  1380. * The meaning of the three parameters is as follows:
  1381. *
  1382. * * Wherever show_mask is 1, _draw_show_mask and _draw_control_mask will be
  1383. * set 1. Thus, show_mask indicates the set of cameras to which the node
  1384. * should be shown.
  1385. *
  1386. * * Wherever hide_mask is 1, _draw_show_mask will be set 0 and
  1387. * _draw_control_mask will be set 1. Thus, hide_mask indicates the set of
  1388. * cameras from which the node should be hidden.
  1389. *
  1390. * * Wherever clear_mask is 1, _draw_control_mask will be set 0. Thus,
  1391. * clear_mask indicates the set of cameras from which the hidden state should
  1392. * be inherited from a parent.
  1393. */
  1394. void PandaNode::
  1395. adjust_draw_mask(DrawMask show_mask, DrawMask hide_mask, DrawMask clear_mask) {
  1396. bool any_changed = false;
  1397. Thread *current_thread = Thread::get_current_thread();
  1398. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1399. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  1400. DrawMask draw_control_mask = (cdata->_draw_control_mask | show_mask | hide_mask) & ~clear_mask;
  1401. DrawMask draw_show_mask = (cdata->_draw_show_mask | show_mask) & ~hide_mask;
  1402. // The uncontrolled bits are implicitly on.
  1403. draw_show_mask |= ~draw_control_mask;
  1404. if (cdata->_draw_control_mask != draw_control_mask ||
  1405. cdata->_draw_show_mask != draw_show_mask) {
  1406. cdata->_draw_control_mask = draw_control_mask;
  1407. cdata->_draw_show_mask = draw_show_mask;
  1408. any_changed = true;
  1409. }
  1410. cdata->set_fancy_bit(FB_draw_mask, !draw_control_mask.is_zero());
  1411. }
  1412. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1413. if (any_changed) {
  1414. mark_bounds_stale(current_thread);
  1415. draw_mask_changed();
  1416. mark_bam_modified();
  1417. }
  1418. }
  1419. /**
  1420. * Returns the set of bits in get_net_draw_show_mask() that have been
  1421. * explicitly set via adjust_draw_mask(), rather than implicitly inherited.
  1422. *
  1423. * A 1 bit in any position of this mask indicates that (a) this node has
  1424. * renderable children, and (b) some child of this node has made an explicit
  1425. * hide() or show_through() call for the corresponding bit.
  1426. */
  1427. DrawMask PandaNode::
  1428. get_net_draw_control_mask() const {
  1429. Thread *current_thread = Thread::get_current_thread();
  1430. int pipeline_stage = current_thread->get_pipeline_stage();
  1431. CDLockedStageReader cdata(_cycler, pipeline_stage, current_thread);
  1432. if (cdata->_last_update != cdata->_next_update) {
  1433. // The cache is stale; it needs to be rebuilt.
  1434. PStatTimer timer(_update_bounds_pcollector);
  1435. CDStageWriter cdataw =
  1436. ((PandaNode *)this)->update_cached(false, pipeline_stage, cdata);
  1437. return cdataw->_net_draw_control_mask;
  1438. }
  1439. return cdata->_net_draw_control_mask;
  1440. }
  1441. /**
  1442. * Returns the union of all draw_show_mask values--of renderable nodes only--
  1443. * at this level and below. If any bit in this mask is 0, there is no reason
  1444. * to traverse below this node for a camera with the corresponding
  1445. * camera_mask.
  1446. *
  1447. * The bits in this mask that do not correspond to a 1 bit in the
  1448. * net_draw_control_mask are meaningless (and will be set to 1). For bits
  1449. * that *do* correspond to a 1 bit in the net_draw_control_mask, a 1 bit
  1450. * indicates that at least one child should be visible, while a 0 bit
  1451. * indicates that all children are hidden.
  1452. */
  1453. DrawMask PandaNode::
  1454. get_net_draw_show_mask() const {
  1455. Thread *current_thread = Thread::get_current_thread();
  1456. int pipeline_stage = current_thread->get_pipeline_stage();
  1457. CDLockedStageReader cdata(_cycler, pipeline_stage, current_thread);
  1458. if (cdata->_last_update != cdata->_next_update) {
  1459. // The cache is stale; it needs to be rebuilt.
  1460. PStatTimer timer(_update_bounds_pcollector);
  1461. CDStageWriter cdataw =
  1462. ((PandaNode *)this)->update_cached(false, pipeline_stage, cdata);
  1463. return cdataw->_net_draw_show_mask;
  1464. }
  1465. return cdata->_net_draw_show_mask;
  1466. }
  1467. /**
  1468. * Sets the "into" CollideMask.
  1469. *
  1470. * This specifies the set of bits that must be shared with a CollisionNode's
  1471. * "from" CollideMask in order for the CollisionNode to detect a collision
  1472. * with this particular node.
  1473. *
  1474. * The actual CollideMask that will be set is masked by the return value from
  1475. * get_legal_collide_mask(). Thus, the into_collide_mask cannot be set to
  1476. * anything other than nonzero except for those types of nodes that can be
  1477. * collided into, such as CollisionNodes and GeomNodes.
  1478. */
  1479. void PandaNode::
  1480. set_into_collide_mask(CollideMask mask) {
  1481. mask &= get_legal_collide_mask();
  1482. bool any_changed = false;
  1483. Thread *current_thread = Thread::get_current_thread();
  1484. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1485. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  1486. if (cdata->_into_collide_mask != mask) {
  1487. cdata->_into_collide_mask = mask;
  1488. any_changed = true;
  1489. }
  1490. }
  1491. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1492. if (any_changed) {
  1493. mark_bounds_stale(current_thread);
  1494. mark_bam_modified();
  1495. }
  1496. }
  1497. /**
  1498. * Returns the subset of CollideMask bits that may be set for this particular
  1499. * type of PandaNode. For most nodes, this is 0; it doesn't make sense to set
  1500. * a CollideMask for most kinds of nodes.
  1501. *
  1502. * For nodes that can be collided with, such as GeomNode and CollisionNode,
  1503. * this returns all bits on.
  1504. */
  1505. CollideMask PandaNode::
  1506. get_legal_collide_mask() const {
  1507. return CollideMask::all_off();
  1508. }
  1509. /**
  1510. * Returns the union of all into_collide_mask() values set at CollisionNodes
  1511. * at this level and below.
  1512. */
  1513. CollideMask PandaNode::
  1514. get_net_collide_mask(Thread *current_thread) const {
  1515. int pipeline_stage = current_thread->get_pipeline_stage();
  1516. CDLockedStageReader cdata(_cycler, pipeline_stage, current_thread);
  1517. if (cdata->_last_update != cdata->_next_update) {
  1518. // The cache is stale; it needs to be rebuilt.
  1519. PStatTimer timer(_update_bounds_pcollector);
  1520. CDStageWriter cdataw =
  1521. ((PandaNode *)this)->update_cached(false, pipeline_stage, cdata);
  1522. return cdataw->_net_collide_mask;
  1523. }
  1524. return cdata->_net_collide_mask;
  1525. }
  1526. /**
  1527. * Returns a ClipPlaneAttrib which represents the union of all of the clip
  1528. * planes that have been turned *off* at this level and below.
  1529. */
  1530. CPT(RenderAttrib) PandaNode::
  1531. get_off_clip_planes(Thread *current_thread) const {
  1532. int pipeline_stage = current_thread->get_pipeline_stage();
  1533. CDLockedStageReader cdata(_cycler, pipeline_stage, current_thread);
  1534. if (cdata->_last_update != cdata->_next_update) {
  1535. // The cache is stale; it needs to be rebuilt.
  1536. PStatTimer timer(_update_bounds_pcollector);
  1537. CDStageWriter cdataw =
  1538. ((PandaNode *)this)->update_cached(false, pipeline_stage, cdata);
  1539. return cdataw->_off_clip_planes;
  1540. }
  1541. return cdata->_off_clip_planes;
  1542. }
  1543. /**
  1544. * Walks through the scene graph beginning at this node, and does whatever
  1545. * initialization is required to render the scene properly with the indicated
  1546. * GSG. It is not strictly necessary to call this, since the GSG will
  1547. * initialize itself when the scene is rendered, but this may take some of the
  1548. * overhead away from that process.
  1549. *
  1550. * In particular, this will ensure that textures and vertex buffers within the
  1551. * scene are loaded into graphics memory.
  1552. */
  1553. void PandaNode::
  1554. prepare_scene(GraphicsStateGuardianBase *gsg, const RenderState *node_state) {
  1555. GeomTransformer transformer;
  1556. Thread *current_thread = Thread::get_current_thread();
  1557. r_prepare_scene(gsg, node_state, transformer, current_thread);
  1558. }
  1559. /**
  1560. * Returns true if this particular node is known to be the render root of some
  1561. * active DisplayRegion associated with the global GraphicsEngine, false
  1562. * otherwise.
  1563. */
  1564. bool PandaNode::
  1565. is_scene_root() const {
  1566. // This function pointer has to be filled in when the global GraphicsEngine
  1567. // is created, because we can't link with the GraphicsEngine functions
  1568. // directly.
  1569. if (_scene_root_func != nullptr) {
  1570. return (*_scene_root_func)(this);
  1571. }
  1572. return false;
  1573. }
  1574. /**
  1575. * Returns true if this particular node is in a live scene graph: that is, it
  1576. * is a child or descendent of a node that is itself a scene root. If this is
  1577. * true, this node may potentially be traversed by the render traverser.
  1578. * Stashed nodes don't count for this purpose, but hidden nodes do.
  1579. */
  1580. bool PandaNode::
  1581. is_under_scene_root() const {
  1582. if (is_scene_root()) {
  1583. return true;
  1584. }
  1585. Parents parents = get_parents();
  1586. for (size_t i = 0; i < parents.get_num_parents(); ++i) {
  1587. PandaNode *parent = parents.get_parent(i);
  1588. if (parent->find_stashed((PandaNode *)this) == -1) {
  1589. if (parent->is_under_scene_root()) {
  1590. return true;
  1591. }
  1592. }
  1593. }
  1594. return false;
  1595. }
  1596. /**
  1597. *
  1598. */
  1599. void PandaNode::
  1600. output(ostream &out) const {
  1601. out << get_type() << " " << get_name();
  1602. }
  1603. /**
  1604. *
  1605. */
  1606. void PandaNode::
  1607. write(ostream &out, int indent_level) const {
  1608. indent(out, indent_level) << *this;
  1609. if (has_tags()) {
  1610. out << " [";
  1611. list_tags(out, " ");
  1612. out << "]";
  1613. }
  1614. CPT(TransformState) transform = get_transform();
  1615. if (!transform->is_identity()) {
  1616. out << " " << *transform;
  1617. }
  1618. CPT(RenderState) state = get_state();
  1619. if (!state->is_empty()) {
  1620. out << " " << *state;
  1621. }
  1622. CPT(RenderEffects) effects = get_effects();
  1623. if (!effects->is_empty()) {
  1624. out << " " << *effects;
  1625. }
  1626. DrawMask draw_control_mask = get_draw_control_mask();
  1627. if (!draw_control_mask.is_zero()) {
  1628. DrawMask draw_show_mask = get_draw_show_mask();
  1629. if (!(draw_control_mask & _overall_bit).is_zero()) {
  1630. if (!(draw_show_mask & _overall_bit).is_zero()) {
  1631. out << " (show_through)";
  1632. } else {
  1633. out << " (hidden)";
  1634. }
  1635. }
  1636. if (!(draw_control_mask & ~_overall_bit).is_zero()) {
  1637. draw_control_mask &= ~_overall_bit;
  1638. if (!(draw_show_mask & draw_control_mask).is_zero()) {
  1639. out << " (per-camera show_through)";
  1640. }
  1641. if (!(~draw_show_mask & draw_control_mask).is_zero()) {
  1642. out << " (per-camera hidden)";
  1643. }
  1644. }
  1645. }
  1646. out << "\n";
  1647. }
  1648. /**
  1649. * Specifies the desired type of bounding volume that will be created for this
  1650. * node. This is normally BoundingVolume::BT_default, which means to set the
  1651. * type according to the config variable "bounds-type".
  1652. *
  1653. * If this is BT_sphere or BT_box, a BoundingSphere or BoundingBox is
  1654. * explicitly created. If it is BT_best, the appropriate type to best enclose
  1655. * the node's children is created.
  1656. *
  1657. * This affects the bounding volume returned by get_bounds(), which is not
  1658. * exactly the same bounding volume modified by set_bounds(), because a new
  1659. * bounding volume has to be created that includes this node and all of its
  1660. * children.
  1661. */
  1662. void PandaNode::
  1663. set_bounds_type(BoundingVolume::BoundsType bounds_type) {
  1664. Thread *current_thread = Thread::get_current_thread();
  1665. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1666. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  1667. cdata->_bounds_type = bounds_type;
  1668. mark_bounds_stale(pipeline_stage, current_thread);
  1669. // GeomNodes, CollisionNodes, and PGItems all have an internal bounds that
  1670. // may need to be updated when the bounds_type changes.
  1671. mark_internal_bounds_stale(pipeline_stage, current_thread);
  1672. mark_bam_modified();
  1673. }
  1674. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1675. }
  1676. /**
  1677. * Returns the bounding volume type set with set_bounds_type().
  1678. */
  1679. BoundingVolume::BoundsType PandaNode::
  1680. get_bounds_type() const {
  1681. CDReader cdata(_cycler);
  1682. return cdata->_bounds_type;
  1683. }
  1684. /**
  1685. * Resets the bounding volume so that it is the indicated volume. When it is
  1686. * explicitly set, the bounding volume will no longer be automatically
  1687. * computed according to the contents of the node itself, for nodes like
  1688. * GeomNodes and TextNodes that contain substance (but the bounding volume
  1689. * will still be automatically expanded to include its children).
  1690. *
  1691. * Call clear_bounds() if you would like to return the bounding volume to its
  1692. * default behavior later.
  1693. */
  1694. void PandaNode::
  1695. set_bounds(const BoundingVolume *volume) {
  1696. Thread *current_thread = Thread::get_current_thread();
  1697. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1698. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  1699. if (volume == nullptr) {
  1700. cdata->_user_bounds = nullptr;
  1701. } else {
  1702. cdata->_user_bounds = volume->make_copy();
  1703. }
  1704. mark_bounds_stale(pipeline_stage, current_thread);
  1705. mark_bam_modified();
  1706. }
  1707. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1708. }
  1709. /**
  1710. * Deprecated. Use set_bounds() instead.
  1711. */
  1712. void PandaNode::
  1713. set_bound(const BoundingVolume *volume) {
  1714. pgraph_cat.warning()
  1715. << "Deprecated PandaNode::set_bound() called. Use set_bounds() instead.\n";
  1716. set_bounds(volume);
  1717. }
  1718. /**
  1719. * Returns the external bounding volume of this node: a bounding volume that
  1720. * contains the user bounding volume, the internal bounding volume, and all of
  1721. * the children's bounding volumes.
  1722. */
  1723. CPT(BoundingVolume) PandaNode::
  1724. get_bounds(Thread *current_thread) const {
  1725. int pipeline_stage = current_thread->get_pipeline_stage();
  1726. CDLockedStageReader cdata(_cycler, pipeline_stage, current_thread);
  1727. if (cdata->_last_bounds_update != cdata->_next_update) {
  1728. // The cache is stale; it needs to be rebuilt.
  1729. CPT(BoundingVolume) result;
  1730. {
  1731. PStatTimer timer(_update_bounds_pcollector);
  1732. CDStageWriter cdataw =
  1733. ((PandaNode *)this)->update_cached(true, pipeline_stage, cdata);
  1734. result = cdataw->_external_bounds;
  1735. }
  1736. return result;
  1737. }
  1738. return cdata->_external_bounds;
  1739. }
  1740. /**
  1741. * This flavor of get_bounds() return the external bounding volume, and also
  1742. * fills in seq with the bounding volume's current sequence number. When this
  1743. * sequence number changes, it indicates that the bounding volume might have
  1744. * changed, e.g. because some nested child's bounding volume has changed.
  1745. *
  1746. * Although this might occasionally increment without changing the bounding
  1747. * volume, the bounding volume will never change without incrementing this
  1748. * counter, so as long as this counter remains unchanged you can be confident
  1749. * the bounding volume is also unchanged.
  1750. */
  1751. CPT(BoundingVolume) PandaNode::
  1752. get_bounds(UpdateSeq &seq, Thread *current_thread) const {
  1753. int pipeline_stage = current_thread->get_pipeline_stage();
  1754. CDLockedStageReader cdata(_cycler, pipeline_stage, current_thread);
  1755. if (cdata->_last_bounds_update != cdata->_next_update) {
  1756. // The cache is stale; it needs to be rebuilt.
  1757. CPT(BoundingVolume) result;
  1758. {
  1759. PStatTimer timer(_update_bounds_pcollector);
  1760. CDStageWriter cdataw =
  1761. ((PandaNode *)this)->update_cached(true, pipeline_stage, cdata);
  1762. result = cdataw->_external_bounds;
  1763. seq = cdataw->_last_bounds_update;
  1764. }
  1765. return result;
  1766. }
  1767. seq = cdata->_last_bounds_update;
  1768. return cdata->_external_bounds;
  1769. }
  1770. /**
  1771. * Returns the total number of vertices that will be rendered by this node and
  1772. * all of its descendents.
  1773. *
  1774. * This is not necessarily an accurate count of vertices that will actually be
  1775. * rendered, since this will include all vertices of all LOD's, and it will
  1776. * also include hidden nodes. It may also omit or only approximate certain
  1777. * kinds of dynamic geometry. However, it will not include stashed nodes.
  1778. */
  1779. int PandaNode::
  1780. get_nested_vertices(Thread *current_thread) const {
  1781. int pipeline_stage = current_thread->get_pipeline_stage();
  1782. CDLockedStageReader cdata(_cycler, pipeline_stage, current_thread);
  1783. if (cdata->_last_bounds_update != cdata->_next_update) {
  1784. // The cache is stale; it needs to be rebuilt.
  1785. int result;
  1786. {
  1787. PStatTimer timer(_update_bounds_pcollector);
  1788. CDStageWriter cdataw =
  1789. ((PandaNode *)this)->update_cached(true, pipeline_stage, cdata);
  1790. result = cdataw->_nested_vertices;
  1791. }
  1792. return result;
  1793. }
  1794. return cdata->_nested_vertices;
  1795. }
  1796. /**
  1797. * Indicates that the bounding volume, or something that influences the
  1798. * bounding volume (or any of the other things stored in CData, like
  1799. * net_collide_mask), may have changed for this node, and that it must be
  1800. * recomputed.
  1801. *
  1802. * With no parameters, this means to iterate through all stages including and
  1803. * upstream of the current pipeline stage.
  1804. *
  1805. * This method is intended for internal use; usually it is not necessary for a
  1806. * user to call this directly. It will be called automatically by derived
  1807. * classes when appropriate.
  1808. */
  1809. void PandaNode::
  1810. mark_bounds_stale(Thread *current_thread) const {
  1811. OPEN_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler, current_thread) {
  1812. mark_bounds_stale(pipeline_stage, current_thread);
  1813. }
  1814. CLOSE_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler);
  1815. }
  1816. /**
  1817. * Should be called by a derived class to mark the internal bounding volume
  1818. * stale, so that compute_internal_bounds() will be called when the bounding
  1819. * volume is next requested.
  1820. *
  1821. * With no parameters, this means to iterate through all stages including and
  1822. * upstream of the current pipeline stage.
  1823. *
  1824. * It is normally not necessary to call this method directly; each node should
  1825. * be responsible for calling it when its internals have changed.
  1826. */
  1827. void PandaNode::
  1828. mark_internal_bounds_stale(Thread *current_thread) {
  1829. OPEN_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler, current_thread) {
  1830. mark_internal_bounds_stale(pipeline_stage, current_thread);
  1831. }
  1832. CLOSE_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler);
  1833. }
  1834. /**
  1835. * A simple downcast check. Returns true if this kind of node happens to
  1836. * inherit from GeomNode, false otherwise.
  1837. *
  1838. * This is provided as a a faster alternative to calling
  1839. * is_of_type(GeomNode::get_class_type()), since this test is so important to
  1840. * rendering.
  1841. */
  1842. bool PandaNode::
  1843. is_geom_node() const {
  1844. return false;
  1845. }
  1846. /**
  1847. * A simple downcast check. Returns true if this kind of node happens to
  1848. * inherit from LODNode, false otherwise.
  1849. *
  1850. * This is provided as a a faster alternative to calling
  1851. * is_of_type(LODNode::get_class_type()).
  1852. */
  1853. bool PandaNode::
  1854. is_lod_node() const {
  1855. return false;
  1856. }
  1857. /**
  1858. * A simple downcast check. Returns true if this kind of node happens to
  1859. * inherit from CollisionNode, false otherwise.
  1860. *
  1861. * This is provided as a a faster alternative to calling
  1862. * is_of_type(CollisionNode::get_class_type()).
  1863. */
  1864. bool PandaNode::
  1865. is_collision_node() const {
  1866. return false;
  1867. }
  1868. /**
  1869. * Cross-casts the node to a Light pointer, if it is one of the four kinds of
  1870. * Light nodes, or returns NULL if it is not.
  1871. */
  1872. Light *PandaNode::
  1873. as_light() {
  1874. return nullptr;
  1875. }
  1876. /**
  1877. * Returns true if this is an AmbientLight, false if it is not a light, or it
  1878. * is some other kind of light.
  1879. */
  1880. bool PandaNode::
  1881. is_ambient_light() const {
  1882. return false;
  1883. }
  1884. /**
  1885. * Reads the bytes created by a previous call to encode_to_bam_stream(), and
  1886. * extracts and returns the single object on those bytes. Returns NULL on
  1887. * error.
  1888. *
  1889. * This method is intended to replace decode_raw_from_bam_stream() when you
  1890. * know the stream in question returns an object of type PandaNode, allowing
  1891. * for easier reference count management. Note that the caller is still
  1892. * responsible for maintaining the reference count on the return value.
  1893. */
  1894. PT(PandaNode) PandaNode::
  1895. decode_from_bam_stream(vector_uchar data, BamReader *reader) {
  1896. TypedWritable *object;
  1897. ReferenceCount *ref_ptr;
  1898. if (TypedWritable::decode_raw_from_bam_stream(object, ref_ptr, std::move(data), reader)) {
  1899. return DCAST(PandaNode, object);
  1900. } else {
  1901. return nullptr;
  1902. }
  1903. }
  1904. /**
  1905. * Returns the node's internal bounding volume. This is the bounding volume
  1906. * around the node alone, without including children.
  1907. */
  1908. CPT(BoundingVolume) PandaNode::
  1909. get_internal_bounds(int pipeline_stage, Thread *current_thread) const {
  1910. while (true) {
  1911. UpdateSeq mark;
  1912. {
  1913. CDStageReader cdata(_cycler, pipeline_stage, current_thread);
  1914. if (cdata->_user_bounds != nullptr) {
  1915. return cdata->_user_bounds;
  1916. }
  1917. if (cdata->_internal_bounds_mark == cdata->_internal_bounds_computed) {
  1918. return cdata->_internal_bounds;
  1919. }
  1920. mark = cdata->_internal_bounds_mark;
  1921. }
  1922. // First, call compute_internal_bounds without acquiring the lock. This
  1923. // avoids a deadlock condition.
  1924. CPT(BoundingVolume) internal_bounds;
  1925. int internal_vertices;
  1926. compute_internal_bounds(internal_bounds, internal_vertices,
  1927. pipeline_stage, current_thread);
  1928. nassertr(!internal_bounds.is_null(), nullptr);
  1929. // Now, acquire the lock, and apply the above-computed bounds.
  1930. CDStageWriter cdataw(((PandaNode *)this)->_cycler, pipeline_stage);
  1931. if (cdataw->_internal_bounds_mark == mark) {
  1932. cdataw->_internal_bounds_computed = mark;
  1933. cdataw->_internal_bounds = internal_bounds;
  1934. cdataw->_internal_vertices = internal_vertices;
  1935. ((PandaNode *)this)->mark_bam_modified();
  1936. return cdataw->_internal_bounds;
  1937. }
  1938. // Dang, someone in another thread incremented _internal_bounds_mark while
  1939. // we weren't holding the lock. That means we need to go back and do it
  1940. // again.
  1941. }
  1942. }
  1943. /**
  1944. * Returns the total number of vertices that will be rendered by this
  1945. * particular node alone, not accounting for its children.
  1946. *
  1947. * This may not include all vertices for certain dynamic effects.
  1948. */
  1949. int PandaNode::
  1950. get_internal_vertices(int pipeline_stage, Thread *current_thread) const {
  1951. while (true) {
  1952. UpdateSeq mark;
  1953. {
  1954. CDStageReader cdata(_cycler, pipeline_stage, current_thread);
  1955. if (cdata->_internal_bounds_mark == cdata->_internal_bounds_computed) {
  1956. return cdata->_internal_vertices;
  1957. }
  1958. mark = cdata->_internal_bounds_mark;
  1959. }
  1960. // First, call compute_internal_bounds without acquiring the lock. This
  1961. // avoids a deadlock condition.
  1962. CPT(BoundingVolume) internal_bounds;
  1963. int internal_vertices;
  1964. compute_internal_bounds(internal_bounds, internal_vertices,
  1965. pipeline_stage, current_thread);
  1966. nassertr(!internal_bounds.is_null(), 0);
  1967. // Now, acquire the lock, and apply the above-computed bounds.
  1968. CDStageWriter cdataw(((PandaNode *)this)->_cycler, pipeline_stage);
  1969. if (cdataw->_internal_bounds_mark == mark) {
  1970. cdataw->_internal_bounds_computed = mark;
  1971. cdataw->_internal_bounds = internal_bounds;
  1972. cdataw->_internal_vertices = internal_vertices;
  1973. ((PandaNode *)this)->mark_bam_modified();
  1974. return cdataw->_internal_vertices;
  1975. }
  1976. // Dang, someone in another thread incremented _internal_bounds_mark while
  1977. // we weren't holding the lock. That means we need to go back and do it
  1978. // again.
  1979. }
  1980. }
  1981. /**
  1982. * This is provided as an alternate way for a node to set its own internal
  1983. * bounds, rather than overloading compute_internal_bounds(). If this method
  1984. * is called, the internal bounding volume will immediately be set to the
  1985. * indicated pointer.
  1986. */
  1987. void PandaNode::
  1988. set_internal_bounds(const BoundingVolume *volume) {
  1989. Thread *current_thread = Thread::get_current_thread();
  1990. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  1991. CDStageWriter cdataw(_cycler, pipeline_stage, current_thread);
  1992. cdataw->_internal_bounds = volume;
  1993. cdataw->_internal_bounds_computed = cdataw->_internal_bounds_mark;
  1994. }
  1995. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  1996. mark_bounds_stale(current_thread);
  1997. mark_bam_modified();
  1998. }
  1999. /**
  2000. * Similar to mark_bounds_stale(), except that the parents of this node marked
  2001. * stale even if this node was already considered stale.
  2002. *
  2003. * With no parameters, this means to iterate through all stages including and
  2004. * upstream of the current pipeline stage.
  2005. */
  2006. void PandaNode::
  2007. force_bounds_stale(Thread *current_thread) {
  2008. OPEN_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler, current_thread) {
  2009. force_bounds_stale(pipeline_stage, current_thread);
  2010. }
  2011. CLOSE_ITERATE_CURRENT_AND_UPSTREAM_NOLOCK(_cycler);
  2012. }
  2013. /**
  2014. * Similar to mark_bounds_stale(), except that the parents of this node marked
  2015. * stale even if this node was already considered stale.
  2016. */
  2017. void PandaNode::
  2018. force_bounds_stale(int pipeline_stage, Thread *current_thread) {
  2019. {
  2020. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  2021. ++cdata->_next_update;
  2022. mark_bam_modified();
  2023. // It is important that we allow this lock to be dropped before we
  2024. // continue up the graph; otherwise, we risk deadlock from another thread
  2025. // walking down the graph.
  2026. }
  2027. // It is similarly important that we use get_parents() here to copy the
  2028. // parents list, instead of keeping the lock open while we walk through the
  2029. // parents list directly on the node.
  2030. Parents parents;
  2031. {
  2032. CDStageReader cdata(_cycler, pipeline_stage, current_thread);
  2033. parents = Parents(cdata);
  2034. }
  2035. int num_parents = parents.get_num_parents();
  2036. for (int i = 0; i < num_parents; ++i) {
  2037. PandaNode *parent = parents.get_parent(i);
  2038. parent->mark_bounds_stale(pipeline_stage, current_thread);
  2039. }
  2040. }
  2041. /**
  2042. * Recursively calls Geom::mark_bounds_stale() on every Geom at this node and
  2043. * below.
  2044. */
  2045. void PandaNode::
  2046. r_mark_geom_bounds_stale(Thread *current_thread) {
  2047. Children children = get_children(current_thread);
  2048. size_t i;
  2049. for (i = 0; i < children.get_num_children(); i++) {
  2050. PandaNode *child = children.get_child(i);
  2051. child->r_mark_geom_bounds_stale(current_thread);
  2052. }
  2053. Stashed stashed = get_stashed(current_thread);
  2054. for (i = 0; i < stashed.get_num_stashed(); i++) {
  2055. PandaNode *child = stashed.get_stashed(i);
  2056. child->r_mark_geom_bounds_stale(current_thread);
  2057. }
  2058. }
  2059. /**
  2060. * Returns a newly-allocated BoundingVolume that represents the internal
  2061. * contents of the node. Should be overridden by PandaNode classes that
  2062. * contain something internally.
  2063. */
  2064. void PandaNode::
  2065. compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
  2066. int &internal_vertices,
  2067. int pipeline_stage,
  2068. Thread *current_thread) const {
  2069. internal_bounds = new BoundingSphere;
  2070. internal_vertices = 0;
  2071. }
  2072. /**
  2073. * Called after a scene graph update that either adds or remove parents from
  2074. * this node, this just provides a hook for derived PandaNode objects that
  2075. * need to update themselves based on the set of parents the node has.
  2076. */
  2077. void PandaNode::
  2078. parents_changed() {
  2079. nassertv((_unexpected_change_flags & UC_parents) == 0);
  2080. }
  2081. /**
  2082. * Called after a scene graph update that either adds or remove children from
  2083. * this node, this just provides a hook for derived PandaNode objects that
  2084. * need to update themselves based on the set of children the node has.
  2085. */
  2086. void PandaNode::
  2087. children_changed() {
  2088. nassertv((_unexpected_change_flags & UC_children) == 0);
  2089. }
  2090. /**
  2091. * Called after the node's transform has been changed for any reason, this
  2092. * just provides a hook so derived classes can do something special in this
  2093. * case.
  2094. */
  2095. void PandaNode::
  2096. transform_changed() {
  2097. nassertv((_unexpected_change_flags & UC_transform) == 0);
  2098. }
  2099. /**
  2100. * Called after the node's RenderState has been changed for any reason, this
  2101. * just provides a hook so derived classes can do something special in this
  2102. * case.
  2103. */
  2104. void PandaNode::
  2105. state_changed() {
  2106. nassertv((_unexpected_change_flags & UC_state) == 0);
  2107. }
  2108. /**
  2109. * Called after the node's DrawMask has been changed for any reason, this just
  2110. * provides a hook so derived classes can do something special in this case.
  2111. */
  2112. void PandaNode::
  2113. draw_mask_changed() {
  2114. nassertv((_unexpected_change_flags & UC_draw_mask) == 0);
  2115. }
  2116. /**
  2117. * This is the recursive implementation of copy_subgraph(). It returns a copy
  2118. * of the entire subgraph rooted at this node.
  2119. *
  2120. * Note that it includes the parameter inst_map, which is a map type, and is
  2121. * not (and cannot be) exported from PANDA.DLL. Thus, any derivative of
  2122. * PandaNode that is not also a member of PANDA.DLL *cannot* access this map.
  2123. */
  2124. PT(PandaNode) PandaNode::
  2125. r_copy_subgraph(PandaNode::InstanceMap &inst_map, Thread *current_thread) const {
  2126. PT(PandaNode) copy = make_copy();
  2127. nassertr(copy != nullptr, nullptr);
  2128. if (copy->get_type() != get_type()) {
  2129. pgraph_cat.warning()
  2130. << "Don't know how to copy nodes of type " << get_type() << "\n";
  2131. if (no_unsupported_copy) {
  2132. nassert_raise("unsupported copy");
  2133. return nullptr;
  2134. }
  2135. }
  2136. copy->r_copy_children(this, inst_map, current_thread);
  2137. return copy;
  2138. }
  2139. /**
  2140. * This is called by r_copy_subgraph(); the copy has already been made of this
  2141. * particular node (and this is the copy); this function's job is to copy all
  2142. * of the children from the original.
  2143. *
  2144. * Note that it includes the parameter inst_map, which is a map type, and is
  2145. * not (and cannot be) exported from PANDA.DLL. Thus, any derivative of
  2146. * PandaNode that is not also a member of PANDA.DLL *cannot* access this map,
  2147. * and probably should not even override this function.
  2148. */
  2149. void PandaNode::
  2150. r_copy_children(const PandaNode *from, PandaNode::InstanceMap &inst_map,
  2151. Thread *current_thread) {
  2152. CDReader from_cdata(from->_cycler, current_thread);
  2153. CPT(Down) from_down = from_cdata->get_down();
  2154. Down::const_iterator di;
  2155. for (di = from_down->begin(); di != from_down->end(); ++di) {
  2156. int sort = (*di).get_sort();
  2157. PandaNode *source_child = (*di).get_child();
  2158. PT(PandaNode) dest_child;
  2159. // Check to see if we have already copied this child. If we have, use the
  2160. // copy. In this way, a subgraph that contains instances will be
  2161. // correctly duplicated into another subgraph that also contains its own
  2162. // instances.
  2163. InstanceMap::const_iterator ci;
  2164. ci = inst_map.find(source_child);
  2165. if (ci != inst_map.end()) {
  2166. dest_child = (*ci).second;
  2167. } else {
  2168. dest_child = source_child->r_copy_subgraph(inst_map, current_thread);
  2169. inst_map[source_child] = dest_child;
  2170. }
  2171. quick_add_new_child(dest_child, sort, current_thread);
  2172. }
  2173. }
  2174. /**
  2175. * The recursive implementation of prepare_scene(). Don't call this directly;
  2176. * call PandaNode::prepare_scene() or NodePath::prepare_scene() instead.
  2177. */
  2178. void PandaNode::
  2179. r_prepare_scene(GraphicsStateGuardianBase *gsg, const RenderState *node_state,
  2180. GeomTransformer &transformer, Thread *current_thread) {
  2181. Children children = get_children(current_thread);
  2182. // We must call get_num_children() each time through the loop, in case we're
  2183. // running SIMPLE_THREADS and we get interrupted.
  2184. size_t i;
  2185. for (i = 0; i < children.get_num_children(); i++) {
  2186. PandaNode *child = children.get_child(i);
  2187. CPT(RenderState) child_state = node_state->compose(child->get_state());
  2188. child->r_prepare_scene(gsg, child_state, transformer, current_thread);
  2189. }
  2190. Stashed stashed = get_stashed(current_thread);
  2191. for (i = 0; i < stashed.get_num_stashed(); i++) {
  2192. PandaNode *child = stashed.get_stashed(i);
  2193. CPT(RenderState) child_state = node_state->compose(child->get_state());
  2194. child->r_prepare_scene(gsg, child_state, transformer, current_thread);
  2195. }
  2196. }
  2197. /**
  2198. * Intended to be called in the constructor by any subclass that defines
  2199. * cull_callback(), this sets up the flags to indicate that the cullback needs
  2200. * to be called.
  2201. */
  2202. void PandaNode::
  2203. set_cull_callback() {
  2204. Thread *current_thread = Thread::get_current_thread();
  2205. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  2206. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  2207. cdata->set_fancy_bit(FB_cull_callback, true);
  2208. }
  2209. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  2210. mark_bam_modified();
  2211. }
  2212. /**
  2213. * disables the call back
  2214. */
  2215. void PandaNode::
  2216. disable_cull_callback() {
  2217. Thread *current_thread = Thread::get_current_thread();
  2218. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  2219. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  2220. cdata->set_fancy_bit(FB_cull_callback, false);
  2221. }
  2222. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  2223. mark_bam_modified();
  2224. }
  2225. /**
  2226. * The private implementation of remove_child(), for a particular pipeline
  2227. * stage.
  2228. */
  2229. bool PandaNode::
  2230. stage_remove_child(PandaNode *child_node, int pipeline_stage,
  2231. Thread *current_thread) {
  2232. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  2233. // First, look for the parent in the child's up list, to ensure the child is
  2234. // known.
  2235. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage,
  2236. current_thread);
  2237. int parent_index = child_node->do_find_parent(this, cdata_child);
  2238. if (parent_index < 0) {
  2239. // Nope, no relation.
  2240. return false;
  2241. }
  2242. PT(Down) down = cdata->modify_down();
  2243. int child_index = do_find_child(child_node, down);
  2244. if (child_index >= 0) {
  2245. // The child exists; remove it.
  2246. down->erase(down->begin() + child_index);
  2247. int num_erased = cdata_child->modify_up()->erase(UpConnection(this));
  2248. nassertr(num_erased == 1, false);
  2249. return true;
  2250. }
  2251. PT(Down) stashed = cdata->modify_stashed();
  2252. int stashed_index = do_find_child(child_node, stashed);
  2253. if (stashed_index >= 0) {
  2254. // The child has been stashed; remove it.
  2255. stashed->erase(stashed->begin() + stashed_index);
  2256. int num_erased = cdata_child->modify_up()->erase(UpConnection(this));
  2257. nassertr(num_erased == 1, false);
  2258. return true;
  2259. }
  2260. // Never heard of this child. This shouldn't be possible, because the
  2261. // parent was in the child's up list, above. Must be some internal error.
  2262. nassertr(false, false);
  2263. return false;
  2264. }
  2265. /**
  2266. * The private implementation of replace_child(), for a particular pipeline
  2267. * stage.
  2268. */
  2269. bool PandaNode::
  2270. stage_replace_child(PandaNode *orig_child, PandaNode *new_child,
  2271. int pipeline_stage, Thread *current_thread) {
  2272. {
  2273. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  2274. CDStageWriter cdata_orig_child(orig_child->_cycler, pipeline_stage, current_thread);
  2275. CDStageWriter cdata_new_child(new_child->_cycler, pipeline_stage, current_thread);
  2276. // First, look for the parent in the child's up list, to ensure the child
  2277. // is known.
  2278. int parent_index = orig_child->do_find_parent(this, cdata_orig_child);
  2279. if (parent_index < 0) {
  2280. // Nope, no relation.
  2281. return false;
  2282. }
  2283. if (orig_child == new_child) {
  2284. // Trivial no-op.
  2285. return true;
  2286. }
  2287. // Don't let orig_child be destructed yet.
  2288. PT(PandaNode) keep_orig_child = orig_child;
  2289. // If we already have new_child as a child, remove it first.
  2290. if (stage_remove_child(new_child, pipeline_stage, current_thread)) {
  2291. sever_connection(this, new_child, pipeline_stage, current_thread);
  2292. }
  2293. PT(Down) down = cdata->modify_down();
  2294. int child_index = do_find_child(orig_child, down);
  2295. if (child_index >= 0) {
  2296. // The child exists; replace it.
  2297. DownConnection &dc = (*down)[child_index];
  2298. nassertr(dc.get_child() == orig_child, false);
  2299. dc.set_child(new_child);
  2300. } else {
  2301. PT(Down) stashed = cdata->modify_stashed();
  2302. int stashed_index = do_find_child(orig_child, stashed);
  2303. if (stashed_index >= 0) {
  2304. // The child has been stashed; remove it.
  2305. DownConnection &dc = (*stashed)[stashed_index];
  2306. nassertr(dc.get_child() == orig_child, false);
  2307. dc.set_child(new_child);
  2308. } else {
  2309. // Never heard of this child. This shouldn't be possible, because the
  2310. // parent was in the child's up list, above. Must be some internal
  2311. // error.
  2312. nassertr(false, false);
  2313. return false;
  2314. }
  2315. }
  2316. // Now adjust the bookkeeping on both children.
  2317. cdata_new_child->modify_up()->insert(UpConnection(this));
  2318. int num_erased = cdata_orig_child->modify_up()->erase(UpConnection(this));
  2319. nassertr(num_erased == 1, false);
  2320. }
  2321. sever_connection(this, orig_child, pipeline_stage, current_thread);
  2322. new_connection(this, new_child, pipeline_stage, current_thread);
  2323. force_bounds_stale(pipeline_stage, current_thread);
  2324. orig_child->parents_changed();
  2325. new_child->parents_changed();
  2326. mark_bam_modified();
  2327. orig_child->mark_bam_modified();
  2328. new_child->mark_bam_modified();
  2329. return true;
  2330. }
  2331. /**
  2332. * Similar to add_child(), but performs fewer checks. The purpose of this
  2333. * method is to add a child node that was newly constructed, to a parent node
  2334. * that was newly constructed, so we know we have to make fewer sanity checks.
  2335. * This is a private method; do not call it directly.
  2336. */
  2337. void PandaNode::
  2338. quick_add_new_child(PandaNode *child_node, int sort, Thread *current_thread) {
  2339. // Apply this operation to the current stage as well as to all upstream
  2340. // stages.
  2341. OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) {
  2342. CDStageWriter cdata(_cycler, pipeline_stage, current_thread);
  2343. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage, current_thread);
  2344. cdata->modify_down()->insert(DownConnection(child_node, sort));
  2345. cdata_child->modify_up()->insert(UpConnection(this));
  2346. }
  2347. CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler);
  2348. }
  2349. /**
  2350. * Raises an assertion when a graph cycle attempt is detected (and aborted).
  2351. */
  2352. void PandaNode::
  2353. report_cycle(PandaNode *child_node) {
  2354. ostringstream strm;
  2355. strm << "Detected attempt to create a cycle in the scene graph: "
  2356. << NodePath::any_path(this) << " : " << *child_node;
  2357. nassert_raise(strm.str());
  2358. }
  2359. /**
  2360. * Returns true if the indicated node is this node, or any ancestor of this
  2361. * node; or false if it is not in this node's ancestry.
  2362. */
  2363. bool PandaNode::
  2364. find_node_above(PandaNode *node) {
  2365. if (node == this) {
  2366. return true;
  2367. }
  2368. Parents parents = get_parents();
  2369. for (size_t i = 0; i < parents.get_num_parents(); ++i) {
  2370. PandaNode *parent = parents.get_parent(i);
  2371. if (parent->find_node_above(node)) {
  2372. return true;
  2373. }
  2374. }
  2375. return false;
  2376. }
  2377. /**
  2378. * Creates a new parent-child relationship, and returns the new
  2379. * NodePathComponent. If the child was already attached to the indicated
  2380. * parent, repositions it and returns the original NodePathComponent.
  2381. *
  2382. * This operation is automatically propagated back up to pipeline 0, from the
  2383. * specified pipeline stage.
  2384. */
  2385. PT(NodePathComponent) PandaNode::
  2386. attach(NodePathComponent *parent, PandaNode *child_node, int sort,
  2387. int pipeline_stage, Thread *current_thread) {
  2388. if (parent == nullptr) {
  2389. // Attaching to NULL means to create a new "instance" with no attachments,
  2390. // and no questions asked.
  2391. PT(NodePathComponent) child =
  2392. new NodePathComponent(child_node, nullptr,
  2393. pipeline_stage, current_thread);
  2394. LightReMutexHolder holder(child_node->_paths_lock);
  2395. child_node->_paths.insert(child);
  2396. return child;
  2397. }
  2398. // See if the child was already attached to the parent. If it was, we'll
  2399. // use that same NodePathComponent.
  2400. PT(NodePathComponent) child = get_component(parent, child_node, pipeline_stage, current_thread);
  2401. if (child == nullptr) {
  2402. // The child was not already attached to the parent, so get a new
  2403. // component.
  2404. child = get_top_component(child_node, true, pipeline_stage, current_thread);
  2405. }
  2406. reparent(parent, child, sort, false, pipeline_stage, current_thread);
  2407. return child;
  2408. }
  2409. /**
  2410. * Breaks a parent-child relationship.
  2411. *
  2412. * This operation is automatically propagated back up to pipeline 0, from the
  2413. * specified pipeline stage.
  2414. */
  2415. void PandaNode::
  2416. detach(NodePathComponent *child, int pipeline_stage, Thread *current_thread) {
  2417. nassertv(child != nullptr);
  2418. for (int pipeline_stage_i = pipeline_stage;
  2419. pipeline_stage_i >= 0;
  2420. --pipeline_stage_i) {
  2421. detach_one_stage(child, pipeline_stage_i, current_thread);
  2422. }
  2423. child->get_node()->parents_changed();
  2424. }
  2425. /**
  2426. * Breaks a parent-child relationship.
  2427. *
  2428. * This operation is not automatically propagated upstream. It is applied to
  2429. * the indicated pipeline stage only.
  2430. */
  2431. void PandaNode::
  2432. detach_one_stage(NodePathComponent *child, int pipeline_stage,
  2433. Thread *current_thread) {
  2434. nassertv(child != nullptr);
  2435. if (child->is_top_node(pipeline_stage, current_thread)) {
  2436. return;
  2437. }
  2438. PT(PandaNode) child_node = child->get_node();
  2439. PT(PandaNode) parent_node = child->get_next(pipeline_stage, current_thread)->get_node();
  2440. CDStageWriter cdata_parent(parent_node->_cycler, pipeline_stage, current_thread);
  2441. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage, current_thread);
  2442. int parent_index = child_node->do_find_parent(parent_node, cdata_child);
  2443. if (parent_index >= 0) {
  2444. // Now look for the child and break the actual connection.
  2445. // First, look for and remove the parent node from the child's up list.
  2446. int num_erased = cdata_child->modify_up()->erase(UpConnection(parent_node));
  2447. nassertv(num_erased == 1);
  2448. // Now, look for and remove the child node from the parent's down list.
  2449. // We also check in the stashed list, in case the child node has been
  2450. // stashed.
  2451. Down::iterator di;
  2452. bool found = false;
  2453. PT(Down) down = cdata_parent->modify_down();
  2454. for (di = down->begin(); di != down->end(); ++di) {
  2455. if ((*di).get_child() == child_node) {
  2456. down->erase(di);
  2457. found = true;
  2458. break;
  2459. }
  2460. }
  2461. if (!found) {
  2462. PT(Down) stashed = cdata_parent->modify_stashed();
  2463. for (di = stashed->begin(); di != stashed->end(); ++di) {
  2464. if ((*di).get_child() == child_node) {
  2465. stashed->erase(di);
  2466. found = true;
  2467. break;
  2468. }
  2469. }
  2470. }
  2471. nassertv(found);
  2472. }
  2473. // Finally, break the NodePathComponent connection.
  2474. sever_connection(parent_node, child_node, pipeline_stage, current_thread);
  2475. parent_node->force_bounds_stale(pipeline_stage, current_thread);
  2476. parent_node->children_changed();
  2477. parent_node->mark_bam_modified();
  2478. }
  2479. /**
  2480. * Switches a node from one parent to another. Returns true if the new
  2481. * connection is allowed, or false if it conflicts with another instance (that
  2482. * is, another instance of the child is already attached to the indicated
  2483. * parent).
  2484. *
  2485. * This operation is automatically propagated back up to pipeline 0, from the
  2486. * specified pipeline stage.
  2487. */
  2488. bool PandaNode::
  2489. reparent(NodePathComponent *new_parent, NodePathComponent *child, int sort,
  2490. bool as_stashed, int pipeline_stage, Thread *current_thread) {
  2491. bool any_ok = false;
  2492. if (new_parent != nullptr &&
  2493. !new_parent->get_node()->verify_child_no_cycles(child->get_node())) {
  2494. // Whoops, adding this child node would introduce a cycle in the scene
  2495. // graph.
  2496. return false;
  2497. }
  2498. for (int pipeline_stage_i = pipeline_stage;
  2499. pipeline_stage_i >= 0;
  2500. --pipeline_stage_i) {
  2501. if (reparent_one_stage(new_parent, child, sort, as_stashed,
  2502. pipeline_stage_i, current_thread)) {
  2503. any_ok = true;
  2504. }
  2505. }
  2506. if (new_parent != nullptr) {
  2507. new_parent->get_node()->children_changed();
  2508. new_parent->get_node()->mark_bam_modified();
  2509. }
  2510. child->get_node()->parents_changed();
  2511. child->get_node()->mark_bam_modified();
  2512. return any_ok;
  2513. }
  2514. /**
  2515. * Switches a node from one parent to another. Returns true if the new
  2516. * connection is allowed, or false if it conflicts with another instance (that
  2517. * is, another instance of the child is already attached to the indicated
  2518. * parent).
  2519. *
  2520. * This operation is not automatically propagated upstream. It is applied to
  2521. * the indicated pipeline stage only.
  2522. */
  2523. bool PandaNode::
  2524. reparent_one_stage(NodePathComponent *new_parent, NodePathComponent *child,
  2525. int sort, bool as_stashed, int pipeline_stage,
  2526. Thread *current_thread) {
  2527. nassertr(child != nullptr, false);
  2528. // Keep a reference count to the new parent, since detaching the child might
  2529. // lose the count.
  2530. PT(NodePathComponent) keep_parent = new_parent;
  2531. if (!child->is_top_node(pipeline_stage, current_thread)) {
  2532. detach(child, pipeline_stage, current_thread);
  2533. }
  2534. if (new_parent != nullptr) {
  2535. PandaNode *child_node = child->get_node();
  2536. PandaNode *parent_node = new_parent->get_node();
  2537. {
  2538. CDStageReader cdata_child(child_node->_cycler, pipeline_stage, current_thread);
  2539. int parent_index = child_node->do_find_parent(parent_node, cdata_child);
  2540. if (parent_index >= 0) {
  2541. // Whoops, there's already another instance of the child there.
  2542. return false;
  2543. }
  2544. }
  2545. // Redirect the connection to the indicated new parent.
  2546. child->set_next(new_parent, pipeline_stage, current_thread);
  2547. // Now reattach the child node at the indicated sort position.
  2548. {
  2549. CDStageWriter cdata_parent(parent_node->_cycler, pipeline_stage, current_thread);
  2550. CDStageWriter cdata_child(child_node->_cycler, pipeline_stage, current_thread);
  2551. if (as_stashed) {
  2552. cdata_parent->modify_stashed()->insert(DownConnection(child_node, sort));
  2553. } else {
  2554. cdata_parent->modify_down()->insert(DownConnection(child_node, sort));
  2555. }
  2556. cdata_child->modify_up()->insert(UpConnection(parent_node));
  2557. #ifndef NDEBUG
  2558. // The NodePathComponent should already be in the set.
  2559. {
  2560. LightReMutexHolder holder(child_node->_paths_lock);
  2561. nassertr(child_node->_paths.find(child) != child_node->_paths.end(), false);
  2562. }
  2563. #endif // NDEBUG
  2564. }
  2565. child_node->fix_path_lengths(pipeline_stage, current_thread);
  2566. parent_node->force_bounds_stale(pipeline_stage, current_thread);
  2567. }
  2568. return true;
  2569. }
  2570. /**
  2571. * Returns the NodePathComponent based on the indicated child of the given
  2572. * parent, or NULL if there is no such parent-child relationship.
  2573. */
  2574. PT(NodePathComponent) PandaNode::
  2575. get_component(NodePathComponent *parent, PandaNode *child_node,
  2576. int pipeline_stage, Thread *current_thread) {
  2577. nassertr(parent != nullptr, nullptr);
  2578. PandaNode *parent_node = parent->get_node();
  2579. LightReMutexHolder holder(child_node->_paths_lock);
  2580. // First, walk through the list of NodePathComponents we already have on the
  2581. // child, looking for one that already exists, referencing the indicated
  2582. // parent component.
  2583. Paths::const_iterator pi;
  2584. for (pi = child_node->_paths.begin(); pi != child_node->_paths.end(); ++pi) {
  2585. if ((*pi)->get_next(pipeline_stage, current_thread) == parent) {
  2586. // If we already have such a component, just return it.
  2587. return (*pi);
  2588. }
  2589. }
  2590. // We don't already have a NodePathComponent referring to this parent-child
  2591. // relationship. Are they actually related?
  2592. CDStageReader cdata_child(child_node->_cycler, pipeline_stage, current_thread);
  2593. int parent_index = child_node->do_find_parent(parent_node, cdata_child);
  2594. if (parent_index >= 0) {
  2595. // They are. Create and return a new one.
  2596. PT(NodePathComponent) child =
  2597. new NodePathComponent(child_node, parent, pipeline_stage, current_thread);
  2598. child_node->_paths.insert(child);
  2599. return child;
  2600. } else {
  2601. // They aren't related. Return NULL.
  2602. return nullptr;
  2603. }
  2604. }
  2605. /**
  2606. * Returns a NodePathComponent referencing the indicated node as a singleton.
  2607. * It is invalid to call this for a node that has parents, unless you are
  2608. * about to create a new instance (and immediately reconnect the
  2609. * NodePathComponent elsewhere).
  2610. *
  2611. * If force is true, this will always return something, even if it needs to
  2612. * create a new top component; otherwise, if force is false, it will return
  2613. * NULL if there is not already a top component available.
  2614. */
  2615. PT(NodePathComponent) PandaNode::
  2616. get_top_component(PandaNode *child_node, bool force, int pipeline_stage,
  2617. Thread *current_thread) {
  2618. LightReMutexHolder holder(child_node->_paths_lock);
  2619. // Walk through the list of NodePathComponents we already have on the child,
  2620. // looking for one that already exists as a top node.
  2621. Paths::const_iterator pi;
  2622. for (pi = child_node->_paths.begin(); pi != child_node->_paths.end(); ++pi) {
  2623. if ((*pi)->is_top_node(pipeline_stage, current_thread)) {
  2624. // If we already have such a component, just return it.
  2625. return (*pi);
  2626. }
  2627. }
  2628. if (!force) {
  2629. // If we don't care to force the point, return NULL to indicate there's
  2630. // not already a top component.
  2631. return nullptr;
  2632. }
  2633. // We don't already have such a NodePathComponent; create and return a new
  2634. // one.
  2635. PT(NodePathComponent) child =
  2636. new NodePathComponent(child_node, nullptr,
  2637. pipeline_stage, current_thread);
  2638. child_node->_paths.insert(child);
  2639. return child;
  2640. }
  2641. /**
  2642. * Returns a NodePathComponent referencing this node as a path from the root.
  2643. *
  2644. * Unless accept_ambiguity is true, it is only valid to call this if there is
  2645. * an unambiguous path from the root; otherwise, a warning will be issued and
  2646. * one path will be chosen arbitrarily.
  2647. */
  2648. PT(NodePathComponent) PandaNode::
  2649. get_generic_component(bool accept_ambiguity, int pipeline_stage,
  2650. Thread *current_thread) {
  2651. bool ambiguity_detected = false;
  2652. PT(NodePathComponent) result =
  2653. r_get_generic_component(accept_ambiguity, ambiguity_detected,
  2654. pipeline_stage, current_thread);
  2655. if (!accept_ambiguity && ambiguity_detected) {
  2656. pgraph_cat.warning()
  2657. << "Chose: " << *result << "\n";
  2658. nassertr(!unambiguous_graph, result);
  2659. }
  2660. return result;
  2661. }
  2662. /**
  2663. * The recursive implementation of get_generic_component, this simply sets the
  2664. * flag when the ambiguity is detected (so we can report the bottom node that
  2665. * started the ambiguous search).
  2666. */
  2667. PT(NodePathComponent) PandaNode::
  2668. r_get_generic_component(bool accept_ambiguity, bool &ambiguity_detected,
  2669. int pipeline_stage, Thread *current_thread) {
  2670. PT(PandaNode) parent_node;
  2671. {
  2672. CDStageReader cdata(_cycler, pipeline_stage, current_thread);
  2673. int num_parents = cdata->get_up()->size();
  2674. if (num_parents == 0) {
  2675. // No parents; no ambiguity. This is the root.
  2676. return get_top_component(this, true, pipeline_stage, current_thread);
  2677. }
  2678. PT(NodePathComponent) result;
  2679. if (num_parents == 1) {
  2680. // Only one parent; no ambiguity.
  2681. PT(NodePathComponent) parent =
  2682. get_parent(0)->r_get_generic_component(accept_ambiguity, ambiguity_detected, pipeline_stage, current_thread);
  2683. return get_component(parent, this, pipeline_stage, current_thread);
  2684. }
  2685. // Oops, multiple parents; the NodePath is ambiguous.
  2686. if (!accept_ambiguity) {
  2687. pgraph_cat.warning()
  2688. << *this << " has " << num_parents
  2689. << " parents; choosing arbitrary path to root.\n";
  2690. }
  2691. ambiguity_detected = true;
  2692. CPT(Up) up = cdata->get_up();
  2693. parent_node = (*up)[0].get_parent();
  2694. }
  2695. // Now that the lock is released, it's safe to recurse.
  2696. PT(NodePathComponent) parent =
  2697. parent_node->r_get_generic_component(accept_ambiguity, ambiguity_detected, pipeline_stage, current_thread);
  2698. return get_component(parent, this, pipeline_stage, current_thread);
  2699. }
  2700. /**
  2701. * Removes a NodePathComponent from the set prior to its deletion. This
  2702. * should only be called by the NodePathComponent destructor.
  2703. */
  2704. void PandaNode::
  2705. delete_component(NodePathComponent *component) {
  2706. LightReMutexHolder holder(_paths_lock);
  2707. int num_erased = _paths.erase(component);
  2708. nassertv(num_erased == 1);
  2709. }
  2710. /**
  2711. * This is called internally when a parent-child connection is broken to
  2712. * update the NodePathComponents that reflected this connection.
  2713. *
  2714. * It severs any NodePathComponents on the child node that reference the
  2715. * indicated parent node. These components remain unattached; there may
  2716. * therefore be multiple "instances" of a node that all have no parent, even
  2717. * while there are other instances that do have parents.
  2718. *
  2719. * This operation is not automatically propagated upstream. It is applied to
  2720. * the indicated pipeline stage only.
  2721. */
  2722. void PandaNode::
  2723. sever_connection(PandaNode *parent_node, PandaNode *child_node,
  2724. int pipeline_stage, Thread *current_thread) {
  2725. {
  2726. LightReMutexHolder holder(child_node->_paths_lock);
  2727. Paths::iterator pi;
  2728. for (pi = child_node->_paths.begin(); pi != child_node->_paths.end(); ++pi) {
  2729. if (!(*pi)->is_top_node(pipeline_stage, current_thread) &&
  2730. (*pi)->get_next(pipeline_stage, current_thread)->get_node() == parent_node) {
  2731. // Sever the component here.
  2732. (*pi)->set_top_node(pipeline_stage, current_thread);
  2733. }
  2734. }
  2735. }
  2736. child_node->fix_path_lengths(pipeline_stage, current_thread);
  2737. }
  2738. /**
  2739. * This is called internally when a parent-child connection is established to
  2740. * update the NodePathComponents that might be involved.
  2741. *
  2742. * It adjusts any NodePathComponents the child has that reference the child as
  2743. * a top node. Any other components we can leave alone, because we are making
  2744. * a new instance of the child.
  2745. *
  2746. * This operation is not automatically propagated upstream. It is applied to
  2747. * the indicated pipeline stage only.
  2748. */
  2749. void PandaNode::
  2750. new_connection(PandaNode *parent_node, PandaNode *child_node,
  2751. int pipeline_stage, Thread *current_thread) {
  2752. {
  2753. LightReMutexHolder holder(child_node->_paths_lock);
  2754. Paths::iterator pi;
  2755. for (pi = child_node->_paths.begin(); pi != child_node->_paths.end(); ++pi) {
  2756. if ((*pi)->is_top_node(pipeline_stage, current_thread)) {
  2757. (*pi)->set_next(parent_node->get_generic_component(false, pipeline_stage, current_thread), pipeline_stage, current_thread);
  2758. }
  2759. }
  2760. }
  2761. child_node->fix_path_lengths(pipeline_stage, current_thread);
  2762. }
  2763. /**
  2764. * Recursively fixes the _length member of each NodePathComponent at this
  2765. * level and below, after an add or delete child operation that might have
  2766. * messed these up.
  2767. *
  2768. * This operation is not automatically propagated upstream. It is applied to
  2769. * the indicated pipeline stage only.
  2770. */
  2771. void PandaNode::
  2772. fix_path_lengths(int pipeline_stage, Thread *current_thread) {
  2773. LightReMutexHolder holder(_paths_lock);
  2774. bool any_wrong = false;
  2775. Paths::const_iterator pi;
  2776. for (pi = _paths.begin(); pi != _paths.end(); ++pi) {
  2777. if ((*pi)->fix_length(pipeline_stage, current_thread)) {
  2778. any_wrong = true;
  2779. }
  2780. }
  2781. // If any paths were updated, we have to recurse on all of our children,
  2782. // since any one of those paths might be shared by any of our child nodes.
  2783. // Don't hold any locks while we recurse.
  2784. if (any_wrong) {
  2785. Children children;
  2786. Stashed stashed;
  2787. {
  2788. CDStageReader cdata(_cycler, pipeline_stage, current_thread);
  2789. children = Children(cdata);
  2790. stashed = Stashed(cdata);
  2791. }
  2792. int num_children = children.get_num_children();
  2793. int i;
  2794. for (i = 0; i < num_children; ++i) {
  2795. PandaNode *child_node = children.get_child(i);
  2796. child_node->fix_path_lengths(pipeline_stage, current_thread);
  2797. }
  2798. int num_stashed = stashed.get_num_stashed();
  2799. for (i = 0; i < num_stashed; ++i) {
  2800. PandaNode *child_node = stashed.get_stashed(i);
  2801. child_node->fix_path_lengths(pipeline_stage, current_thread);
  2802. }
  2803. }
  2804. }
  2805. /**
  2806. * The recursive implementation of ls().
  2807. */
  2808. void PandaNode::
  2809. r_list_descendants(ostream &out, int indent_level) const {
  2810. write(out, indent_level);
  2811. Children children = get_children();
  2812. int num_children = children.get_num_children();
  2813. for (int i = 0; i < num_children; ++i) {
  2814. PandaNode *child = children.get_child(i);
  2815. child->r_list_descendants(out, indent_level + 2);
  2816. }
  2817. // Also report the number of stashed nodes at this level.
  2818. int num_stashed = get_num_stashed();
  2819. if (num_stashed != 0) {
  2820. indent(out, indent_level) << "(" << num_stashed << " stashed)\n";
  2821. }
  2822. }
  2823. /**
  2824. * The private implementation of find_child().
  2825. */
  2826. int PandaNode::
  2827. do_find_child(PandaNode *node, const PandaNode::Down *down) const {
  2828. nassertr(node != nullptr, -1);
  2829. // We have to search for the child by brute force, since we don't know what
  2830. // sort index it was added as.
  2831. Down::const_iterator di;
  2832. for (di = down->begin(); di != down->end(); ++di) {
  2833. if ((*di).get_child() == node) {
  2834. return di - down->begin();
  2835. }
  2836. }
  2837. return -1;
  2838. }
  2839. /**
  2840. * Updates the cached values of the node that are dependent on its children,
  2841. * such as the external bounding volume, the _net_collide_mask, and the
  2842. * _off_clip_planes.
  2843. *
  2844. * If update_bounds is false, it will not update the bounding volume or vertex
  2845. * count.
  2846. *
  2847. * The old value should be passed in; it will be released. The new value is
  2848. * returned.
  2849. */
  2850. PandaNode::CDStageWriter PandaNode::
  2851. update_cached(bool update_bounds, int pipeline_stage, PandaNode::CDLockedStageReader &cdata) {
  2852. // We might need to try this a couple of times, in case someone else steps
  2853. // on our result.
  2854. if (drawmask_cat.is_debug()) {
  2855. drawmask_cat.debug(false)
  2856. << *this << "::update_cached() {\n";
  2857. }
  2858. Thread *current_thread = cdata.get_current_thread();
  2859. do {
  2860. // Grab the last_update counter.
  2861. UpdateSeq last_update = cdata->_last_update;
  2862. UpdateSeq next_update = cdata->_next_update;
  2863. UpdateSeq last_bounds_update = cdata->_last_bounds_update;
  2864. nassertr(last_update != next_update ||
  2865. (update_bounds && last_bounds_update != next_update),
  2866. CDStageWriter(_cycler, pipeline_stage, cdata));
  2867. // Start with a clean slate.
  2868. CollideMask net_collide_mask = cdata->_into_collide_mask;
  2869. DrawMask net_draw_control_mask, net_draw_show_mask;
  2870. bool renderable = is_renderable();
  2871. if (renderable) {
  2872. // If this node is itself renderable, it contributes to the net draw
  2873. // mask.
  2874. net_draw_control_mask = cdata->_draw_control_mask;
  2875. net_draw_show_mask = cdata->_draw_show_mask;
  2876. }
  2877. if (drawmask_cat.is_debug()) {
  2878. drawmask_cat.debug(false)
  2879. << "net_draw_control_mask = " << net_draw_control_mask
  2880. << "\nnet_draw_show_mask = " << net_draw_show_mask
  2881. << "\n";
  2882. }
  2883. CPT(RenderAttrib) off_clip_planes = cdata->_state->get_attrib(ClipPlaneAttrib::get_class_slot());
  2884. if (off_clip_planes == nullptr) {
  2885. off_clip_planes = ClipPlaneAttrib::make();
  2886. }
  2887. // Also get the list of the node's children.
  2888. Children children(cdata);
  2889. int num_vertices = cdata->_internal_vertices;
  2890. // Now that we've got all the data we need from the node, we can release
  2891. // the lock.
  2892. _cycler.release_read_stage(pipeline_stage, cdata.take_pointer());
  2893. int num_children = children.get_num_children();
  2894. // We need to keep references to the bounding volumes, since in a threaded
  2895. // environment the pointers might go away while we're working (since we're
  2896. // not holding a lock on our set of children right now). But we also need
  2897. // the regular pointers, to pass to BoundingVolume::around().
  2898. const BoundingVolume **child_volumes;
  2899. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  2900. pvector<CPT(BoundingVolume) > child_volumes_ref;
  2901. if (update_bounds) {
  2902. child_volumes_ref.reserve(num_children + 1);
  2903. }
  2904. #endif
  2905. int child_volumes_i = 0;
  2906. bool all_box = true;
  2907. CPT(BoundingVolume) internal_bounds = nullptr;
  2908. if (update_bounds) {
  2909. child_volumes = (const BoundingVolume **)alloca(sizeof(BoundingVolume *) * (num_children + 1));
  2910. internal_bounds = get_internal_bounds(pipeline_stage, current_thread);
  2911. if (!internal_bounds->is_empty()) {
  2912. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  2913. child_volumes_ref.push_back(internal_bounds);
  2914. #endif
  2915. nassertr(child_volumes_i < num_children + 1, CDStageWriter(_cycler, pipeline_stage, cdata));
  2916. child_volumes[child_volumes_i++] = internal_bounds;
  2917. if (internal_bounds->as_bounding_box() == nullptr) {
  2918. all_box = false;
  2919. }
  2920. }
  2921. }
  2922. // Now expand those contents to include all of our children.
  2923. for (int i = 0; i < num_children; ++i) {
  2924. PandaNode *child = children.get_child(i);
  2925. const ClipPlaneAttrib *orig_cp = DCAST(ClipPlaneAttrib, off_clip_planes);
  2926. CDLockedStageReader child_cdata(child->_cycler, pipeline_stage, current_thread);
  2927. UpdateSeq last_child_update = update_bounds
  2928. ? child_cdata->_last_bounds_update
  2929. : child_cdata->_last_update;
  2930. if (last_child_update != child_cdata->_next_update) {
  2931. // Child needs update.
  2932. CDStageWriter child_cdataw = child->update_cached(update_bounds, pipeline_stage, child_cdata);
  2933. net_collide_mask |= child_cdataw->_net_collide_mask;
  2934. if (drawmask_cat.is_debug()) {
  2935. drawmask_cat.debug(false)
  2936. << "\nchild update " << *child << ":\n";
  2937. }
  2938. DrawMask child_control_mask = child_cdataw->_net_draw_control_mask;
  2939. DrawMask child_show_mask = child_cdataw->_net_draw_show_mask;
  2940. if (!(child_control_mask | child_show_mask).is_zero()) {
  2941. // This child includes a renderable node or subtree. Thus, we
  2942. // should propagate its draw masks.
  2943. renderable = true;
  2944. // For each bit position in the masks, we have assigned the
  2945. // following semantic meaning. The number on the left represents
  2946. // the pairing of the corresponding bit from the control mask and
  2947. // from the show mask:
  2948. // 00 : not a renderable node (control 0, show 0) 01 : a normally
  2949. // visible node (control 0, show 1) 10 : a hidden node
  2950. // (control 1, show 0) 11 : a show-through node (control 1, show
  2951. // 1)
  2952. // Now, when we accumulate these masks, we want to do so according
  2953. // to the following table, for each bit position:
  2954. // 00 01 10 11 (child) --------------------- 00 | 00 01
  2955. // 10 11 01 | 01 01 01* 11 10 | 10 01* 10 11 11 | 11
  2956. // 11 11 11 (parent)
  2957. // This table is almost the same as the union of both masks, with
  2958. // one exception, marked with a * in the above table: if one is 10
  2959. // and the other is 01--that is, one is hidden and the other is
  2960. // normally visible--then the result should be 01, normally visible.
  2961. // This is because we only want to propagate the hidden bit upwards
  2962. // if *all* renderable nodes are hidden.
  2963. // Get the set of exception bits for which the above rule applies.
  2964. // These are the bits for which both bits have flipped, but which
  2965. // were not the same in the original.
  2966. DrawMask exception_mask = (net_draw_control_mask ^ child_control_mask) & (net_draw_show_mask ^ child_show_mask);
  2967. exception_mask &= (net_draw_control_mask ^ net_draw_show_mask);
  2968. if (drawmask_cat.is_debug()) {
  2969. drawmask_cat.debug(false)
  2970. << "exception_mask = " << exception_mask << "\n";
  2971. }
  2972. // Now compute the union, applying the above exception.
  2973. net_draw_control_mask |= child_control_mask;
  2974. net_draw_show_mask |= child_show_mask;
  2975. net_draw_control_mask &= ~exception_mask;
  2976. net_draw_show_mask |= exception_mask;
  2977. }
  2978. if (drawmask_cat.is_debug()) {
  2979. drawmask_cat.debug(false)
  2980. << "child_control_mask = " << child_control_mask
  2981. << "\nchild_show_mask = " << child_show_mask
  2982. << "\nnet_draw_control_mask = " << net_draw_control_mask
  2983. << "\nnet_draw_show_mask = " << net_draw_show_mask
  2984. << "\n";
  2985. }
  2986. off_clip_planes = orig_cp->compose_off(child_cdataw->_off_clip_planes);
  2987. if (update_bounds) {
  2988. if (!child_cdataw->_external_bounds->is_empty()) {
  2989. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  2990. child_volumes_ref.push_back(child_cdataw->_external_bounds);
  2991. #endif
  2992. nassertr(child_volumes_i < num_children + 1, CDStageWriter(_cycler, pipeline_stage, cdata));
  2993. child_volumes[child_volumes_i++] = child_cdataw->_external_bounds;
  2994. if (child_cdataw->_external_bounds->as_bounding_box() == nullptr) {
  2995. all_box = false;
  2996. }
  2997. }
  2998. num_vertices += child_cdataw->_nested_vertices;
  2999. }
  3000. } else {
  3001. // Child is good.
  3002. net_collide_mask |= child_cdata->_net_collide_mask;
  3003. // See comments in similar block above.
  3004. if (drawmask_cat.is_debug()) {
  3005. drawmask_cat.debug(false)
  3006. << "\nchild fresh " << *child << ":\n";
  3007. }
  3008. DrawMask child_control_mask = child_cdata->_net_draw_control_mask;
  3009. DrawMask child_show_mask = child_cdata->_net_draw_show_mask;
  3010. if (!(child_control_mask | child_show_mask).is_zero()) {
  3011. renderable = true;
  3012. DrawMask exception_mask = (net_draw_control_mask ^ child_control_mask) & (net_draw_show_mask ^ child_show_mask);
  3013. exception_mask &= (net_draw_control_mask ^ net_draw_show_mask);
  3014. if (drawmask_cat.is_debug()) {
  3015. drawmask_cat.debug(false)
  3016. << "exception_mask = " << exception_mask << "\n";
  3017. }
  3018. // Now compute the union, applying the above exception.
  3019. net_draw_control_mask |= child_control_mask;
  3020. net_draw_show_mask |= child_show_mask;
  3021. net_draw_control_mask &= ~exception_mask;
  3022. net_draw_show_mask |= exception_mask;
  3023. }
  3024. if (drawmask_cat.is_debug()) {
  3025. drawmask_cat.debug(false)
  3026. << "child_control_mask = " << child_control_mask
  3027. << "\nchild_show_mask = " << child_show_mask
  3028. << "\nnet_draw_control_mask = " << net_draw_control_mask
  3029. << "\nnet_draw_show_mask = " << net_draw_show_mask
  3030. << "\n";
  3031. }
  3032. off_clip_planes = orig_cp->compose_off(child_cdata->_off_clip_planes);
  3033. if (update_bounds) {
  3034. if (!child_cdata->_external_bounds->is_empty()) {
  3035. #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
  3036. child_volumes_ref.push_back(child_cdata->_external_bounds);
  3037. #endif
  3038. nassertr(child_volumes_i < num_children + 1, CDStageWriter(_cycler, pipeline_stage, cdata));
  3039. child_volumes[child_volumes_i++] = child_cdata->_external_bounds;
  3040. if (child_cdata->_external_bounds->as_bounding_box() == nullptr) {
  3041. all_box = false;
  3042. }
  3043. }
  3044. num_vertices += child_cdata->_nested_vertices;
  3045. }
  3046. }
  3047. }
  3048. {
  3049. // Now grab the write lock on this node.
  3050. CDStageWriter cdataw(_cycler, pipeline_stage, current_thread);
  3051. if (last_update == cdataw->_last_update &&
  3052. next_update == cdataw->_next_update) {
  3053. // Great, no one has monkeyed with these while we were computing the
  3054. // cache. Safe to store the computed values and return.
  3055. cdataw->_net_collide_mask = net_collide_mask;
  3056. if (renderable) {
  3057. // Any explicit draw control mask on this node trumps anything
  3058. // inherited from below, except a show-through.
  3059. DrawMask draw_control_mask = cdataw->_draw_control_mask;
  3060. DrawMask draw_show_mask = cdataw->_draw_show_mask;
  3061. DrawMask show_through_mask = net_draw_control_mask & net_draw_show_mask;
  3062. net_draw_control_mask |= draw_control_mask;
  3063. net_draw_show_mask = (net_draw_show_mask & ~draw_control_mask) | (draw_show_mask & draw_control_mask);
  3064. net_draw_show_mask |= show_through_mask;
  3065. // There are renderable nodes below, so the implicit draw bits are
  3066. // all on.
  3067. cdataw->_net_draw_control_mask = net_draw_control_mask;
  3068. cdataw->_net_draw_show_mask = net_draw_show_mask | ~net_draw_control_mask;
  3069. if (drawmask_cat.is_debug()) {
  3070. drawmask_cat.debug(false)
  3071. << "renderable, set mask " << cdataw->_net_draw_show_mask << "\n";
  3072. }
  3073. } else {
  3074. // There are no renderable nodes below, so the implicit draw bits
  3075. // are all off. Also, we don't care about the draw mask on this
  3076. // particular node (since nothing below it is renderable anyway).
  3077. cdataw->_net_draw_control_mask = net_draw_control_mask;
  3078. cdataw->_net_draw_show_mask = net_draw_show_mask;
  3079. if (drawmask_cat.is_debug()) {
  3080. drawmask_cat.debug(false)
  3081. << "not renderable, set mask " << cdataw->_net_draw_show_mask << "\n";
  3082. }
  3083. }
  3084. cdataw->_off_clip_planes = off_clip_planes;
  3085. if (update_bounds) {
  3086. cdataw->_nested_vertices = num_vertices;
  3087. CPT(TransformState) transform = get_transform(current_thread);
  3088. PT(GeometricBoundingVolume) gbv;
  3089. BoundingVolume::BoundsType btype = cdataw->_bounds_type;
  3090. if (btype == BoundingVolume::BT_default) {
  3091. btype = bounds_type;
  3092. }
  3093. if (btype == BoundingVolume::BT_box ||
  3094. (btype != BoundingVolume::BT_sphere && all_box && transform->is_identity())) {
  3095. // If all of the child volumes are a BoundingBox, and we have no
  3096. // transform, then our volume is also a BoundingBox.
  3097. gbv = new BoundingBox;
  3098. } else {
  3099. // Otherwise, it's a sphere.
  3100. gbv = new BoundingSphere;
  3101. }
  3102. if (child_volumes_i > 0) {
  3103. const BoundingVolume **child_begin = &child_volumes[0];
  3104. const BoundingVolume **child_end = child_begin + child_volumes_i;
  3105. ((BoundingVolume *)gbv)->around(child_begin, child_end);
  3106. // If we have a transform, apply it to the bounding volume we just
  3107. // computed.
  3108. if (!transform->is_identity()) {
  3109. gbv->xform(transform->get_mat());
  3110. }
  3111. }
  3112. cdataw->_external_bounds = gbv;
  3113. cdataw->_last_bounds_update = next_update;
  3114. }
  3115. cdataw->_last_update = next_update;
  3116. if (drawmask_cat.is_debug()) {
  3117. drawmask_cat.debug(false)
  3118. << "} " << *this << "::update_cached();\n";
  3119. }
  3120. nassertr(cdataw->_last_update == cdataw->_next_update, cdataw);
  3121. // Even though implicit bounding volume is not (yet?) part of the bam
  3122. // stream.
  3123. mark_bam_modified();
  3124. return cdataw;
  3125. }
  3126. if (cdataw->_last_update == cdataw->_next_update &&
  3127. (!update_bounds || cdataw->_last_bounds_update == cdataw->_next_update)) {
  3128. // Someone else has computed the cache for us. OK.
  3129. return cdataw;
  3130. }
  3131. }
  3132. // We need to go around again. Release the write lock, and grab the read
  3133. // lock back.
  3134. cdata = CDLockedStageReader(_cycler, pipeline_stage, current_thread);
  3135. if (cdata->_last_update == cdata->_next_update &&
  3136. (!update_bounds || cdata->_last_bounds_update == cdata->_next_update)) {
  3137. // Someone else has computed the cache for us while we were diddling
  3138. // with the locks. OK.
  3139. return CDStageWriter(_cycler, pipeline_stage, cdata);
  3140. }
  3141. } while (true);
  3142. }
  3143. /**
  3144. * This is used by the GraphicsEngine to hook in a pointer to the
  3145. * scene_root_func(), the function to determine whether the node is an active
  3146. * scene root. This back-pointer is necessary because we can't make calls
  3147. * directly into GraphicsEngine, which is in the display module.
  3148. */
  3149. void PandaNode::
  3150. set_scene_root_func(SceneRootFunc *func) {
  3151. _scene_root_func = func;
  3152. }
  3153. /**
  3154. * Tells the BamReader how to create objects of type PandaNode.
  3155. */
  3156. void PandaNode::
  3157. register_with_read_factory() {
  3158. BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
  3159. }
  3160. /**
  3161. * Writes the contents of this object to the datagram for shipping out to a
  3162. * Bam file.
  3163. */
  3164. void PandaNode::
  3165. write_datagram(BamWriter *manager, Datagram &dg) {
  3166. TypedWritable::write_datagram(manager, dg);
  3167. dg.add_string(get_name());
  3168. manager->write_cdata(dg, _cycler);
  3169. }
  3170. /**
  3171. * Called by the BamWriter when this object has not itself been modified
  3172. * recently, but it should check its nested objects for updates.
  3173. */
  3174. void PandaNode::
  3175. update_bam_nested(BamWriter *manager) {
  3176. CDReader cdata(_cycler);
  3177. cdata->update_bam_nested(manager);
  3178. }
  3179. /**
  3180. * This method is provided for the benefit of classes (like MouseRecorder)
  3181. * that inherit from PandaMode and also RecorderBase. It's not virtual at
  3182. * this level since it doesn't need to be (it's called up from the derived
  3183. * class).
  3184. *
  3185. * This method acts very like write_datagram, but it writes the node as
  3186. * appropriate for writing a RecorderBase object as described in the beginning
  3187. * of a session file, meaning it doesn't need to write things such as
  3188. * children. It balances with fillin_recorder().
  3189. */
  3190. void PandaNode::
  3191. write_recorder(BamWriter *, Datagram &dg) {
  3192. dg.add_string(get_name());
  3193. }
  3194. /**
  3195. * This function is called by the BamReader's factory when a new object of
  3196. * type PandaNode is encountered in the Bam file. It should create the
  3197. * PandaNode and extract its information from the file.
  3198. */
  3199. TypedWritable *PandaNode::
  3200. make_from_bam(const FactoryParams &params) {
  3201. PandaNode *node = new PandaNode("");
  3202. DatagramIterator scan;
  3203. BamReader *manager;
  3204. parse_params(params, scan, manager);
  3205. node->fillin(scan, manager);
  3206. return node;
  3207. }
  3208. /**
  3209. * This internal function is called by make_from_bam to read in all of the
  3210. * relevant data from the BamFile for the new PandaNode.
  3211. */
  3212. void PandaNode::
  3213. fillin(DatagramIterator &scan, BamReader *manager) {
  3214. TypedWritable::fillin(scan, manager);
  3215. remove_all_children();
  3216. string name = scan.get_string();
  3217. set_name(name);
  3218. manager->read_cdata(scan, _cycler);
  3219. }
  3220. /**
  3221. * This internal function is called by make_recorder (in classes derived from
  3222. * RecorderBase, such as MouseRecorder) to read in all of the relevant data
  3223. * from the session file. It balances with write_recorder().
  3224. */
  3225. void PandaNode::
  3226. fillin_recorder(DatagramIterator &scan, BamReader *) {
  3227. string name = scan.get_string();
  3228. set_name(name);
  3229. }
  3230. /**
  3231. *
  3232. */
  3233. PandaNode::CData::
  3234. CData() :
  3235. _state(RenderState::make_empty()),
  3236. _transform(TransformState::make_identity()),
  3237. _prev_transform(TransformState::make_identity()),
  3238. _effects(RenderEffects::make_empty()),
  3239. _draw_control_mask(DrawMask::all_off()),
  3240. _draw_show_mask(DrawMask::all_on()),
  3241. _into_collide_mask(CollideMask::all_off()),
  3242. _bounds_type(BoundingVolume::BT_default),
  3243. _user_bounds(nullptr),
  3244. _final_bounds(false),
  3245. _fancy_bits(0),
  3246. _net_collide_mask(CollideMask::all_off()),
  3247. _net_draw_control_mask(DrawMask::all_off()),
  3248. _net_draw_show_mask(DrawMask::all_off()),
  3249. _down(new PandaNode::Down(PandaNode::get_class_type())),
  3250. _stashed(new PandaNode::Down(PandaNode::get_class_type())),
  3251. _up(new PandaNode::Up(PandaNode::get_class_type()))
  3252. {
  3253. ++_next_update;
  3254. }
  3255. /**
  3256. *
  3257. */
  3258. PandaNode::CData::
  3259. CData(const PandaNode::CData &copy) :
  3260. BoundsData(copy),
  3261. _state(copy._state),
  3262. _transform(copy._transform),
  3263. _prev_transform(copy._prev_transform),
  3264. _effects(copy._effects),
  3265. _tag_data(copy._tag_data),
  3266. _draw_control_mask(copy._draw_control_mask),
  3267. _draw_show_mask(copy._draw_show_mask),
  3268. _into_collide_mask(copy._into_collide_mask),
  3269. _bounds_type(copy._bounds_type),
  3270. _user_bounds(copy._user_bounds),
  3271. _final_bounds(copy._final_bounds),
  3272. _fancy_bits(copy._fancy_bits),
  3273. _net_collide_mask(copy._net_collide_mask),
  3274. _net_draw_control_mask(copy._net_draw_control_mask),
  3275. _net_draw_show_mask(copy._net_draw_show_mask),
  3276. _off_clip_planes(copy._off_clip_planes),
  3277. _nested_vertices(copy._nested_vertices),
  3278. _external_bounds(copy._external_bounds),
  3279. _last_update(copy._last_update),
  3280. _next_update(copy._next_update),
  3281. _last_bounds_update(copy._last_bounds_update),
  3282. _down(copy._down),
  3283. _stashed(copy._stashed),
  3284. _up(copy._up)
  3285. {
  3286. // Note that this copy constructor is not used by the PandaNode copy
  3287. // constructor! Any elements that must be copied between nodes should also
  3288. // be explicitly copied there.
  3289. }
  3290. /**
  3291. *
  3292. */
  3293. PandaNode::CData::
  3294. ~CData() {
  3295. }
  3296. /**
  3297. *
  3298. */
  3299. CycleData *PandaNode::CData::
  3300. make_copy() const {
  3301. return new CData(*this);
  3302. }
  3303. /**
  3304. * Writes the contents of this object to the datagram for shipping out to a
  3305. * Bam file.
  3306. */
  3307. void PandaNode::CData::
  3308. write_datagram(BamWriter *manager, Datagram &dg) const {
  3309. manager->write_pointer(dg, _state);
  3310. manager->write_pointer(dg, _transform);
  3311. manager->write_pointer(dg, _effects);
  3312. dg.add_uint32(_draw_control_mask.get_word());
  3313. dg.add_uint32(_draw_show_mask.get_word());
  3314. dg.add_uint32(_into_collide_mask.get_word());
  3315. dg.add_uint8(_bounds_type);
  3316. dg.add_uint32(_tag_data.size());
  3317. for (size_t n = 0; n < _tag_data.size(); ++n) {
  3318. dg.add_string(_tag_data.get_key(n));
  3319. dg.add_string(_tag_data.get_data(n));
  3320. }
  3321. write_up_list(*get_up(), manager, dg);
  3322. write_down_list(*get_down(), manager, dg);
  3323. write_down_list(*get_stashed(), manager, dg);
  3324. }
  3325. /**
  3326. * Called by the BamWriter when this object has not itself been modified
  3327. * recently, but it should check its nested objects for updates.
  3328. */
  3329. void PandaNode::CData::
  3330. update_bam_nested(BamWriter *manager) const {
  3331. // No need to check the state pointers for updates, since they're all
  3332. // immutable objects. manager->consider_update(_state);
  3333. // manager->consider_update(_transform); manager->consider_update(_effects);
  3334. update_up_list(*get_up(), manager);
  3335. update_down_list(*get_down(), manager);
  3336. update_down_list(*get_stashed(), manager);
  3337. }
  3338. /**
  3339. * Receives an array of pointers, one for each time manager->read_pointer()
  3340. * was called in fillin(). Returns the number of pointers processed.
  3341. */
  3342. int PandaNode::CData::
  3343. complete_pointers(TypedWritable **p_list, BamReader *manager) {
  3344. int pi = CycleData::complete_pointers(p_list, manager);
  3345. // Get the state and transform pointers.
  3346. RenderState *state;
  3347. DCAST_INTO_R(state, p_list[pi++], pi);
  3348. _state = state;
  3349. TransformState *transform;
  3350. DCAST_INTO_R(transform, p_list[pi++], pi);
  3351. _prev_transform = _transform = transform;
  3352. /*
  3353. * Finalize these pointers now to decrement their artificially-held reference
  3354. * counts. We do this now, rather than later, in case some other object
  3355. * reassigns them a little later on during initialization, before they can
  3356. * finalize themselves normally (for instance, the character may change the
  3357. * node's transform). If that happens, the pointer may discover that no one
  3358. * else holds its reference count when it finalizes, which will constitute a
  3359. * memory leak (see the comments in TransformState::finalize(), etc.).
  3360. */
  3361. manager->finalize_now((RenderState *)_state.p());
  3362. manager->finalize_now((TransformState *)_transform.p());
  3363. // Get the effects pointer.
  3364. RenderEffects *effects;
  3365. DCAST_INTO_R(effects, p_list[pi++], pi);
  3366. _effects = effects;
  3367. /*
  3368. * Finalize these pointers now to decrement their artificially-held reference
  3369. * counts. We do this now, rather than later, in case some other object
  3370. * reassigns them a little later on during initialization, before they can
  3371. * finalize themselves normally (for instance, the character may change the
  3372. * node's transform). If that happens, the pointer may discover that no one
  3373. * else holds its reference count when it finalizes, which will constitute a
  3374. * memory leak (see the comments in TransformState::finalize(), etc.).
  3375. */
  3376. manager->finalize_now((RenderEffects *)_effects.p());
  3377. // Get the parent and child pointers.
  3378. pi += complete_up_list(*modify_up(), "up", p_list + pi, manager);
  3379. pi += complete_down_list(*modify_down(), "down", p_list + pi, manager);
  3380. pi += complete_down_list(*modify_stashed(), "stashed", p_list + pi, manager);
  3381. // Since the _effects and _states members have been finalized by now, this
  3382. // should be safe.
  3383. set_fancy_bit(FB_transform, !_transform->is_identity());
  3384. set_fancy_bit(FB_state, !_state->is_empty());
  3385. set_fancy_bit(FB_effects, !_effects->is_empty());
  3386. set_fancy_bit(FB_tag, !_tag_data.is_empty());
  3387. // Mark the bounds stale.
  3388. ++_next_update;
  3389. nassertr(!_transform->is_invalid(), pi);
  3390. nassertr(!_prev_transform->is_invalid(), pi);
  3391. return pi;
  3392. }
  3393. /**
  3394. * This internal function is called by make_from_bam to read in all of the
  3395. * relevant data from the BamFile for the new PandaNode.
  3396. */
  3397. void PandaNode::CData::
  3398. fillin(DatagramIterator &scan, BamReader *manager) {
  3399. // Read the state and transform pointers.
  3400. manager->read_pointer(scan);
  3401. manager->read_pointer(scan);
  3402. // Read the effects pointer.
  3403. manager->read_pointer(scan);
  3404. if (manager->get_file_minor_ver() < 2) {
  3405. DrawMask draw_mask;
  3406. draw_mask.set_word(scan.get_uint32());
  3407. if (draw_mask == DrawMask::all_off()) {
  3408. // Hidden.
  3409. _draw_control_mask = _overall_bit;
  3410. _draw_show_mask = ~_overall_bit;
  3411. } else if (draw_mask == DrawMask::all_on()) {
  3412. // Normally visible.
  3413. _draw_control_mask = DrawMask::all_off();
  3414. _draw_show_mask = DrawMask::all_on();
  3415. } else {
  3416. // Some per-camera combination.
  3417. draw_mask &= ~_overall_bit;
  3418. _draw_control_mask = ~draw_mask;
  3419. _draw_show_mask = draw_mask;
  3420. }
  3421. } else {
  3422. _draw_control_mask.set_word(scan.get_uint32());
  3423. _draw_show_mask.set_word(scan.get_uint32());
  3424. }
  3425. _into_collide_mask.set_word(scan.get_uint32());
  3426. _bounds_type = BoundingVolume::BT_default;
  3427. if (manager->get_file_minor_ver() >= 19) {
  3428. _bounds_type = (BoundingVolume::BoundsType)scan.get_uint8();
  3429. }
  3430. // Read in the tag list.
  3431. int num_tags = scan.get_uint32();
  3432. for (int i = 0; i < num_tags; i++) {
  3433. string key = scan.get_string();
  3434. string value = scan.get_string();
  3435. _tag_data.store(key, value);
  3436. }
  3437. fillin_up_list(*modify_up(), "up", scan, manager);
  3438. fillin_down_list(*modify_down(), "down", scan, manager);
  3439. fillin_down_list(*modify_stashed(), "stashed", scan, manager);
  3440. }
  3441. /**
  3442. * Writes the indicated list of parent node pointers to the datagram.
  3443. */
  3444. void PandaNode::CData::
  3445. write_up_list(const PandaNode::Up &up_list,
  3446. BamWriter *manager, Datagram &dg) const {
  3447. /*
  3448. * When we write a PandaNode, we write out its complete list of child node
  3449. * pointers, but we only write out the parent node pointers that have already
  3450. * been added to the bam file by a previous write operation. This is a bit of
  3451. * trickery that allows us to write out just a subgraph (instead of the
  3452. * complete graph) when we write out an arbitrary node in the graph, yet also
  3453. * allows us to keep nodes completely in sync when we use the bam format for
  3454. * streaming scene graph operations over the network.
  3455. */
  3456. int num_parents = 0;
  3457. Up::const_iterator ui;
  3458. for (ui = up_list.begin(); ui != up_list.end(); ++ui) {
  3459. PandaNode *parent_node = (*ui).get_parent();
  3460. if (manager->has_object(parent_node)) {
  3461. num_parents++;
  3462. }
  3463. }
  3464. nassertv(num_parents == (int)(uint16_t)num_parents);
  3465. dg.add_uint16(num_parents);
  3466. for (ui = up_list.begin(); ui != up_list.end(); ++ui) {
  3467. PandaNode *parent_node = (*ui).get_parent();
  3468. if (manager->has_object(parent_node)) {
  3469. manager->write_pointer(dg, parent_node);
  3470. }
  3471. }
  3472. }
  3473. /**
  3474. * Writes the indicated list of child node pointers to the datagram.
  3475. */
  3476. void PandaNode::CData::
  3477. write_down_list(const PandaNode::Down &down_list,
  3478. BamWriter *manager, Datagram &dg) const {
  3479. int num_children = down_list.size();
  3480. nassertv(num_children == (int)(uint16_t)num_children);
  3481. dg.add_uint16(num_children);
  3482. // Should we smarten up the writing of the sort number? Most of the time
  3483. // these will all be zero.
  3484. Down::const_iterator di;
  3485. for (di = down_list.begin(); di != down_list.end(); ++di) {
  3486. PandaNode *child_node = (*di).get_child();
  3487. int sort = (*di).get_sort();
  3488. manager->write_pointer(dg, child_node);
  3489. dg.add_int32(sort);
  3490. }
  3491. }
  3492. /**
  3493. * Calls consider_update on each node of the indicated up list.
  3494. */
  3495. void PandaNode::CData::
  3496. update_up_list(const PandaNode::Up &up_list, BamWriter *manager) const {
  3497. Up::const_iterator ui;
  3498. for (ui = up_list.begin(); ui != up_list.end(); ++ui) {
  3499. PandaNode *parent_node = (*ui).get_parent();
  3500. if (manager->has_object(parent_node)) {
  3501. manager->consider_update(parent_node);
  3502. }
  3503. }
  3504. }
  3505. /**
  3506. * Calls consider_update on each node of the indicated up list.
  3507. */
  3508. void PandaNode::CData::
  3509. update_down_list(const PandaNode::Down &down_list, BamWriter *manager) const {
  3510. Down::const_iterator di;
  3511. for (di = down_list.begin(); di != down_list.end(); ++di) {
  3512. PandaNode *child_node = (*di).get_child();
  3513. manager->consider_update(child_node);
  3514. }
  3515. }
  3516. /**
  3517. * Calls complete_pointers() on the list of parent node pointers.
  3518. */
  3519. int PandaNode::CData::
  3520. complete_up_list(PandaNode::Up &up_list, const string &tag,
  3521. TypedWritable **p_list, BamReader *manager) {
  3522. int pi = 0;
  3523. int num_parents = manager->get_int_tag(tag);
  3524. Up new_up_list(PandaNode::get_class_type());
  3525. new_up_list.reserve(num_parents);
  3526. for (int i = 0; i < num_parents; i++) {
  3527. PandaNode *parent_node = DCAST(PandaNode, p_list[pi++]);
  3528. UpConnection connection(parent_node);
  3529. new_up_list.push_back(connection);
  3530. }
  3531. // Now we should sort the list, since the sorting is based on pointer order,
  3532. // which might be different from one session to the next.
  3533. new_up_list.sort();
  3534. // Make it permanent.
  3535. up_list.swap(new_up_list);
  3536. new_up_list.clear();
  3537. return pi;
  3538. }
  3539. /**
  3540. * Calls complete_pointers() on the list of child node pointers.
  3541. */
  3542. int PandaNode::CData::
  3543. complete_down_list(PandaNode::Down &down_list, const string &tag,
  3544. TypedWritable **p_list, BamReader *manager) {
  3545. int pi = 0;
  3546. BamReaderAuxDataDown *aux;
  3547. DCAST_INTO_R(aux, manager->get_aux_tag(tag), pi);
  3548. Down &new_down_list = aux->_down_list;
  3549. for (Down::iterator di = new_down_list.begin();
  3550. di != new_down_list.end();
  3551. ++di) {
  3552. PandaNode *child_node = DCAST(PandaNode, p_list[pi++]);
  3553. (*di).set_child(child_node);
  3554. }
  3555. // Unlike the up list, we should *not* sort the down list. The down list is
  3556. // stored in a specific order, not related to pointer order; and this order
  3557. // should be preserved from one session to the next.
  3558. // Make it permanent.
  3559. down_list.swap(new_down_list);
  3560. new_down_list.clear();
  3561. return pi;
  3562. }
  3563. /**
  3564. * Reads the indicated list parent node pointers from the datagram (or at
  3565. * least calls read_pointer() for each one).
  3566. */
  3567. void PandaNode::CData::
  3568. fillin_up_list(PandaNode::Up &up_list, const string &tag,
  3569. DatagramIterator &scan, BamReader *manager) {
  3570. int num_parents = scan.get_uint16();
  3571. manager->set_int_tag(tag, num_parents);
  3572. manager->read_pointers(scan, num_parents);
  3573. }
  3574. /**
  3575. * Reads the indicated list child node pointers from the datagram (or at least
  3576. * calls read_pointer() for each one).
  3577. */
  3578. void PandaNode::CData::
  3579. fillin_down_list(PandaNode::Down &down_list, const string &tag,
  3580. DatagramIterator &scan, BamReader *manager) {
  3581. int num_children = scan.get_uint16();
  3582. // Create a temporary down_list, with the right number of elements, but a
  3583. // NULL value for each pointer (we'll fill in the pointers later). We need
  3584. // to do this to associate the sort values with their pointers.
  3585. Down new_down_list(PandaNode::get_class_type());
  3586. new_down_list.reserve(num_children);
  3587. for (int i = 0; i < num_children; i++) {
  3588. manager->read_pointer(scan);
  3589. int sort = scan.get_int32();
  3590. DownConnection connection(nullptr, sort);
  3591. new_down_list.push_back(connection);
  3592. }
  3593. // Now store the temporary down_list in the BamReader, so we can get it
  3594. // during the call to complete_down_list().
  3595. PT(BamReaderAuxDataDown) aux = new BamReaderAuxDataDown;
  3596. aux->_down_list.swap(new_down_list);
  3597. manager->set_aux_tag(tag, aux);
  3598. }
  3599. /**
  3600. * Ensures that the draw masks etc. are properly computed on this node. If
  3601. * update_bounds is true, also checks the bounding volume.
  3602. */
  3603. void PandaNodePipelineReader::
  3604. check_cached(bool update_bounds) const {
  3605. UpdateSeq last_update = update_bounds
  3606. ? _cdata->_last_bounds_update
  3607. : _cdata->_last_update;
  3608. if (last_update != _cdata->_next_update) {
  3609. // The cache is stale; it needs to be rebuilt.
  3610. // We'll need to get a fresh read pointer, since another thread might
  3611. // already have modified the pointer on the object since we queried it.
  3612. #ifdef DO_PIPELINING
  3613. node_unref_delete((CycleData *)_cdata);
  3614. #endif // DO_PIPELINING
  3615. ((PandaNodePipelineReader *)this)->_cdata = nullptr;
  3616. int pipeline_stage = _current_thread->get_pipeline_stage();
  3617. PandaNode::CDLockedStageReader fresh_cdata(_node->_cycler, pipeline_stage, _current_thread);
  3618. if (fresh_cdata->_last_update == fresh_cdata->_next_update &&
  3619. (!update_bounds || fresh_cdata->_last_bounds_update == fresh_cdata->_next_update)) {
  3620. // What luck, some other thread has already freshened the cache for us.
  3621. // Save the new pointer, and let the lock release itself.
  3622. if (_cdata != (const PandaNode::CData *)fresh_cdata) {
  3623. ((PandaNodePipelineReader *)this)->_cdata = fresh_cdata;
  3624. #ifdef DO_PIPELINING
  3625. _cdata->node_ref();
  3626. #endif // DO_PIPELINING
  3627. }
  3628. } else {
  3629. // No, the cache is still stale. We have to do the work of freshening
  3630. // it.
  3631. PStatTimer timer(PandaNode::_update_bounds_pcollector);
  3632. PandaNode::CDStageWriter cdataw = ((PandaNode *)_node)->update_cached(update_bounds, pipeline_stage, fresh_cdata);
  3633. nassertv(cdataw->_last_update == cdataw->_next_update);
  3634. // As above, we save the new pointer, and then let the lock release
  3635. // itself.
  3636. if (_cdata != (const PandaNode::CData *)cdataw) {
  3637. ((PandaNodePipelineReader *)this)->_cdata = cdataw;
  3638. #ifdef DO_PIPELINING
  3639. _cdata->node_ref();
  3640. #endif // DO_PIPELINING
  3641. }
  3642. }
  3643. }
  3644. nassertv(_cdata->_last_update == _cdata->_next_update);
  3645. nassertv(!update_bounds || _cdata->_last_bounds_update == _cdata->_next_update);
  3646. }