nodePath.cxx 147 KB

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