composite.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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/composite.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 11/27/01 12:45a $*
  29. * *
  30. * $Revision:: 6 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * CompositeRenderObjClass::CompositeRenderObjClass -- Constructor *
  35. * CompositeRenderObjClass::CompositeRenderObjClass -- copy constructor *
  36. * CompositeRenderObjClass::~CompositeRenderObjClass -- Destructor *
  37. * CompositeRenderObjClass::operator -- assignment operator *
  38. * CompositeRenderObjClass::Restart -- Recursively call Restart on all sub-objects *
  39. * CompositeRenderObjClass::Get_Name -- returns the name of this render object *
  40. * CompositeRenderObjClass::Set_Name -- sets the name of this render object *
  41. * CompositeRenderObjClass::Set_Base_Model_Name -- sets the "base-model-name" *
  42. * CompositeRenderObjClass::Get_Num_Polys -- returns the number of polys *
  43. * CompositeRenderObjClass::Notify_Added -- notify all sub-objects that they were added *
  44. * CompositeRenderObjClass::Notify_Removed -- notifies all subobjs they were removed from th *
  45. * CompositeRenderObjClass::Cast_Ray -- cast a ray against this object *
  46. * CompositeRenderObjClass::Cast_AABox -- cast a swept AABox against this object *
  47. * CompositeRenderObjClass::Cast_OBBox -- cast a swept OBBox against this object *
  48. * CompositeRenderObjClass::Intersect_AABox -- intersect this object with an AABox *
  49. * CompositeRenderObjClass::Intersect_OBBox -- intersect this object with an OBBox *
  50. * CompositeRenderObjClass::Create_Decal -- create a decal on this object *
  51. * CompositeRenderObjClass::Delete_Decal -- remove a logical decal from this object *
  52. * CompositeRenderObjClass::Update_Obj_Space_Bounding_Volumes -- updates the object-space BV *
  53. * CompositeRenderObjClass::Set_User_Data -- set the userdata pointer *
  54. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  55. #include "composite.h"
  56. #include "wwdebug.h"
  57. #include <stdlib.h>
  58. #include <string.h>
  59. /***********************************************************************************************
  60. * CompositeRenderObjClass::CompositeRenderObjClass -- Constructor *
  61. * *
  62. * INPUT: *
  63. * *
  64. * OUTPUT: *
  65. * *
  66. * WARNINGS: *
  67. * *
  68. * HISTORY: *
  69. *=============================================================================================*/
  70. CompositeRenderObjClass::CompositeRenderObjClass(void)
  71. {
  72. }
  73. /***********************************************************************************************
  74. * CompositeRenderObjClass::CompositeRenderObjClass -- copy constructor *
  75. * *
  76. * INPUT: *
  77. * *
  78. * OUTPUT: *
  79. * *
  80. * WARNINGS: *
  81. * *
  82. * HISTORY: *
  83. * 1/26/00 gth : Created. *
  84. *=============================================================================================*/
  85. CompositeRenderObjClass::CompositeRenderObjClass(const CompositeRenderObjClass & that)
  86. {
  87. Set_Name(that.Get_Name());
  88. Set_Base_Model_Name(that.Get_Base_Model_Name());
  89. }
  90. /***********************************************************************************************
  91. * CompositeRenderObjClass::~CompositeRenderObjClass -- Destructor *
  92. * *
  93. * INPUT: *
  94. * *
  95. * OUTPUT: *
  96. * *
  97. * WARNINGS: *
  98. * *
  99. * HISTORY: *
  100. * 1/26/00 gth : Created. *
  101. *=============================================================================================*/
  102. CompositeRenderObjClass::~CompositeRenderObjClass(void)
  103. {
  104. }
  105. /***********************************************************************************************
  106. * CompositeRenderObjClass::operator -- assignment operator *
  107. * *
  108. * INPUT: *
  109. * *
  110. * OUTPUT: *
  111. * *
  112. * WARNINGS: *
  113. * *
  114. * HISTORY: *
  115. * 1/26/00 gth : Created. *
  116. *=============================================================================================*/
  117. CompositeRenderObjClass & CompositeRenderObjClass::operator = (const CompositeRenderObjClass & that)
  118. {
  119. Set_Name(that.Get_Name());
  120. Set_Base_Model_Name(that.Get_Base_Model_Name());
  121. return *this;
  122. }
  123. /***********************************************************************************************
  124. * CompositeRenderObjClass::Restart -- Recursively call Restart on all sub-objects *
  125. * *
  126. * INPUT: *
  127. * *
  128. * OUTPUT: *
  129. * *
  130. * WARNINGS: *
  131. * *
  132. * HISTORY: *
  133. * 5/30/2001 gth : Created. *
  134. *=============================================================================================*/
  135. void CompositeRenderObjClass::Restart(void)
  136. {
  137. for (int ni = 0; ni < Get_Num_Sub_Objects(); ni++) {
  138. RenderObjClass * robj = Get_Sub_Object(ni);
  139. WWASSERT(robj);
  140. robj->Restart();
  141. robj->Release_Ref();
  142. }
  143. }
  144. /***********************************************************************************************
  145. * CompositeRenderObjClass::Get_Name -- returns the name of this render object *
  146. * *
  147. * INPUT: *
  148. * *
  149. * OUTPUT: *
  150. * *
  151. * WARNINGS: *
  152. * *
  153. * HISTORY: *
  154. * 1/26/00 gth : Created. *
  155. *=============================================================================================*/
  156. const char * CompositeRenderObjClass::Get_Name(void) const
  157. {
  158. return Name;
  159. }
  160. /***********************************************************************************************
  161. * CompositeRenderObjClass::Set_Name -- sets the name of this render object *
  162. * *
  163. * INPUT: *
  164. * *
  165. * OUTPUT: *
  166. * *
  167. * WARNINGS: *
  168. * *
  169. * HISTORY: *
  170. * 1/26/00 gth : Created. *
  171. *=============================================================================================*/
  172. void CompositeRenderObjClass::Set_Name(const char * name)
  173. {
  174. Name=name;
  175. }
  176. /***********************************************************************************************
  177. * CompositeRenderObjClass::Set_Base_Model_Name -- sets the "base-model-name" *
  178. * *
  179. * The base-model-name was needed by the aggregate code. Ask Patrick Smith about it :-) *
  180. * *
  181. * INPUT: *
  182. * *
  183. * OUTPUT: *
  184. * *
  185. * WARNINGS: *
  186. * *
  187. * HISTORY: *
  188. * 1/26/00 gth : Created. *
  189. *=============================================================================================*/
  190. void CompositeRenderObjClass::Set_Base_Model_Name(const char *name)
  191. {
  192. // NULL is a legal value for BaseModelName. Unfortunately,
  193. // StringClass::operator= does not modify the string when
  194. // assigning NULL, so we explicitly handle that case here.
  195. if (name != 0) {
  196. BaseModelName = name;
  197. } else {
  198. BaseModelName = "";
  199. }
  200. }
  201. /***********************************************************************************************
  202. * CompositeRenderObjClass::Get_Num_Polys -- returns the number of polys *
  203. * *
  204. * INPUT: *
  205. * *
  206. * OUTPUT: *
  207. * *
  208. * WARNINGS: *
  209. * *
  210. * HISTORY: *
  211. * 1/26/00 gth : Created. *
  212. *=============================================================================================*/
  213. int CompositeRenderObjClass::Get_Num_Polys(void) const
  214. {
  215. int count = 0;
  216. for (int ni = 0; ni < Get_Num_Sub_Objects(); ni++) {
  217. RenderObjClass * robj = Get_Sub_Object(ni);
  218. WWASSERT(robj);
  219. count += robj->Get_Num_Polys();
  220. robj->Release_Ref();
  221. }
  222. return count;
  223. }
  224. /***********************************************************************************************
  225. * CompositeRenderObjClass::Notify_Added -- notify all sub-objects that they were added *
  226. * *
  227. * INPUT: *
  228. * *
  229. * OUTPUT: *
  230. * *
  231. * WARNINGS: *
  232. * *
  233. * HISTORY: *
  234. * 1/26/00 gth : Created. *
  235. *=============================================================================================*/
  236. void CompositeRenderObjClass::Notify_Added(SceneClass * scene)
  237. {
  238. RenderObjClass::Notify_Added(scene);
  239. for (int ni = 0; ni < Get_Num_Sub_Objects(); ni++) {
  240. RenderObjClass * robj = Get_Sub_Object(ni);
  241. WWASSERT(robj);
  242. robj->Notify_Added(scene);
  243. robj->Release_Ref();
  244. }
  245. }
  246. /***********************************************************************************************
  247. * CompositeRenderObjClass::Notify_Removed -- notifies all subobjs they were removed from the *
  248. * *
  249. * INPUT: *
  250. * *
  251. * OUTPUT: *
  252. * *
  253. * WARNINGS: *
  254. * *
  255. * HISTORY: *
  256. * 1/26/00 gth : Created. *
  257. *=============================================================================================*/
  258. void CompositeRenderObjClass::Notify_Removed(SceneClass * scene)
  259. {
  260. for (int ni = 0; ni < Get_Num_Sub_Objects(); ni++) {
  261. RenderObjClass * robj = Get_Sub_Object(ni);
  262. WWASSERT(robj);
  263. robj->Notify_Removed(scene);
  264. robj->Release_Ref();
  265. }
  266. RenderObjClass::Notify_Removed(scene);
  267. }
  268. /***********************************************************************************************
  269. * CompositeRenderObjClass::Cast_Ray -- cast a ray against this object *
  270. * *
  271. * INPUT: *
  272. * *
  273. * OUTPUT: *
  274. * *
  275. * WARNINGS: *
  276. * *
  277. * HISTORY: *
  278. * 1/26/00 gth : Created. *
  279. *=============================================================================================*/
  280. bool CompositeRenderObjClass::Cast_Ray(RayCollisionTestClass & raytest)
  281. {
  282. bool res = false;
  283. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  284. RenderObjClass * robj = Get_Sub_Object(i);
  285. WWASSERT(robj);
  286. res |= robj->Cast_Ray(raytest);
  287. robj->Release_Ref();
  288. }
  289. return res;
  290. }
  291. /***********************************************************************************************
  292. * CompositeRenderObjClass::Cast_AABox -- cast a swept AABox against this object *
  293. * *
  294. * INPUT: *
  295. * *
  296. * OUTPUT: *
  297. * *
  298. * WARNINGS: *
  299. * *
  300. * HISTORY: *
  301. * 1/26/00 gth : Created. *
  302. *=============================================================================================*/
  303. bool CompositeRenderObjClass::Cast_AABox(AABoxCollisionTestClass & boxtest)
  304. {
  305. bool res = false;
  306. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  307. RenderObjClass * robj = Get_Sub_Object(i);
  308. WWASSERT(robj);
  309. res |= robj->Cast_AABox(boxtest);
  310. robj->Release_Ref();
  311. }
  312. return res;
  313. }
  314. /***********************************************************************************************
  315. * CompositeRenderObjClass::Cast_OBBox -- cast a swept OBBox against this object *
  316. * *
  317. * INPUT: *
  318. * *
  319. * OUTPUT: *
  320. * *
  321. * WARNINGS: *
  322. * *
  323. * HISTORY: *
  324. * 1/26/00 gth : Created. *
  325. *=============================================================================================*/
  326. bool CompositeRenderObjClass::Cast_OBBox(OBBoxCollisionTestClass & boxtest)
  327. {
  328. bool res = false;
  329. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  330. RenderObjClass * robj = Get_Sub_Object(i);
  331. WWASSERT(robj);
  332. res |= robj->Cast_OBBox(boxtest);
  333. robj->Release_Ref();
  334. }
  335. return res;
  336. }
  337. /***********************************************************************************************
  338. * CompositeRenderObjClass::Intersect_AABox -- intersect this object with an AABox *
  339. * *
  340. * INPUT: *
  341. * *
  342. * OUTPUT: *
  343. * *
  344. * WARNINGS: *
  345. * *
  346. * HISTORY: *
  347. * 1/26/00 gth : Created. *
  348. *=============================================================================================*/
  349. bool CompositeRenderObjClass::Intersect_AABox(AABoxIntersectionTestClass & boxtest)
  350. {
  351. bool res = false;
  352. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  353. RenderObjClass * robj = Get_Sub_Object(i);
  354. WWASSERT(robj);
  355. res |= robj->Intersect_AABox(boxtest);
  356. robj->Release_Ref();
  357. }
  358. return res;
  359. }
  360. /***********************************************************************************************
  361. * CompositeRenderObjClass::Intersect_OBBox -- intersect this object with an OBBox *
  362. * *
  363. * INPUT: *
  364. * *
  365. * OUTPUT: *
  366. * *
  367. * WARNINGS: *
  368. * *
  369. * HISTORY: *
  370. * 1/26/00 gth : Created. *
  371. *=============================================================================================*/
  372. bool CompositeRenderObjClass::Intersect_OBBox(OBBoxIntersectionTestClass & boxtest)
  373. {
  374. bool res = false;
  375. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  376. RenderObjClass * robj = Get_Sub_Object(i);
  377. WWASSERT(robj);
  378. res |= robj->Intersect_OBBox(boxtest);
  379. robj->Release_Ref();
  380. }
  381. return res;
  382. }
  383. /***********************************************************************************************
  384. * CompositeRenderObjClass::Create_Decal -- create a decal on this object *
  385. * *
  386. * INPUT: *
  387. * *
  388. * OUTPUT: *
  389. * *
  390. * WARNINGS: *
  391. * *
  392. * HISTORY: *
  393. * 1/26/00 gth : Created. *
  394. *=============================================================================================*/
  395. void CompositeRenderObjClass::Create_Decal(DecalGeneratorClass * generator)
  396. {
  397. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  398. RenderObjClass * robj = Get_Sub_Object(i);
  399. WWASSERT(robj);
  400. robj->Create_Decal(generator);
  401. robj->Release_Ref();
  402. }
  403. }
  404. /***********************************************************************************************
  405. * CompositeRenderObjClass::Delete_Decal -- remove a logical decal from this object *
  406. * *
  407. * This internally removes all decals with the given ID. The ID comes from the generator *
  408. * which was used to create the decals. *
  409. * *
  410. * INPUT: *
  411. * *
  412. * OUTPUT: *
  413. * *
  414. * WARNINGS: *
  415. * *
  416. * HISTORY: *
  417. * 1/26/00 gth : Created. *
  418. *=============================================================================================*/
  419. void CompositeRenderObjClass::Delete_Decal(uint32 decal_id)
  420. {
  421. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  422. RenderObjClass * robj = Get_Sub_Object(i);
  423. WWASSERT(robj);
  424. robj->Delete_Decal(decal_id);
  425. robj->Release_Ref();
  426. }
  427. }
  428. /***********************************************************************************************
  429. * CompositeRenderObjClass::Update_Obj_Space_Bounding_Volumes -- updates the object-space BVs *
  430. * *
  431. * INPUT: *
  432. * *
  433. * OUTPUT: *
  434. * *
  435. * WARNINGS: *
  436. * *
  437. * HISTORY: *
  438. * 1/26/00 gth : Created. *
  439. *=============================================================================================*/
  440. void CompositeRenderObjClass::Update_Obj_Space_Bounding_Volumes(void)
  441. {
  442. int i;
  443. RenderObjClass * robj = NULL;
  444. // if we don't have any sub objects, just set default bounds
  445. if (Get_Num_Sub_Objects() <= 0) {
  446. ObjSphere.Init(Vector3(0,0,0),0);
  447. ObjBox.Center.Set(0,0,0);
  448. ObjBox.Extent.Set(0,0,0);
  449. return;
  450. }
  451. AABoxClass obj_aabox;
  452. MinMaxAABoxClass box;
  453. SphereClass sphere;
  454. // loop through all sub-objects, combining their object-space bounding spheres and boxes.
  455. robj = Get_Sub_Object(0);
  456. WWASSERT(robj);
  457. robj->Get_Obj_Space_Bounding_Sphere(ObjSphere);
  458. robj->Get_Obj_Space_Bounding_Box(obj_aabox);
  459. robj->Release_Ref();
  460. box.Init(obj_aabox);
  461. for (i=1; i<Get_Num_Sub_Objects(); i++) {
  462. robj = Get_Sub_Object(i);
  463. WWASSERT(robj);
  464. robj->Get_Obj_Space_Bounding_Sphere(sphere);
  465. robj->Get_Obj_Space_Bounding_Box(obj_aabox);
  466. ObjSphere.Add_Sphere(sphere);
  467. box.Add_Box(obj_aabox);
  468. robj->Release_Ref();
  469. }
  470. ObjBox.Init(box);
  471. Invalidate_Cached_Bounding_Volumes();
  472. // Now update the object space bounding volumes of this object's container:
  473. RenderObjClass *container = Get_Container();
  474. if (container) container->Update_Obj_Space_Bounding_Volumes();
  475. }
  476. /***********************************************************************************************
  477. * CompositeRenderObjClass::Set_User_Data -- set the userdata *
  478. * *
  479. * INPUT: *
  480. * *
  481. * OUTPUT: *
  482. * *
  483. * WARNINGS: *
  484. * *
  485. * HISTORY: *
  486. * 1/26/00 gth : Created. *
  487. *=============================================================================================*/
  488. void CompositeRenderObjClass::Set_User_Data(void *value, bool recursive)
  489. {
  490. RenderObjClass::Set_User_Data(value);
  491. if (recursive) {
  492. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  493. RenderObjClass * robj = Get_Sub_Object(i);
  494. WWASSERT(robj);
  495. robj->Set_User_Data(value,recursive);
  496. robj->Release_Ref();
  497. }
  498. }
  499. }
  500. const char * CompositeRenderObjClass::Get_Base_Model_Name (void) const
  501. {
  502. if (BaseModelName.Is_Empty()) {
  503. return NULL;
  504. }
  505. return BaseModelName;
  506. }