nodePath.cxx 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933
  1. // Filename: nodePath.cxx
  2. // Created by: drose (25Feb02)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "nodePath.h"
  19. #include "nodePathCollection.h"
  20. #include "findApproxPath.h"
  21. #include "findApproxLevelEntry.h"
  22. #include "findApproxLevel.h"
  23. #include "config_pgraph.h"
  24. #include "colorAttrib.h"
  25. #include "colorScaleAttrib.h"
  26. #include "cullBinAttrib.h"
  27. #include "textureAttrib.h"
  28. #include "materialAttrib.h"
  29. #include "fogAttrib.h"
  30. #include "renderModeAttrib.h"
  31. #include "cullFaceAttrib.h"
  32. #include "alphaTestAttrib.h"
  33. #include "depthTestAttrib.h"
  34. #include "depthWriteAttrib.h"
  35. #include "billboardEffect.h"
  36. #include "compassEffect.h"
  37. #include "showBoundsEffect.h"
  38. #include "transparencyAttrib.h"
  39. #include "materialPool.h"
  40. #include "look_at.h"
  41. #include "plist.h"
  42. #include "boundingSphere.h"
  43. #include "geomNode.h"
  44. #include "sceneGraphReducer.h"
  45. #include "textureCollection.h"
  46. #include "globPattern.h"
  47. #include "config_gobj.h"
  48. #include "bamFile.h"
  49. #include "dcast.h"
  50. // stack seems to overflow on Intel C++ at 7000. If we need more than
  51. // 7000, need to increase stack size.
  52. int NodePath::_max_search_depth = 7000;
  53. TypeHandle NodePath::_type_handle;
  54. ////////////////////////////////////////////////////////////////////
  55. // Function: NodePath::get_num_nodes
  56. // Access: Published
  57. // Description: Returns the number of nodes in the path.
  58. ////////////////////////////////////////////////////////////////////
  59. int NodePath::
  60. get_num_nodes() const {
  61. if (is_empty()) {
  62. return 0;
  63. }
  64. return _head->get_length();
  65. }
  66. ////////////////////////////////////////////////////////////////////
  67. // Function: NodePath::get_node
  68. // Access: Published
  69. // Description: Returns the nth node of the path, where 0 is the
  70. // referenced (bottom) node and get_num_nodes() - 1 is
  71. // the top node. This requires iterating through the
  72. // path.
  73. ////////////////////////////////////////////////////////////////////
  74. PandaNode *NodePath::
  75. get_node(int index) const {
  76. nassertr(index >= 0 && index < get_num_nodes(), NULL);
  77. NodePathComponent *comp = _head;
  78. while (index > 0) {
  79. // If this assertion fails, the index was out of range; the
  80. // component's length must have been invalid.
  81. nassertr(comp != (NodePathComponent *)NULL, NULL);
  82. comp = comp->get_next();
  83. index--;
  84. }
  85. // If this assertion fails, the index was out of range; the
  86. // component's length must have been invalid.
  87. nassertr(comp != (NodePathComponent *)NULL, NULL);
  88. return comp->get_node();
  89. }
  90. ////////////////////////////////////////////////////////////////////
  91. // Function: NodePath::get_top_node
  92. // Access: Published
  93. // Description: Returns the top node of the path, or NULL if the path
  94. // is empty. This requires iterating through the path.
  95. ////////////////////////////////////////////////////////////////////
  96. PandaNode *NodePath::
  97. get_top_node() const {
  98. if (is_empty()) {
  99. return (PandaNode *)NULL;
  100. }
  101. NodePathComponent *comp = _head;
  102. while (!comp->is_top_node()) {
  103. comp = comp->get_next();
  104. nassertr(comp != (NodePathComponent *)NULL, NULL);
  105. }
  106. return comp->get_node();
  107. }
  108. ////////////////////////////////////////////////////////////////////
  109. // Function: NodePath::get_children
  110. // Access: Published
  111. // Description: Returns the set of all child nodes of the referenced
  112. // node.
  113. ////////////////////////////////////////////////////////////////////
  114. NodePathCollection NodePath::
  115. get_children() const {
  116. NodePathCollection result;
  117. nassertr_always(!is_empty(), result);
  118. PandaNode *bottom_node = node();
  119. PandaNode::Children cr = bottom_node->get_children();
  120. int num_children = cr.get_num_children();
  121. for (int i = 0; i < num_children; i++) {
  122. NodePath child;
  123. child._head = PandaNode::get_component(_head, cr.get_child(i));
  124. result.add_path(child);
  125. }
  126. return result;
  127. }
  128. ////////////////////////////////////////////////////////////////////
  129. // Function: NodePath::get_stashed_children
  130. // Access: Published
  131. // Description: Returns the set of all child nodes of the referenced
  132. // node that have been stashed. These children are not
  133. // normally visible on the node, and do not appear in
  134. // the list returned by get_children().
  135. ////////////////////////////////////////////////////////////////////
  136. NodePathCollection NodePath::
  137. get_stashed_children() const {
  138. NodePathCollection result;
  139. nassertr_always(!is_empty(), result);
  140. PandaNode *bottom_node = node();
  141. int num_stashed = bottom_node->get_num_stashed();
  142. for (int i = 0; i < num_stashed; i++) {
  143. NodePath stashed;
  144. stashed._head = PandaNode::get_component(_head, bottom_node->get_stashed(i));
  145. result.add_path(stashed);
  146. }
  147. return result;
  148. }
  149. ////////////////////////////////////////////////////////////////////
  150. // Function: NodePath::get_sort
  151. // Access: Published
  152. // Description: Returns the sort value of the referenced node within
  153. // its parent; that is, the sort number passed on the
  154. // last reparenting operation for this node. This will
  155. // control the position of the node within its parent's
  156. // list of children.
  157. ////////////////////////////////////////////////////////////////////
  158. int NodePath::
  159. get_sort() const {
  160. if (!has_parent()) {
  161. return 0;
  162. }
  163. PandaNode *parent = _head->get_next()->get_node();
  164. PandaNode *child = node();
  165. nassertr(parent != (PandaNode *)NULL && child != (PandaNode *)NULL, 0);
  166. int child_index = parent->find_child(child);
  167. if (child_index != -1) {
  168. return parent->get_child_sort(child_index);
  169. }
  170. child_index = parent->find_stashed(child);
  171. if (child_index != -1) {
  172. return parent->get_stashed_sort(child_index);
  173. }
  174. nassertr(false, 0);
  175. return 0;
  176. }
  177. ////////////////////////////////////////////////////////////////////
  178. // Function: NodePath::find
  179. // Access: Published
  180. // Description: Searches for a node below the referenced node that
  181. // matches the indicated string. Returns the shortest
  182. // match found, if any, or an empty NodePath if no match
  183. // can be found.
  184. ////////////////////////////////////////////////////////////////////
  185. NodePath NodePath::
  186. find(const string &path) const {
  187. nassertr_always(!is_empty(), fail());
  188. NodePathCollection col;
  189. find_matches(col, path, 1);
  190. if (col.is_empty()) {
  191. return NodePath::not_found();
  192. }
  193. return col.get_path(0);
  194. }
  195. ////////////////////////////////////////////////////////////////////
  196. // Function: NodePath::find_path_to
  197. // Access: Published
  198. // Description: Searches for the indicated node below this node and
  199. // returns the shortest NodePath that connects them.
  200. ////////////////////////////////////////////////////////////////////
  201. NodePath NodePath::
  202. find_path_to(PandaNode *node) const {
  203. nassertr_always(!is_empty(), fail());
  204. nassertr(node != (PandaNode *)NULL, fail());
  205. NodePathCollection col;
  206. FindApproxPath approx_path;
  207. approx_path.add_match_many(0);
  208. approx_path.add_match_pointer(node, 0);
  209. find_matches(col, approx_path, 1);
  210. if (col.is_empty()) {
  211. return NodePath::not_found();
  212. }
  213. return col.get_path(0);
  214. }
  215. ////////////////////////////////////////////////////////////////////
  216. // Function: NodePath::find_all_matches
  217. // Access: Published
  218. // Description: Returns the complete set of all NodePaths that begin
  219. // with this NodePath and can be extended by
  220. // path. The shortest paths will be listed
  221. // first.
  222. ////////////////////////////////////////////////////////////////////
  223. NodePathCollection NodePath::
  224. find_all_matches(const string &path) const {
  225. NodePathCollection col;
  226. nassertr_always(!is_empty(), col);
  227. nassertr(verify_complete(), col);
  228. find_matches(col, path, -1);
  229. return col;
  230. }
  231. ////////////////////////////////////////////////////////////////////
  232. // Function: NodePath::find_all_paths_to
  233. // Access: Published
  234. // Description: Returns the set of all NodePaths that extend from
  235. // this NodePath down to the indicated node. The
  236. // shortest paths will be listed first.
  237. ////////////////////////////////////////////////////////////////////
  238. NodePathCollection NodePath::
  239. find_all_paths_to(PandaNode *node) const {
  240. NodePathCollection col;
  241. nassertr_always(!is_empty(), col);
  242. nassertr(verify_complete(), col);
  243. nassertr(node != (PandaNode *)NULL, col);
  244. FindApproxPath approx_path;
  245. approx_path.add_match_many(0);
  246. approx_path.add_match_pointer(node, 0);
  247. find_matches(col, approx_path, -1);
  248. return col;
  249. }
  250. ////////////////////////////////////////////////////////////////////
  251. // Function: NodePath::reparent_to
  252. // Access: Published
  253. // Description: Removes the referenced node of the NodePath from its
  254. // current parent and attaches it to the referenced node of
  255. // the indicated NodePath.
  256. ////////////////////////////////////////////////////////////////////
  257. void NodePath::
  258. reparent_to(const NodePath &other, int sort) {
  259. nassertv(verify_complete());
  260. nassertv(other.verify_complete());
  261. nassertv_always(!is_empty());
  262. nassertv(other._error_type == ET_ok);
  263. // Reparenting implicitly resents the delta vector.
  264. node()->reset_prev_transform();
  265. bool reparented = PandaNode::reparent(other._head, _head, sort, false);
  266. nassertv(reparented);
  267. }
  268. ////////////////////////////////////////////////////////////////////
  269. // Function: NodePath::wrt_reparent_to
  270. // Access: Published
  271. // Description: This functions identically to reparent_to(), except
  272. // the transform on this node is also adjusted so that
  273. // the node remains in the same place in world
  274. // coordinates, even if it is reparented into a
  275. // different coordinate system.
  276. ////////////////////////////////////////////////////////////////////
  277. void NodePath::
  278. wrt_reparent_to(const NodePath &other, int sort) {
  279. nassertv(verify_complete());
  280. nassertv(other.verify_complete());
  281. nassertv_always(!is_empty());
  282. nassertv(other._error_type == ET_ok);
  283. if (get_transform() == get_prev_transform()) {
  284. set_transform(get_transform(other));
  285. node()->reset_prev_transform();
  286. } else {
  287. set_transform(get_transform(other));
  288. set_prev_transform(get_prev_transform(other));
  289. }
  290. reparent_to(other, sort);
  291. }
  292. ////////////////////////////////////////////////////////////////////
  293. // Function: NodePath::instance_to
  294. // Access: Published
  295. // Description: Adds the referenced node of the NodePath as a child
  296. // of the referenced node of the indicated other
  297. // NodePath. Any other parent-child relations of the
  298. // node are unchanged; in particular, the node is not
  299. // removed from its existing parent, if any.
  300. //
  301. // If the node already had an existing parent, this
  302. // method will create a new instance of the node within
  303. // the scene graph.
  304. //
  305. // This does not change the NodePath itself, but does
  306. // return a new NodePath that reflects the new instance
  307. // node.
  308. ////////////////////////////////////////////////////////////////////
  309. NodePath NodePath::
  310. instance_to(const NodePath &other, int sort) const {
  311. nassertr(verify_complete(), NodePath::fail());
  312. nassertr(other.verify_complete(), NodePath::fail());
  313. nassertr_always(!is_empty(), NodePath::fail());
  314. nassertr(other._error_type == ET_ok, NodePath::fail());
  315. NodePath new_instance;
  316. // First, we'll attach to NULL, to guarantee we get a brand new
  317. // instance.
  318. new_instance._head = PandaNode::attach(NULL, node(), sort);
  319. // Now, we'll reparent the new instance to the target node.
  320. bool reparented = PandaNode::reparent(other._head, new_instance._head,
  321. sort, false);
  322. nassertr(reparented, new_instance);
  323. // instance_to() doesn't reset the velocity delta, unlike most of
  324. // the other reparenting operations. The reasoning is that
  325. // instance_to() is not necessarily a reparenting operation, since
  326. // it doesn't change the original instance.
  327. return new_instance;
  328. }
  329. ////////////////////////////////////////////////////////////////////
  330. // Function: NodePath::instance_under_node
  331. // Access: Published
  332. // Description: Behaves like instance_to(), but implicitly creates a
  333. // new node to instance the geometry under, and returns a
  334. // NodePath to that new node. This allows the
  335. // programmer to set a unique state and/or transform on
  336. // this instance.
  337. ////////////////////////////////////////////////////////////////////
  338. NodePath NodePath::
  339. instance_under_node(const NodePath &other, const string &name, int sort) const {
  340. NodePath new_node = other.attach_new_node(name, sort);
  341. NodePath instance = instance_to(new_node);
  342. if (instance.is_empty()) {
  343. new_node.remove_node();
  344. return instance;
  345. }
  346. return new_node;
  347. }
  348. ////////////////////////////////////////////////////////////////////
  349. // Function: NodePath::copy_to
  350. // Access: Published
  351. // Description: Functions like instance_to(), except a deep
  352. // copy is made of the referenced node and all of its
  353. // descendents, which is then parented to the indicated
  354. // node. A NodePath to the newly created copy is
  355. // returned.
  356. ////////////////////////////////////////////////////////////////////
  357. NodePath NodePath::
  358. copy_to(const NodePath &other, int sort) const {
  359. nassertr(verify_complete(), fail());
  360. nassertr(other.verify_complete(), fail());
  361. nassertr_always(!is_empty(), fail());
  362. nassertr(!other.is_empty(), fail());
  363. nassertr(other._error_type == ET_ok, fail());
  364. PandaNode *source_node = node();
  365. PT(PandaNode) copy_node = source_node->copy_subgraph();
  366. nassertr(copy_node != (PandaNode *)NULL, fail());
  367. copy_node->reset_prev_transform();
  368. return other.attach_new_node(copy_node, sort);
  369. }
  370. ////////////////////////////////////////////////////////////////////
  371. // Function: NodePath::attach_new_node
  372. // Access: Published
  373. // Description: Attaches a new node, with or without existing
  374. // parents, to the scene graph below the referenced node
  375. // of this NodePath. This is the preferred way to add
  376. // nodes to the graph.
  377. //
  378. // If the node was already a child of the parent, this
  379. // returns a NodePath to the existing child.
  380. //
  381. // This does *not* automatically extend the current
  382. // NodePath to reflect the attachment; however, a
  383. // NodePath that does reflect this extension is
  384. // returned.
  385. ////////////////////////////////////////////////////////////////////
  386. NodePath NodePath::
  387. attach_new_node(PandaNode *node, int sort) const {
  388. nassertr(verify_complete(), NodePath::fail());
  389. nassertr_always(!is_empty(), NodePath::fail());
  390. nassertr(node != (PandaNode *)NULL, NodePath::fail());
  391. NodePath new_path(*this);
  392. new_path._head = PandaNode::attach(_head, node, sort);
  393. return new_path;
  394. }
  395. ////////////////////////////////////////////////////////////////////
  396. // Function: NodePath::remove_node
  397. // Access: Published
  398. // Description: Disconnects the referenced node from the scene graph.
  399. // This will also delete the node if there are no other
  400. // pointers to it.
  401. //
  402. // Normally, this should be called only when you are
  403. // really done with the node. If you want to remove a
  404. // node from the scene graph but keep it around for
  405. // later, you should probably use detach_node() instead.
  406. //
  407. // In practice, the only difference between
  408. // remove_node() and detach_node() is that remove_node()
  409. // also resets the NodePath to empty, which will cause
  410. // the node to be deleted immediately if there are no
  411. // other references. On the other hand, detach_node()
  412. // leaves the NodePath referencing the node, which will
  413. // keep at least one reference to the node for as long
  414. // as the NodePath exists.
  415. ////////////////////////////////////////////////////////////////////
  416. void NodePath::
  417. remove_node() {
  418. nassertv(_error_type != ET_not_found);
  419. // If we have no parents, remove_node() is just a do-nothing
  420. // operation; if we have no nodes, maybe we were already removed.
  421. // In either case, quietly do nothing except to ensure the
  422. // NodePath is clear.
  423. if (!is_empty() && !is_singleton()) {
  424. node()->reset_prev_transform();
  425. PandaNode::detach(_head);
  426. }
  427. if (is_empty() || _head->has_key()) {
  428. // Preserve the key we had on the node before we removed it.
  429. int key = get_key();
  430. (*this) = NodePath::removed();
  431. _backup_key = key;
  432. } else {
  433. // We didn't have a key; just clear the NodePath.
  434. (*this) = NodePath::removed();
  435. }
  436. }
  437. ////////////////////////////////////////////////////////////////////
  438. // Function: NodePath::detach_node
  439. // Access: Published
  440. // Description: Disconnects the referenced node from its parent, but
  441. // does not immediately delete it. The NodePath retains
  442. // a pointer to the node. If there are no other
  443. // instances to the node, this becomes a singleton
  444. // NodePath; otherwise, this NodePath becomes the same
  445. // as another arbitrary instance.
  446. //
  447. // In practice, the only difference between
  448. // remove_node() and detach_node() is that remove_node()
  449. // also resets the NodePath to empty, which will cause
  450. // the node to be deleted immediately if there are no
  451. // other references. On the other hand, detach_node()
  452. // leaves the NodePath referencing the node, which will
  453. // keep at least one reference to the node for as long
  454. // as the NodePath exists.
  455. ////////////////////////////////////////////////////////////////////
  456. void NodePath::
  457. detach_node() {
  458. nassertv(_error_type != ET_not_found);
  459. if (!is_empty() && !is_singleton()) {
  460. node()->reset_prev_transform();
  461. PandaNode::detach(_head);
  462. }
  463. }
  464. ////////////////////////////////////////////////////////////////////
  465. // Function: NodePath::output
  466. // Access: Published
  467. // Description: Writes a sensible description of the NodePath to the
  468. // indicated output stream.
  469. ////////////////////////////////////////////////////////////////////
  470. void NodePath::
  471. output(ostream &out) const {
  472. switch (_error_type) {
  473. case ET_not_found:
  474. out << "**not found**";
  475. return;
  476. case ET_removed:
  477. out << "**removed**";
  478. return;
  479. case ET_fail:
  480. out << "**error**";
  481. return;
  482. default:
  483. break;
  484. }
  485. if (_head == (NodePathComponent *)NULL) {
  486. out << "(empty)";
  487. } else {
  488. _head->output(out);
  489. }
  490. }
  491. ////////////////////////////////////////////////////////////////////
  492. // Function: NodePath::get_state
  493. // Access: Published
  494. // Description: Returns the state changes that must be made to
  495. // transition from the render state of this node to the
  496. // render state of the other node.
  497. ////////////////////////////////////////////////////////////////////
  498. CPT(RenderState) NodePath::
  499. get_state(const NodePath &other) const {
  500. nassertr(_error_type == ET_ok && other._error_type == ET_ok, RenderState::make_empty());
  501. if (is_empty()) {
  502. return other.get_net_state();
  503. }
  504. if (other.is_empty()) {
  505. return get_net_state()->invert_compose(RenderState::make_empty());
  506. }
  507. nassertr(verify_complete(), RenderState::make_empty());
  508. nassertr(other.verify_complete(), RenderState::make_empty());
  509. int a_count, b_count;
  510. if (find_common_ancestor(*this, other, a_count, b_count) == (NodePathComponent *)NULL) {
  511. if (allow_unrelated_wrt) {
  512. pgraph_cat.debug()
  513. << *this << " is not related to " << other << "\n";
  514. } else {
  515. pgraph_cat.error()
  516. << *this << " is not related to " << other << "\n";
  517. nassertr(false, RenderState::make_empty());
  518. }
  519. }
  520. CPT(RenderState) a_state = r_get_partial_state(_head, a_count);
  521. CPT(RenderState) b_state = r_get_partial_state(other._head, b_count);
  522. return a_state->invert_compose(b_state);
  523. }
  524. ////////////////////////////////////////////////////////////////////
  525. // Function: NodePath::set_state
  526. // Access: Published
  527. // Description: Sets the state object on this node, relative to
  528. // the other node. This computes a new state object
  529. // that has the indicated value when seen relative to
  530. // the other node.
  531. ////////////////////////////////////////////////////////////////////
  532. void NodePath::
  533. set_state(const NodePath &other, const RenderState *state) {
  534. nassertv(_error_type == ET_ok && other._error_type == ET_ok);
  535. nassertv_always(!is_empty());
  536. // First, we perform a wrt to the parent, to get the conversion.
  537. NodePath parent = get_parent();
  538. CPT(RenderState) rel_state = parent.get_state(other);
  539. CPT(RenderState) new_state = rel_state->compose(state);
  540. set_state(new_state);
  541. }
  542. ////////////////////////////////////////////////////////////////////
  543. // Function: NodePath::get_transform
  544. // Access: Published
  545. // Description: Returns the relative transform to this node from the
  546. // other node; i.e. the transformation of this node
  547. // as seen from the other node.
  548. ////////////////////////////////////////////////////////////////////
  549. CPT(TransformState) NodePath::
  550. get_transform(const NodePath &other) const {
  551. nassertr(_error_type == ET_ok && other._error_type == ET_ok, TransformState::make_identity());
  552. if (other.is_empty()) {
  553. return get_net_transform();
  554. }
  555. if (is_empty()) {
  556. return other.get_net_transform()->invert_compose(TransformState::make_identity());
  557. }
  558. nassertr(verify_complete(), TransformState::make_identity());
  559. nassertr(other.verify_complete(), TransformState::make_identity());
  560. int a_count, b_count;
  561. if (find_common_ancestor(*this, other, a_count, b_count) == (NodePathComponent *)NULL) {
  562. if (allow_unrelated_wrt) {
  563. pgraph_cat.debug()
  564. << *this << " is not related to " << other << "\n";
  565. } else {
  566. pgraph_cat.error()
  567. << *this << " is not related to " << other << "\n";
  568. nassertr(false, TransformState::make_identity());
  569. }
  570. }
  571. CPT(TransformState) a_transform = r_get_partial_transform(_head, a_count);
  572. CPT(TransformState) b_transform = r_get_partial_transform(other._head, b_count);
  573. return b_transform->invert_compose(a_transform);
  574. }
  575. ////////////////////////////////////////////////////////////////////
  576. // Function: NodePath::set_transform
  577. // Access: Published
  578. // Description: Sets the transform object on this node, relative to
  579. // the other node. This computes a new transform object
  580. // that will have the indicated value when seen from the
  581. // other node.
  582. ////////////////////////////////////////////////////////////////////
  583. void NodePath::
  584. set_transform(const NodePath &other, const TransformState *transform) {
  585. nassertv(_error_type == ET_ok && other._error_type == ET_ok);
  586. nassertv_always(!is_empty());
  587. // First, we perform a wrt to the parent, to get the conversion.
  588. CPT(TransformState) rel_trans;
  589. if (has_parent()) {
  590. rel_trans = other.get_transform(get_parent());
  591. } else {
  592. rel_trans = other.get_transform(NodePath());
  593. }
  594. CPT(TransformState) new_trans = rel_trans->compose(transform);
  595. set_transform(new_trans);
  596. }
  597. ////////////////////////////////////////////////////////////////////
  598. // Function: NodePath::get_prev_transform
  599. // Access: Published
  600. // Description: Returns the relative "previous" transform to this
  601. // node from the other node; i.e. the position of this
  602. // node in the previous frame, as seen by the other node
  603. // in the previous frame.
  604. ////////////////////////////////////////////////////////////////////
  605. CPT(TransformState) NodePath::
  606. get_prev_transform(const NodePath &other) const {
  607. nassertr(_error_type == ET_ok && other._error_type == ET_ok, TransformState::make_identity());
  608. if (other.is_empty()) {
  609. return get_net_prev_transform();
  610. }
  611. if (is_empty()) {
  612. return other.get_net_prev_transform()->invert_compose(TransformState::make_identity());
  613. }
  614. nassertr(verify_complete(), TransformState::make_identity());
  615. nassertr(other.verify_complete(), TransformState::make_identity());
  616. int a_count, b_count;
  617. if (find_common_ancestor(*this, other, a_count, b_count) == (NodePathComponent *)NULL) {
  618. if (allow_unrelated_wrt) {
  619. pgraph_cat.debug()
  620. << *this << " is not related to " << other << "\n";
  621. } else {
  622. pgraph_cat.error()
  623. << *this << " is not related to " << other << "\n";
  624. nassertr(false, TransformState::make_identity());
  625. }
  626. }
  627. CPT(TransformState) a_prev_transform = r_get_partial_prev_transform(_head, a_count);
  628. CPT(TransformState) b_prev_transform = r_get_partial_prev_transform(other._head, b_count);
  629. return b_prev_transform->invert_compose(a_prev_transform);
  630. }
  631. ////////////////////////////////////////////////////////////////////
  632. // Function: NodePath::set_prev_transform
  633. // Access: Published
  634. // Description: Sets the "previous" transform object on this node,
  635. // relative to the other node. This computes a new
  636. // transform object that will have the indicated value
  637. // when seen from the other node.
  638. ////////////////////////////////////////////////////////////////////
  639. void NodePath::
  640. set_prev_transform(const NodePath &other, const TransformState *transform) {
  641. nassertv(_error_type == ET_ok && other._error_type == ET_ok);
  642. nassertv_always(!is_empty());
  643. // First, we perform a wrt to the parent, to get the conversion.
  644. CPT(TransformState) rel_trans;
  645. if (has_parent()) {
  646. rel_trans = other.get_prev_transform(get_parent());
  647. } else {
  648. rel_trans = other.get_prev_transform(NodePath());
  649. }
  650. CPT(TransformState) new_trans = rel_trans->compose(transform);
  651. set_prev_transform(new_trans);
  652. }
  653. ////////////////////////////////////////////////////////////////////
  654. // Function: NodePath::set_pos
  655. // Access: Published
  656. // Description: Sets the translation component of the transform,
  657. // leaving rotation and scale untouched. This also
  658. // resets the node's "previous" position, so that the
  659. // collision system will see the node as having suddenly
  660. // appeared in the new position, without passing any
  661. // points in between.
  662. // See Also: NodePath::set_fluid_pos
  663. ////////////////////////////////////////////////////////////////////
  664. void NodePath::
  665. set_pos(const LVecBase3f &pos) {
  666. nassertv_always(!is_empty());
  667. set_transform(get_transform()->set_pos(pos));
  668. node()->reset_prev_transform();
  669. }
  670. void NodePath::
  671. set_x(float x) {
  672. nassertv_always(!is_empty());
  673. LPoint3f pos = get_pos();
  674. pos[0] = x;
  675. set_pos(pos);
  676. }
  677. void NodePath::
  678. set_y(float y) {
  679. nassertv_always(!is_empty());
  680. LPoint3f pos = get_pos();
  681. pos[1] = y;
  682. set_pos(pos);
  683. }
  684. void NodePath::
  685. set_z(float z) {
  686. nassertv_always(!is_empty());
  687. LPoint3f pos = get_pos();
  688. pos[2] = z;
  689. set_pos(pos);
  690. }
  691. ////////////////////////////////////////////////////////////////////
  692. // Function: NodePath::set_fluid_pos
  693. // Access: Published
  694. // Description: Sets the translation component, without changing the
  695. // "previous" position, so that the collision system
  696. // will see the node as moving fluidly from its previous
  697. // position to its new position.
  698. // See Also: NodePath::set_pos
  699. ////////////////////////////////////////////////////////////////////
  700. void NodePath::
  701. set_fluid_pos(const LVecBase3f &pos) {
  702. nassertv_always(!is_empty());
  703. set_transform(get_transform()->set_pos(pos));
  704. }
  705. void NodePath::
  706. set_fluid_x(float x) {
  707. nassertv_always(!is_empty());
  708. LPoint3f pos = get_pos();
  709. pos[0] = x;
  710. set_fluid_pos(pos);
  711. }
  712. void NodePath::
  713. set_fluid_y(float y) {
  714. nassertv_always(!is_empty());
  715. LPoint3f pos = get_pos();
  716. pos[1] = y;
  717. set_fluid_pos(pos);
  718. }
  719. void NodePath::
  720. set_fluid_z(float z) {
  721. nassertv_always(!is_empty());
  722. LPoint3f pos = get_pos();
  723. pos[2] = z;
  724. set_fluid_pos(pos);
  725. }
  726. ////////////////////////////////////////////////////////////////////
  727. // Function: NodePath::get_pos
  728. // Access: Published
  729. // Description: Retrieves the translation component of the transform.
  730. ////////////////////////////////////////////////////////////////////
  731. LPoint3f NodePath::
  732. get_pos() const {
  733. nassertr_always(!is_empty(), LPoint3f(0.0f, 0.0f, 0.0f));
  734. return get_transform()->get_pos();
  735. }
  736. ////////////////////////////////////////////////////////////////////
  737. // Function: NodePath::get_pos_delta
  738. // Access: Published
  739. // Description: Returns the delta vector from this node's position in
  740. // the previous frame (according to
  741. // set_prev_transform(), typically set via the use of
  742. // set_fluid_pos()) and its position in the current
  743. // frame. This is the vector used to determine
  744. // collisions. Generally, if the node was last
  745. // repositioned via set_pos(), the delta will be zero;
  746. // if it was adjusted via set_fluid_pos(), the delta
  747. // will represent the change from the previous frame's
  748. // position.
  749. ////////////////////////////////////////////////////////////////////
  750. LVector3f NodePath::
  751. get_pos_delta() const {
  752. nassertr_always(!is_empty(), LPoint3f(0.0f, 0.0f, 0.0f));
  753. return get_transform()->get_pos() - get_prev_transform()->get_pos();
  754. }
  755. ////////////////////////////////////////////////////////////////////
  756. // Function: NodePath::set_hpr
  757. // Access: Published
  758. // Description: Sets the rotation component of the transform,
  759. // leaving translation and scale untouched.
  760. ////////////////////////////////////////////////////////////////////
  761. void NodePath::
  762. set_hpr(const LVecBase3f &hpr) {
  763. nassertv_always(!is_empty());
  764. CPT(TransformState) transform = get_transform();
  765. nassertv(transform->has_hpr());
  766. set_transform(transform->set_hpr(hpr));
  767. }
  768. void NodePath::
  769. set_h(float h) {
  770. nassertv_always(!is_empty());
  771. CPT(TransformState) transform = get_transform();
  772. nassertv(transform->has_hpr());
  773. LVecBase3f hpr = transform->get_hpr();
  774. hpr[0] = h;
  775. set_transform(transform->set_hpr(hpr));
  776. }
  777. void NodePath::
  778. set_p(float p) {
  779. nassertv_always(!is_empty());
  780. CPT(TransformState) transform = get_transform();
  781. nassertv(transform->has_hpr());
  782. LVecBase3f hpr = transform->get_hpr();
  783. hpr[1] = p;
  784. set_transform(transform->set_hpr(hpr));
  785. }
  786. void NodePath::
  787. set_r(float r) {
  788. nassertv_always(!is_empty());
  789. CPT(TransformState) transform = get_transform();
  790. nassertv(transform->has_hpr());
  791. LVecBase3f hpr = transform->get_hpr();
  792. hpr[2] = r;
  793. set_transform(transform->set_hpr(hpr));
  794. }
  795. ////////////////////////////////////////////////////////////////////
  796. // Function: NodePath::get_hpr
  797. // Access: Published
  798. // Description: Retrieves the rotation component of the transform.
  799. ////////////////////////////////////////////////////////////////////
  800. LVecBase3f NodePath::
  801. get_hpr() const {
  802. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  803. CPT(TransformState) transform = get_transform();
  804. nassertr(transform->has_hpr(), LVecBase3f(0.0f, 0.0f, 0.0f));
  805. return transform->get_hpr();
  806. }
  807. ////////////////////////////////////////////////////////////////////
  808. // Function: NodePath::set_quat
  809. // Access: Published
  810. // Description: Sets the rotation component of the transform,
  811. // leaving translation and scale untouched.
  812. ////////////////////////////////////////////////////////////////////
  813. void NodePath::
  814. set_quat(const LQuaternionf &quat) {
  815. nassertv_always(!is_empty());
  816. CPT(TransformState) transform = get_transform();
  817. set_transform(transform->set_quat(quat));
  818. }
  819. ////////////////////////////////////////////////////////////////////
  820. // Function: NodePath::get_quat
  821. // Access: Published
  822. // Description: Retrieves the rotation component of the transform.
  823. ////////////////////////////////////////////////////////////////////
  824. LQuaternionf NodePath::
  825. get_quat() const {
  826. nassertr_always(!is_empty(), LQuaternionf::ident_quat());
  827. CPT(TransformState) transform = get_transform();
  828. return transform->get_quat();
  829. }
  830. ////////////////////////////////////////////////////////////////////
  831. // Function: NodePath::set_scale
  832. // Access: Published
  833. // Description: Sets the scale component of the transform,
  834. // leaving translation and rotation untouched.
  835. ////////////////////////////////////////////////////////////////////
  836. void NodePath::
  837. set_scale(const LVecBase3f &scale) {
  838. nassertv_always(!is_empty());
  839. CPT(TransformState) transform = get_transform();
  840. set_transform(transform->set_scale(scale));
  841. }
  842. void NodePath::
  843. set_sx(float sx) {
  844. nassertv_always(!is_empty());
  845. CPT(TransformState) transform = get_transform();
  846. LVecBase3f scale = transform->get_scale();
  847. scale[0] = sx;
  848. set_transform(transform->set_scale(scale));
  849. }
  850. void NodePath::
  851. set_sy(float sy) {
  852. nassertv_always(!is_empty());
  853. CPT(TransformState) transform = get_transform();
  854. LVecBase3f scale = transform->get_scale();
  855. scale[1] = sy;
  856. set_transform(transform->set_scale(scale));
  857. }
  858. void NodePath::
  859. set_sz(float sz) {
  860. nassertv_always(!is_empty());
  861. CPT(TransformState) transform = get_transform();
  862. LVecBase3f scale = transform->get_scale();
  863. scale[2] = sz;
  864. set_transform(transform->set_scale(scale));
  865. }
  866. ////////////////////////////////////////////////////////////////////
  867. // Function: NodePath::get_scale
  868. // Access: Published
  869. // Description: Retrieves the scale component of the transform.
  870. ////////////////////////////////////////////////////////////////////
  871. LVecBase3f NodePath::
  872. get_scale() const {
  873. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  874. CPT(TransformState) transform = get_transform();
  875. return transform->get_scale();
  876. }
  877. ////////////////////////////////////////////////////////////////////
  878. // Function: NodePath::set_shear
  879. // Access: Published
  880. // Description: Sets the shear component of the transform,
  881. // leaving translation and rotation untouched.
  882. ////////////////////////////////////////////////////////////////////
  883. void NodePath::
  884. set_shear(const LVecBase3f &shear) {
  885. nassertv_always(!is_empty());
  886. CPT(TransformState) transform = get_transform();
  887. set_transform(transform->set_shear(shear));
  888. }
  889. void NodePath::
  890. set_shxy(float shxy) {
  891. nassertv_always(!is_empty());
  892. CPT(TransformState) transform = get_transform();
  893. LVecBase3f shear = transform->get_shear();
  894. shear[0] = shxy;
  895. set_transform(transform->set_shear(shear));
  896. }
  897. void NodePath::
  898. set_shxz(float shxz) {
  899. nassertv_always(!is_empty());
  900. CPT(TransformState) transform = get_transform();
  901. LVecBase3f shear = transform->get_shear();
  902. shear[1] = shxz;
  903. set_transform(transform->set_shear(shear));
  904. }
  905. void NodePath::
  906. set_shyz(float shyz) {
  907. nassertv_always(!is_empty());
  908. CPT(TransformState) transform = get_transform();
  909. LVecBase3f shear = transform->get_shear();
  910. shear[2] = shyz;
  911. set_transform(transform->set_shear(shear));
  912. }
  913. ////////////////////////////////////////////////////////////////////
  914. // Function: NodePath::get_shear
  915. // Access: Published
  916. // Description: Retrieves the shear component of the transform.
  917. ////////////////////////////////////////////////////////////////////
  918. LVecBase3f NodePath::
  919. get_shear() const {
  920. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  921. CPT(TransformState) transform = get_transform();
  922. return transform->get_shear();
  923. }
  924. ////////////////////////////////////////////////////////////////////
  925. // Function: NodePath::set_pos_hpr
  926. // Access: Published
  927. // Description: Sets the translation and rotation component of the
  928. // transform, leaving scale untouched.
  929. ////////////////////////////////////////////////////////////////////
  930. void NodePath::
  931. set_pos_hpr(const LVecBase3f &pos, const LVecBase3f &hpr) {
  932. nassertv_always(!is_empty());
  933. CPT(TransformState) transform = get_transform();
  934. transform = TransformState::make_pos_hpr_scale_shear
  935. (pos, hpr, transform->get_scale(), transform->get_shear());
  936. set_transform(transform);
  937. node()->reset_prev_transform();
  938. }
  939. ////////////////////////////////////////////////////////////////////
  940. // Function: NodePath::set_hpr_scale
  941. // Access: Published
  942. // Description: Sets the rotation and scale components of the
  943. // transform, leaving translation untouched.
  944. ////////////////////////////////////////////////////////////////////
  945. void NodePath::
  946. set_hpr_scale(const LVecBase3f &hpr, const LVecBase3f &scale) {
  947. nassertv_always(!is_empty());
  948. CPT(TransformState) transform = get_transform();
  949. transform = TransformState::make_pos_hpr_scale_shear
  950. (transform->get_pos(), hpr, scale, transform->get_shear());
  951. set_transform(transform);
  952. }
  953. ////////////////////////////////////////////////////////////////////
  954. // Function: NodePath::set_pos_hpr_scale
  955. // Access: Published
  956. // Description: Replaces the translation, rotation, and scale
  957. // components, implicitly setting shear to 0.
  958. ////////////////////////////////////////////////////////////////////
  959. void NodePath::
  960. set_pos_hpr_scale(const LVecBase3f &pos, const LVecBase3f &hpr,
  961. const LVecBase3f &scale) {
  962. nassertv_always(!is_empty());
  963. set_transform(TransformState::make_pos_hpr_scale
  964. (pos, hpr, scale));
  965. node()->reset_prev_transform();
  966. }
  967. ////////////////////////////////////////////////////////////////////
  968. // Function: NodePath::set_pos_quat_scale
  969. // Access: Published
  970. // Description: Replaces the translation, rotation, and scale
  971. // components, implicitly setting shear to 0.
  972. ////////////////////////////////////////////////////////////////////
  973. void NodePath::
  974. set_pos_quat_scale(const LVecBase3f &pos, const LQuaternionf &quat,
  975. const LVecBase3f &scale) {
  976. nassertv_always(!is_empty());
  977. set_transform(TransformState::make_pos_quat_scale
  978. (pos, quat, scale));
  979. node()->reset_prev_transform();
  980. }
  981. ////////////////////////////////////////////////////////////////////
  982. // Function: NodePath::set_pos_hpr_scale_shear
  983. // Access: Published
  984. // Description: Completely replaces the transform with new
  985. // translation, rotation, scale, and shear components.
  986. ////////////////////////////////////////////////////////////////////
  987. void NodePath::
  988. set_pos_hpr_scale_shear(const LVecBase3f &pos, const LVecBase3f &hpr,
  989. const LVecBase3f &scale, const LVecBase3f &shear) {
  990. nassertv_always(!is_empty());
  991. set_transform(TransformState::make_pos_hpr_scale_shear
  992. (pos, hpr, scale, shear));
  993. node()->reset_prev_transform();
  994. }
  995. ////////////////////////////////////////////////////////////////////
  996. // Function: NodePath::set_pos_quat_scale_shear
  997. // Access: Published
  998. // Description: Completely replaces the transform with new
  999. // translation, rotation, scale, and shear components.
  1000. ////////////////////////////////////////////////////////////////////
  1001. void NodePath::
  1002. set_pos_quat_scale_shear(const LVecBase3f &pos, const LQuaternionf &quat,
  1003. const LVecBase3f &scale, const LVecBase3f &shear) {
  1004. nassertv_always(!is_empty());
  1005. set_transform(TransformState::make_pos_quat_scale_shear
  1006. (pos, quat, scale, shear));
  1007. node()->reset_prev_transform();
  1008. }
  1009. ////////////////////////////////////////////////////////////////////
  1010. // Function: NodePath::set_mat
  1011. // Access: Published
  1012. // Description: Directly sets an arbitrary 4x4 transform matrix.
  1013. ////////////////////////////////////////////////////////////////////
  1014. void NodePath::
  1015. set_mat(const LMatrix4f &mat) {
  1016. nassertv_always(!is_empty());
  1017. set_transform(TransformState::make_mat(mat));
  1018. node()->reset_prev_transform();
  1019. }
  1020. ////////////////////////////////////////////////////////////////////
  1021. // Function: NodePath::look_at
  1022. // Access: Published
  1023. // Description: Sets the hpr on this NodePath so that it
  1024. // rotates to face the indicated point in space.
  1025. ////////////////////////////////////////////////////////////////////
  1026. void NodePath::
  1027. look_at(const LPoint3f &point, const LVector3f &up) {
  1028. nassertv_always(!is_empty());
  1029. LPoint3f pos = get_pos();
  1030. LQuaternionf quat;
  1031. ::look_at(quat, point - pos, up);
  1032. set_quat(quat);
  1033. }
  1034. ////////////////////////////////////////////////////////////////////
  1035. // Function: NodePath::heads_up
  1036. // Access: Published
  1037. // Description: Behaves like look_at(), but with a strong preference
  1038. // to keeping the up vector oriented in the indicated
  1039. // "up" direction.
  1040. ////////////////////////////////////////////////////////////////////
  1041. void NodePath::
  1042. heads_up(const LPoint3f &point, const LVector3f &up) {
  1043. nassertv_always(!is_empty());
  1044. LPoint3f pos = get_pos();
  1045. LQuaternionf quat;
  1046. ::heads_up(quat, point - pos, up);
  1047. set_quat(quat);
  1048. }
  1049. ////////////////////////////////////////////////////////////////////
  1050. // Function: NodePath::set_pos
  1051. // Access: Published
  1052. // Description: Sets the translation component of the transform,
  1053. // relative to the other node.
  1054. ////////////////////////////////////////////////////////////////////
  1055. void NodePath::
  1056. set_pos(const NodePath &other, const LVecBase3f &pos) {
  1057. nassertv_always(!is_empty());
  1058. CPT(TransformState) rel_transform = get_transform(other);
  1059. CPT(TransformState) orig_transform = get_transform();
  1060. if (orig_transform->has_components()) {
  1061. // If we had a componentwise transform before we started, we
  1062. // should be careful to preserve the other three components. We
  1063. // wouldn't need to do this, except for the possibility of
  1064. // numerical error or decompose ambiguity.
  1065. const LVecBase3f &orig_hpr = orig_transform->get_hpr();
  1066. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1067. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1068. set_transform(other, rel_transform->set_pos(pos));
  1069. set_pos_hpr_scale_shear(get_transform()->get_pos(), orig_hpr, orig_scale, orig_shear);
  1070. } else {
  1071. // If we didn't have a componentwise transform already, never
  1072. // mind.
  1073. set_transform(other, rel_transform->set_pos(pos));
  1074. node()->reset_prev_transform();
  1075. }
  1076. }
  1077. void NodePath::
  1078. set_x(const NodePath &other, float x) {
  1079. nassertv_always(!is_empty());
  1080. LPoint3f pos = get_pos(other);
  1081. pos[0] = x;
  1082. set_pos(other, pos);
  1083. }
  1084. void NodePath::
  1085. set_y(const NodePath &other, float y) {
  1086. nassertv_always(!is_empty());
  1087. LPoint3f pos = get_pos(other);
  1088. pos[1] = y;
  1089. set_pos(other, pos);
  1090. }
  1091. void NodePath::
  1092. set_z(const NodePath &other, float z) {
  1093. nassertv_always(!is_empty());
  1094. LPoint3f pos = get_pos(other);
  1095. pos[2] = z;
  1096. set_pos(other, pos);
  1097. }
  1098. ////////////////////////////////////////////////////////////////////
  1099. // Function: NodePath::get_pos
  1100. // Access: Published
  1101. // Description: Returns the relative position of the referenced node
  1102. // as seen from the other node.
  1103. ////////////////////////////////////////////////////////////////////
  1104. LPoint3f NodePath::
  1105. get_pos(const NodePath &other) const {
  1106. nassertr_always(!is_empty(), LPoint3f(0.0f, 0.0f, 0.0f));
  1107. return get_transform(other)->get_pos();
  1108. }
  1109. ////////////////////////////////////////////////////////////////////
  1110. // Function: NodePath::get_pos_delta
  1111. // Access: Published
  1112. // Description: Returns the delta vector from this node's position in
  1113. // the previous frame (according to
  1114. // set_prev_transform(), typically set via the use of
  1115. // set_fluid_pos()) and its position in the current
  1116. // frame, as seen in the indicated node's coordinate
  1117. // space. This is the vector used to determine
  1118. // collisions. Generally, if the node was last
  1119. // repositioned via set_pos(), the delta will be zero;
  1120. // if it was adjusted via set_fluid_pos(), the delta
  1121. // will represent the change from the previous frame's
  1122. // position.
  1123. ////////////////////////////////////////////////////////////////////
  1124. LVector3f NodePath::
  1125. get_pos_delta(const NodePath &other) const {
  1126. nassertr_always(!is_empty(), LPoint3f(0.0f, 0.0f, 0.0f));
  1127. return get_transform(other)->get_pos() - get_prev_transform(other)->get_pos();
  1128. }
  1129. ////////////////////////////////////////////////////////////////////
  1130. // Function: NodePath::set_hpr
  1131. // Access: Published
  1132. // Description: Sets the rotation component of the transform,
  1133. // relative to the other node.
  1134. ////////////////////////////////////////////////////////////////////
  1135. void NodePath::
  1136. set_hpr(const NodePath &other, const LVecBase3f &hpr) {
  1137. nassertv_always(!is_empty());
  1138. CPT(TransformState) rel_transform = get_transform(other);
  1139. nassertv(rel_transform->has_hpr());
  1140. CPT(TransformState) orig_transform = get_transform();
  1141. if (orig_transform->has_components()) {
  1142. // If we had a componentwise transform before we started, we
  1143. // should be careful to preserve the other three components. We
  1144. // wouldn't need to do this, except for the possibility of
  1145. // numerical error or decompose ambiguity.
  1146. const LVecBase3f &orig_pos = orig_transform->get_pos();
  1147. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1148. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1149. set_transform(other, rel_transform->set_hpr(hpr));
  1150. const TransformState *new_transform = get_transform();
  1151. if (new_transform->has_components()) {
  1152. set_transform(TransformState::make_pos_hpr_scale_shear
  1153. (orig_pos, new_transform->get_hpr(), orig_scale, orig_shear));
  1154. }
  1155. } else {
  1156. // If we didn't have a componentwise transform already, never
  1157. // mind.
  1158. set_transform(other, rel_transform->set_hpr(hpr));
  1159. }
  1160. }
  1161. void NodePath::
  1162. set_h(const NodePath &other, float h) {
  1163. nassertv_always(!is_empty());
  1164. LVecBase3f hpr = get_hpr(other);
  1165. hpr[0] = h;
  1166. set_hpr(other, hpr);
  1167. }
  1168. void NodePath::
  1169. set_p(const NodePath &other, float p) {
  1170. nassertv_always(!is_empty());
  1171. LVecBase3f hpr = get_hpr(other);
  1172. hpr[1] = p;
  1173. set_hpr(other, hpr);
  1174. }
  1175. void NodePath::
  1176. set_r(const NodePath &other, float r) {
  1177. nassertv_always(!is_empty());
  1178. LVecBase3f hpr = get_hpr(other);
  1179. hpr[2] = r;
  1180. set_hpr(other, hpr);
  1181. }
  1182. ////////////////////////////////////////////////////////////////////
  1183. // Function: NodePath::get_hpr
  1184. // Access: Published
  1185. // Description: Returns the relative orientation of the bottom node
  1186. // as seen from the other node.
  1187. ////////////////////////////////////////////////////////////////////
  1188. LVecBase3f NodePath::
  1189. get_hpr(const NodePath &other) const {
  1190. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  1191. CPT(TransformState) transform = get_transform(other);
  1192. nassertr(transform->has_hpr(), LVecBase3f(0.0f, 0.0f, 0.0f));
  1193. return transform->get_hpr();
  1194. }
  1195. ////////////////////////////////////////////////////////////////////
  1196. // Function: NodePath::set_quat
  1197. // Access: Published
  1198. // Description: Sets the rotation component of the transform,
  1199. // relative to the other node.
  1200. ////////////////////////////////////////////////////////////////////
  1201. void NodePath::
  1202. set_quat(const NodePath &other, const LQuaternionf &quat) {
  1203. nassertv_always(!is_empty());
  1204. CPT(TransformState) rel_transform = get_transform(other);
  1205. CPT(TransformState) orig_transform = get_transform();
  1206. if (orig_transform->has_components()) {
  1207. // If we had a componentwise transform before we started, we
  1208. // should be careful to preserve the other three components. We
  1209. // wouldn't need to do this, except for the possibility of
  1210. // numerical error or decompose ambiguity.
  1211. const LVecBase3f &orig_pos = orig_transform->get_pos();
  1212. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1213. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1214. set_transform(other, rel_transform->set_quat(quat));
  1215. const TransformState *new_transform = get_transform();
  1216. if (new_transform->has_components()) {
  1217. set_transform(TransformState::make_pos_quat_scale_shear
  1218. (orig_pos, new_transform->get_quat(), orig_scale, orig_shear));
  1219. }
  1220. } else {
  1221. // If we didn't have a componentwise transform already, never
  1222. // mind.
  1223. set_transform(other, rel_transform->set_quat(quat));
  1224. }
  1225. }
  1226. ////////////////////////////////////////////////////////////////////
  1227. // Function: NodePath::get_quat
  1228. // Access: Published
  1229. // Description: Returns the relative orientation of the bottom node
  1230. // as seen from the other node.
  1231. ////////////////////////////////////////////////////////////////////
  1232. LQuaternionf NodePath::
  1233. get_quat(const NodePath &other) const {
  1234. nassertr_always(!is_empty(), LQuaternionf::ident_quat());
  1235. CPT(TransformState) transform = get_transform(other);
  1236. return transform->get_quat();
  1237. }
  1238. ////////////////////////////////////////////////////////////////////
  1239. // Function: NodePath::set_scale
  1240. // Access: Published
  1241. // Description: Sets the scale component of the transform,
  1242. // relative to the other node.
  1243. ////////////////////////////////////////////////////////////////////
  1244. void NodePath::
  1245. set_scale(const NodePath &other, const LVecBase3f &scale) {
  1246. nassertv_always(!is_empty());
  1247. CPT(TransformState) rel_transform = get_transform(other);
  1248. CPT(TransformState) orig_transform = get_transform();
  1249. if (orig_transform->has_components()) {
  1250. // If we had a componentwise transform before we started, we
  1251. // should be careful to preserve the other three components. We
  1252. // wouldn't need to do this, except for the possibility of
  1253. // numerical error or decompose ambiguity.
  1254. const LVecBase3f &orig_pos = orig_transform->get_pos();
  1255. const LVecBase3f &orig_hpr = orig_transform->get_hpr();
  1256. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1257. set_transform(other, rel_transform->set_scale(scale));
  1258. const TransformState *new_transform = get_transform();
  1259. if (new_transform->has_components()) {
  1260. set_transform(TransformState::make_pos_hpr_scale_shear
  1261. (orig_pos, orig_hpr, new_transform->get_scale(), orig_shear));
  1262. }
  1263. } else {
  1264. // If we didn't have a componentwise transform already, never
  1265. // mind.
  1266. set_transform(other, rel_transform->set_scale(scale));
  1267. }
  1268. }
  1269. void NodePath::
  1270. set_sx(const NodePath &other, float sx) {
  1271. nassertv_always(!is_empty());
  1272. LVecBase3f scale = get_scale(other);
  1273. scale[0] = sx;
  1274. set_scale(other, scale);
  1275. }
  1276. void NodePath::
  1277. set_sy(const NodePath &other, float sy) {
  1278. nassertv_always(!is_empty());
  1279. LVecBase3f scale = get_scale(other);
  1280. scale[1] = sy;
  1281. set_scale(other, scale);
  1282. }
  1283. void NodePath::
  1284. set_sz(const NodePath &other, float sz) {
  1285. nassertv_always(!is_empty());
  1286. LVecBase3f scale = get_scale(other);
  1287. scale[2] = sz;
  1288. set_scale(other, scale);
  1289. }
  1290. ////////////////////////////////////////////////////////////////////
  1291. // Function: NodePath::get_scale
  1292. // Access: Published
  1293. // Description: Returns the relative scale of the bottom node
  1294. // as seen from the other node.
  1295. ////////////////////////////////////////////////////////////////////
  1296. LVecBase3f NodePath::
  1297. get_scale(const NodePath &other) const {
  1298. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  1299. CPT(TransformState) transform = get_transform(other);
  1300. return transform->get_scale();
  1301. }
  1302. ////////////////////////////////////////////////////////////////////
  1303. // Function: NodePath::set_shear
  1304. // Access: Published
  1305. // Description: Sets the shear component of the transform,
  1306. // relative to the other node.
  1307. ////////////////////////////////////////////////////////////////////
  1308. void NodePath::
  1309. set_shear(const NodePath &other, const LVecBase3f &shear) {
  1310. nassertv_always(!is_empty());
  1311. CPT(TransformState) rel_transform = get_transform(other);
  1312. CPT(TransformState) orig_transform = get_transform();
  1313. if (orig_transform->has_components()) {
  1314. // If we had a componentwise transform before we started, we
  1315. // should be careful to preserve the other three components. We
  1316. // wouldn't need to do this, except for the possibility of
  1317. // numerical error or decompose ambiguity.
  1318. const LVecBase3f &orig_pos = orig_transform->get_pos();
  1319. const LVecBase3f &orig_hpr = orig_transform->get_hpr();
  1320. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1321. set_transform(other, rel_transform->set_shear(shear));
  1322. const TransformState *new_transform = get_transform();
  1323. if (new_transform->has_components()) {
  1324. set_transform(TransformState::make_pos_hpr_scale_shear
  1325. (orig_pos, orig_hpr, orig_scale, new_transform->get_shear()));
  1326. }
  1327. } else {
  1328. // If we didn't have a componentwise transform already, never
  1329. // mind.
  1330. set_transform(other, rel_transform->set_shear(shear));
  1331. }
  1332. }
  1333. void NodePath::
  1334. set_shxy(const NodePath &other, float shxy) {
  1335. nassertv_always(!is_empty());
  1336. LVecBase3f shear = get_shear(other);
  1337. shear[0] = shxy;
  1338. set_shear(other, shear);
  1339. }
  1340. void NodePath::
  1341. set_shxz(const NodePath &other, float shxz) {
  1342. nassertv_always(!is_empty());
  1343. LVecBase3f shear = get_shear(other);
  1344. shear[1] = shxz;
  1345. set_shear(other, shear);
  1346. }
  1347. void NodePath::
  1348. set_shyz(const NodePath &other, float shyz) {
  1349. nassertv_always(!is_empty());
  1350. LVecBase3f shear = get_shear(other);
  1351. shear[2] = shyz;
  1352. set_shear(other, shear);
  1353. }
  1354. ////////////////////////////////////////////////////////////////////
  1355. // Function: NodePath::get_shear
  1356. // Access: Published
  1357. // Description: Returns the relative shear of the bottom node
  1358. // as seen from the other node.
  1359. ////////////////////////////////////////////////////////////////////
  1360. LVecBase3f NodePath::
  1361. get_shear(const NodePath &other) const {
  1362. nassertr_always(!is_empty(), LVecBase3f(0.0f, 0.0f, 0.0f));
  1363. CPT(TransformState) transform = get_transform(other);
  1364. return transform->get_shear();
  1365. }
  1366. ////////////////////////////////////////////////////////////////////
  1367. // Function: NodePath::set_pos_hpr
  1368. // Access: Published
  1369. // Description: Sets the translation and rotation component of the
  1370. // transform, relative to the other node.
  1371. ////////////////////////////////////////////////////////////////////
  1372. void NodePath::
  1373. set_pos_hpr(const NodePath &other, const LVecBase3f &pos,
  1374. const LVecBase3f &hpr) {
  1375. nassertv_always(!is_empty());
  1376. CPT(TransformState) rel_transform = get_transform(other);
  1377. CPT(TransformState) orig_transform = get_transform();
  1378. if (orig_transform->has_components()) {
  1379. // If we had a componentwise transform before we started, we
  1380. // should be careful to preserve the other two components. We
  1381. // wouldn't need to do this, except for the possibility of
  1382. // numerical error or decompose ambiguity.
  1383. const LVecBase3f &orig_scale = orig_transform->get_scale();
  1384. const LVecBase3f &orig_shear = orig_transform->get_shear();
  1385. set_transform(other, TransformState::make_pos_hpr_scale_shear
  1386. (pos, hpr, rel_transform->get_scale(), rel_transform->get_shear()));
  1387. const TransformState *new_transform = get_transform();
  1388. if (new_transform->has_components()) {
  1389. set_pos_hpr_scale_shear(new_transform->get_pos(), new_transform->get_hpr(),
  1390. orig_scale, orig_shear);
  1391. }
  1392. } else {
  1393. // If we didn't have a componentwise transform already, never
  1394. // mind.
  1395. set_transform(other, TransformState::make_pos_hpr_scale_shear
  1396. (pos, hpr, rel_transform->get_scale(), rel_transform->get_shear()));
  1397. node()->reset_prev_transform();
  1398. }
  1399. }
  1400. ////////////////////////////////////////////////////////////////////
  1401. // Function: NodePath::set_hpr_scale
  1402. // Access: Published
  1403. // Description: Sets the rotation and scale components of the
  1404. // transform, leaving translation untouched. This, or
  1405. // set_pos_hpr_scale, is the preferred way to update a
  1406. // transform when both hpr and scale are to be changed.
  1407. ////////////////////////////////////////////////////////////////////
  1408. void NodePath::
  1409. set_hpr_scale(const NodePath &other, const LVecBase3f &hpr, const LVecBase3f &scale) {
  1410. // We don't bother trying very hard to preserve pos across this
  1411. // operation, unlike the work we do above to preserve hpr or scale,
  1412. // since it generally doesn't matter that much if pos is off by a
  1413. // few thousandths.
  1414. nassertv_always(!is_empty());
  1415. CPT(TransformState) transform = get_transform(other);
  1416. transform = TransformState::make_pos_hpr_scale_shear
  1417. (transform->get_pos(), hpr, scale, transform->get_shear());
  1418. set_transform(other, transform);
  1419. }
  1420. ////////////////////////////////////////////////////////////////////
  1421. // Function: NodePath::set_pos_hpr_scale
  1422. // Access: Published
  1423. // Description: Completely replaces the transform with new
  1424. // translation, rotation, and scale components, relative
  1425. // to the other node, implicitly setting shear to 0.
  1426. ////////////////////////////////////////////////////////////////////
  1427. void NodePath::
  1428. set_pos_hpr_scale(const NodePath &other,
  1429. const LVecBase3f &pos, const LVecBase3f &hpr,
  1430. const LVecBase3f &scale) {
  1431. nassertv_always(!is_empty());
  1432. set_transform(other, TransformState::make_pos_hpr_scale
  1433. (pos, hpr, scale));
  1434. node()->reset_prev_transform();
  1435. }
  1436. ////////////////////////////////////////////////////////////////////
  1437. // Function: NodePath::set_pos_quat_scale
  1438. // Access: Published
  1439. // Description: Completely replaces the transform with new
  1440. // translation, rotation, and scale components, relative
  1441. // to the other node, implicitly setting shear to 0.
  1442. ////////////////////////////////////////////////////////////////////
  1443. void NodePath::
  1444. set_pos_quat_scale(const NodePath &other,
  1445. const LVecBase3f &pos, const LQuaternionf &quat,
  1446. const LVecBase3f &scale) {
  1447. nassertv_always(!is_empty());
  1448. set_transform(other, TransformState::make_pos_quat_scale
  1449. (pos, quat, scale));
  1450. node()->reset_prev_transform();
  1451. }
  1452. ////////////////////////////////////////////////////////////////////
  1453. // Function: NodePath::set_pos_hpr_scale_shear
  1454. // Access: Published
  1455. // Description: Completely replaces the transform with new
  1456. // translation, rotation, scale, and shear components,
  1457. // relative to the other node.
  1458. ////////////////////////////////////////////////////////////////////
  1459. void NodePath::
  1460. set_pos_hpr_scale_shear(const NodePath &other,
  1461. const LVecBase3f &pos, const LVecBase3f &hpr,
  1462. const LVecBase3f &scale, const LVecBase3f &shear) {
  1463. nassertv_always(!is_empty());
  1464. set_transform(other, TransformState::make_pos_hpr_scale_shear
  1465. (pos, hpr, scale, shear));
  1466. node()->reset_prev_transform();
  1467. }
  1468. ////////////////////////////////////////////////////////////////////
  1469. // Function: NodePath::set_pos_quat_scale_shear
  1470. // Access: Published
  1471. // Description: Completely replaces the transform with new
  1472. // translation, rotation, scale, and shear components,
  1473. // relative to the other node.
  1474. ////////////////////////////////////////////////////////////////////
  1475. void NodePath::
  1476. set_pos_quat_scale_shear(const NodePath &other,
  1477. const LVecBase3f &pos, const LQuaternionf &quat,
  1478. const LVecBase3f &scale, const LVecBase3f &shear) {
  1479. nassertv_always(!is_empty());
  1480. set_transform(other, TransformState::make_pos_quat_scale_shear
  1481. (pos, quat, scale, shear));
  1482. node()->reset_prev_transform();
  1483. }
  1484. ////////////////////////////////////////////////////////////////////
  1485. // Function: NodePath::get_mat
  1486. // Access: Published
  1487. // Description: Returns the matrix that describes the coordinate
  1488. // space of the bottom node, relative to the other
  1489. // path's bottom node's coordinate space.
  1490. ////////////////////////////////////////////////////////////////////
  1491. const LMatrix4f &NodePath::
  1492. get_mat(const NodePath &other) const {
  1493. CPT(TransformState) transform = get_transform(other);
  1494. // We can safely assume the transform won't go away when the
  1495. // function returns, since its reference count is also held in the
  1496. // cache. This assumption allows us to return a reference to the
  1497. // matrix, instead of having to return a matrix on the stack.
  1498. nassertr(transform->get_ref_count() > 1, LMatrix4f::ident_mat());
  1499. return transform->get_mat();
  1500. }
  1501. ////////////////////////////////////////////////////////////////////
  1502. // Function: NodePath::set_mat
  1503. // Access: Published
  1504. // Description: Converts the indicated matrix from the other's
  1505. // coordinate space to the local coordinate space, and
  1506. // applies it to the node.
  1507. ////////////////////////////////////////////////////////////////////
  1508. void NodePath::
  1509. set_mat(const NodePath &other, const LMatrix4f &mat) {
  1510. nassertv_always(!is_empty());
  1511. set_transform(other, TransformState::make_mat(mat));
  1512. node()->reset_prev_transform();
  1513. }
  1514. ////////////////////////////////////////////////////////////////////
  1515. // Function: NodePath::get_relative_point
  1516. // Access: Published
  1517. // Description: Given that the indicated point is in the coordinate
  1518. // system of the other node, returns the same point in
  1519. // this node's coordinate system.
  1520. ////////////////////////////////////////////////////////////////////
  1521. LPoint3f NodePath::
  1522. get_relative_point(const NodePath &other, const LVecBase3f &point) {
  1523. LPoint3f rel_point = LPoint3f(point) * other.get_mat(*this);
  1524. return rel_point;
  1525. }
  1526. ////////////////////////////////////////////////////////////////////
  1527. // Function: NodePath::get_relative_vector
  1528. // Access: Published
  1529. // Description: Given that the indicated vector is in the coordinate
  1530. // system of the other node, returns the same vector in
  1531. // this node's coordinate system.
  1532. ////////////////////////////////////////////////////////////////////
  1533. LVector3f NodePath::
  1534. get_relative_vector(const NodePath &other, const LVecBase3f &vec) {
  1535. LVector3f rel_vector = LVector3f(vec) * other.get_mat(*this);
  1536. return rel_vector;
  1537. }
  1538. ////////////////////////////////////////////////////////////////////
  1539. // Function: NodePath::look_at
  1540. // Access: Published
  1541. // Description: Sets the transform on this NodePath so that it
  1542. // rotates to face the indicated point in space, which
  1543. // is relative to the other NodePath.
  1544. ////////////////////////////////////////////////////////////////////
  1545. void NodePath::
  1546. look_at(const NodePath &other, const LPoint3f &point, const LVector3f &up) {
  1547. nassertv_always(!is_empty());
  1548. NodePath parent = get_parent();
  1549. LPoint3f rel_point = point * other.get_mat(parent);
  1550. LPoint3f pos = get_pos();
  1551. LQuaternionf quat;
  1552. ::look_at(quat, rel_point - pos, up);
  1553. set_quat(quat);
  1554. }
  1555. ////////////////////////////////////////////////////////////////////
  1556. // Function: NodePath::heads_up
  1557. // Access: Published
  1558. // Description: Behaves like look_at(), but with a strong preference
  1559. // to keeping the up vector oriented in the indicated
  1560. // "up" direction.
  1561. ////////////////////////////////////////////////////////////////////
  1562. void NodePath::
  1563. heads_up(const NodePath &other, const LPoint3f &point, const LVector3f &up) {
  1564. nassertv_always(!is_empty());
  1565. NodePath parent = get_parent();
  1566. LPoint3f rel_point = point * other.get_mat(parent);
  1567. LPoint3f pos = get_pos();
  1568. LQuaternionf quat;
  1569. ::heads_up(quat, rel_point - pos, up);
  1570. set_quat(quat);
  1571. }
  1572. ////////////////////////////////////////////////////////////////////
  1573. // Function: NodePath::set_color
  1574. // Access: Published
  1575. // Description: Applies a scene-graph color to the referenced node.
  1576. // This color will apply to all geometry at this level
  1577. // and below (that does not specify a new color or a
  1578. // set_color_off()).
  1579. ////////////////////////////////////////////////////////////////////
  1580. void NodePath::
  1581. set_color(float r, float g, float b, float a,
  1582. int priority) {
  1583. set_color(Colorf(r, g, b, a), priority);
  1584. }
  1585. ////////////////////////////////////////////////////////////////////
  1586. // Function: NodePath::set_color
  1587. // Access: Published
  1588. // Description: Applies a scene-graph color to the referenced node.
  1589. // This color will apply to all geometry at this level
  1590. // and below (that does not specify a new color or a
  1591. // set_color_off()).
  1592. ////////////////////////////////////////////////////////////////////
  1593. void NodePath::
  1594. set_color(const Colorf &color, int priority) {
  1595. nassertv_always(!is_empty());
  1596. node()->set_attrib(ColorAttrib::make_flat(color), priority);
  1597. }
  1598. ////////////////////////////////////////////////////////////////////
  1599. // Function: NodePath::set_color_off
  1600. // Access: Published
  1601. // Description: Sets the geometry at this level and below to render
  1602. // using the geometry color. This is normally the
  1603. // default, but it may be useful to use this to
  1604. // contradict set_color() at a higher node level (or,
  1605. // with a priority, to override a set_color() at a lower
  1606. // level).
  1607. ////////////////////////////////////////////////////////////////////
  1608. void NodePath::
  1609. set_color_off(int priority) {
  1610. nassertv_always(!is_empty());
  1611. node()->set_attrib(ColorAttrib::make_vertex(), priority);
  1612. }
  1613. ////////////////////////////////////////////////////////////////////
  1614. // Function: NodePath::clear_color
  1615. // Access: Published
  1616. // Description: Completely removes any color adjustment from the node.
  1617. // This allows the natural color of the geometry, or
  1618. // whatever color transitions might be otherwise
  1619. // affecting the geometry, to show instead.
  1620. ////////////////////////////////////////////////////////////////////
  1621. void NodePath::
  1622. clear_color() {
  1623. nassertv_always(!is_empty());
  1624. node()->clear_attrib(ColorAttrib::get_class_type());
  1625. }
  1626. ////////////////////////////////////////////////////////////////////
  1627. // Function: NodePath::has_color
  1628. // Access: Published
  1629. // Description: Returns true if a color has been applied to the given
  1630. // node, false otherwise.
  1631. ////////////////////////////////////////////////////////////////////
  1632. bool NodePath::
  1633. has_color() const {
  1634. nassertr_always(!is_empty(), false);
  1635. return node()->has_attrib(ColorAttrib::get_class_type());
  1636. }
  1637. ////////////////////////////////////////////////////////////////////
  1638. // Function: NodePath::get_color
  1639. // Access: Published
  1640. // Description: Returns the color that has been assigned to the node,
  1641. // or black if no color has been assigned.
  1642. ////////////////////////////////////////////////////////////////////
  1643. Colorf NodePath::
  1644. get_color() const {
  1645. nassertr_always(!is_empty(), false);
  1646. const RenderAttrib *attrib =
  1647. node()->get_attrib(ColorAttrib::get_class_type());
  1648. if (attrib != (const RenderAttrib *)NULL) {
  1649. const ColorAttrib *ca = DCAST(ColorAttrib, attrib);
  1650. if (ca->get_color_type() == ColorAttrib::T_flat) {
  1651. return ca->get_color();
  1652. }
  1653. }
  1654. pgraph_cat.warning()
  1655. << "get_color() called on " << *this << " which has no color set.\n";
  1656. return Colorf(1.0f, 1.0f, 1.0f, 1.0f);
  1657. }
  1658. ////////////////////////////////////////////////////////////////////
  1659. // Function: NodePath::has_color_scale
  1660. // Access: Published
  1661. // Description: Returns true if a color scale has been applied
  1662. // to the referenced node, false otherwise. It is still
  1663. // possible that color at this node might have been
  1664. // scaled by an ancestor node.
  1665. ////////////////////////////////////////////////////////////////////
  1666. bool NodePath::
  1667. has_color_scale() const {
  1668. nassertr_always(!is_empty(), false);
  1669. return node()->has_attrib(ColorScaleAttrib::get_class_type());
  1670. }
  1671. ////////////////////////////////////////////////////////////////////
  1672. // Function: NodePath::clear_color_scale
  1673. // Access: Published
  1674. // Description: Completely removes any color scale from the
  1675. // referenced node. This is preferable to simply
  1676. // setting the color scale to identity, as it also
  1677. // removes the overhead associated with having a color
  1678. // scale at all.
  1679. ////////////////////////////////////////////////////////////////////
  1680. void NodePath::
  1681. clear_color_scale() {
  1682. nassertv_always(!is_empty());
  1683. node()->clear_attrib(ColorScaleAttrib::get_class_type());
  1684. }
  1685. ////////////////////////////////////////////////////////////////////
  1686. // Function: NodePath::set_color_scale
  1687. // Access: Published
  1688. // Description: Sets the color scale component of the transform,
  1689. // leaving translation and rotation untouched.
  1690. ////////////////////////////////////////////////////////////////////
  1691. void NodePath::
  1692. set_color_scale(const LVecBase4f &scale, int priority) {
  1693. nassertv_always(!is_empty());
  1694. node()->set_attrib(ColorScaleAttrib::make(scale), priority);
  1695. }
  1696. ////////////////////////////////////////////////////////////////////
  1697. // Function: NodePath::set_alpha_scale
  1698. // Access: Published
  1699. // Description: Sets the alpha scale component of the transform
  1700. // without affecting the color scale. Note that any
  1701. // priority specified will also apply to the color
  1702. // scale.
  1703. ////////////////////////////////////////////////////////////////////
  1704. void NodePath::
  1705. set_alpha_scale(float scale, int priority) {
  1706. nassertv_always(!is_empty());
  1707. const RenderAttrib *attrib =
  1708. node()->get_attrib(ColorScaleAttrib::get_class_type());
  1709. if (attrib != (const RenderAttrib *)NULL) {
  1710. const ColorScaleAttrib *csa = DCAST(ColorScaleAttrib, attrib);
  1711. const LVecBase4f &sc = csa->get_scale();
  1712. set_color_scale(sc[0], sc[1], sc[2], scale, priority);
  1713. } else {
  1714. set_color_scale(1.0f, 1.0f, 1.0f, scale, priority);
  1715. }
  1716. }
  1717. ////////////////////////////////////////////////////////////////////
  1718. // Function: NodePath::set_all_color_scale
  1719. // Access: Published
  1720. // Description: Scales all the color components of the object by the
  1721. // same amount, darkening the object, without affecting
  1722. // alpha. Note that any priority specified will also
  1723. // apply to the alpha scale.
  1724. ////////////////////////////////////////////////////////////////////
  1725. void NodePath::
  1726. set_all_color_scale(float scale, int priority) {
  1727. nassertv_always(!is_empty());
  1728. const RenderAttrib *attrib =
  1729. node()->get_attrib(ColorScaleAttrib::get_class_type());
  1730. if (attrib != (const RenderAttrib *)NULL) {
  1731. const ColorScaleAttrib *csa = DCAST(ColorScaleAttrib, attrib);
  1732. const LVecBase4f &sc = csa->get_scale();
  1733. set_color_scale(scale, scale, scale, sc[3], priority);
  1734. } else {
  1735. set_color_scale(scale, scale, scale, 1.0f, priority);
  1736. }
  1737. }
  1738. ////////////////////////////////////////////////////////////////////
  1739. // Function: NodePath::get_color_scale
  1740. // Access: Published
  1741. // Description: Returns the complete color scale vector that has been
  1742. // applied to the bottom node, or all 1's (identity) if
  1743. // no scale has been applied.
  1744. ////////////////////////////////////////////////////////////////////
  1745. const LVecBase4f &NodePath::
  1746. get_color_scale() const {
  1747. static const LVecBase4f ident_scale(1.0f, 1.0f, 1.0f, 1.0f);
  1748. nassertr_always(!is_empty(), ident_scale);
  1749. const RenderAttrib *attrib =
  1750. node()->get_attrib(ColorScaleAttrib::get_class_type());
  1751. if (attrib != (const RenderAttrib *)NULL) {
  1752. const ColorScaleAttrib *csa = DCAST(ColorScaleAttrib, attrib);
  1753. return csa->get_scale();
  1754. }
  1755. return ident_scale;
  1756. }
  1757. ////////////////////////////////////////////////////////////////////
  1758. // Function: NodePath::set_bin
  1759. // Access: Published
  1760. // Description: Assigns the geometry at this level and below to the
  1761. // named rendering bin. It is the user's responsibility
  1762. // to ensure that such a bin already exists, either via
  1763. // the cull-bin Configrc variable, or by explicitly
  1764. // creating a GeomBin of the appropriate type at
  1765. // runtime.
  1766. //
  1767. // There are two default bins created when Panda is
  1768. // started: "default" and "fixed". Normally, all
  1769. // geometry is assigned to "default" unless specified
  1770. // otherwise. This bin renders opaque geometry in
  1771. // state-sorted order, followed by transparent geometry
  1772. // sorted back-to-front. If any geometry is assigned to
  1773. // "fixed", this will be rendered following all the
  1774. // geometry in "default", in the order specified by
  1775. // draw_order for each piece of geometry so assigned.
  1776. //
  1777. // The draw_order parameter is meaningful only for
  1778. // GeomBinFixed type bins, e.g. "fixed". Other kinds of
  1779. // bins ignore it.
  1780. ////////////////////////////////////////////////////////////////////
  1781. void NodePath::
  1782. set_bin(const string &bin_name, int draw_order, int priority) {
  1783. nassertv_always(!is_empty());
  1784. node()->set_attrib(CullBinAttrib::make(bin_name, draw_order), priority);
  1785. }
  1786. ////////////////////////////////////////////////////////////////////
  1787. // Function: NodePath::clear_bin
  1788. // Access: Published
  1789. // Description: Completely removes any bin adjustment that may have
  1790. // been set via set_bin() from this particular node.
  1791. ////////////////////////////////////////////////////////////////////
  1792. void NodePath::
  1793. clear_bin() {
  1794. nassertv_always(!is_empty());
  1795. node()->clear_attrib(CullBinAttrib::get_class_type());
  1796. }
  1797. ////////////////////////////////////////////////////////////////////
  1798. // Function: NodePath::has_bin
  1799. // Access: Published
  1800. // Description: Returns true if the node has been assigned to the a
  1801. // particular rendering bin via set_bin(), false
  1802. // otherwise.
  1803. ////////////////////////////////////////////////////////////////////
  1804. bool NodePath::
  1805. has_bin() const {
  1806. nassertr_always(!is_empty(), false);
  1807. return node()->has_attrib(CullBinAttrib::get_class_type());
  1808. }
  1809. ////////////////////////////////////////////////////////////////////
  1810. // Function: NodePath::get_bin_name
  1811. // Access: Published
  1812. // Description: Returns the name of the bin that this particular node
  1813. // was assigned to via set_bin(), or the empty string if
  1814. // no bin was assigned. See set_bin() and has_bin().
  1815. ////////////////////////////////////////////////////////////////////
  1816. string NodePath::
  1817. get_bin_name() const {
  1818. nassertr_always(!is_empty(), string());
  1819. const RenderAttrib *attrib =
  1820. node()->get_attrib(CullBinAttrib::get_class_type());
  1821. if (attrib != (const RenderAttrib *)NULL) {
  1822. const CullBinAttrib *ba = DCAST(CullBinAttrib, attrib);
  1823. return ba->get_bin_name();
  1824. }
  1825. return string();
  1826. }
  1827. ////////////////////////////////////////////////////////////////////
  1828. // Function: NodePath::get_bin_draw_order
  1829. // Access: Published
  1830. // Description: Returns the drawing order associated with the bin
  1831. // that this particular node was assigned to via
  1832. // set_bin(), or 0 if no bin was assigned. See
  1833. // set_bin() and has_bin().
  1834. ////////////////////////////////////////////////////////////////////
  1835. int NodePath::
  1836. get_bin_draw_order() const {
  1837. nassertr_always(!is_empty(), false);
  1838. const RenderAttrib *attrib =
  1839. node()->get_attrib(CullBinAttrib::get_class_type());
  1840. if (attrib != (const RenderAttrib *)NULL) {
  1841. const CullBinAttrib *ba = DCAST(CullBinAttrib, attrib);
  1842. return ba->get_draw_order();
  1843. }
  1844. return 0;
  1845. }
  1846. ////////////////////////////////////////////////////////////////////
  1847. // Function: NodePath::set_texture
  1848. // Access: Published
  1849. // Description: Sets the geometry at this level and below to render
  1850. // using the indicated texture.
  1851. ////////////////////////////////////////////////////////////////////
  1852. void NodePath::
  1853. set_texture(Texture *tex, int priority) {
  1854. nassertv_always(!is_empty());
  1855. node()->set_attrib(TextureAttrib::make(tex), priority);
  1856. }
  1857. ////////////////////////////////////////////////////////////////////
  1858. // Function: NodePath::set_texture_off
  1859. // Access: Published
  1860. // Description: Sets the geometry at this level and below to render
  1861. // using no texture. This is normally the default, but
  1862. // it may be useful to use this to contradict
  1863. // set_texture() at a higher node level (or, with a
  1864. // priority, to override a set_texture() at a lower
  1865. // level).
  1866. ////////////////////////////////////////////////////////////////////
  1867. void NodePath::
  1868. set_texture_off(int priority) {
  1869. nassertv_always(!is_empty());
  1870. node()->set_attrib(TextureAttrib::make_off(), priority);
  1871. }
  1872. ////////////////////////////////////////////////////////////////////
  1873. // Function: NodePath::clear_texture
  1874. // Access: Published
  1875. // Description: Completely removes any texture adjustment that may
  1876. // have been set via set_texture() or set_texture_off()
  1877. // from this particular node. This allows whatever
  1878. // textures might be otherwise affecting the geometry to
  1879. // show instead.
  1880. ////////////////////////////////////////////////////////////////////
  1881. void NodePath::
  1882. clear_texture() {
  1883. nassertv_always(!is_empty());
  1884. node()->clear_attrib(TextureAttrib::get_class_type());
  1885. }
  1886. ////////////////////////////////////////////////////////////////////
  1887. // Function: NodePath::has_texture
  1888. // Access: Published
  1889. // Description: Returns true if a texture has been applied to this
  1890. // particular node via set_texture(), false otherwise.
  1891. // This is not the same thing as asking whether the
  1892. // geometry at this node will be rendered with
  1893. // texturing, as there may be a texture in effect from a
  1894. // higher or lower level.
  1895. ////////////////////////////////////////////////////////////////////
  1896. bool NodePath::
  1897. has_texture() const {
  1898. nassertr_always(!is_empty(), false);
  1899. const RenderAttrib *attrib =
  1900. node()->get_attrib(TextureAttrib::get_class_type());
  1901. if (attrib != (const RenderAttrib *)NULL) {
  1902. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  1903. return !ta->is_off();
  1904. }
  1905. return false;
  1906. }
  1907. ////////////////////////////////////////////////////////////////////
  1908. // Function: NodePath::has_texture_off
  1909. // Access: Published
  1910. // Description: Returns true if a texture has been specifically
  1911. // disabled on this particular node via
  1912. // set_texture_off(), false otherwise. This is not the
  1913. // same thing as asking whether the geometry at this
  1914. // node will be rendered untextured, as there may be a
  1915. // texture in effect from a higher or lower level.
  1916. ////////////////////////////////////////////////////////////////////
  1917. bool NodePath::
  1918. has_texture_off() const {
  1919. nassertr_always(!is_empty(), false);
  1920. const RenderAttrib *attrib =
  1921. node()->get_attrib(ColorAttrib::get_class_type());
  1922. if (attrib != (const RenderAttrib *)NULL) {
  1923. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  1924. return ta->is_off();
  1925. }
  1926. return false;
  1927. }
  1928. ////////////////////////////////////////////////////////////////////
  1929. // Function: NodePath::get_texture
  1930. // Access: Published
  1931. // Description: Returns the texture that has been set on this
  1932. // particular node, or NULL if no texture has been set.
  1933. // This is not necessarily the texture that will be
  1934. // applied to the geometry at or below this level, as
  1935. // another texture at a higher or lower level may
  1936. // override.
  1937. //
  1938. // See also find_texture().
  1939. ////////////////////////////////////////////////////////////////////
  1940. Texture *NodePath::
  1941. get_texture() const {
  1942. nassertr_always(!is_empty(), NULL);
  1943. const RenderAttrib *attrib =
  1944. node()->get_attrib(TextureAttrib::get_class_type());
  1945. if (attrib != (const RenderAttrib *)NULL) {
  1946. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  1947. return ta->get_texture();
  1948. }
  1949. return NULL;
  1950. }
  1951. ////////////////////////////////////////////////////////////////////
  1952. // Function: NodePath::find_texture
  1953. // Access: Published
  1954. // Description: Returns the first texture found applied to geometry
  1955. // at this node or below that matches the indicated name
  1956. // (which may contain wildcards). Returns the texture
  1957. // if it is found, or NULL if it is not.
  1958. ////////////////////////////////////////////////////////////////////
  1959. Texture *NodePath::
  1960. find_texture(const string &name) const {
  1961. GlobPattern glob(name);
  1962. return r_find_texture(node(), get_net_state(), glob);
  1963. }
  1964. ////////////////////////////////////////////////////////////////////
  1965. // Function: NodePath::find_all_textures
  1966. // Access: Published
  1967. // Description: Returns a list of a textures applied to geometry at
  1968. // this node and below.
  1969. ////////////////////////////////////////////////////////////////////
  1970. TextureCollection NodePath::
  1971. find_all_textures() const {
  1972. Textures textures;
  1973. r_find_all_textures(node(), get_net_state(), textures);
  1974. TextureCollection tc;
  1975. Textures::iterator ti;
  1976. for (ti = textures.begin(); ti != textures.end(); ++ti) {
  1977. tc.add_texture(*ti);
  1978. }
  1979. return tc;
  1980. }
  1981. ////////////////////////////////////////////////////////////////////
  1982. // Function: NodePath::find_all_textures
  1983. // Access: Published
  1984. // Description: Returns a list of a textures applied to geometry at
  1985. // this node and below that match the indicated name
  1986. // (which may contain wildcard characters).
  1987. ////////////////////////////////////////////////////////////////////
  1988. TextureCollection NodePath::
  1989. find_all_textures(const string &name) const {
  1990. Textures textures;
  1991. r_find_all_textures(node(), get_net_state(), textures);
  1992. GlobPattern glob(name);
  1993. TextureCollection tc;
  1994. Textures::iterator ti;
  1995. for (ti = textures.begin(); ti != textures.end(); ++ti) {
  1996. Texture *texture = (*ti);
  1997. if (glob.matches(texture->get_name())) {
  1998. tc.add_texture(texture);
  1999. }
  2000. }
  2001. return tc;
  2002. }
  2003. ////////////////////////////////////////////////////////////////////
  2004. // Function: NodePath::set_material
  2005. // Access: Published
  2006. // Description: Sets the geometry at this level and below to render
  2007. // using the indicated material.
  2008. //
  2009. // This operation copies the given material pointer. If
  2010. // the material structure is changed later, it must be
  2011. // reapplied via another call to set_material().
  2012. ////////////////////////////////////////////////////////////////////
  2013. void NodePath::
  2014. set_material(Material *mat, int priority) {
  2015. nassertv_always(!is_empty());
  2016. nassertv(mat != NULL);
  2017. // We create a temporary Material pointer, a copy of the one we are
  2018. // given, to allow the user to monkey with the material and set it
  2019. // again later, with the desired effect. If we stored the user's
  2020. // pointer directly, it would be bad if the user later modified the
  2021. // values within the Material.
  2022. PT(Material) temp = new Material(*mat);
  2023. const Material *mp = MaterialPool::get_material(temp);
  2024. node()->set_attrib(MaterialAttrib::make(mp), priority);
  2025. }
  2026. ////////////////////////////////////////////////////////////////////
  2027. // Function: NodePath::set_material_off
  2028. // Access: Published
  2029. // Description: Sets the geometry at this level and below to render
  2030. // using no material. This is normally the default, but
  2031. // it may be useful to use this to contradict
  2032. // set_material() at a higher node level (or, with a
  2033. // priority, to override a set_material() at a lower
  2034. // level).
  2035. ////////////////////////////////////////////////////////////////////
  2036. void NodePath::
  2037. set_material_off(int priority) {
  2038. nassertv_always(!is_empty());
  2039. node()->set_attrib(MaterialAttrib::make_off(), priority);
  2040. }
  2041. ////////////////////////////////////////////////////////////////////
  2042. // Function: NodePath::clear_material
  2043. // Access: Published
  2044. // Description: Completely removes any material adjustment that may
  2045. // have been set via set_material() from this particular
  2046. // node.
  2047. ////////////////////////////////////////////////////////////////////
  2048. void NodePath::
  2049. clear_material() {
  2050. nassertv_always(!is_empty());
  2051. node()->clear_attrib(MaterialAttrib::get_class_type());
  2052. }
  2053. ////////////////////////////////////////////////////////////////////
  2054. // Function: NodePath::has_material
  2055. // Access: Published
  2056. // Description: Returns true if a material has been applied to this
  2057. // particular node via set_material(), false otherwise.
  2058. ////////////////////////////////////////////////////////////////////
  2059. bool NodePath::
  2060. has_material() const {
  2061. nassertr_always(!is_empty(), false);
  2062. const RenderAttrib *attrib =
  2063. node()->get_attrib(MaterialAttrib::get_class_type());
  2064. if (attrib != (const RenderAttrib *)NULL) {
  2065. const MaterialAttrib *ma = DCAST(MaterialAttrib, attrib);
  2066. return !ma->is_off();
  2067. }
  2068. return false;
  2069. }
  2070. ////////////////////////////////////////////////////////////////////
  2071. // Function: NodePath::get_material
  2072. // Access: Published
  2073. // Description: Returns the material that has been set on this
  2074. // particular node, or NULL if no material has been set.
  2075. // This is not necessarily the material that will be
  2076. // applied to the geometry at or below this level, as
  2077. // another material at a higher or lower level may
  2078. // override.
  2079. //
  2080. // This function returns a copy of the given material,
  2081. // to allow changes, if desired. Once changes are made,
  2082. // they should be reapplied via set_material().
  2083. ////////////////////////////////////////////////////////////////////
  2084. PT(Material) NodePath::
  2085. get_material() const {
  2086. nassertr_always(!is_empty(), NULL);
  2087. const RenderAttrib *attrib =
  2088. node()->get_attrib(MaterialAttrib::get_class_type());
  2089. if (attrib != (const RenderAttrib *)NULL) {
  2090. const MaterialAttrib *ma = DCAST(MaterialAttrib, attrib);
  2091. return new Material(*ma->get_material());
  2092. }
  2093. return NULL;
  2094. }
  2095. ////////////////////////////////////////////////////////////////////
  2096. // Function: NodePath::set_fog
  2097. // Access: Published
  2098. // Description: Sets the geometry at this level and below to render
  2099. // using the indicated fog.
  2100. ////////////////////////////////////////////////////////////////////
  2101. void NodePath::
  2102. set_fog(Fog *fog, int priority) {
  2103. nassertv_always(!is_empty());
  2104. node()->set_attrib(FogAttrib::make(fog), priority);
  2105. }
  2106. ////////////////////////////////////////////////////////////////////
  2107. // Function: NodePath::set_fog_off
  2108. // Access: Published
  2109. // Description: Sets the geometry at this level and below to render
  2110. // using no fog. This is normally the default, but
  2111. // it may be useful to use this to contradict
  2112. // set_fog() at a higher node level (or, with a
  2113. // priority, to override a set_fog() at a lower
  2114. // level).
  2115. ////////////////////////////////////////////////////////////////////
  2116. void NodePath::
  2117. set_fog_off(int priority) {
  2118. nassertv_always(!is_empty());
  2119. node()->set_attrib(FogAttrib::make_off(), priority);
  2120. }
  2121. ////////////////////////////////////////////////////////////////////
  2122. // Function: NodePath::clear_fog
  2123. // Access: Published
  2124. // Description: Completely removes any fog adjustment that may
  2125. // have been set via set_fog() or set_fog_off()
  2126. // from this particular node. This allows whatever
  2127. // fogs might be otherwise affecting the geometry to
  2128. // show instead.
  2129. ////////////////////////////////////////////////////////////////////
  2130. void NodePath::
  2131. clear_fog() {
  2132. nassertv_always(!is_empty());
  2133. node()->clear_attrib(FogAttrib::get_class_type());
  2134. }
  2135. ////////////////////////////////////////////////////////////////////
  2136. // Function: NodePath::has_fog
  2137. // Access: Published
  2138. // Description: Returns true if a fog has been applied to this
  2139. // particular node via set_fog(), false otherwise.
  2140. // This is not the same thing as asking whether the
  2141. // geometry at this node will be rendered with
  2142. // fog, as there may be a fog in effect from a higher or
  2143. // lower level.
  2144. ////////////////////////////////////////////////////////////////////
  2145. bool NodePath::
  2146. has_fog() const {
  2147. nassertr_always(!is_empty(), false);
  2148. const RenderAttrib *attrib =
  2149. node()->get_attrib(FogAttrib::get_class_type());
  2150. if (attrib != (const RenderAttrib *)NULL) {
  2151. const FogAttrib *fa = DCAST(FogAttrib, attrib);
  2152. return !fa->is_off();
  2153. }
  2154. return false;
  2155. }
  2156. ////////////////////////////////////////////////////////////////////
  2157. // Function: NodePath::has_fog_off
  2158. // Access: Published
  2159. // Description: Returns true if a fog has been specifically
  2160. // disabled on this particular node via
  2161. // set_fog_off(), false otherwise. This is not the
  2162. // same thing as asking whether the geometry at this
  2163. // node will be rendered unfogged, as there may be a
  2164. // fog in effect from a higher or lower level.
  2165. ////////////////////////////////////////////////////////////////////
  2166. bool NodePath::
  2167. has_fog_off() const {
  2168. nassertr_always(!is_empty(), false);
  2169. const RenderAttrib *attrib =
  2170. node()->get_attrib(FogAttrib::get_class_type());
  2171. if (attrib != (const RenderAttrib *)NULL) {
  2172. const FogAttrib *fa = DCAST(FogAttrib, attrib);
  2173. return fa->is_off();
  2174. }
  2175. return false;
  2176. }
  2177. ////////////////////////////////////////////////////////////////////
  2178. // Function: NodePath::get_fog
  2179. // Access: Published
  2180. // Description: Returns the fog that has been set on this
  2181. // particular node, or NULL if no fog has been set.
  2182. // This is not necessarily the fog that will be
  2183. // applied to the geometry at or below this level, as
  2184. // another fog at a higher or lower level may
  2185. // override.
  2186. ////////////////////////////////////////////////////////////////////
  2187. Fog *NodePath::
  2188. get_fog() const {
  2189. nassertr_always(!is_empty(), NULL);
  2190. const RenderAttrib *attrib =
  2191. node()->get_attrib(FogAttrib::get_class_type());
  2192. if (attrib != (const RenderAttrib *)NULL) {
  2193. const FogAttrib *fa = DCAST(FogAttrib, attrib);
  2194. return fa->get_fog();
  2195. }
  2196. return NULL;
  2197. }
  2198. ////////////////////////////////////////////////////////////////////
  2199. // Function: NodePath::set_render_mode_wireframe
  2200. // Access: Published
  2201. // Description: Sets up the geometry at this level and below (unless
  2202. // overridden) to render in wireframe mode.
  2203. ////////////////////////////////////////////////////////////////////
  2204. void NodePath::
  2205. set_render_mode_wireframe(int priority) {
  2206. nassertv_always(!is_empty());
  2207. node()->set_attrib(RenderModeAttrib::make(RenderModeAttrib::M_wireframe), priority);
  2208. }
  2209. ////////////////////////////////////////////////////////////////////
  2210. // Function: NodePath::set_render_mode_filled
  2211. // Access: Published
  2212. // Description: Sets up the geometry at this level and below (unless
  2213. // overridden) to render in filled (i.e. not wireframe)
  2214. // mode.
  2215. ////////////////////////////////////////////////////////////////////
  2216. void NodePath::
  2217. set_render_mode_filled(int priority) {
  2218. nassertv_always(!is_empty());
  2219. node()->set_attrib(RenderModeAttrib::make(RenderModeAttrib::M_filled), priority);
  2220. }
  2221. ////////////////////////////////////////////////////////////////////
  2222. // Function: NodePath::clear_render_mode
  2223. // Access: Published
  2224. // Description: Completely removes any render mode adjustment that
  2225. // may have been set on this node via
  2226. // set_render_mode_wireframe() or
  2227. // set_render_mode_filled().
  2228. ////////////////////////////////////////////////////////////////////
  2229. void NodePath::
  2230. clear_render_mode() {
  2231. nassertv_always(!is_empty());
  2232. node()->clear_attrib(RenderModeAttrib::get_class_type());
  2233. }
  2234. ////////////////////////////////////////////////////////////////////
  2235. // Function: NodePath::has_render_mode
  2236. // Access: Published
  2237. // Description: Returns true if a render mode has been explicitly set
  2238. // on this particular node via
  2239. // set_render_mode_wireframe() or
  2240. // set_render_mode_filled(), false otherwise.
  2241. ////////////////////////////////////////////////////////////////////
  2242. bool NodePath::
  2243. has_render_mode() const {
  2244. nassertr_always(!is_empty(), false);
  2245. return node()->has_attrib(RenderModeAttrib::get_class_type());
  2246. }
  2247. ////////////////////////////////////////////////////////////////////
  2248. // Function: NodePath::set_two_sided
  2249. // Access: Published
  2250. // Description: Specifically sets or disables two-sided rendering
  2251. // mode on this particular node. If no other nodes
  2252. // override, this will cause backfacing polygons to be
  2253. // drawn (in two-sided mode, true) or culled (in
  2254. // one-sided mode, false).
  2255. ////////////////////////////////////////////////////////////////////
  2256. void NodePath::
  2257. set_two_sided(bool two_sided, int priority) {
  2258. nassertv_always(!is_empty());
  2259. CullFaceAttrib::Mode mode =
  2260. two_sided ?
  2261. CullFaceAttrib::M_cull_none :
  2262. CullFaceAttrib::M_cull_clockwise;
  2263. node()->set_attrib(CullFaceAttrib::make(mode), priority);
  2264. }
  2265. ////////////////////////////////////////////////////////////////////
  2266. // Function: NodePath::clear_two_sided
  2267. // Access: Published
  2268. // Description: Completely removes any two-sided adjustment that
  2269. // may have been set on this node via set_two_sided().
  2270. // The geometry at this level and below will
  2271. // subsequently be rendered either two-sided or
  2272. // one-sided, according to whatever other nodes may have
  2273. // had set_two_sided() on it, or according to the
  2274. // initial state otherwise.
  2275. ////////////////////////////////////////////////////////////////////
  2276. void NodePath::
  2277. clear_two_sided() {
  2278. nassertv_always(!is_empty());
  2279. node()->clear_attrib(CullFaceAttrib::get_class_type());
  2280. }
  2281. ////////////////////////////////////////////////////////////////////
  2282. // Function: NodePath::has_two_sided
  2283. // Access: Published
  2284. // Description: Returns true if a two-sided adjustment has been
  2285. // explicitly set on this particular node via
  2286. // set_two_sided(). If this returns true, then
  2287. // get_two_sided() may be called to determine which has
  2288. // been set.
  2289. ////////////////////////////////////////////////////////////////////
  2290. bool NodePath::
  2291. has_two_sided() const {
  2292. nassertr_always(!is_empty(), false);
  2293. return node()->has_attrib(CullFaceAttrib::get_class_type());
  2294. }
  2295. ////////////////////////////////////////////////////////////////////
  2296. // Function: NodePath::get_two_sided
  2297. // Access: Published
  2298. // Description: Returns true if two-sided rendering has been
  2299. // specifically set on this node via set_two_sided(), or
  2300. // false if one-sided rendering has been specifically
  2301. // set, or if nothing has been specifically set. See
  2302. // also has_two_sided(). This does not necessarily
  2303. // imply that the geometry will or will not be rendered
  2304. // two-sided, as there may be other nodes that override.
  2305. ////////////////////////////////////////////////////////////////////
  2306. bool NodePath::
  2307. get_two_sided() const {
  2308. nassertr_always(!is_empty(), false);
  2309. const RenderAttrib *attrib =
  2310. node()->get_attrib(CullFaceAttrib::get_class_type());
  2311. if (attrib != (const RenderAttrib *)NULL) {
  2312. const CullFaceAttrib *cfa = DCAST(CullFaceAttrib, attrib);
  2313. return (cfa->get_actual_mode() == CullFaceAttrib::M_cull_none);
  2314. }
  2315. return false;
  2316. }
  2317. #if 0
  2318. // programmers prolly wont need alpha-test control
  2319. ////////////////////////////////////////////////////////////////////
  2320. // Function: NodePath::set_alpha_test
  2321. // Access: Published
  2322. // Description: Specifically sets or disables the testing of the
  2323. // alpha buffer on this particular node. This is
  2324. // normally on in the 3-d scene graph and off in the 2-d
  2325. // scene graph; it should be on for rendering most 3-d
  2326. // objects properly.
  2327. ////////////////////////////////////////////////////////////////////
  2328. void NodePath::
  2329. set_alpha_test(RenderAttrib::PandaCompareFunc alpha_test_mode,float reference_alpha, int priority) {
  2330. nassertv_always(!is_empty());
  2331. node()->set_attrib(AlphaTestAttrib::make(alpha_test_mode,reference_alpha), priority);
  2332. }
  2333. ////////////////////////////////////////////////////////////////////
  2334. // Function: NodePath::clear_alpha_test
  2335. // Access: Published
  2336. // Description: Completely removes any alpha-test adjustment that
  2337. // may have been set on this node via set_alpha_test().
  2338. ////////////////////////////////////////////////////////////////////
  2339. void NodePath::
  2340. clear_alpha_test() {
  2341. nassertv_always(!is_empty());
  2342. node()->clear_attrib(AlphaTestAttrib::get_class_type());
  2343. }
  2344. ////////////////////////////////////////////////////////////////////
  2345. // Function: NodePath::has_alpha_test
  2346. // Access: Published
  2347. // Description: Returns true if a alpha-test adjustment has been
  2348. // explicitly set on this particular node via
  2349. // set_alpha_test(). If this returns true, then
  2350. // get_alpha_test() may be called to determine which has
  2351. // been set.
  2352. ////////////////////////////////////////////////////////////////////
  2353. bool NodePath::
  2354. has_alpha_test() const {
  2355. nassertr_always(!is_empty(), false);
  2356. return node()->has_attrib(AlphaTestAttrib::get_class_type());
  2357. }
  2358. ////////////////////////////////////////////////////////////////////
  2359. // Function: NodePath::get_alpha_test
  2360. // Access: Published
  2361. // Description: Returns true if alpha-test rendering has been
  2362. // specifically set on this node via set_alpha_test(), or
  2363. // false if alpha-test rendering has been specifically
  2364. // disabled, or if nothing has been specifically set. See
  2365. // also has_alpha_test().
  2366. ////////////////////////////////////////////////////////////////////
  2367. bool NodePath::
  2368. get_alpha_test() const {
  2369. nassertr_always(!is_empty(), false);
  2370. const RenderAttrib *attrib =
  2371. node()->get_attrib(AlphaTestAttrib::get_class_type());
  2372. if (attrib != (const RenderAttrib *)NULL) {
  2373. const AlphaTestAttrib *dta = DCAST(AlphaTestAttrib, attrib);
  2374. return (dta->get_mode() != AlphaTestAttrib::M_none);
  2375. }
  2376. return false;
  2377. }
  2378. #endif
  2379. ////////////////////////////////////////////////////////////////////
  2380. // Function: NodePath::set_depth_test
  2381. // Access: Published
  2382. // Description: Specifically sets or disables the testing of the
  2383. // depth buffer on this particular node. This is
  2384. // normally on in the 3-d scene graph and off in the 2-d
  2385. // scene graph; it should be on for rendering most 3-d
  2386. // objects properly.
  2387. ////////////////////////////////////////////////////////////////////
  2388. void NodePath::
  2389. set_depth_test(bool depth_test, int priority) {
  2390. nassertv_always(!is_empty());
  2391. DepthTestAttrib::PandaCompareFunc mode =
  2392. depth_test ?
  2393. DepthTestAttrib::M_less :
  2394. DepthTestAttrib::M_none;
  2395. node()->set_attrib(DepthTestAttrib::make(mode), priority);
  2396. }
  2397. ////////////////////////////////////////////////////////////////////
  2398. // Function: NodePath::clear_depth_test
  2399. // Access: Published
  2400. // Description: Completely removes any depth-test adjustment that
  2401. // may have been set on this node via set_depth_test().
  2402. ////////////////////////////////////////////////////////////////////
  2403. void NodePath::
  2404. clear_depth_test() {
  2405. nassertv_always(!is_empty());
  2406. node()->clear_attrib(DepthTestAttrib::get_class_type());
  2407. }
  2408. ////////////////////////////////////////////////////////////////////
  2409. // Function: NodePath::has_depth_test
  2410. // Access: Published
  2411. // Description: Returns true if a depth-test adjustment has been
  2412. // explicitly set on this particular node via
  2413. // set_depth_test(). If this returns true, then
  2414. // get_depth_test() may be called to determine which has
  2415. // been set.
  2416. ////////////////////////////////////////////////////////////////////
  2417. bool NodePath::
  2418. has_depth_test() const {
  2419. nassertr_always(!is_empty(), false);
  2420. return node()->has_attrib(DepthTestAttrib::get_class_type());
  2421. }
  2422. ////////////////////////////////////////////////////////////////////
  2423. // Function: NodePath::get_depth_test
  2424. // Access: Published
  2425. // Description: Returns true if depth-test rendering has been
  2426. // specifically set on this node via set_depth_test(), or
  2427. // false if depth-test rendering has been specifically
  2428. // disabled, or if nothing has been specifically set. See
  2429. // also has_depth_test().
  2430. ////////////////////////////////////////////////////////////////////
  2431. bool NodePath::
  2432. get_depth_test() const {
  2433. nassertr_always(!is_empty(), false);
  2434. const RenderAttrib *attrib =
  2435. node()->get_attrib(DepthTestAttrib::get_class_type());
  2436. if (attrib != (const RenderAttrib *)NULL) {
  2437. const DepthTestAttrib *dta = DCAST(DepthTestAttrib, attrib);
  2438. return (dta->get_mode() != DepthTestAttrib::M_none);
  2439. }
  2440. return false;
  2441. }
  2442. ////////////////////////////////////////////////////////////////////
  2443. // Function: NodePath::set_depth_write
  2444. // Access: Published
  2445. // Description: Specifically sets or disables the writing to the
  2446. // depth buffer on this particular node. This is
  2447. // normally on in the 3-d scene graph and off in the 2-d
  2448. // scene graph; it should be on for rendering most 3-d
  2449. // objects properly.
  2450. ////////////////////////////////////////////////////////////////////
  2451. void NodePath::
  2452. set_depth_write(bool depth_write, int priority) {
  2453. nassertv_always(!is_empty());
  2454. DepthWriteAttrib::Mode mode =
  2455. depth_write ?
  2456. DepthWriteAttrib::M_on :
  2457. DepthWriteAttrib::M_off;
  2458. node()->set_attrib(DepthWriteAttrib::make(mode), priority);
  2459. }
  2460. ////////////////////////////////////////////////////////////////////
  2461. // Function: NodePath::clear_depth_write
  2462. // Access: Published
  2463. // Description: Completely removes any depth-write adjustment that
  2464. // may have been set on this node via set_depth_write().
  2465. ////////////////////////////////////////////////////////////////////
  2466. void NodePath::
  2467. clear_depth_write() {
  2468. nassertv_always(!is_empty());
  2469. node()->clear_attrib(DepthWriteAttrib::get_class_type());
  2470. }
  2471. ////////////////////////////////////////////////////////////////////
  2472. // Function: NodePath::has_depth_write
  2473. // Access: Published
  2474. // Description: Returns true if a depth-write adjustment has been
  2475. // explicitly set on this particular node via
  2476. // set_depth_write(). If this returns true, then
  2477. // get_depth_write() may be called to determine which has
  2478. // been set.
  2479. ////////////////////////////////////////////////////////////////////
  2480. bool NodePath::
  2481. has_depth_write() const {
  2482. nassertr_always(!is_empty(), false);
  2483. return node()->has_attrib(DepthWriteAttrib::get_class_type());
  2484. }
  2485. ////////////////////////////////////////////////////////////////////
  2486. // Function: NodePath::get_depth_write
  2487. // Access: Published
  2488. // Description: Returns true if depth-write rendering has been
  2489. // specifically set on this node via set_depth_write(), or
  2490. // false if depth-write rendering has been specifically
  2491. // disabled, or if nothing has been specifically set. See
  2492. // also has_depth_write().
  2493. ////////////////////////////////////////////////////////////////////
  2494. bool NodePath::
  2495. get_depth_write() const {
  2496. nassertr_always(!is_empty(), false);
  2497. const RenderAttrib *attrib =
  2498. node()->get_attrib(DepthWriteAttrib::get_class_type());
  2499. if (attrib != (const RenderAttrib *)NULL) {
  2500. const DepthWriteAttrib *dta = DCAST(DepthWriteAttrib, attrib);
  2501. return (dta->get_mode() != DepthWriteAttrib::M_off);
  2502. }
  2503. return false;
  2504. }
  2505. ////////////////////////////////////////////////////////////////////
  2506. // Function: NodePath::do_billboard_axis
  2507. // Access: Published
  2508. // Description: Performs a billboard-type rotate to the indicated
  2509. // camera node, one time only, and leaves the object
  2510. // rotated. This is similar in principle to heads_up().
  2511. ////////////////////////////////////////////////////////////////////
  2512. void NodePath::
  2513. do_billboard_axis(const NodePath &camera, float offset) {
  2514. nassertv_always(!is_empty());
  2515. NodePath parent = get_parent();
  2516. LMatrix4f rel_mat = camera.get_mat(parent);
  2517. LVector3f up = LVector3f::up();
  2518. LVector3f rel_pos = -rel_mat.get_row3(3);
  2519. LQuaternionf quat;
  2520. ::heads_up(quat, rel_pos, up);
  2521. set_quat(quat);
  2522. // Also slide the geometry towards the camera according to the
  2523. // offset factor.
  2524. if (offset != 0.0f) {
  2525. LVector3f translate = rel_mat.get_row3(3);
  2526. translate.normalize();
  2527. translate *= offset;
  2528. set_pos(translate);
  2529. }
  2530. }
  2531. ////////////////////////////////////////////////////////////////////
  2532. // Function: NodePath::do_billboard_point_eye
  2533. // Access: Published
  2534. // Description: Performs a billboard-type rotate to the indicated
  2535. // camera node, one time only, and leaves the object
  2536. // rotated. This is similar in principle to look_at(),
  2537. // although the point_eye billboard effect cannot be
  2538. // achieved using the ordinary look_at() call.
  2539. ////////////////////////////////////////////////////////////////////
  2540. void NodePath::
  2541. do_billboard_point_eye(const NodePath &camera, float offset) {
  2542. nassertv_always(!is_empty());
  2543. NodePath parent = get_parent();
  2544. LMatrix4f rel_mat = camera.get_mat(parent);
  2545. LVector3f up = LVector3f::up() * rel_mat;
  2546. LVector3f rel_pos = LVector3f::forward() * rel_mat;
  2547. LQuaternionf quat;
  2548. ::look_at(quat, rel_pos, up);
  2549. set_quat(quat);
  2550. // Also slide the geometry towards the camera according to the
  2551. // offset factor.
  2552. if (offset != 0.0f) {
  2553. LVector3f translate = rel_mat.get_row3(3);
  2554. translate.normalize();
  2555. translate *= offset;
  2556. set_pos(translate);
  2557. }
  2558. }
  2559. ////////////////////////////////////////////////////////////////////
  2560. // Function: NodePath::do_billboard_point_world
  2561. // Access: Published
  2562. // Description: Performs a billboard-type rotate to the indicated
  2563. // camera node, one time only, and leaves the object
  2564. // rotated. This is similar in principle to look_at().
  2565. ////////////////////////////////////////////////////////////////////
  2566. void NodePath::
  2567. do_billboard_point_world(const NodePath &camera, float offset) {
  2568. nassertv_always(!is_empty());
  2569. NodePath parent = get_parent();
  2570. LMatrix4f rel_mat = camera.get_mat(parent);
  2571. LVector3f up = LVector3f::up();
  2572. LVector3f rel_pos = -rel_mat.get_row3(3);
  2573. LQuaternionf quat;
  2574. ::look_at(quat, rel_pos, up);
  2575. set_quat(quat);
  2576. // Also slide the geometry towards the camera according to the
  2577. // offset factor.
  2578. if (offset != 0.0f) {
  2579. LVector3f translate = rel_mat.get_row3(3);
  2580. translate.normalize();
  2581. translate *= offset;
  2582. set_pos(translate);
  2583. }
  2584. }
  2585. ////////////////////////////////////////////////////////////////////
  2586. // Function: NodePath::set_billboard_axis
  2587. // Access: Published
  2588. // Description: Puts a billboard transition on the node such that it
  2589. // will rotate in two dimensions around the up axis,
  2590. // towards a specified "camera" instead of to the
  2591. // viewing camera.
  2592. ////////////////////////////////////////////////////////////////////
  2593. void NodePath::
  2594. set_billboard_axis(const NodePath &camera, float offset) {
  2595. nassertv_always(!is_empty());
  2596. CPT(RenderEffect) billboard = BillboardEffect::make
  2597. (LVector3f::up(), false, true,
  2598. offset, camera, LPoint3f(0.0f, 0.0f, 0.0f));
  2599. node()->set_effect(billboard);
  2600. }
  2601. ////////////////////////////////////////////////////////////////////
  2602. // Function: NodePath::set_billboard_point_eye
  2603. // Access: Published
  2604. // Description: Puts a billboard transition on the node such that it
  2605. // will rotate in three dimensions about the origin,
  2606. // keeping its up vector oriented to the top of the
  2607. // camera, towards a specified "camera" instead of to
  2608. // the viewing camera.
  2609. ////////////////////////////////////////////////////////////////////
  2610. void NodePath::
  2611. set_billboard_point_eye(const NodePath &camera, float offset) {
  2612. nassertv_always(!is_empty());
  2613. CPT(RenderEffect) billboard = BillboardEffect::make
  2614. (LVector3f::up(), true, false,
  2615. offset, camera, LPoint3f(0.0f, 0.0f, 0.0f));
  2616. node()->set_effect(billboard);
  2617. }
  2618. ////////////////////////////////////////////////////////////////////
  2619. // Function: NodePath::set_billboard_point_world
  2620. // Access: Published
  2621. // Description: Puts a billboard transition on the node such that it
  2622. // will rotate in three dimensions about the origin,
  2623. // keeping its up vector oriented to the sky, towards a
  2624. // specified "camera" instead of to the viewing camera.
  2625. ////////////////////////////////////////////////////////////////////
  2626. void NodePath::
  2627. set_billboard_point_world(const NodePath &camera, float offset) {
  2628. nassertv_always(!is_empty());
  2629. CPT(RenderEffect) billboard = BillboardEffect::make
  2630. (LVector3f::up(), false, false,
  2631. offset, camera, LPoint3f(0.0f, 0.0f, 0.0f));
  2632. node()->set_effect(billboard);
  2633. }
  2634. ////////////////////////////////////////////////////////////////////
  2635. // Function: NodePath::clear_billboard
  2636. // Access: Published
  2637. // Description: Removes any billboard effect from the node.
  2638. ////////////////////////////////////////////////////////////////////
  2639. void NodePath::
  2640. clear_billboard() {
  2641. nassertv_always(!is_empty());
  2642. node()->clear_effect(BillboardEffect::get_class_type());
  2643. }
  2644. ////////////////////////////////////////////////////////////////////
  2645. // Function: NodePath::has_billboard
  2646. // Access: Published
  2647. // Description: Returns true if there is any billboard effect on
  2648. // the node.
  2649. ////////////////////////////////////////////////////////////////////
  2650. bool NodePath::
  2651. has_billboard() const {
  2652. nassertr_always(!is_empty(), false);
  2653. return node()->has_effect(BillboardEffect::get_class_type());
  2654. }
  2655. ////////////////////////////////////////////////////////////////////
  2656. // Function: NodePath::set_compass
  2657. // Access: Published
  2658. // Description: Puts a compass effect on the node, so that it will
  2659. // retain a fixed rotation relative to the reference
  2660. // node (or render if the reference node is empty)
  2661. // regardless of the transforms above it.
  2662. ////////////////////////////////////////////////////////////////////
  2663. void NodePath::
  2664. set_compass(const NodePath &reference) {
  2665. nassertv_always(!is_empty());
  2666. node()->set_effect(CompassEffect::make(reference));
  2667. }
  2668. ////////////////////////////////////////////////////////////////////
  2669. // Function: NodePath::clear_compass
  2670. // Access: Published
  2671. // Description: Removes any compass effect from the node.
  2672. ////////////////////////////////////////////////////////////////////
  2673. void NodePath::
  2674. clear_compass() {
  2675. nassertv_always(!is_empty());
  2676. node()->clear_effect(CompassEffect::get_class_type());
  2677. }
  2678. ////////////////////////////////////////////////////////////////////
  2679. // Function: NodePath::has_compass
  2680. // Access: Published
  2681. // Description: Returns true if there is any compass effect on
  2682. // the node.
  2683. ////////////////////////////////////////////////////////////////////
  2684. bool NodePath::
  2685. has_compass() const {
  2686. nassertr_always(!is_empty(), false);
  2687. return node()->has_effect(CompassEffect::get_class_type());
  2688. }
  2689. ////////////////////////////////////////////////////////////////////
  2690. // Function: NodePath::set_transparency
  2691. // Access: Published
  2692. // Description: Specifically sets or disables transparent rendering
  2693. // mode on this particular node. If no other nodes
  2694. // override, this will cause items with a non-1 value
  2695. // for alpha color to be rendered partially transparent.
  2696. ////////////////////////////////////////////////////////////////////
  2697. void NodePath::
  2698. set_transparency(bool transparency, int priority) {
  2699. nassertv_always(!is_empty());
  2700. TransparencyAttrib::Mode mode =
  2701. transparency ?
  2702. TransparencyAttrib::M_alpha :
  2703. TransparencyAttrib::M_none;
  2704. node()->set_attrib(TransparencyAttrib::make(mode), priority);
  2705. }
  2706. ////////////////////////////////////////////////////////////////////
  2707. // Function: NodePath::clear_transparency
  2708. // Access: Published
  2709. // Description: Completely removes any transparency adjustment that
  2710. // may have been set on this node via set_transparency().
  2711. // The geometry at this level and below will
  2712. // subsequently be rendered either transparent or not,
  2713. // to whatever other nodes may have had
  2714. // set_transparency() on them.
  2715. ////////////////////////////////////////////////////////////////////
  2716. void NodePath::
  2717. clear_transparency() {
  2718. nassertv_always(!is_empty());
  2719. node()->clear_attrib(TransparencyAttrib::get_class_type());
  2720. }
  2721. ////////////////////////////////////////////////////////////////////
  2722. // Function: NodePath::has_transparency
  2723. // Access: Published
  2724. // Description: Returns true if a transparent-rendering adjustment
  2725. // has been explicitly set on this particular node via
  2726. // set_transparency(). If this returns true, then
  2727. // get_transparency() may be called to determine whether
  2728. // transparency has been explicitly enabled or
  2729. // explicitly disabled for this node.
  2730. ////////////////////////////////////////////////////////////////////
  2731. bool NodePath::
  2732. has_transparency() const {
  2733. nassertr_always(!is_empty(), false);
  2734. return node()->has_attrib(TransparencyAttrib::get_class_type());
  2735. }
  2736. ////////////////////////////////////////////////////////////////////
  2737. // Function: NodePath::get_transparency
  2738. // Access: Published
  2739. // Description: Returns true if transparent rendering has been
  2740. // specifically set on this node via set_transparency(), or
  2741. // false if nontransparent rendering has been specifically
  2742. // set, or if nothing has been specifically set. See
  2743. // also has_transparency(). This does not necessarily
  2744. // imply that the geometry will or will not be rendered
  2745. // transparent, as there may be other nodes that override.
  2746. ////////////////////////////////////////////////////////////////////
  2747. bool NodePath::
  2748. get_transparency() const {
  2749. nassertr_always(!is_empty(), false);
  2750. const RenderAttrib *attrib =
  2751. node()->get_attrib(TransparencyAttrib::get_class_type());
  2752. if (attrib != (const RenderAttrib *)NULL) {
  2753. const TransparencyAttrib *ta = DCAST(TransparencyAttrib, attrib);
  2754. return (ta->get_mode() != TransparencyAttrib::M_none);
  2755. }
  2756. return false;
  2757. }
  2758. ////////////////////////////////////////////////////////////////////
  2759. // Function: NodePath::get_hidden_ancestor
  2760. // Access: Published
  2761. // Description: Returns the NodePath at or above the referenced node
  2762. // that is hidden to the indicated camera(s), or an
  2763. // empty NodePath if no ancestor of the referenced node
  2764. // is hidden (and the node should be visible).
  2765. ////////////////////////////////////////////////////////////////////
  2766. NodePath NodePath::
  2767. get_hidden_ancestor(DrawMask camera_mask) const {
  2768. NodePathComponent *comp;
  2769. for (comp = _head;
  2770. comp != (NodePathComponent *)NULL;
  2771. comp = comp->get_next()) {
  2772. PandaNode *node = comp->get_node();
  2773. if ((node->get_draw_mask() & camera_mask).is_zero()) {
  2774. NodePath result;
  2775. result._head = comp;
  2776. return result;
  2777. }
  2778. }
  2779. return not_found();
  2780. }
  2781. ////////////////////////////////////////////////////////////////////
  2782. // Function: NodePath::stash
  2783. // Access: Published
  2784. // Description: Removes the referenced node (and the entire subgraph
  2785. // below this node) from the scene graph in any normal
  2786. // sense. The node will no longer be visible and is not
  2787. // tested for collisions; furthermore, no normal scene
  2788. // graph traversal will visit the node. The node's
  2789. // bounding volume no longer contributes to its parent's
  2790. // bounding volume.
  2791. //
  2792. // A stashed node cannot be located by a normal find()
  2793. // operation (although a special find string can still
  2794. // retrieve it).
  2795. ////////////////////////////////////////////////////////////////////
  2796. void NodePath::
  2797. stash(int sort) {
  2798. nassertv_always(!is_singleton() && !is_empty());
  2799. nassertv(verify_complete());
  2800. bool reparented = PandaNode::reparent(_head->get_next(), _head, sort, true);
  2801. nassertv(reparented);
  2802. }
  2803. ////////////////////////////////////////////////////////////////////
  2804. // Function: NodePath::unstash
  2805. // Access: Published
  2806. // Description: Undoes the effect of a previous stash() on this
  2807. // node: makes the referenced node (and the entire
  2808. // subgraph below this node) once again part of the
  2809. // scene graph.
  2810. ////////////////////////////////////////////////////////////////////
  2811. void NodePath::
  2812. unstash(int sort) {
  2813. nassertv_always(!is_singleton() && !is_empty());
  2814. nassertv(verify_complete());
  2815. bool reparented = PandaNode::reparent(_head->get_next(), _head, sort, false);
  2816. nassertv(reparented);
  2817. }
  2818. ////////////////////////////////////////////////////////////////////
  2819. // Function: NodePath::unstash_all
  2820. // Access: Published
  2821. // Description: Unstashes this node and all stashed child nodes.
  2822. ////////////////////////////////////////////////////////////////////
  2823. void NodePath::
  2824. unstash_all() {
  2825. NodePathCollection stashed_descendents = find_all_matches("**/@@*");
  2826. stashed_descendents.unstash();
  2827. unstash();
  2828. }
  2829. ////////////////////////////////////////////////////////////////////
  2830. // Function: NodePath::get_stashed_ancestor
  2831. // Access: Published
  2832. // Description: Returns the NodePath at or above the referenced node
  2833. // that is stashed, or an empty NodePath if no ancestor
  2834. // of the referenced node is stashed (and the node should
  2835. // be visible).
  2836. ////////////////////////////////////////////////////////////////////
  2837. NodePath NodePath::
  2838. get_stashed_ancestor() const {
  2839. NodePathComponent *comp = _head;
  2840. if (comp != (NodePathComponent *)NULL) {
  2841. NodePathComponent *next = comp->get_next();
  2842. while (next != (NodePathComponent *)NULL) {
  2843. PandaNode *node = comp->get_node();
  2844. PandaNode *parent_node = next->get_node();
  2845. if (parent_node->find_stashed(node) >= 0) {
  2846. NodePath result;
  2847. result._head = comp;
  2848. return result;
  2849. }
  2850. comp = next;
  2851. next = next->get_next();
  2852. }
  2853. }
  2854. return not_found();
  2855. }
  2856. ////////////////////////////////////////////////////////////////////
  2857. // Function: NodePath::compare_to
  2858. // Access: Published
  2859. // Description: Returns a number less than zero if this NodePath
  2860. // sorts before the other one, greater than zero if it
  2861. // sorts after, or zero if they are equivalent.
  2862. //
  2863. // Two NodePaths are considered equivalent if they
  2864. // consist of exactly the same list of nodes in the same
  2865. // order. Otherwise, they are different; different
  2866. // NodePaths will be ranked in a consistent but
  2867. // undefined ordering; the ordering is useful only for
  2868. // placing the NodePaths in a sorted container like an
  2869. // STL set.
  2870. ////////////////////////////////////////////////////////////////////
  2871. int NodePath::
  2872. compare_to(const NodePath &other) const {
  2873. // Nowadays, the NodePathComponents at the head are pointerwise
  2874. // equivalent if and only if the NodePaths are equivalent. So we
  2875. // only have to compare pointers.
  2876. if (_head != other._head) {
  2877. return _head < other._head ? -1 : 1;
  2878. }
  2879. return 0;
  2880. }
  2881. ////////////////////////////////////////////////////////////////////
  2882. // Function: NodePath::verify_complete
  2883. // Access: Published
  2884. // Description: Returns true if all of the nodes described in the
  2885. // NodePath are connected, or false otherwise.
  2886. ////////////////////////////////////////////////////////////////////
  2887. bool NodePath::
  2888. verify_complete() const {
  2889. if (is_empty()) {
  2890. return true;
  2891. }
  2892. const NodePathComponent *comp = _head;
  2893. nassertr(comp != (const NodePathComponent *)NULL, false);
  2894. PandaNode *node = comp->get_node();
  2895. nassertr(node != (const PandaNode *)NULL, false);
  2896. int length = comp->get_length();
  2897. comp = comp->get_next();
  2898. length--;
  2899. while (comp != (const NodePathComponent *)NULL) {
  2900. PandaNode *next_node = comp->get_node();
  2901. nassertr(next_node != (const PandaNode *)NULL, false);
  2902. if (node->find_parent(next_node) < 0) {
  2903. pgraph_cat.warning()
  2904. << *this << " is incomplete; " << *node << " is not a child of "
  2905. << *next_node << "\n";
  2906. return false;
  2907. }
  2908. if (comp->get_length() != length) {
  2909. pgraph_cat.warning()
  2910. << *this << " is incomplete; length at " << *next_node
  2911. << " indicates " << comp->get_length() << " while length at "
  2912. << *node << " indicates " << length << "\n";
  2913. return false;
  2914. }
  2915. node = next_node;
  2916. comp = comp->get_next();
  2917. length--;
  2918. }
  2919. return true;
  2920. }
  2921. ////////////////////////////////////////////////////////////////////
  2922. // Function: NodePath::prepare_scene
  2923. // Access: Published
  2924. // Description: Walks through the scene graph beginning at the bottom
  2925. // node, and does whatever initialization is required to
  2926. // render the scene properly with the indicated GSG. It
  2927. // is not strictly necessary to call this, since the GSG
  2928. // will initialize itself when the scene is rendered,
  2929. // but this may take some of the overhead away from that
  2930. // process.
  2931. //
  2932. // If force_retained_mode is true, retained mode is set
  2933. // on the geometry encountered, regardless of the
  2934. // setting of the retained-mode Config variable.
  2935. // Otherwise, retained mode is set only if the
  2936. // retained-mode Config variable is true.
  2937. ////////////////////////////////////////////////////////////////////
  2938. void NodePath::
  2939. prepare_scene(GraphicsStateGuardianBase *gsg, bool force_retained_mode) {
  2940. nassertv_always(!is_empty());
  2941. CPT(RenderState) net_state = get_net_state();
  2942. r_prepare_scene(node(), net_state, gsg,
  2943. retained_mode || force_retained_mode);
  2944. }
  2945. ////////////////////////////////////////////////////////////////////
  2946. // Function: NodePath::show_bounds
  2947. // Access: Published
  2948. // Description: Causes the bounding volume of the bottom node and all
  2949. // of its descendants (that is, the bounding volume
  2950. // associated with the the bottom arc) to be rendered,
  2951. // if possible. The rendering method is less than
  2952. // optimal; this is intended primarily for debugging.
  2953. ////////////////////////////////////////////////////////////////////
  2954. void NodePath::
  2955. show_bounds() {
  2956. nassertv_always(!is_empty());
  2957. node()->set_effect(ShowBoundsEffect::make());
  2958. }
  2959. ////////////////////////////////////////////////////////////////////
  2960. // Function: NodePath::hide_bounds
  2961. // Access: Published
  2962. // Description: Stops the rendering of the bounding volume begun with
  2963. // show_bounds().
  2964. ////////////////////////////////////////////////////////////////////
  2965. void NodePath::
  2966. hide_bounds() {
  2967. nassertv_always(!is_empty());
  2968. node()->clear_effect(ShowBoundsEffect::get_class_type());
  2969. }
  2970. ////////////////////////////////////////////////////////////////////
  2971. // Function: NodePath::get_bounds
  2972. // Access: Published
  2973. // Description: Returns a newly-allocated bounding volume containing
  2974. // the bottom node and all of its descendants. This is
  2975. // the bounding volume on the bottom arc, converted to
  2976. // the local coordinate space of the node.
  2977. ////////////////////////////////////////////////////////////////////
  2978. PT(BoundingVolume) NodePath::
  2979. get_bounds() const {
  2980. nassertr_always(!is_empty(), new BoundingSphere);
  2981. return node()->get_bound().make_copy();
  2982. }
  2983. ////////////////////////////////////////////////////////////////////
  2984. // Function: NodePath::force_recompute_bounds
  2985. // Access: Published
  2986. // Description: Forces the recomputing of all the bounding volumes at
  2987. // every node in the subgraph beginning at this node and
  2988. // below.
  2989. //
  2990. // This should not normally need to be called, since the
  2991. // bounding volumes are supposed to be recomputed
  2992. // automatically when necessary. It may be useful when
  2993. // debugging, to verify that the bounding volumes have
  2994. // not become inadvertently stale; it may also be useful
  2995. // to force animated characters to update their bounding
  2996. // volumes (which does not presently happen
  2997. // automatically).
  2998. ////////////////////////////////////////////////////////////////////
  2999. void NodePath::
  3000. force_recompute_bounds() {
  3001. nassertv_always(!is_empty());
  3002. r_force_recompute_bounds(node());
  3003. }
  3004. ////////////////////////////////////////////////////////////////////
  3005. // Function: NodePath::write_bounds
  3006. // Access: Published
  3007. // Description: Writes a description of the bounding volume
  3008. // containing the bottom node and all of its descendants
  3009. // to the indicated output stream.
  3010. ////////////////////////////////////////////////////////////////////
  3011. void NodePath::
  3012. write_bounds(ostream &out) const {
  3013. get_bounds()->write(out);
  3014. }
  3015. ////////////////////////////////////////////////////////////////////
  3016. // Function: NodePath::calc_tight_bounds
  3017. // Access: Published
  3018. // Description: Calculates the minimum and maximum vertices of all
  3019. // Geoms at this NodePath's bottom node and below. This
  3020. // is a tight bounding box; it will generally be tighter
  3021. // than the bounding volume returned by get_bounds()
  3022. // (but it is more expensive to compute).
  3023. //
  3024. // The return value is true if any points are within the
  3025. // bounding volume, or false if none are.
  3026. ////////////////////////////////////////////////////////////////////
  3027. bool NodePath::
  3028. calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point) {
  3029. min_point.set(0.0f, 0.0f, 0.0f);
  3030. max_point.set(0.0f, 0.0f, 0.0f);
  3031. nassertr_always(!is_empty(), false);
  3032. bool found_any = false;
  3033. node()->calc_tight_bounds(min_point, max_point, found_any,
  3034. TransformState::make_identity());
  3035. return found_any;
  3036. }
  3037. ////////////////////////////////////////////////////////////////////
  3038. // Function: NodePath::flatten_light
  3039. // Access: Published
  3040. // Description: Lightly flattens out the hierarchy below this node by
  3041. // applying transforms, colors, and texture matrices
  3042. // from the arcs onto the vertices, but does not remove
  3043. // any nodes.
  3044. //
  3045. // This can result in improved rendering performance
  3046. // because there will be fewer transforms in the
  3047. // resulting scene graph, but the number of nodes will
  3048. // remain the same.
  3049. //
  3050. // Particularly, any NodePaths that reference nodes
  3051. // within this hierarchy will not be damaged. However,
  3052. // since this operation will remove transforms from the
  3053. // scene graph, it may be dangerous to apply to arcs
  3054. // where you expect to dynamically modify the transform,
  3055. // or where you expect the geometry to remain in a
  3056. // particular local coordinate system.
  3057. //
  3058. // The return value is always 0, since flatten_light
  3059. // does not remove any arcs.
  3060. ////////////////////////////////////////////////////////////////////
  3061. int NodePath::
  3062. flatten_light() {
  3063. nassertr_always(!is_empty(), 0);
  3064. SceneGraphReducer gr;
  3065. gr.apply_attribs(node());
  3066. return 0;
  3067. }
  3068. ////////////////////////////////////////////////////////////////////
  3069. // Function: NodePath::flatten_medium
  3070. // Access: Published
  3071. // Description: A more thorough flattening than flatten_light(), this
  3072. // first applies all the transforms, colors, and texture
  3073. // matrices from the arcs onto the vertices, and then
  3074. // removes unneeded grouping nodes--nodes that have
  3075. // exactly one child, for instance, but have no special
  3076. // properties in themselves.
  3077. //
  3078. // This results in improved perforamance over
  3079. // flatten_light() because the number of nodes in the
  3080. // scene graph is reduced.
  3081. //
  3082. // If max_children is specified, it represents the
  3083. // maximum number of children a node is allowed to have
  3084. // and still be flattened. Normally, this is 1; we
  3085. // don't typically want to flatten a node that has
  3086. // multiple children. However, sometimes this may be
  3087. // desirable; set this parameter to control the limit.
  3088. // If this is set to -1, there is no limit.
  3089. //
  3090. // The return value is the number of arcs removed.
  3091. ////////////////////////////////////////////////////////////////////
  3092. int NodePath::
  3093. flatten_medium() {
  3094. nassertr_always(!is_empty(), 0);
  3095. SceneGraphReducer gr;
  3096. gr.apply_attribs(node());
  3097. int num_removed = gr.flatten(node(), false);
  3098. return num_removed;
  3099. }
  3100. ////////////////////////////////////////////////////////////////////
  3101. // Function: NodePath::flatten_strong
  3102. // Access: Published
  3103. // Description: The strongest possible flattening. This first
  3104. // applies all of the transforms to the vertices, as in
  3105. // flatten_medium(), but then it will combine sibling
  3106. // nodes together when possible, in addition to removing
  3107. // unnecessary parent-child nodes. This can result in
  3108. // substantially fewer nodes, but any nicely-grouped
  3109. // hierachical bounding volumes may be lost.
  3110. //
  3111. // It is generally a good idea to apply this kind of
  3112. // flattening only to nodes that will be culled largely
  3113. // as a single unit, like a car. Applying this to an
  3114. // entire scene may result in overall poorer performance
  3115. // because of less-effective culling.
  3116. ////////////////////////////////////////////////////////////////////
  3117. int NodePath::
  3118. flatten_strong() {
  3119. nassertr_always(!is_empty(), 0);
  3120. SceneGraphReducer gr;
  3121. gr.apply_attribs(node());
  3122. int num_removed = gr.flatten(node(), true);
  3123. return num_removed;
  3124. }
  3125. ////////////////////////////////////////////////////////////////////
  3126. // Function: NodePath::find_net_tag
  3127. // Access: Published
  3128. // Description: Returns the lowest ancestor of this node that
  3129. // contains a tag definition with the indicated key, if
  3130. // any, or an empty NodePath if no ancestor of this node
  3131. // contains this tag definition. See set_tag().
  3132. ////////////////////////////////////////////////////////////////////
  3133. NodePath NodePath::
  3134. find_net_tag(const string &key) const {
  3135. if (is_empty()) {
  3136. return NodePath::not_found();
  3137. }
  3138. if (has_tag(key)) {
  3139. return *this;
  3140. }
  3141. return get_parent().find_net_tag(key);
  3142. }
  3143. ////////////////////////////////////////////////////////////////////
  3144. // Function: NodePath::write_bam_file
  3145. // Access: Published
  3146. // Description: Writes the contents of this node and below out to a
  3147. // bam file with the indicated filename. This file may
  3148. // then be read in again, as is, at some later point.
  3149. // Returns true if successful, false on some kind of
  3150. // error.
  3151. ////////////////////////////////////////////////////////////////////
  3152. bool NodePath::
  3153. write_bam_file(const string &filename) const {
  3154. nassertr_always(!is_empty(), false);
  3155. BamFile bam_file;
  3156. bool okflag = false;
  3157. if (bam_file.open_write(filename)) {
  3158. if (bam_file.write_object(node())) {
  3159. okflag = true;
  3160. }
  3161. bam_file.close();
  3162. }
  3163. return okflag;
  3164. }
  3165. ////////////////////////////////////////////////////////////////////
  3166. // Function: NodePath::find_common_ancestor
  3167. // Access: Private, Static
  3168. // Description: Walks up from both NodePaths to find the first node
  3169. // that both have in common, if any. Fills a_count and
  3170. // b_count with the number of nodes below the common
  3171. // node in each path.
  3172. //
  3173. // The return value is the NodePathComponent of the node
  3174. // they have in common, or NULL if they have nothing in
  3175. // common.
  3176. ////////////////////////////////////////////////////////////////////
  3177. NodePathComponent *NodePath::
  3178. find_common_ancestor(const NodePath &a, const NodePath &b,
  3179. int &a_count, int &b_count) {
  3180. nassertr(!a.is_empty() && !b.is_empty(), NULL);
  3181. NodePathComponent *ac = a._head;
  3182. NodePathComponent *bc = b._head;
  3183. a_count = 0;
  3184. b_count = 0;
  3185. // Shorten up the longer one until they are the same length.
  3186. while (ac->get_length() > bc->get_length()) {
  3187. nassertr(ac != (NodePathComponent *)NULL, NULL);
  3188. ac = ac->get_next();
  3189. a_count++;
  3190. }
  3191. while (bc->get_length() > ac->get_length()) {
  3192. nassertr(bc != (NodePathComponent *)NULL, NULL);
  3193. bc = bc->get_next();
  3194. b_count++;
  3195. }
  3196. // Now shorten them both up until we reach the same component.
  3197. while (ac != bc) {
  3198. // These shouldn't go to NULL unless they both go there together.
  3199. nassertr(ac != (NodePathComponent *)NULL, NULL);
  3200. nassertr(bc != (NodePathComponent *)NULL, NULL);
  3201. ac = ac->get_next();
  3202. a_count++;
  3203. bc = bc->get_next();
  3204. b_count++;
  3205. }
  3206. return ac;
  3207. }
  3208. ////////////////////////////////////////////////////////////////////
  3209. // Function: NodePath::r_get_net_state
  3210. // Access: Private
  3211. // Description: Recursively determines the net state changes to the
  3212. // indicated component node from the root of the graph.
  3213. ////////////////////////////////////////////////////////////////////
  3214. CPT(RenderState) NodePath::
  3215. r_get_net_state(NodePathComponent *comp) const {
  3216. if (comp == (NodePathComponent *)NULL) {
  3217. return RenderState::make_empty();
  3218. } else {
  3219. CPT(RenderState) state = comp->get_node()->get_state();
  3220. return r_get_net_state(comp->get_next())->compose(state);
  3221. }
  3222. }
  3223. ////////////////////////////////////////////////////////////////////
  3224. // Function: NodePath::r_get_partial_state
  3225. // Access: Private
  3226. // Description: Recursively determines the net state changes to the
  3227. // indicated component node from the nth node above it.
  3228. // If n exceeds the length of the path, this returns the
  3229. // net transform from the root of the graph.
  3230. ////////////////////////////////////////////////////////////////////
  3231. CPT(RenderState) NodePath::
  3232. r_get_partial_state(NodePathComponent *comp, int n) const {
  3233. if (n == 0 || comp == (NodePathComponent *)NULL) {
  3234. return RenderState::make_empty();
  3235. } else {
  3236. CPT(RenderState) state = comp->get_node()->get_state();
  3237. return r_get_partial_state(comp->get_next(), n - 1)->compose(state);
  3238. }
  3239. }
  3240. ////////////////////////////////////////////////////////////////////
  3241. // Function: NodePath::r_get_net_transform
  3242. // Access: Private
  3243. // Description: Recursively determines the net transform to the
  3244. // indicated component node from the root of the graph.
  3245. ////////////////////////////////////////////////////////////////////
  3246. CPT(TransformState) NodePath::
  3247. r_get_net_transform(NodePathComponent *comp) const {
  3248. if (comp == (NodePathComponent *)NULL) {
  3249. return TransformState::make_identity();
  3250. } else {
  3251. CPT(TransformState) transform = comp->get_node()->get_transform();
  3252. return r_get_net_transform(comp->get_next())->compose(transform);
  3253. }
  3254. }
  3255. ////////////////////////////////////////////////////////////////////
  3256. // Function: NodePath::r_get_partial_transform
  3257. // Access: Private
  3258. // Description: Recursively determines the net transform to the
  3259. // indicated component node from the nth node above it.
  3260. // If n exceeds the length of the path, this returns the
  3261. // net transform from the root of the graph.
  3262. ////////////////////////////////////////////////////////////////////
  3263. CPT(TransformState) NodePath::
  3264. r_get_partial_transform(NodePathComponent *comp, int n) const {
  3265. if (n == 0 || comp == (NodePathComponent *)NULL) {
  3266. return TransformState::make_identity();
  3267. } else {
  3268. CPT(TransformState) transform = comp->get_node()->get_transform();
  3269. return r_get_partial_transform(comp->get_next(), n - 1)->compose(transform);
  3270. }
  3271. }
  3272. ////////////////////////////////////////////////////////////////////
  3273. // Function: NodePath::r_get_net_prev_transform
  3274. // Access: Private
  3275. // Description: Recursively determines the net "previous" transform
  3276. // to the indicated component node from the root of the
  3277. // graph.
  3278. ////////////////////////////////////////////////////////////////////
  3279. CPT(TransformState) NodePath::
  3280. r_get_net_prev_transform(NodePathComponent *comp) const {
  3281. if (comp == (NodePathComponent *)NULL) {
  3282. return TransformState::make_identity();
  3283. } else {
  3284. CPT(TransformState) transform = comp->get_node()->get_prev_transform();
  3285. return r_get_net_prev_transform(comp->get_next())->compose(transform);
  3286. }
  3287. }
  3288. ////////////////////////////////////////////////////////////////////
  3289. // Function: NodePath::r_get_partial_prev_transform
  3290. // Access: Private
  3291. // Description: Recursively determines the net "previous" transform
  3292. // to the indicated component node from the nth node
  3293. // above it. If n exceeds the length of the path, this
  3294. // returns the net previous transform from the root of
  3295. // the graph.
  3296. ////////////////////////////////////////////////////////////////////
  3297. CPT(TransformState) NodePath::
  3298. r_get_partial_prev_transform(NodePathComponent *comp, int n) const {
  3299. if (n == 0 || comp == (NodePathComponent *)NULL) {
  3300. return TransformState::make_identity();
  3301. } else {
  3302. CPT(TransformState) transform = comp->get_node()->get_prev_transform();
  3303. return r_get_partial_prev_transform(comp->get_next(), n - 1)->compose(transform);
  3304. }
  3305. }
  3306. ////////////////////////////////////////////////////////////////////
  3307. // Function: NodePath::find_matches
  3308. // Access: Private
  3309. // Description: Finds up to max_matches matches against the given
  3310. // path string from this node and deeper. The
  3311. // max_matches count indicates the maximum number of
  3312. // matches to return, or -1 not to limit the number
  3313. // returned.
  3314. ////////////////////////////////////////////////////////////////////
  3315. void NodePath::
  3316. find_matches(NodePathCollection &result, const string &path,
  3317. int max_matches) const {
  3318. if (is_empty()) {
  3319. pgraph_cat.warning()
  3320. << "Attempt to extend an empty NodePath by '" << path
  3321. << "'.\n";
  3322. return;
  3323. }
  3324. FindApproxPath approx_path;
  3325. if (approx_path.add_string(path)) {
  3326. find_matches(result, approx_path, max_matches);
  3327. }
  3328. }
  3329. ////////////////////////////////////////////////////////////////////
  3330. // Function: NodePath::find_matches
  3331. // Access: Private
  3332. // Description: Finds up to max_matches matches against the given
  3333. // approx_path from this node and deeper. The
  3334. // max_matches count indicates the maximum number of
  3335. // matches to return, or -1 not to limit the number
  3336. // returned.
  3337. ////////////////////////////////////////////////////////////////////
  3338. void NodePath::
  3339. find_matches(NodePathCollection &result, FindApproxPath &approx_path,
  3340. int max_matches) const {
  3341. if (is_empty()) {
  3342. pgraph_cat.warning()
  3343. << "Attempt to extend an empty NodePath by: " << approx_path << ".\n";
  3344. return;
  3345. }
  3346. FindApproxLevelEntry start(WorkingNodePath(*this), approx_path);
  3347. nassertv(start._node_path.is_valid());
  3348. FindApproxLevel level;
  3349. level.add_entry(start);
  3350. r_find_matches(result, level, max_matches, _max_search_depth);
  3351. }
  3352. ////////////////////////////////////////////////////////////////////
  3353. // Function: NodePath::r_find_matches
  3354. // Access: Private
  3355. // Description: The recursive implementation of find_matches.
  3356. ////////////////////////////////////////////////////////////////////
  3357. void NodePath::
  3358. r_find_matches(NodePathCollection &result,
  3359. const FindApproxLevel &level,
  3360. int max_matches, int num_levels_remaining) const {
  3361. // Go on to the next level. If we exceeded the requested maximum
  3362. // depth, stop.
  3363. if (num_levels_remaining <= 0) {
  3364. return;
  3365. }
  3366. num_levels_remaining--;
  3367. FindApproxLevel next_level;
  3368. bool okflag = true;
  3369. // For each node in the current level, build up the set of possible
  3370. // matches in the next level.
  3371. FindApproxLevel::Vec::const_iterator li;
  3372. for (li = level._v.begin(); li != level._v.end() && okflag; ++li) {
  3373. const FindApproxLevelEntry &entry = (*li);
  3374. if (entry.is_solution(0)) {
  3375. // Does this entry already represent a solution?
  3376. result.add_path(entry._node_path.get_node_path());
  3377. } else {
  3378. entry.consider_node(result, next_level, max_matches, 0);
  3379. }
  3380. if (max_matches > 0 && result.get_num_paths() >= max_matches) {
  3381. // Really, we just want to return here. But returning from
  3382. // within the conditional within the for loop seems to sometimes
  3383. // cause a compiler fault in GCC. We'll use a semaphore
  3384. // variable instead.
  3385. okflag = false;
  3386. }
  3387. }
  3388. // Now recurse on the next level.
  3389. if (okflag) {
  3390. r_find_matches(result, next_level, max_matches, num_levels_remaining);
  3391. }
  3392. }
  3393. ////////////////////////////////////////////////////////////////////
  3394. // Function: NodePath::r_adjust_all_priorities
  3395. // Access: Private
  3396. // Description: The recursive implementation of
  3397. // adjust_all_priorities(). This walks through the
  3398. // subgraph defined by the indicated node and below.
  3399. ////////////////////////////////////////////////////////////////////
  3400. void NodePath::
  3401. r_adjust_all_priorities(PandaNode *node, int adjustment) {
  3402. node->set_state(node->get_state()->adjust_all_priorities(adjustment));
  3403. if (node->is_geom_node()) {
  3404. GeomNode *gnode;
  3405. DCAST_INTO_V(gnode, node);
  3406. int num_geoms = gnode->get_num_geoms();
  3407. for (int i = 0; i < num_geoms; i++) {
  3408. gnode->set_geom_state(i, gnode->get_geom_state(i)->adjust_all_priorities(adjustment));
  3409. }
  3410. }
  3411. PandaNode::Children cr = node->get_children();
  3412. int num_children = cr.get_num_children();
  3413. for (int i = 0; i < num_children; i++) {
  3414. r_adjust_all_priorities(cr.get_child(i), adjustment);
  3415. }
  3416. }
  3417. ////////////////////////////////////////////////////////////////////
  3418. // Function: NodePath::r_force_recompute_bounds
  3419. // Access: Private
  3420. // Description:
  3421. ////////////////////////////////////////////////////////////////////
  3422. void NodePath::
  3423. r_force_recompute_bounds(PandaNode *node) {
  3424. if (node->is_geom_node()) {
  3425. GeomNode *gnode;
  3426. DCAST_INTO_V(gnode, node);
  3427. int num_geoms = gnode->get_num_geoms();
  3428. for (int i = 0; i < num_geoms; i++) {
  3429. gnode->get_geom(i)->mark_bound_stale();
  3430. }
  3431. }
  3432. node->mark_bound_stale();
  3433. // Now consider children.
  3434. PandaNode::Children cr = node->get_children();
  3435. int num_children = cr.get_num_children();
  3436. for (int i = 0; i < num_children; i++) {
  3437. r_force_recompute_bounds(cr.get_child(i));
  3438. }
  3439. }
  3440. ////////////////////////////////////////////////////////////////////
  3441. // Function: NodePath::r_find_texture
  3442. // Access: Private
  3443. // Description:
  3444. ////////////////////////////////////////////////////////////////////
  3445. Texture * NodePath::
  3446. r_find_texture(PandaNode *node, const RenderState *state,
  3447. const GlobPattern &glob) const {
  3448. if (node->is_geom_node()) {
  3449. GeomNode *gnode;
  3450. DCAST_INTO_R(gnode, node, NULL);
  3451. int num_geoms = gnode->get_num_geoms();
  3452. for (int i = 0; i < num_geoms; i++) {
  3453. CPT(RenderState) geom_state =
  3454. state->compose(gnode->get_geom_state(i));
  3455. // Look for a TextureAttrib on the state.
  3456. const RenderAttrib *attrib =
  3457. geom_state->get_attrib(TextureAttrib::get_class_type());
  3458. if (attrib != (const RenderAttrib *)NULL) {
  3459. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  3460. Texture *texture = ta->get_texture();
  3461. if (texture != (Texture *)NULL) {
  3462. if (glob.matches(texture->get_name())) {
  3463. return texture;
  3464. }
  3465. }
  3466. }
  3467. }
  3468. }
  3469. // Now consider children.
  3470. PandaNode::Children cr = node->get_children();
  3471. int num_children = cr.get_num_children();
  3472. for (int i = 0; i < num_children; i++) {
  3473. PandaNode *child = cr.get_child(i);
  3474. CPT(RenderState) next_state = state->compose(child->get_state());
  3475. Texture *result = r_find_texture(child, next_state, glob);
  3476. if (result != (Texture *)NULL) {
  3477. return result;
  3478. }
  3479. }
  3480. return NULL;
  3481. }
  3482. ////////////////////////////////////////////////////////////////////
  3483. // Function: NodePath::r_find_all_textures
  3484. // Access: Private
  3485. // Description:
  3486. ////////////////////////////////////////////////////////////////////
  3487. void NodePath::
  3488. r_find_all_textures(PandaNode *node, const RenderState *state,
  3489. NodePath::Textures &textures) const {
  3490. if (node->is_geom_node()) {
  3491. GeomNode *gnode;
  3492. DCAST_INTO_V(gnode, node);
  3493. int num_geoms = gnode->get_num_geoms();
  3494. for (int i = 0; i < num_geoms; i++) {
  3495. CPT(RenderState) geom_state =
  3496. state->compose(gnode->get_geom_state(i));
  3497. // Look for a TextureAttrib on the state.
  3498. const RenderAttrib *attrib =
  3499. geom_state->get_attrib(TextureAttrib::get_class_type());
  3500. if (attrib != (const RenderAttrib *)NULL) {
  3501. const TextureAttrib *ta = DCAST(TextureAttrib, attrib);
  3502. Texture *texture = ta->get_texture();
  3503. if (texture != (Texture *)NULL) {
  3504. textures.insert(texture);
  3505. }
  3506. }
  3507. }
  3508. }
  3509. // Now consider children.
  3510. PandaNode::Children cr = node->get_children();
  3511. int num_children = cr.get_num_children();
  3512. for (int i = 0; i < num_children; i++) {
  3513. PandaNode *child = cr.get_child(i);
  3514. CPT(RenderState) next_state = state->compose(child->get_state());
  3515. r_find_all_textures(child, next_state, textures);
  3516. }
  3517. }
  3518. ////////////////////////////////////////////////////////////////////
  3519. // Function: NodePath::r_prepare_scene
  3520. // Access: Private
  3521. // Description: The recursive implementation of prepare_scene.
  3522. ////////////////////////////////////////////////////////////////////
  3523. void NodePath::
  3524. r_prepare_scene(PandaNode *node, const RenderState *state,
  3525. GraphicsStateGuardianBase *gsg, bool do_retained_mode) {
  3526. if (node->is_geom_node()) {
  3527. GeomNode *gnode;
  3528. DCAST_INTO_V(gnode, node);
  3529. /*
  3530. Not implemented yet in pgraph. Maybe we don't need this anyway.
  3531. if (do_retained_mode) {
  3532. gnode->prepare(gsg);
  3533. }
  3534. */
  3535. int num_geoms = gnode->get_num_geoms();
  3536. for (int i = 0; i < num_geoms; i++) {
  3537. CPT(RenderState) geom_state = state->compose(gnode->get_geom_state(i));
  3538. const RenderAttrib *attrib =
  3539. geom_state->get_attrib(TextureAttrib::get_class_type());
  3540. if (attrib != (const RenderAttrib *)NULL) {
  3541. const TextureAttrib *ta;
  3542. DCAST_INTO_V(ta, attrib);
  3543. Texture *texture = ta->get_texture();
  3544. if (texture != (Texture *)NULL) {
  3545. texture->prepare(gsg);
  3546. }
  3547. }
  3548. }
  3549. }
  3550. int num_children = node->get_num_children();
  3551. for (int i = 0; i < num_children; i++) {
  3552. PandaNode *child = node->get_child(i);
  3553. CPT(RenderState) child_state = state->compose(child->get_state());
  3554. r_prepare_scene(child, child_state, gsg, do_retained_mode);
  3555. }
  3556. }