LightNode.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 : LevelEdit *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/LevelEdit/LightNode.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 2/14/02 5:14p $*
  29. * *
  30. * $Revision:: 21 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "lightnode.h"
  37. #include "sceneeditor.h"
  38. #include "lightdefinition.h"
  39. #include "filemgr.h"
  40. #include "_assetmgr.h"
  41. #include "editorassetmgr.h"
  42. #include "w3d_file.h"
  43. #include "cameramgr.h"
  44. #include "collisiongroups.h"
  45. #include "persistfactory.h"
  46. #include "editorchunkids.h"
  47. #include "preset.h"
  48. #include "light.h"
  49. #include "nodeinfopage.h"
  50. #include "positionpage.h"
  51. #include "preset.h"
  52. #include "editorpropsheet.h"
  53. #include "presetsettingstab.h"
  54. #include "nodemgr.h"
  55. #include "matinfo.h"
  56. #include "modelutils.h"
  57. //////////////////////////////////////////////////////////////////////////////
  58. // Persist factory
  59. //////////////////////////////////////////////////////////////////////////////
  60. SimplePersistFactoryClass<LightNodeClass, CHUNKID_NODE_LIGHT> _LightNodePersistFactory;
  61. enum
  62. {
  63. CHUNKID_VARIABLES = 0x01040345,
  64. CHUNKID_BASE_CLASS,
  65. CHUNKID_INSTANCE_SETTINGS
  66. };
  67. enum
  68. {
  69. VARID_VIS_SECTOR_ID = 1,
  70. };
  71. //////////////////////////////////////////////////////////////////////////////
  72. //
  73. // LightNodeClass
  74. //
  75. //////////////////////////////////////////////////////////////////////////////
  76. LightNodeClass::LightNodeClass (PresetClass *preset)
  77. : m_LightPhysObj (NULL),
  78. m_DisplayObj (NULL),
  79. m_UsePreset (true),
  80. m_Sphere (NULL),
  81. m_VisSectorID (0xFFFFFFFF),
  82. NodeClass (preset)
  83. {
  84. return ;
  85. }
  86. //////////////////////////////////////////////////////////////////////////////
  87. //
  88. // LightNodeClass
  89. //
  90. //////////////////////////////////////////////////////////////////////////////
  91. LightNodeClass::LightNodeClass (const LightNodeClass &src)
  92. : m_LightPhysObj (NULL),
  93. m_DisplayObj (NULL),
  94. m_UsePreset (true),
  95. m_Sphere (NULL),
  96. m_VisSectorID (0xFFFFFFFF),
  97. NodeClass (NULL)
  98. {
  99. *this = src;
  100. return ;
  101. }
  102. //////////////////////////////////////////////////////////////////////////////
  103. //
  104. // ~LightNodeClass
  105. //
  106. //////////////////////////////////////////////////////////////////////////////
  107. LightNodeClass::~LightNodeClass (void)
  108. {
  109. Remove_From_Scene ();
  110. MEMBER_RELEASE (m_LightPhysObj);
  111. MEMBER_RELEASE (m_DisplayObj);
  112. MEMBER_RELEASE (m_Sphere);
  113. return ;
  114. }
  115. //////////////////////////////////////////////////////////////////////////////
  116. //
  117. // Initialize
  118. //
  119. // Note: This may be called more than once. It is used as an 'initialize'
  120. // and a 're-initialize'.
  121. //
  122. //////////////////////////////////////////////////////////////////////////////
  123. void
  124. LightNodeClass::Initialize (void)
  125. {
  126. MEMBER_RELEASE (m_LightPhysObj);
  127. MEMBER_RELEASE (m_DisplayObj);
  128. //
  129. // Use the preset's settings (if desired)
  130. //
  131. if (m_UsePreset == true && m_Preset != NULL) {
  132. LightDefinitionClass *definition = static_cast<LightDefinitionClass *> (m_Preset->Get_Definition ());
  133. if (definition != NULL) {
  134. m_InstanceSettings = *definition;
  135. }
  136. }
  137. //
  138. // Create the light render-object this node depends on
  139. //
  140. LightClass *light_obj = new LightClass;
  141. m_LightPhysObj = new LightPhysClass(true);
  142. m_DisplayObj = new DecorationPhysClass;
  143. //
  144. // Configure the physics object with information about
  145. // its new render object and collision data.
  146. //
  147. m_LightPhysObj->Set_Model (light_obj);
  148. m_DisplayObj->Set_Model_By_Name ("POINTLIGHT");
  149. m_DisplayObj->Peek_Model ()->Set_User_Data ((PVOID)&m_HitTestInfo, FALSE);
  150. ::Set_Model_Collision_Type (m_DisplayObj->Peek_Model (), COLLISION_TYPE_6);
  151. m_LightPhysObj->Set_Transform (m_Transform);
  152. m_DisplayObj->Set_Transform (m_Transform);
  153. //
  154. // Make sure the light settings are correct
  155. //
  156. Update_Light ();
  157. // Release our hold on the light object pointer
  158. MEMBER_RELEASE (light_obj);
  159. return ;
  160. }
  161. ////////////////////////////////////////////////////////////////
  162. //
  163. // Get_Factory
  164. //
  165. ////////////////////////////////////////////////////////////////
  166. const PersistFactoryClass &
  167. LightNodeClass::Get_Factory (void) const
  168. {
  169. return _LightNodePersistFactory;
  170. }
  171. /////////////////////////////////////////////////////////////////
  172. //
  173. // operator=
  174. //
  175. /////////////////////////////////////////////////////////////////
  176. const LightNodeClass &
  177. LightNodeClass::operator= (const LightNodeClass &src)
  178. {
  179. NodeClass::operator= (src);
  180. m_InstanceSettings = src.m_InstanceSettings;
  181. return *this;
  182. }
  183. /////////////////////////////////////////////////////////////////
  184. //
  185. // Show_Settings_Dialog
  186. //
  187. /////////////////////////////////////////////////////////////////
  188. bool
  189. LightNodeClass::Show_Settings_Dialog (void)
  190. {
  191. NodeInfoPageClass info_tab (this);
  192. PositionPageClass pos_tab (this);
  193. //
  194. // Display the settings tab for instance overrides
  195. //
  196. PresetClass dummy_preset;
  197. dummy_preset.Set_Definition (&m_InstanceSettings);
  198. PresetSettingsTabClass settings_tab (&dummy_preset);
  199. //
  200. // Add each tab to the property sheet
  201. //
  202. EditorPropSheetClass prop_sheet;
  203. prop_sheet.Add_Page (&info_tab);
  204. prop_sheet.Add_Page (&pos_tab);
  205. prop_sheet.Add_Page (&settings_tab);
  206. // Show the property sheet
  207. UINT ret_code = prop_sheet.DoModal ();
  208. if (ret_code == IDOK) {
  209. Update_Light ();
  210. m_UsePreset = false;
  211. }
  212. // Return true if the user clicked OK
  213. return (ret_code == IDOK);
  214. }
  215. //////////////////////////////////////////////////////////////////////
  216. //
  217. // Pre_Export
  218. //
  219. //////////////////////////////////////////////////////////////////////
  220. void
  221. LightNodeClass::Pre_Export (void)
  222. {
  223. //
  224. // Remove ourselves from the 'system' so we don't get accidentally
  225. // saved during the export.
  226. //
  227. Add_Ref ();
  228. if (m_DisplayObj != NULL && m_IsInScene) {
  229. ::Get_Scene_Editor ()->Remove_Object (m_DisplayObj);
  230. }
  231. return ;
  232. }
  233. //////////////////////////////////////////////////////////////////////
  234. //
  235. // Post_Export
  236. //
  237. //////////////////////////////////////////////////////////////////////
  238. void
  239. LightNodeClass::Post_Export (void)
  240. {
  241. //
  242. // Put ourselves back into the system
  243. //
  244. if (m_DisplayObj != NULL && m_IsInScene) {
  245. ::Get_Scene_Editor ()->Add_Dynamic_Object (m_DisplayObj);
  246. }
  247. Release_Ref ();
  248. return ;
  249. }
  250. //////////////////////////////////////////////////////////////////////////////
  251. //
  252. // Add_To_Scene
  253. //
  254. //////////////////////////////////////////////////////////////////////////////
  255. void
  256. LightNodeClass::Add_To_Scene (void)
  257. {
  258. SceneEditorClass *scene = ::Get_Scene_Editor ();
  259. if ( m_LightPhysObj != NULL &&
  260. scene->Contains (m_LightPhysObj) == false) {
  261. //
  262. // Add the static light to the scene and the display object
  263. // the editor uses as a visual representation of the light
  264. //
  265. scene->Add_Static_Light (m_LightPhysObj);
  266. scene->Add_Dynamic_Object (m_DisplayObj);
  267. if (m_Sphere != NULL) {
  268. m_Sphere->Display_Around_Node (*this);
  269. }
  270. }
  271. m_IsInScene = true;
  272. return ;
  273. }
  274. //////////////////////////////////////////////////////////////////////////////
  275. //
  276. // Remove_From_Scene
  277. //
  278. //////////////////////////////////////////////////////////////////////////////
  279. void
  280. LightNodeClass::Remove_From_Scene (void)
  281. {
  282. SceneEditorClass *scene = ::Get_Scene_Editor ();
  283. if ( m_LightPhysObj != NULL &&
  284. scene->Contains (m_LightPhysObj)) {
  285. //
  286. // Remove the static light from the scene and the display object
  287. // the editor uses as a visual representation of the light
  288. //
  289. scene->Remove_Object (m_LightPhysObj);
  290. scene->Remove_Object (m_DisplayObj);
  291. if (m_Sphere != NULL) {
  292. m_Sphere->Remove_From_Scene ();
  293. }
  294. }
  295. m_IsInScene = false;
  296. return ;
  297. }
  298. /////////////////////////////////////////////////////////////////
  299. //
  300. // Save
  301. //
  302. /////////////////////////////////////////////////////////////////
  303. bool
  304. LightNodeClass::Save (ChunkSaveClass &csave)
  305. {
  306. csave.Begin_Chunk (CHUNKID_BASE_CLASS);
  307. NodeClass::Save (csave);
  308. csave.End_Chunk ();
  309. if (m_UsePreset == false) {
  310. csave.Begin_Chunk (CHUNKID_INSTANCE_SETTINGS);
  311. m_InstanceSettings.Save (csave);
  312. csave.End_Chunk ();
  313. }
  314. csave.Begin_Chunk (CHUNKID_VARIABLES);
  315. Save_Variables (csave);
  316. csave.End_Chunk ();
  317. return true;
  318. }
  319. /////////////////////////////////////////////////////////////////
  320. //
  321. // Load
  322. //
  323. /////////////////////////////////////////////////////////////////
  324. bool
  325. LightNodeClass::Load (ChunkLoadClass &cload)
  326. {
  327. while (cload.Open_Chunk ()) {
  328. switch (cload.Cur_Chunk_ID ()) {
  329. case CHUNKID_BASE_CLASS:
  330. NodeClass::Load (cload);
  331. break;
  332. case CHUNKID_VARIABLES:
  333. Load_Variables (cload);
  334. break;
  335. case CHUNKID_INSTANCE_SETTINGS:
  336. m_InstanceSettings.Load (cload);
  337. m_UsePreset = false;
  338. break;
  339. }
  340. cload.Close_Chunk ();
  341. }
  342. return true;
  343. }
  344. //////////////////////////////////////////////////////////////////////////////////
  345. //
  346. // Save_Variables
  347. //
  348. //////////////////////////////////////////////////////////////////////////////////
  349. bool
  350. LightNodeClass::Save_Variables (ChunkSaveClass &csave)
  351. {
  352. bool retval = true;
  353. WRITE_MICRO_CHUNK (csave, VARID_VIS_SECTOR_ID, m_VisSectorID)
  354. return retval;
  355. }
  356. //////////////////////////////////////////////////////////////////////////////////
  357. //
  358. // Load_Variables
  359. //
  360. //////////////////////////////////////////////////////////////////////////////////
  361. bool
  362. LightNodeClass::Load_Variables (ChunkLoadClass &cload)
  363. {
  364. bool retval = true;
  365. //
  366. // Loop through all the microchunks that define the variables
  367. //
  368. while (cload.Open_Micro_Chunk ()) {
  369. switch (cload.Cur_Micro_Chunk_ID ()) {
  370. READ_MICRO_CHUNK (cload, VARID_VIS_SECTOR_ID, m_VisSectorID)
  371. }
  372. cload.Close_Micro_Chunk ();
  373. }
  374. return retval;
  375. }
  376. /////////////////////////////////////////////////////////////////
  377. //
  378. // Update_Light
  379. //
  380. /////////////////////////////////////////////////////////////////
  381. void
  382. LightNodeClass::Update_Light (void)
  383. {
  384. //
  385. // Do we have a light to setup?
  386. //
  387. if ( m_LightPhysObj != NULL &&
  388. m_LightPhysObj->Peek_Model () != NULL )
  389. {
  390. LightClass *light_obj = new LightClass (m_InstanceSettings.Get_Light_Type ());
  391. Vector3 ambient = m_InstanceSettings.Get_Ambient_Color ();
  392. Vector3 diffuse = m_InstanceSettings.Get_Diffuse_Color ();
  393. Vector3 specular = m_InstanceSettings.Get_Specular_Color ();
  394. float intensity = m_InstanceSettings.Get_Intensity ();
  395. double inner_radius = m_InstanceSettings.Get_Inner_Radius ();
  396. double outer_radius = m_InstanceSettings.Get_Outer_Radius ();
  397. float spot_angle = m_InstanceSettings.Get_Spot_Angle ();
  398. Vector3 spot_dir = m_InstanceSettings.Get_Spot_Direction ();
  399. float spot_exp = m_InstanceSettings.Get_Spot_Exponent ();
  400. bool casts_shadows = m_InstanceSettings.Does_Cast_Shadows ();
  401. //
  402. // Configure the light object
  403. //
  404. light_obj->Set_Flag (LightClass::NEAR_ATTENUATION, true);
  405. light_obj->Set_Flag (LightClass::FAR_ATTENUATION, true);
  406. light_obj->Set_Intensity (intensity);
  407. light_obj->Set_Ambient (ambient);
  408. light_obj->Set_Diffuse (diffuse);
  409. light_obj->Set_Specular (specular);
  410. light_obj->Set_Far_Attenuation_Range (inner_radius, outer_radius);
  411. light_obj->Set_Spot_Angle (spot_angle);
  412. light_obj->Set_Spot_Direction (spot_dir);
  413. light_obj->Set_Spot_Exponent (spot_exp);
  414. light_obj->Enable_Shadows (casts_shadows);
  415. //
  416. // Pass the new model onto the light physics object
  417. //
  418. m_LightPhysObj->Set_Model (light_obj);
  419. m_LightPhysObj->Set_Transform (m_Transform);
  420. m_LightPhysObj->Set_Vis_Sector_ID (m_VisSectorID);
  421. //
  422. // Loop through all the vertext materials in the display object
  423. // and make them match the light...
  424. //
  425. MaterialInfoClass *material_info = light_obj->Get_Material_Info ();
  426. if (material_info != NULL) {
  427. int counter = material_info->Vertex_Material_Count ();
  428. while (counter --) {
  429. VertexMaterialClass *vertex_mat = material_info->Peek_Vertex_Material (counter);
  430. if (vertex_mat != NULL) {
  431. vertex_mat->Set_Ambient (ambient);
  432. vertex_mat->Set_Emissive (ambient);
  433. vertex_mat->Set_Diffuse (diffuse);
  434. vertex_mat->Set_Specular (specular);
  435. }
  436. }
  437. MEMBER_RELEASE (material_info);
  438. }
  439. //
  440. // Update the sphere's settings
  441. //
  442. if (m_Sphere != NULL) {
  443. m_Sphere->Set_Radius (outer_radius);
  444. m_Sphere->Set_Color (ambient);
  445. }
  446. //
  447. // Release our hold on the light object
  448. //
  449. MEMBER_RELEASE (light_obj);
  450. }
  451. //
  452. // Make sure the attenuation spheres are correctly displayed
  453. //
  454. Show_Attenuation_Spheres (::Get_Scene_Editor ()->Are_Light_Spheres_Displayed ());
  455. ::Get_Scene_Editor ()->Update_Lighting ();
  456. return ;
  457. }
  458. /////////////////////////////////////////////////////////////////
  459. //
  460. // Initialize_From_Light
  461. //
  462. /////////////////////////////////////////////////////////////////
  463. void
  464. LightNodeClass::Initialize_From_Light (LightClass *light)
  465. {
  466. m_UsePreset = false;
  467. //
  468. // Read the settings we care about from the light
  469. //
  470. Vector3 ambient_color (0, 0, 0);
  471. Vector3 diffuse_color (0, 0, 0);
  472. Vector3 specular_color (0, 0, 0);
  473. light->Get_Ambient (&ambient_color);
  474. light->Get_Diffuse (&diffuse_color);
  475. light->Get_Specular (&specular_color);
  476. double inner = 0;
  477. double outer = 0;
  478. light->Get_Far_Attenuation_Range (inner, outer);
  479. float intensity = light->Get_Intensity ();
  480. LightClass::LightType type = light->Get_Type ();
  481. float spot_angle = light->Get_Spot_Angle ();
  482. float spot_exp = light->Get_Spot_Exponent ();
  483. Vector3 spot_dir (0, 0, 0);
  484. light->Get_Spot_Direction (spot_dir);
  485. bool casts_shadows = light->Are_Shadows_Enabled ();
  486. //
  487. // Store the new settings
  488. //
  489. m_InstanceSettings.Set_Ambient_Color (ambient_color);
  490. m_InstanceSettings.Set_Diffuse_Color (diffuse_color);
  491. m_InstanceSettings.Set_Specular_Color (specular_color);
  492. m_InstanceSettings.Set_Intensity (intensity);
  493. m_InstanceSettings.Set_Inner_Radius (inner);
  494. m_InstanceSettings.Set_Outer_Radius (outer);
  495. m_InstanceSettings.Set_Light_Type (type);
  496. m_InstanceSettings.Set_Spot_Angle (spot_angle);
  497. m_InstanceSettings.Set_Spot_Direction (spot_dir);
  498. m_InstanceSettings.Set_Spot_Exponent (spot_exp);
  499. m_InstanceSettings.Casts_Shadows (casts_shadows);
  500. //
  501. // Update the light we use...
  502. //
  503. Update_Light ();
  504. return ;
  505. }
  506. //////////////////////////////////////////////////////////////////////
  507. //
  508. // Show_Attenuation_Spheres
  509. //
  510. //////////////////////////////////////////////////////////////////////
  511. void
  512. LightNodeClass::Show_Attenuation_Spheres (bool onoff)
  513. {
  514. if (onoff && m_Sphere == NULL) {
  515. m_Sphere = new AttenuationSphereClass;
  516. m_Sphere->Display_Around_Node (*this);
  517. m_Sphere->Set_Color (m_InstanceSettings.Get_Ambient_Color ());
  518. m_Sphere->Set_Radius (Get_Attenuation_Radius ());
  519. m_Sphere->Set_Opacity (0.25F);
  520. } else if (onoff == false && m_Sphere != NULL) {
  521. m_Sphere->Remove_From_Scene ();
  522. MEMBER_RELEASE (m_Sphere);
  523. }
  524. return ;
  525. }
  526. //////////////////////////////////////////////////////////////////////
  527. //
  528. // Peek_Light
  529. //
  530. //////////////////////////////////////////////////////////////////////
  531. LightClass *
  532. LightNodeClass::Peek_Light (void)
  533. {
  534. LightClass *light = NULL;
  535. //
  536. // Check to make sure we have a light objects
  537. //
  538. if ( m_LightPhysObj != NULL &&
  539. m_LightPhysObj->Peek_Model () != NULL )
  540. {
  541. //
  542. // Can we get at the light from its phys-wrapper?
  543. //
  544. RenderObjClass *model = m_LightPhysObj->Peek_Model ();
  545. if (model != NULL && model->Class_ID () == RenderObjClass::CLASSID_LIGHT) {
  546. light = (LightClass *)model;
  547. }
  548. }
  549. return light;
  550. }
  551. //////////////////////////////////////////////////////////////////////
  552. //
  553. // Set_Vis_Sector_ID
  554. //
  555. //////////////////////////////////////////////////////////////////////
  556. void
  557. LightNodeClass::Set_Vis_Sector_ID (uint32 vis_id)
  558. {
  559. m_VisSectorID = vis_id;
  560. //
  561. // Update the physics object as well
  562. //
  563. if (m_LightPhysObj != NULL) {
  564. m_LightPhysObj->Set_Vis_Sector_ID (m_VisSectorID);
  565. }
  566. return ;
  567. }
  568. /////////////////////////////////////////////////////////////////
  569. //
  570. // Update_Cached_Vis_IDs
  571. //
  572. /////////////////////////////////////////////////////////////////
  573. void
  574. LightNodeClass::Update_Cached_Vis_IDs (void)
  575. {
  576. if (m_LightPhysObj != NULL) {
  577. m_VisSectorID = m_LightPhysObj->Get_Vis_Sector_ID ();
  578. }
  579. return ;
  580. }