composite.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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/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. BaseModelName=name;
  193. }
  194. /***********************************************************************************************
  195. * CompositeRenderObjClass::Get_Num_Polys -- returns the number of polys *
  196. * *
  197. * INPUT: *
  198. * *
  199. * OUTPUT: *
  200. * *
  201. * WARNINGS: *
  202. * *
  203. * HISTORY: *
  204. * 1/26/00 gth : Created. *
  205. *=============================================================================================*/
  206. int CompositeRenderObjClass::Get_Num_Polys(void) const
  207. {
  208. int count = 0;
  209. for (int ni = 0; ni < Get_Num_Sub_Objects(); ni++) {
  210. RenderObjClass * robj = Get_Sub_Object(ni);
  211. WWASSERT(robj);
  212. count += robj->Get_Num_Polys();
  213. robj->Release_Ref();
  214. }
  215. return count;
  216. }
  217. /***********************************************************************************************
  218. * CompositeRenderObjClass::Notify_Added -- notify all sub-objects that they were added *
  219. * *
  220. * INPUT: *
  221. * *
  222. * OUTPUT: *
  223. * *
  224. * WARNINGS: *
  225. * *
  226. * HISTORY: *
  227. * 1/26/00 gth : Created. *
  228. *=============================================================================================*/
  229. void CompositeRenderObjClass::Notify_Added(SceneClass * scene)
  230. {
  231. RenderObjClass::Notify_Added(scene);
  232. for (int ni = 0; ni < Get_Num_Sub_Objects(); ni++) {
  233. RenderObjClass * robj = Get_Sub_Object(ni);
  234. WWASSERT(robj);
  235. robj->Notify_Added(scene);
  236. robj->Release_Ref();
  237. }
  238. }
  239. /***********************************************************************************************
  240. * CompositeRenderObjClass::Notify_Removed -- notifies all subobjs they were removed from the *
  241. * *
  242. * INPUT: *
  243. * *
  244. * OUTPUT: *
  245. * *
  246. * WARNINGS: *
  247. * *
  248. * HISTORY: *
  249. * 1/26/00 gth : Created. *
  250. *=============================================================================================*/
  251. void CompositeRenderObjClass::Notify_Removed(SceneClass * scene)
  252. {
  253. for (int ni = 0; ni < Get_Num_Sub_Objects(); ni++) {
  254. RenderObjClass * robj = Get_Sub_Object(ni);
  255. WWASSERT(robj);
  256. robj->Notify_Removed(scene);
  257. robj->Release_Ref();
  258. }
  259. RenderObjClass::Notify_Removed(scene);
  260. }
  261. /***********************************************************************************************
  262. * CompositeRenderObjClass::Cast_Ray -- cast a ray against this object *
  263. * *
  264. * INPUT: *
  265. * *
  266. * OUTPUT: *
  267. * *
  268. * WARNINGS: *
  269. * *
  270. * HISTORY: *
  271. * 1/26/00 gth : Created. *
  272. *=============================================================================================*/
  273. bool CompositeRenderObjClass::Cast_Ray(RayCollisionTestClass & raytest)
  274. {
  275. bool res = false;
  276. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  277. RenderObjClass * robj = Get_Sub_Object(i);
  278. WWASSERT(robj);
  279. res |= robj->Cast_Ray(raytest);
  280. robj->Release_Ref();
  281. }
  282. return res;
  283. }
  284. /***********************************************************************************************
  285. * CompositeRenderObjClass::Cast_AABox -- cast a swept AABox against this object *
  286. * *
  287. * INPUT: *
  288. * *
  289. * OUTPUT: *
  290. * *
  291. * WARNINGS: *
  292. * *
  293. * HISTORY: *
  294. * 1/26/00 gth : Created. *
  295. *=============================================================================================*/
  296. bool CompositeRenderObjClass::Cast_AABox(AABoxCollisionTestClass & boxtest)
  297. {
  298. bool res = false;
  299. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  300. RenderObjClass * robj = Get_Sub_Object(i);
  301. WWASSERT(robj);
  302. res |= robj->Cast_AABox(boxtest);
  303. robj->Release_Ref();
  304. }
  305. return res;
  306. }
  307. /***********************************************************************************************
  308. * CompositeRenderObjClass::Cast_OBBox -- cast a swept OBBox against this object *
  309. * *
  310. * INPUT: *
  311. * *
  312. * OUTPUT: *
  313. * *
  314. * WARNINGS: *
  315. * *
  316. * HISTORY: *
  317. * 1/26/00 gth : Created. *
  318. *=============================================================================================*/
  319. bool CompositeRenderObjClass::Cast_OBBox(OBBoxCollisionTestClass & boxtest)
  320. {
  321. bool res = false;
  322. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  323. RenderObjClass * robj = Get_Sub_Object(i);
  324. WWASSERT(robj);
  325. res |= robj->Cast_OBBox(boxtest);
  326. robj->Release_Ref();
  327. }
  328. return res;
  329. }
  330. /***********************************************************************************************
  331. * CompositeRenderObjClass::Intersect_AABox -- intersect this object with an AABox *
  332. * *
  333. * INPUT: *
  334. * *
  335. * OUTPUT: *
  336. * *
  337. * WARNINGS: *
  338. * *
  339. * HISTORY: *
  340. * 1/26/00 gth : Created. *
  341. *=============================================================================================*/
  342. bool CompositeRenderObjClass::Intersect_AABox(AABoxIntersectionTestClass & boxtest)
  343. {
  344. bool res = false;
  345. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  346. RenderObjClass * robj = Get_Sub_Object(i);
  347. WWASSERT(robj);
  348. res |= robj->Intersect_AABox(boxtest);
  349. robj->Release_Ref();
  350. }
  351. return res;
  352. }
  353. /***********************************************************************************************
  354. * CompositeRenderObjClass::Intersect_OBBox -- intersect this object with an OBBox *
  355. * *
  356. * INPUT: *
  357. * *
  358. * OUTPUT: *
  359. * *
  360. * WARNINGS: *
  361. * *
  362. * HISTORY: *
  363. * 1/26/00 gth : Created. *
  364. *=============================================================================================*/
  365. bool CompositeRenderObjClass::Intersect_OBBox(OBBoxIntersectionTestClass & boxtest)
  366. {
  367. bool res = false;
  368. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  369. RenderObjClass * robj = Get_Sub_Object(i);
  370. WWASSERT(robj);
  371. res |= robj->Intersect_OBBox(boxtest);
  372. robj->Release_Ref();
  373. }
  374. return res;
  375. }
  376. /***********************************************************************************************
  377. * CompositeRenderObjClass::Create_Decal -- create a decal on this object *
  378. * *
  379. * INPUT: *
  380. * *
  381. * OUTPUT: *
  382. * *
  383. * WARNINGS: *
  384. * *
  385. * HISTORY: *
  386. * 1/26/00 gth : Created. *
  387. *=============================================================================================*/
  388. void CompositeRenderObjClass::Create_Decal(DecalGeneratorClass * generator)
  389. {
  390. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  391. RenderObjClass * robj = Get_Sub_Object(i);
  392. WWASSERT(robj);
  393. robj->Create_Decal(generator);
  394. robj->Release_Ref();
  395. }
  396. }
  397. /***********************************************************************************************
  398. * CompositeRenderObjClass::Delete_Decal -- remove a logical decal from this object *
  399. * *
  400. * This internally removes all decals with the given ID. The ID comes from the generator *
  401. * which was used to create the decals. *
  402. * *
  403. * INPUT: *
  404. * *
  405. * OUTPUT: *
  406. * *
  407. * WARNINGS: *
  408. * *
  409. * HISTORY: *
  410. * 1/26/00 gth : Created. *
  411. *=============================================================================================*/
  412. void CompositeRenderObjClass::Delete_Decal(uint32 decal_id)
  413. {
  414. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  415. RenderObjClass * robj = Get_Sub_Object(i);
  416. WWASSERT(robj);
  417. robj->Delete_Decal(decal_id);
  418. robj->Release_Ref();
  419. }
  420. }
  421. /***********************************************************************************************
  422. * CompositeRenderObjClass::Update_Obj_Space_Bounding_Volumes -- updates the object-space BVs *
  423. * *
  424. * INPUT: *
  425. * *
  426. * OUTPUT: *
  427. * *
  428. * WARNINGS: *
  429. * *
  430. * HISTORY: *
  431. * 1/26/00 gth : Created. *
  432. *=============================================================================================*/
  433. void CompositeRenderObjClass::Update_Obj_Space_Bounding_Volumes(void)
  434. {
  435. int i;
  436. RenderObjClass * robj = NULL;
  437. // if we don't have any sub objects, just set default bounds
  438. if (Get_Num_Sub_Objects() <= 0) {
  439. ObjSphere.Init(Vector3(0,0,0),0);
  440. ObjBox.Center.Set(0,0,0);
  441. ObjBox.Extent.Set(0,0,0);
  442. return;
  443. }
  444. AABoxClass obj_aabox;
  445. MinMaxAABoxClass box;
  446. SphereClass sphere;
  447. // loop through all sub-objects, combining their object-space bounding spheres and boxes.
  448. robj = Get_Sub_Object(0);
  449. WWASSERT(robj);
  450. robj->Get_Obj_Space_Bounding_Sphere(ObjSphere);
  451. robj->Get_Obj_Space_Bounding_Box(obj_aabox);
  452. robj->Release_Ref();
  453. box.Init(obj_aabox);
  454. for (i=1; i<Get_Num_Sub_Objects(); i++) {
  455. robj = Get_Sub_Object(i);
  456. WWASSERT(robj);
  457. robj->Get_Obj_Space_Bounding_Sphere(sphere);
  458. robj->Get_Obj_Space_Bounding_Box(obj_aabox);
  459. ObjSphere.Add_Sphere(sphere);
  460. box.Add_Box(obj_aabox);
  461. robj->Release_Ref();
  462. }
  463. ObjBox.Init(box);
  464. Invalidate_Cached_Bounding_Volumes();
  465. // Now update the object space bounding volumes of this object's container:
  466. RenderObjClass *container = Get_Container();
  467. if (container) container->Update_Obj_Space_Bounding_Volumes();
  468. }
  469. /***********************************************************************************************
  470. * CompositeRenderObjClass::Set_User_Data -- set the userdata *
  471. * *
  472. * INPUT: *
  473. * *
  474. * OUTPUT: *
  475. * *
  476. * WARNINGS: *
  477. * *
  478. * HISTORY: *
  479. * 1/26/00 gth : Created. *
  480. *=============================================================================================*/
  481. void CompositeRenderObjClass::Set_User_Data(void *value, bool recursive)
  482. {
  483. RenderObjClass::Set_User_Data(value);
  484. if (recursive) {
  485. for (int i=0; i<Get_Num_Sub_Objects(); i++) {
  486. RenderObjClass * robj = Get_Sub_Object(i);
  487. WWASSERT(robj);
  488. robj->Set_User_Data(value,recursive);
  489. robj->Release_Ref();
  490. }
  491. }
  492. }