collect.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/collect.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 1/08/01 10:04a $*
  29. * *
  30. * $Revision:: 1 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * CollectionClass::CollectionClass -- default constructor for collection render object *
  35. * CollectionClass::CollectionClass -- constructor for collection render object *
  36. * CollectionClass::CollectionClass -- copy constructor *
  37. * CollectionClass::CollectionClass -- assignment operator *
  38. * CollectionClass::~CollectionClass -- destructor *
  39. * CollectionClass::Clone -- virtual copy constructor *
  40. * CollectionClass::Free -- releases all assets in use by this collection *
  41. * CollectionClass::Class_ID -- returns class id for collection render objects *
  42. * CollectionClass::Get_Num_Polys -- returns the number of polygons in this collection *
  43. * CollectionClass::Render -- render this collection *
  44. * CollectionClass::Special_Render -- passes the special render call to all sub-objects *
  45. * CollectionClass::Set_Transform -- set the transform for this collection *
  46. * CollectionClass::Set_Position -- set the position for this collection *
  47. * CollectionClass::Get_Num_Sub_Objects -- returns the number of sub objects *
  48. * CollectionClass::Get_Sub_Object -- returns a pointer to the desired sub object *
  49. * CollectionClass::Add_Sub_Object -- adds another object into this collection *
  50. * CollectionClass::Remove_Sub_Object -- removes a sub object from this collection *
  51. * CollectionClass::Cast_Ray -- passes the ray test to each sub object *
  52. * CollectionClass::Cast_AABox -- passes the axis-aligned box test to each sub object *
  53. * CollectionClass::Cast_OBBox -- passes the oriented box test to each sub object *
  54. * CollectionClass::Intersect_AABox -- test for intersection with an AABox *
  55. * CollectionClass::Intersect_OBBox -- test for intersection with an OBBox *
  56. * CollectionClass::Get_Obj_Space_Bounding_Sphere -- returns the object space bounding spher *
  57. * CollectionClass::Get_Obj_Space_Bounding_Box -- returns the object-space bounding box *
  58. * CollectionClass::Snap_Point_Count -- returns the number of snap points in this collecion *
  59. * CollectionClass::Get_Snap_Point -- return the desired snap point *
  60. * CollectionClass::Scale -- scale the objects in this collection *
  61. * CollectionClass::Scale -- scale the objects in this collection *
  62. * CollectionClass::Update_Obj_Space_Bounding_Volumes -- recomputes the object space boundin *
  63. * CollectionClass::Update_Sub_Object_Transforms -- recomputes all sub object transforms *
  64. * CollectionLoaderClass::Load -- reads a collection from a w3d file *
  65. * CollectionDefClass::CollectionDefClass -- constructor *
  66. * CollectionDefClass::~CollectionDefClass -- destructor for collection definition *
  67. * CollectionDefClass::Free -- releases assets in use by a collection definition *
  68. * CollectionDefClass::Get_Name -- returns name of the collection *
  69. * CollectionDefClass::Load -- loads a collection definition from a w3d file *
  70. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  71. #include "collect.h"
  72. #include "chunkio.h"
  73. #include "camera.h"
  74. #include "wwdebug.h"
  75. #include "snappts.h"
  76. #include "assetmgr.h"
  77. #include "ww3d.h"
  78. #include "w3derr.h"
  79. //#include "sr.hpp"
  80. CollectionLoaderClass _CollectionLoader;
  81. /*
  82. ** CollectionDefClass. This is the "blueprint" for a collection object
  83. ** The asset manager will store these until someone actually asks it to
  84. ** create an instance of the collection object
  85. */
  86. class CollectionDefClass
  87. {
  88. public:
  89. CollectionDefClass(void);
  90. ~CollectionDefClass(void);
  91. const char * Get_Name(void) const;
  92. WW3DErrorType Load(ChunkLoadClass & cload);
  93. protected:
  94. void Free(void);
  95. char Name[W3D_NAME_LEN];
  96. DynamicVectorClass<char *> ObjectNames;
  97. SnapPointsClass * SnapPoints;
  98. DynamicVectorClass <ProxyClass> ProxyList;
  99. friend class CollectionClass;
  100. };
  101. /*
  102. ** CollectionPrototypeClass this is the render object prototype for
  103. ** Collections.
  104. */
  105. class CollectionPrototypeClass : public PrototypeClass
  106. {
  107. public:
  108. CollectionPrototypeClass(CollectionDefClass * def) { ColDef = def; WWASSERT(ColDef); }
  109. virtual ~CollectionPrototypeClass(void) { delete ColDef; }
  110. virtual const char * Get_Name(void) const { return ColDef->Get_Name(); }
  111. virtual int Get_Class_ID(void) const { return RenderObjClass::CLASSID_COLLECTION; }
  112. virtual RenderObjClass * Create(void) { return NEW_REF( CollectionClass, (*ColDef)); }
  113. CollectionDefClass * ColDef;
  114. };
  115. /***********************************************************************************************
  116. * CollectionClass::CollectionClass -- default constructor for collection render object *
  117. * *
  118. * INPUT: *
  119. * *
  120. * OUTPUT: *
  121. * *
  122. * WARNINGS: *
  123. * *
  124. * HISTORY: *
  125. * 23/8/00 GTH : Created. *
  126. *=============================================================================================*/
  127. CollectionClass::CollectionClass(void) :
  128. SnapPoints(NULL)
  129. {
  130. Update_Obj_Space_Bounding_Volumes();
  131. }
  132. /***********************************************************************************************
  133. * CollectionClass::CollectionClass -- constructor for collection render object *
  134. * *
  135. * INPUT: *
  136. * *
  137. * OUTPUT: *
  138. * *
  139. * WARNINGS: *
  140. * *
  141. * HISTORY: *
  142. * 12/8/98 GTH : Created. *
  143. *=============================================================================================*/
  144. CollectionClass::CollectionClass(const CollectionDefClass & def) :
  145. SubObjects(def.ObjectNames.Count()),
  146. SnapPoints(NULL)
  147. {
  148. // Set our name
  149. Set_Name (def.Get_Name ());
  150. // create the sub objects
  151. SubObjects.Resize(def.ObjectNames.Count());
  152. for (int i=0; i<def.ObjectNames.Count(); i++) {
  153. WWASSERT(SubObjects.Count() == i);
  154. SubObjects.Add(WW3DAssetManager::Get_Instance()->Create_Render_Obj(def.ObjectNames[i]));
  155. SubObjects[i]->Set_Container(this);
  156. }
  157. // Copy the list of placeholder objects from the definition
  158. ProxyList = def.ProxyList;
  159. // grab ahold of the snap points.
  160. SnapPoints = def.SnapPoints;
  161. if (SnapPoints) SnapPoints->Add_Ref();
  162. // set up our collision typeas the union of all of our sub-objects
  163. Update_Sub_Object_Bits();
  164. // update the object bounding volumes
  165. Update_Obj_Space_Bounding_Volumes();
  166. }
  167. /***********************************************************************************************
  168. * CollectionClass::CollectionClass -- copy constructor *
  169. * *
  170. * INPUT: *
  171. * *
  172. * OUTPUT: *
  173. * *
  174. * WARNINGS: *
  175. * *
  176. * HISTORY: *
  177. * 12/8/98 GTH : Created. *
  178. *=============================================================================================*/
  179. CollectionClass::CollectionClass(const CollectionClass & src) :
  180. CompositeRenderObjClass(src),
  181. SubObjects(src.SubObjects.Count()),
  182. SnapPoints(NULL)
  183. {
  184. *this = src;
  185. }
  186. /***********************************************************************************************
  187. * CollectionClass::CollectionClass -- assignment operator *
  188. * *
  189. * INPUT: *
  190. * *
  191. * OUTPUT: *
  192. * *
  193. * WARNINGS: *
  194. * *
  195. * HISTORY: *
  196. * 12/8/98 GTH : Created. *
  197. *=============================================================================================*/
  198. CollectionClass & CollectionClass::operator = (const CollectionClass & that)
  199. {
  200. if (this != &that) {
  201. Free();
  202. CompositeRenderObjClass::operator = (that);
  203. SubObjects.Resize(that.SubObjects.Count());
  204. for (int i=0; i<that.SubObjects.Count(); i++) {
  205. WWASSERT(SubObjects.Count() == i);
  206. SubObjects.Add(that.SubObjects[i]->Clone());
  207. SubObjects[i]->Set_Container(this);
  208. }
  209. // Copy the list of placeholder objects from the definition
  210. ProxyList = that.ProxyList;
  211. SnapPoints = that.SnapPoints;
  212. if (SnapPoints) SnapPoints->Add_Ref();
  213. Update_Sub_Object_Bits();
  214. Update_Obj_Space_Bounding_Volumes();
  215. }
  216. return * this;
  217. }
  218. /***********************************************************************************************
  219. * CollectionClass::~CollectionClass -- destructor *
  220. * *
  221. * INPUT: *
  222. * *
  223. * OUTPUT: *
  224. * *
  225. * WARNINGS: *
  226. * *
  227. * HISTORY: *
  228. * 12/8/98 GTH : Created. *
  229. *=============================================================================================*/
  230. CollectionClass::~CollectionClass(void)
  231. {
  232. Free();
  233. }
  234. /***********************************************************************************************
  235. * CollectionClass::Clone -- virtual copy constructor *
  236. * *
  237. * INPUT: *
  238. * *
  239. * OUTPUT: *
  240. * *
  241. * WARNINGS: *
  242. * *
  243. * HISTORY: *
  244. * 12/8/98 GTH : Created. *
  245. *=============================================================================================*/
  246. RenderObjClass * CollectionClass::Clone(void) const
  247. {
  248. return NEW_REF( CollectionClass, (*this));
  249. }
  250. /***********************************************************************************************
  251. * CollectionClass::Free -- releases all assets in use by this collection *
  252. * *
  253. * INPUT: *
  254. * *
  255. * OUTPUT: *
  256. * *
  257. * WARNINGS: *
  258. * *
  259. * HISTORY: *
  260. * 12/8/98 GTH : Created. *
  261. *=============================================================================================*/
  262. void CollectionClass::Free(void)
  263. {
  264. for (int i=0; i<SubObjects.Count(); i++) {
  265. SubObjects[i]->Set_Container(NULL);
  266. SubObjects[i]->Release_Ref();
  267. SubObjects[i] = NULL;
  268. }
  269. SubObjects.Delete_All();
  270. ProxyList.Delete_All ();
  271. REF_PTR_RELEASE(SnapPoints);
  272. }
  273. /***********************************************************************************************
  274. * CollectionClass::Class_ID -- returns class id for collection render objects *
  275. * *
  276. * INPUT: *
  277. * *
  278. * OUTPUT: *
  279. * *
  280. * WARNINGS: *
  281. * *
  282. * HISTORY: *
  283. * 12/8/98 GTH : Created. *
  284. *=============================================================================================*/
  285. int CollectionClass::Class_ID(void) const
  286. {
  287. return RenderObjClass::CLASSID_COLLECTION;
  288. }
  289. /***********************************************************************************************
  290. * CollectionClass::Get_Num_Polys -- returns the number of polygons in this collection *
  291. * *
  292. * INPUT: *
  293. * *
  294. * OUTPUT: *
  295. * *
  296. * WARNINGS: *
  297. * *
  298. * HISTORY: *
  299. * 12/8/98 GTH : Created. *
  300. *=============================================================================================*/
  301. int CollectionClass::Get_Num_Polys(void) const
  302. {
  303. int pcount = 0;
  304. for (int i=0; i<SubObjects.Count(); i++) {
  305. pcount += SubObjects[i]->Get_Num_Polys();
  306. }
  307. return pcount;
  308. }
  309. /***********************************************************************************************
  310. * CollectionClass::Render -- render this collection *
  311. * *
  312. * INPUT: *
  313. * *
  314. * OUTPUT: *
  315. * *
  316. * WARNINGS: *
  317. * *
  318. * HISTORY: *
  319. * 12/8/98 GTH : Created. *
  320. *=============================================================================================*/
  321. void CollectionClass::Render(RenderInfoClass & rinfo)
  322. {
  323. if (Is_Not_Hidden_At_All() == false) {
  324. return;
  325. }
  326. if (Are_Sub_Object_Transforms_Dirty()) {
  327. Update_Sub_Object_Transforms();
  328. }
  329. for (int i=0; i<SubObjects.Count(); i++) {
  330. SubObjects[i]->Render(rinfo);
  331. }
  332. }
  333. /***********************************************************************************************
  334. * CollectionClass::Special_Render -- passes the special render call to all sub-objects *
  335. * *
  336. * INPUT: *
  337. * *
  338. * OUTPUT: *
  339. * *
  340. * WARNINGS: *
  341. * *
  342. * HISTORY: *
  343. * 3/2/99 GTH : Created. *
  344. *=============================================================================================*/
  345. void CollectionClass::Special_Render(SpecialRenderInfoClass & rinfo)
  346. {
  347. if (Is_Not_Hidden_At_All() == false) {
  348. return;
  349. }
  350. if (Are_Sub_Object_Transforms_Dirty()) {
  351. Update_Sub_Object_Transforms();
  352. }
  353. for (int i=0; i<SubObjects.Count(); i++) {
  354. SubObjects[i]->Special_Render(rinfo);
  355. }
  356. }
  357. /***********************************************************************************************
  358. * CollectionClass::Set_Transform -- set the transform for this collection *
  359. * *
  360. * INPUT: *
  361. * *
  362. * OUTPUT: *
  363. * *
  364. * WARNINGS: *
  365. * *
  366. * HISTORY: *
  367. * 12/8/98 GTH : Created. *
  368. *=============================================================================================*/
  369. void CollectionClass::Set_Transform(const Matrix3D &m)
  370. {
  371. RenderObjClass::Set_Transform(m);
  372. Set_Sub_Object_Transforms_Dirty(true);
  373. }
  374. /***********************************************************************************************
  375. * CollectionClass::Set_Position -- set the position for this collection *
  376. * *
  377. * INPUT: *
  378. * *
  379. * OUTPUT: *
  380. * *
  381. * WARNINGS: *
  382. * *
  383. * HISTORY: *
  384. * 12/8/98 GTH : Created. *
  385. *=============================================================================================*/
  386. void CollectionClass::Set_Position(const Vector3 &v)
  387. {
  388. RenderObjClass::Set_Position(v);
  389. Set_Sub_Object_Transforms_Dirty(true);
  390. }
  391. /***********************************************************************************************
  392. * CollectionClass::Get_Num_Sub_Objects -- returns the number of sub objects *
  393. * *
  394. * INPUT: *
  395. * *
  396. * OUTPUT: *
  397. * *
  398. * WARNINGS: *
  399. * *
  400. * HISTORY: *
  401. * 12/8/98 GTH : Created. *
  402. *=============================================================================================*/
  403. int CollectionClass::Get_Num_Sub_Objects(void) const
  404. {
  405. return SubObjects.Count();
  406. }
  407. /***********************************************************************************************
  408. * CollectionClass::Get_Sub_Object -- returns a pointer to the desired sub object *
  409. * *
  410. * INPUT: *
  411. * *
  412. * OUTPUT: *
  413. * *
  414. * WARNINGS: *
  415. * *
  416. * HISTORY: *
  417. * 12/8/98 GTH : Created. *
  418. *=============================================================================================*/
  419. RenderObjClass * CollectionClass::Get_Sub_Object(int index) const
  420. {
  421. if (SubObjects[index]) {
  422. SubObjects[index]->Add_Ref();
  423. }
  424. return SubObjects[index];
  425. }
  426. /***********************************************************************************************
  427. * CollectionClass::Add_Sub_Object -- adds another object into this collection *
  428. * *
  429. * INPUT: *
  430. * *
  431. * OUTPUT: *
  432. * *
  433. * WARNINGS: *
  434. * *
  435. * HISTORY: *
  436. * 12/8/98 GTH : Created. *
  437. *=============================================================================================*/
  438. int CollectionClass::Add_Sub_Object(RenderObjClass * subobj)
  439. {
  440. WWASSERT(subobj);
  441. subobj->Add_Ref();
  442. subobj->Set_Container(this);
  443. subobj->Set_Transform(Transform);
  444. int res = SubObjects.Add(subobj);
  445. Update_Sub_Object_Bits();
  446. Update_Obj_Space_Bounding_Volumes();
  447. if (Is_In_Scene()) {
  448. subobj->Notify_Added(Scene);
  449. }
  450. return res;
  451. }
  452. /***********************************************************************************************
  453. * CollectionClass::Remove_Sub_Object -- removes a sub object from this collection *
  454. * *
  455. * INPUT: *
  456. * *
  457. * OUTPUT: *
  458. * *
  459. * WARNINGS: *
  460. * *
  461. * HISTORY: *
  462. * 12/8/98 GTH : Created. *
  463. *=============================================================================================*/
  464. int CollectionClass::Remove_Sub_Object(RenderObjClass * robj)
  465. {
  466. if (robj == NULL) return 0;
  467. int res = 0;
  468. Matrix3D tm = Get_Transform();
  469. for (int i=0; i<SubObjects.Count(); i++) {
  470. if (robj == SubObjects[i]) {
  471. if (Is_In_Scene()) {
  472. SubObjects[i]->Notify_Removed(Scene);
  473. }
  474. SubObjects[i]->Set_Container(NULL);
  475. SubObjects[i]->Set_Transform(tm);
  476. SubObjects[i]->Release_Ref();
  477. res = SubObjects.Delete(i);
  478. break;
  479. }
  480. }
  481. if (res != 0) {
  482. Update_Sub_Object_Bits();
  483. Update_Obj_Space_Bounding_Volumes();
  484. }
  485. return res;
  486. }
  487. /***********************************************************************************************
  488. * CollectionClass::Cast_Ray -- passes the ray test to each sub object *
  489. * *
  490. * INPUT: *
  491. * *
  492. * OUTPUT: *
  493. * *
  494. * WARNINGS: *
  495. * *
  496. * HISTORY: *
  497. * 12/8/98 GTH : Created. *
  498. *=============================================================================================*/
  499. bool CollectionClass::Cast_Ray(RayCollisionTestClass & raytest)
  500. {
  501. bool res = false;
  502. for (int i=0; i<SubObjects.Count(); i++) {
  503. res |= SubObjects[i]->Cast_Ray(raytest);
  504. }
  505. return res;
  506. }
  507. /***********************************************************************************************
  508. * CollectionClass::Cast_AABox -- passes the axis-aligned box test to each sub object *
  509. * *
  510. * INPUT: *
  511. * *
  512. * OUTPUT: *
  513. * *
  514. * WARNINGS: *
  515. * *
  516. * HISTORY: *
  517. * 12/8/98 GTH : Created. *
  518. *=============================================================================================*/
  519. bool CollectionClass::Cast_AABox(AABoxCollisionTestClass & boxtest)
  520. {
  521. bool res = false;
  522. for (int i=0; i<SubObjects.Count(); i++) {
  523. res |= SubObjects[i]->Cast_AABox(boxtest);
  524. }
  525. return res;
  526. }
  527. /***********************************************************************************************
  528. * CollectionClass::Cast_OBBox -- passes the oriented box test to each sub object *
  529. * *
  530. * INPUT: *
  531. * *
  532. * OUTPUT: *
  533. * *
  534. * WARNINGS: *
  535. * *
  536. * HISTORY: *
  537. * 12/8/98 GTH : Created. *
  538. *=============================================================================================*/
  539. bool CollectionClass::Cast_OBBox(OBBoxCollisionTestClass & boxtest)
  540. {
  541. bool res = false;
  542. for (int i=0; i<SubObjects.Count(); i++) {
  543. res |= SubObjects[i]->Cast_OBBox(boxtest);
  544. }
  545. return res;
  546. }
  547. /***********************************************************************************************
  548. * CollectionClass::Intersect_AABox -- test for intersection with an AABox *
  549. * *
  550. * INPUT: *
  551. * *
  552. * OUTPUT: *
  553. * *
  554. * WARNINGS: *
  555. * *
  556. * HISTORY: *
  557. * 1/19/00 gth : Created. *
  558. *=============================================================================================*/
  559. bool CollectionClass::Intersect_AABox(AABoxIntersectionTestClass & boxtest)
  560. {
  561. bool res = false;
  562. for (int i=0; i<SubObjects.Count(); i++) {
  563. res |= SubObjects[i]->Intersect_AABox(boxtest);
  564. }
  565. return res;
  566. }
  567. /***********************************************************************************************
  568. * CollectionClass::Intersect_OBBox -- test for intersection with an OBBox *
  569. * *
  570. * INPUT: *
  571. * *
  572. * OUTPUT: *
  573. * *
  574. * WARNINGS: *
  575. * *
  576. * HISTORY: *
  577. * 1/19/00 gth : Created. *
  578. *=============================================================================================*/
  579. bool CollectionClass::Intersect_OBBox(OBBoxIntersectionTestClass & boxtest)
  580. {
  581. bool res = false;
  582. for (int i=0; i<SubObjects.Count(); i++) {
  583. res |= SubObjects[i]->Intersect_OBBox(boxtest);
  584. }
  585. return res;
  586. }
  587. /***********************************************************************************************
  588. * CollectionClass::Get_Obj_Space_Bounding_Sphere -- returns the object space bounding sphere. *
  589. * *
  590. * INPUT: *
  591. * *
  592. * OUTPUT: *
  593. * *
  594. * WARNINGS: *
  595. * *
  596. * HISTORY: *
  597. * 12/8/98 GTH : Created. *
  598. *=============================================================================================*/
  599. void CollectionClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  600. {
  601. sphere = BoundSphere;
  602. }
  603. /***********************************************************************************************
  604. * CollectionClass::Get_Obj_Space_Bounding_Box -- returns the object-space bounding box *
  605. * *
  606. * INPUT: *
  607. * *
  608. * OUTPUT: *
  609. * *
  610. * WARNINGS: *
  611. * *
  612. * HISTORY: *
  613. * 12/8/98 GTH : Created. *
  614. *=============================================================================================*/
  615. void CollectionClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  616. {
  617. box = BoundBox;
  618. }
  619. /***********************************************************************************************
  620. * CollectionClass::Snap_Point_Count -- returns the number of snap points in this collecion *
  621. * *
  622. * INPUT: *
  623. * *
  624. * OUTPUT: *
  625. * *
  626. * WARNINGS: *
  627. * *
  628. * HISTORY: *
  629. * 12/8/98 GTH : Created. *
  630. *=============================================================================================*/
  631. int CollectionClass::Snap_Point_Count(void)
  632. {
  633. if (SnapPoints) {
  634. return SnapPoints->Count();
  635. } else {
  636. return 0;
  637. }
  638. }
  639. /***********************************************************************************************
  640. * CollectionClass::Get_Snap_Point -- return the desired snap point *
  641. * *
  642. * This function will set the passed vector to be equal to the object space coordinates of *
  643. * the desired snap point. *
  644. * *
  645. * INPUT: *
  646. * *
  647. * OUTPUT: *
  648. * *
  649. * WARNINGS: *
  650. * *
  651. * HISTORY: *
  652. * 12/8/98 GTH : Created. *
  653. *=============================================================================================*/
  654. void CollectionClass::Get_Snap_Point(int index,Vector3 * set)
  655. {
  656. WWASSERT(set != NULL);
  657. if (SnapPoints) {
  658. *set = (*SnapPoints)[index];
  659. } else {
  660. set->X = set->Y = set->Z = 0;
  661. }
  662. }
  663. /***********************************************************************************************
  664. * CollectionClass::Scale -- scale the objects in this collection *
  665. * *
  666. * INPUT: *
  667. * *
  668. * OUTPUT: *
  669. * *
  670. * WARNINGS: *
  671. * *
  672. * HISTORY: *
  673. * 12/8/98 GTH : Created. *
  674. *=============================================================================================*/
  675. void CollectionClass::Scale(float scale)
  676. {
  677. for (int i=0; i<SubObjects.Count(); i++) {
  678. SubObjects[i]->Scale(scale);
  679. }
  680. }
  681. /***********************************************************************************************
  682. * CollectionClass::Scale -- scale the objects in this collection *
  683. * *
  684. * INPUT: *
  685. * *
  686. * OUTPUT: *
  687. * *
  688. * WARNINGS: *
  689. * *
  690. * HISTORY: *
  691. * 12/8/98 GTH : Created. *
  692. *=============================================================================================*/
  693. void CollectionClass::Scale(float scalex, float scaley, float scalez)
  694. {
  695. for (int i=0; i<SubObjects.Count(); i++) {
  696. SubObjects[i]->Scale(scalex,scaley,scalez);
  697. }
  698. }
  699. /***********************************************************************************************
  700. * CollectionClass::Update_Obj_Space_Bounding_Volumes -- recomputes the object space bounding *
  701. * *
  702. * INPUT: *
  703. * *
  704. * OUTPUT: *
  705. * *
  706. * WARNINGS: *
  707. * *
  708. * HISTORY: *
  709. * 12/8/98 GTH : Created. *
  710. *=============================================================================================*/
  711. void CollectionClass::Update_Obj_Space_Bounding_Volumes(void)
  712. {
  713. int i;
  714. if (SubObjects.Count() <= 0) {
  715. BoundSphere = SphereClass(Vector3(0,0,0),0);
  716. BoundBox.Center.Set(0,0,0);
  717. BoundBox.Extent.Set(0,0,0);
  718. return;
  719. }
  720. Matrix3D tm = Get_Transform();
  721. Set_Transform(Matrix3D(1));
  722. // loop through all sub-objects, combining their bounding spheres.
  723. BoundSphere = SubObjects[0]->Get_Bounding_Sphere();
  724. for (i=1; i < SubObjects.Count(); i++) {
  725. BoundSphere.Add_Sphere(SubObjects[i]->Get_Bounding_Sphere());
  726. }
  727. // loop through the sub-objects, computing a box in the root coordinate
  728. // system which bounds all of the meshes. Note that we've set the
  729. // root coordinate system to identity for this.
  730. MinMaxAABoxClass box(Vector3(FLT_MAX,FLT_MAX,FLT_MAX),Vector3(-FLT_MAX,-FLT_MAX,-FLT_MAX));
  731. for (i=0; i < SubObjects.Count(); i++) {
  732. box.Add_Box(SubObjects[i]->Get_Bounding_Box());
  733. }
  734. BoundBox.Init(box);
  735. Invalidate_Cached_Bounding_Volumes();
  736. // Now update the object space bounding volumes of this object's container:
  737. RenderObjClass *container = Get_Container();
  738. if (container) container->Update_Obj_Space_Bounding_Volumes();
  739. Set_Transform(tm);
  740. }
  741. /***********************************************************************************************
  742. * CollectionClass::Update_Sub_Object_Transforms -- recomputes all sub object transforms *
  743. * *
  744. * INPUT: *
  745. * *
  746. * OUTPUT: *
  747. * *
  748. * WARNINGS: *
  749. * *
  750. * HISTORY: *
  751. * 12/8/98 GTH : Created. *
  752. *=============================================================================================*/
  753. void CollectionClass::Update_Sub_Object_Transforms(void)
  754. {
  755. RenderObjClass::Update_Sub_Object_Transforms();
  756. for (int i=0; i<SubObjects.Count(); i++) {
  757. SubObjects[i]->Set_Transform(Transform);
  758. SubObjects[i]->Update_Sub_Object_Transforms();
  759. }
  760. Set_Sub_Object_Transforms_Dirty(false);
  761. }
  762. /***********************************************************************************************
  763. * CollectionClass::Get_Placeholder -- Returns information about a placeholder object.
  764. * *
  765. * INPUT: *
  766. * *
  767. * OUTPUT: *
  768. * *
  769. * WARNINGS: *
  770. * *
  771. * HISTORY: *
  772. * 4/28/99 PDS : Created. *
  773. *=============================================================================================*/
  774. bool CollectionClass::Get_Proxy (int index, ProxyClass &proxy) const
  775. {
  776. bool retval = false;
  777. if (index >= 0 && index < ProxyList.Count ()) {
  778. //
  779. // Return the proxy information to the caller
  780. //
  781. proxy = ProxyList[index];
  782. retval = true;
  783. }
  784. return retval;
  785. }
  786. /***********************************************************************************************
  787. * CollectionClass::Get_Proxy_Count -- Returns the count of proxy objects in the collection.
  788. * *
  789. * INPUT: *
  790. * *
  791. * OUTPUT: *
  792. * *
  793. * WARNINGS: *
  794. * *
  795. * HISTORY: *
  796. * 4/28/99 PDS : Created. *
  797. *=============================================================================================*/
  798. int CollectionClass::Get_Proxy_Count (void) const
  799. {
  800. return ProxyList.Count ();
  801. }
  802. /***********************************************************************************************
  803. * CollectionDefClass::CollectionDefClass -- constructor *
  804. * *
  805. * INPUT: *
  806. * *
  807. * OUTPUT: *
  808. * *
  809. * WARNINGS: *
  810. * *
  811. * HISTORY: *
  812. * 12/8/98 GTH : Created. *
  813. *=============================================================================================*/
  814. CollectionDefClass::CollectionDefClass(void)
  815. {
  816. SnapPoints = NULL;
  817. }
  818. /***********************************************************************************************
  819. * CollectionDefClass::~CollectionDefClass -- destructor for collection definition *
  820. * *
  821. * INPUT: *
  822. * *
  823. * OUTPUT: *
  824. * *
  825. * WARNINGS: *
  826. * *
  827. * HISTORY: *
  828. * 12/8/98 GTH : Created. *
  829. *=============================================================================================*/
  830. CollectionDefClass::~CollectionDefClass(void)
  831. {
  832. Free();
  833. }
  834. /***********************************************************************************************
  835. * CollectionDefClass::Free -- releases assets in use by a collection definition *
  836. * *
  837. * INPUT: *
  838. * *
  839. * OUTPUT: *
  840. * *
  841. * WARNINGS: *
  842. * *
  843. * HISTORY: *
  844. * 12/8/98 GTH : Created. *
  845. *=============================================================================================*/
  846. void CollectionDefClass::Free(void)
  847. {
  848. for (int i=0; i<ObjectNames.Count(); i++) {
  849. delete[] ObjectNames[i];
  850. }
  851. ProxyList.Delete_All ();
  852. }
  853. /***********************************************************************************************
  854. * CollectionDefClass::Get_Name -- returns name of the collection *
  855. * *
  856. * INPUT: *
  857. * *
  858. * OUTPUT: *
  859. * *
  860. * WARNINGS: *
  861. * *
  862. * HISTORY: *
  863. * 12/8/98 GTH : Created. *
  864. *=============================================================================================*/
  865. const char * CollectionDefClass::Get_Name(void) const
  866. {
  867. return Name;
  868. }
  869. /***********************************************************************************************
  870. * CollectionDefClass::Load -- loads a collection definition from a w3d file *
  871. * *
  872. * INPUT: *
  873. * *
  874. * OUTPUT: *
  875. * *
  876. * WARNINGS: *
  877. * *
  878. * HISTORY: *
  879. * 12/8/98 GTH : Created. *
  880. *=============================================================================================*/
  881. WW3DErrorType CollectionDefClass::Load(ChunkLoadClass & cload)
  882. {
  883. Free();
  884. // open the header chunk and read it in
  885. W3dCollectionHeaderStruct header;
  886. if (!cload.Open_Chunk()) goto Error;
  887. if (cload.Cur_Chunk_ID() != W3D_CHUNK_COLLECTION_HEADER) goto Error;
  888. if (cload.Read(&header,sizeof(header)) != sizeof(header)) goto Error;
  889. if (!cload.Close_Chunk()) goto Error;
  890. strncpy(Name,header.Name,W3D_NAME_LEN);
  891. ObjectNames.Resize(header.RenderObjectCount);
  892. while (cload.Open_Chunk()) {
  893. switch (cload.Cur_Chunk_ID())
  894. {
  895. case W3D_CHUNK_COLLECTION_OBJ_NAME:
  896. {
  897. WWASSERT(cload.Cur_Chunk_Length() > 0);
  898. char * name = new char [cload.Cur_Chunk_Length()];
  899. cload.Read(name,cload.Cur_Chunk_Length());
  900. ObjectNames.Add(name);
  901. break;
  902. }
  903. case W3D_CHUNK_PLACEHOLDER:
  904. {
  905. // Read the placeholder information from the chunk
  906. WWASSERT(cload.Cur_Chunk_Length() > 0);
  907. W3dPlaceholderStruct info;
  908. cload.Read(&info, sizeof (info));
  909. // Read the placeholder name from the chunk
  910. char *name = new char[info.name_len + 1];
  911. cload.Read(name, info.name_len);
  912. name[info.name_len] = 0;
  913. // Create a matrix from the data in the chunk
  914. Matrix3D transform (info.transform[0][0], info.transform[1][0], info.transform[2][0], info.transform[3][0],
  915. info.transform[0][1], info.transform[1][1], info.transform[2][1], info.transform[3][1],
  916. info.transform[0][2], info.transform[1][2], info.transform[2][2], info.transform[3][2]);
  917. // Add this placeholder to our list
  918. ProxyList.Add (ProxyClass (name, transform));
  919. // Free the name array
  920. delete [] name;
  921. break;
  922. }
  923. case W3D_CHUNK_POINTS:
  924. SnapPoints = NEW_REF(SnapPointsClass, ());
  925. SnapPoints->Load_W3D(cload);
  926. break;
  927. }
  928. cload.Close_Chunk();
  929. }
  930. return WW3D_ERROR_OK;
  931. Error:
  932. return WW3D_ERROR_LOAD_FAILED;
  933. }
  934. /***********************************************************************************************
  935. * CollectionLoaderClass::Load -- reads a collection from a w3d file *
  936. * *
  937. * INPUT: *
  938. * *
  939. * OUTPUT: *
  940. * *
  941. * WARNINGS: *
  942. * *
  943. * HISTORY: *
  944. * 12/8/98 GTH : Created. *
  945. *=============================================================================================*/
  946. PrototypeClass * CollectionLoaderClass::Load_W3D(ChunkLoadClass & cload)
  947. {
  948. CollectionDefClass * def = new CollectionDefClass;
  949. if (def == NULL) {
  950. return NULL;
  951. }
  952. if (def->Load(cload) != WW3D_ERROR_OK) {
  953. // load failed, delete the model and return an error
  954. delete def;
  955. return NULL;
  956. } else {
  957. // ok, accept this model!
  958. CollectionPrototypeClass * proto = new CollectionPrototypeClass(def);
  959. return proto;
  960. }
  961. }