collect.cpp 61 KB

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