CameraMgr.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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/CameraMgr.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 11/06/01 10:29a $*
  29. * *
  30. * $Revision:: 46 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "utils.h"
  37. #include "camera.h"
  38. #include "cameramgr.h"
  39. #include "mousemgr.h"
  40. #include "leveleditdoc.h"
  41. #include "groupmgr.h"
  42. #include "formtoolbar.h"
  43. #include "camerasettingsform.h"
  44. #include "mainfrm.h"
  45. #include "filemgr.h"
  46. #include "editorassetmgr.h"
  47. #include "_assetmgr.h"
  48. #include "sceneeditor.h"
  49. #include "node.h"
  50. #include "quat.h"
  51. #include "combat.h"
  52. #include "filelocations.h"
  53. #include "combatchunkid.h"
  54. #include "ccamera.h"
  55. #include "input.h"
  56. #include "leveleditview.h"
  57. #include "nodemgr.h"
  58. #include "actionparams.h"
  59. #include "soldier.h"
  60. ////////////////////////////////////////////////////////////////////////////
  61. //
  62. // Constants
  63. //
  64. ////////////////////////////////////////////////////////////////////////////
  65. const float DEF_CAMERA_DIST = 80.00F;
  66. const float MIN_MOVE_SCALE = 30.00F;
  67. const float WALK_THRU_VEL_FWD = 3.5F;
  68. const float WALK_THRU_VEL_BKWD = -WALK_THRU_VEL_FWD;
  69. const float WALK_THRU_VEL_LEFT = 0.8F;
  70. const float WALK_THRU_VEL_RIGHT = -WALK_THRU_VEL_LEFT;
  71. const float WALK_THRU_VEL_STRAFE = 4.5F;
  72. ////////////////////////////////////////////////////////////////////////////
  73. //
  74. // Static member initialization
  75. //
  76. ////////////////////////////////////////////////////////////////////////////
  77. CameraMgr::UPDATE_CAMERA_FN CameraMgr::_pfnUpdateMethods[CAMERA_MODE::MODE_COUNT] =
  78. {
  79. CameraMgr::Update_Camera_MOVE_ZOOM,
  80. CameraMgr::Update_Camera_MOVE_PLANE,
  81. CameraMgr::Update_Camera_FLY_THROUGH,
  82. CameraMgr::Update_Camera_WALK_THROUGH,
  83. CameraMgr::Update_Camera_ROTATE_FREE,
  84. CameraMgr::Update_Camera_ROTATE_X,
  85. CameraMgr::Update_Camera_ROTATE_Y,
  86. CameraMgr::Update_Camera_ROTATE_Z,
  87. CameraMgr::Update_Camera_ORBIT,
  88. CameraMgr::Update_Camera_FLY_TO
  89. };
  90. bool CameraMgr::_pKeyboardState[256] = { 0 };
  91. HHOOK CameraMgr::_hHook = NULL;
  92. ////////////////////////////////////////////////////////////////////////////
  93. //
  94. // CameraMgr
  95. //
  96. ////////////////////////////////////////////////////////////////////////////
  97. CameraMgr::CameraMgr (void)
  98. : m_pCamera (NULL),
  99. m_bAutoLevel (false),
  100. m_CameraMode (MODE_MOVE_PLANE),
  101. m_SpeedModifier (6.0F),
  102. m_WalkThruObj (NULL),
  103. m_WalkThruDef (NULL)
  104. {
  105. Init_Camera ();
  106. return ;
  107. }
  108. ////////////////////////////////////////////////////////////////////////////
  109. //
  110. // CameraMgr
  111. //
  112. ////////////////////////////////////////////////////////////////////////////
  113. CameraMgr::~CameraMgr (void)
  114. {
  115. MEMBER_RELEASE (m_pCamera);
  116. if (m_WalkThruObj != NULL) {
  117. m_WalkThruObj->Set_Delete_Pending ();
  118. m_WalkThruObj = NULL;
  119. Input::Shutdown ();
  120. }
  121. if (m_WalkThruDef != NULL) {
  122. delete m_WalkThruDef;
  123. }
  124. //MEMBER_RELEASE (m_pWalkThroughPhys);
  125. // Remove our hook procedure from the chain
  126. if (_hHook != NULL) {
  127. ::UnhookWindowsHookEx (_hHook);
  128. _hHook = NULL;
  129. }
  130. return ;
  131. }
  132. ////////////////////////////////////////////////////////////////////////////
  133. //
  134. // fnCameraKeyboardHook
  135. //
  136. ////////////////////////////////////////////////////////////////////////////
  137. LRESULT CALLBACK
  138. fnCameraKeyboardHook
  139. (
  140. int code,
  141. WPARAM wParam,
  142. LPARAM lParam
  143. )
  144. {
  145. // Should we process this message?
  146. if (code >= 0) {
  147. if ((wParam >= 0) && (wParam <= 255)) {
  148. CameraMgr::_pKeyboardState[wParam] = bool((lParam & 0x80000000) != 0x80000000);
  149. }
  150. }
  151. // Call the next hook procedure in the chain
  152. return ::CallNextHookEx (CameraMgr::_hHook, code, wParam, lParam);
  153. }
  154. ////////////////////////////////////////////////////////////////////////////
  155. //
  156. // Init_Camera
  157. //
  158. ////////////////////////////////////////////////////////////////////////////
  159. void
  160. CameraMgr::Init_Camera (void)
  161. {
  162. if (m_pCamera == NULL) {
  163. // Create a new camera object
  164. m_pCamera = new CameraClass;
  165. }
  166. // Were we able to create a new object?
  167. ASSERT (m_pCamera!= NULL);
  168. if (m_pCamera) {
  169. // Create a transformation matrix
  170. Matrix3D transform_matrix (1);
  171. transform_matrix.Translate (Vector3 (0.0F, 0.0F, DEF_CAMERA_DIST));
  172. m_pCamera->Set_Clip_Planes (0.1F, 200.0F);
  173. //m_pCamera->Set_Environment_Range (9000.0F, 10000.0F);
  174. // Move the camera back along the z-axis looking at world center
  175. Set_Transform (transform_matrix);
  176. m_pCamera->Set_Transform (transform_matrix);
  177. }
  178. // Install a windows hook procedure so we know when keys are pressed
  179. if (_hHook == NULL) {
  180. ::memset (_pKeyboardState, 0, sizeof (_pKeyboardState));
  181. _hHook = ::SetWindowsHookEx (WH_KEYBOARD,
  182. fnCameraKeyboardHook,
  183. ::AfxGetInstanceHandle (),
  184. ::GetCurrentThreadId ());
  185. }
  186. // Reset the camera mode
  187. m_CameraMode = MODE_MOVE_PLANE;
  188. return ;
  189. }
  190. double _XRot = 0;
  191. double _YRot = 0;
  192. ////////////////////////////////////////////////////////////////////////////
  193. //
  194. // Set_Camera_Mode
  195. //
  196. ////////////////////////////////////////////////////////////////////////////
  197. void
  198. CameraMgr::Set_Camera_Mode (CAMERA_MODE new_mode)
  199. {
  200. // Is this really a new mode?
  201. if (new_mode != m_CameraMode) {
  202. if (new_mode == MODE_WALK_THROUGH) {
  203. ::Get_Scene_Editor ()->Set_Selection (NULL);
  204. GameInFocus = true;
  205. if (m_WalkThruObj == NULL) {
  206. Input::Init ();
  207. Input::Load_Configuration (DEFAULT_INPUT_FILENAME);
  208. Input::Set_Mouse_Invert (true);
  209. //
  210. // Create a commando game object and initialize it
  211. // with the first commando preset we found
  212. //
  213. SoldierGameObjDef *definition = (SoldierGameObjDef *)DefinitionMgrClass::Find_Typed_Definition ("Walk-Thru", CLASSID_GAME_OBJECT_DEF_SOLDIER, false);
  214. m_WalkThruObj = new SoldierGameObj;
  215. m_WalkThruObj->Init (*definition);
  216. ActionParamsStruct parameters;
  217. m_WalkThruObj->Get_Action()->Follow_Input( parameters );
  218. }
  219. m_WalkThruObj->Control_Enable (true);
  220. m_WalkThruObj->Set_Control_Owner (CombatManager::Get_My_Id ());
  221. CombatManager::Set_I_Am_Client (true);
  222. CombatManager::Set_The_Star (m_WalkThruObj);
  223. //
  224. // Give the physics object an initial transform
  225. //
  226. PhysClass *phys_obj = m_WalkThruObj->Peek_Physical_Object ();
  227. if (phys_obj != NULL) {
  228. Matrix3D transform = m_pCamera->Get_Transform ();
  229. Matrix3D cam_to_world (Vector3 (0, 0, -1), Vector3 (-1, 0, 0), Vector3 (0, 1, 0), Vector3 (0, 0, 0));
  230. transform = transform * cam_to_world;
  231. phys_obj->Set_Transform (transform);
  232. }
  233. } else if (m_WalkThruObj != NULL) {
  234. m_WalkThruObj->Set_Delete_Pending ();
  235. m_WalkThruObj = NULL;
  236. Input::Shutdown ();
  237. }
  238. m_CameraMode = new_mode;
  239. }
  240. return ;
  241. }
  242. ////////////////////////////////////////////////////////////////////////////
  243. //
  244. // Update_Camera
  245. //
  246. ////////////////////////////////////////////////////////////////////////////
  247. void
  248. CameraMgr::Update_Camera
  249. (
  250. float deltax,
  251. float deltay
  252. )
  253. {
  254. // State OK?
  255. ASSERT (m_pCamera != NULL);
  256. if (m_pCamera != NULL) {
  257. // Call into the state table to perform the update
  258. _pfnUpdateMethods[m_CameraMode] (*m_pCamera, deltax, deltay);
  259. if (m_bAutoLevel) {
  260. // If ew are auto-leveling, then reset the initial
  261. // rotation matrix and percent
  262. m_AutoLevelInitalMatrix = m_pCamera->Get_Transform ();
  263. m_AutoLevelPercent = 0.00F;
  264. }
  265. }
  266. return ;
  267. }
  268. ////////////////////////////////////////////////////////////////////////////
  269. //
  270. // Level_Camera
  271. //
  272. ////////////////////////////////////////////////////////////////////////////
  273. void
  274. CameraMgr::Level_Camera (void)
  275. {
  276. m_bAutoLevel = true;
  277. // Reset the initial rotation matrix and percent
  278. m_AutoLevelInitalMatrix = m_pCamera->Get_Transform ();
  279. m_AutoLevelPercent = 0.00F;
  280. return ;
  281. }
  282. ////////////////////////////////////////////////////////////////////////////
  283. //
  284. // Auto_Level
  285. //
  286. ////////////////////////////////////////////////////////////////////////////
  287. void
  288. CameraMgr::Auto_Level (void)
  289. {
  290. // Is there a mouse button down?
  291. // We don't want to auto-level if the mouse button is down
  292. BOOL mouse_down = BOOL(::GetKeyState (VK_LBUTTON) & 0xF000);
  293. mouse_down |= BOOL(::GetKeyState (VK_RBUTTON) & 0xF000);
  294. // Should we auto-level the camera?
  295. if ((m_AutoLevelPercent <= 1) && (mouse_down == false)) {
  296. // Get the camera's current transformation matrix
  297. Matrix3D matrix = m_pCamera->Get_Transform ();
  298. Vector3 cam_pos;
  299. Vector3 cam_target;
  300. // Get the camera position
  301. matrix.Get_Translation (&cam_pos);
  302. // Get a vector 1 meter below the camera position
  303. cam_target = matrix * Vector3 (0.00F, 0.00F, -1.00F);
  304. // Force the z components of the 2 vectors to be the same.
  305. cam_target.Z = cam_pos.Z;
  306. // Create a new transformation matrix which is positioned
  307. // at the camera and is looking at the vector we created
  308. Matrix3D new_matrix;
  309. new_matrix.Look_At (cam_pos, cam_target, 0.0F);
  310. // Build quaternions for the start and end rotation matricies
  311. Quaternion initial_quat = ::Build_Quaternion (m_AutoLevelInitalMatrix);
  312. Quaternion end_quat = ::Build_Quaternion (new_matrix);
  313. // Linerally-interpolate between the 2 rotations (from 0.0 - 1.0)
  314. Quaternion rotation_now;
  315. ::Slerp (rotation_now, initial_quat, end_quat, m_AutoLevelPercent);
  316. // Increment the current interpolation percent
  317. m_AutoLevelPercent += 0.04F;
  318. // Set the rotation for the camera's transormation matrix
  319. matrix.Set_Rotation (rotation_now);
  320. // Set the camera's new rotation
  321. Set_Transform (matrix);
  322. if (m_AutoLevelPercent >= 1) {
  323. m_bAutoLevel = false;
  324. }
  325. }
  326. return ;
  327. }
  328. ////////////////////////////////////////////////////////////////////////////
  329. //
  330. // Increase_Speed
  331. //
  332. ////////////////////////////////////////////////////////////////////////////
  333. void
  334. CameraMgr::Increase_Speed (void)
  335. {
  336. if (m_SpeedModifier < 1.0F) {
  337. m_SpeedModifier += 0.1F;
  338. } else {
  339. m_SpeedModifier += 0.75F;
  340. }
  341. // Normalize the speed
  342. if (m_SpeedModifier > 25.00F) {
  343. m_SpeedModifier = 25.00F;
  344. }
  345. return ;
  346. }
  347. ////////////////////////////////////////////////////////////////////////////
  348. //
  349. // Decrease_Speed
  350. //
  351. ////////////////////////////////////////////////////////////////////////////
  352. void
  353. CameraMgr::Decrease_Speed (void)
  354. {
  355. if (m_SpeedModifier < 2.0F) {
  356. m_SpeedModifier -= 0.1F;
  357. } else {
  358. m_SpeedModifier -= 0.75F;
  359. }
  360. // Normalize the speed
  361. if (m_SpeedModifier < 0.25F) {
  362. m_SpeedModifier = 0.25F;
  363. }
  364. return ;
  365. }
  366. ////////////////////////////////////////////////////////////////////////////
  367. //
  368. // On_Frame
  369. //
  370. ////////////////////////////////////////////////////////////////////////////
  371. void
  372. CameraMgr::On_Frame (void)
  373. {
  374. float speed_mod = CameraMgr::_pKeyboardState[VK_CONTROL] ? m_SpeedModifier : 1.0F;
  375. if ((_pKeyboardState[VK_NUMPAD8]) ||
  376. (_pKeyboardState[VK_NUMPAD2])) {
  377. // Get the camera's current position
  378. Vector3 position = m_pCamera->Get_Position ();
  379. float orig_z = position.Z;
  380. // Get the current direction the camera is looking at
  381. Matrix3D matrix = m_pCamera->Get_Transform ();
  382. float delta = _pKeyboardState[VK_NUMPAD8] ? -0.35F : 0.35F;
  383. delta *= speed_mod;
  384. // Should we lock the z-position?
  385. if (::GetKeyState (VK_CAPITAL) & 0x0001) {
  386. // Scale the position 'forward' and 'backward'
  387. //position += (delta * matrix.Get_Z_Vector ());
  388. //position.Z = orig_z;
  389. Vector3 y_vector = Vector3::Cross_Product (matrix.Get_X_Vector (), Vector3 (0, 0, 1));
  390. y_vector.Normalize ();
  391. position += delta * y_vector;
  392. //delta += y_vector * (-0.05F * modifier);
  393. } else {
  394. // Scale the position along the direction vector
  395. position += (delta * matrix.Get_Z_Vector ());
  396. }
  397. // Set the camera's new position
  398. Set_Position (position);
  399. }
  400. if ((_pKeyboardState[VK_NUMPAD9]) ||
  401. (_pKeyboardState[VK_NUMPAD3])) {
  402. // Get the camera's current transform
  403. Matrix3D transform = m_pCamera->Get_Transform ();
  404. // Rotate by the specified number of radians
  405. float rotation = _pKeyboardState[VK_NUMPAD9] ? 0.0314F : -0.0314F;
  406. rotation *= (speed_mod / 2.5F);
  407. if (rotation > 0.045F) {
  408. rotation = 0.045F;
  409. } else if (rotation < -0.045F) {
  410. rotation = -0.045F;
  411. }
  412. transform.Rotate_X (rotation);
  413. // Set the camera's new transform
  414. Set_Transform (transform);
  415. }
  416. if ((_pKeyboardState[VK_NUMPAD4]) ||
  417. (_pKeyboardState[VK_NUMPAD6])) {
  418. if (::GetKeyState (VK_MENU) < 0) {
  419. // Get the camera's current position
  420. Vector3 position = m_pCamera->Get_Position ();
  421. float delta = (_pKeyboardState[VK_NUMPAD4]) ? -0.25F : 0.25F;
  422. delta *= speed_mod;
  423. position += (delta * m_pCamera->Get_Transform ().Get_X_Vector ());
  424. // Set the camera's new position
  425. Set_Position (position);
  426. } else {
  427. // Get the pure rotation from the transform
  428. Matrix3D transform = m_pCamera->Get_Transform ();
  429. Vector3 translation = transform.Get_Translation ();
  430. transform.Set_Translation (Vector3 (0, 0, 0));
  431. // Determine an appropriate rotation delta
  432. float delta = (_pKeyboardState[VK_NUMPAD4]) ? 0.0314F : -0.0314F;
  433. delta *= speed_mod;
  434. if (delta > 0.06F) {
  435. delta = 0.06F;
  436. } else if (delta < -0.06F) {
  437. delta = -0.06F;
  438. }
  439. Matrix3D world_rotation(1);
  440. world_rotation.Rotate_Z (delta);
  441. // Rotate using the world's coord system
  442. transform = world_rotation * transform;
  443. transform.Set_Translation (translation);
  444. // Set the camera's new transform
  445. Set_Transform (transform);
  446. }
  447. }
  448. if ((_pKeyboardState[VK_NUMPAD7]) ||
  449. (_pKeyboardState[VK_NUMPAD1])) {
  450. // Get the camera's current position
  451. Vector3 position = m_pCamera->Get_Position ();
  452. float delta = (_pKeyboardState[VK_NUMPAD1]) ? -0.25F : 0.25F;
  453. delta *= speed_mod;
  454. position.Z += delta;
  455. // Set the camera's new position
  456. Set_Position (position);
  457. }
  458. // Are we currently in 'walk-thru' mode?
  459. if (m_CameraMode == MODE_WALK_THROUGH) {
  460. if (m_WalkThruObj != NULL) {
  461. if (::GetAsyncKeyState (VK_ESCAPE) < 0 || ::GetAsyncKeyState (VK_F6) < 0) {
  462. ::Get_Mouse_Mgr ()->Set_Mouse_Mode (MouseMgrClass::MODE_OBJECT_MANIPULATE);
  463. } else {
  464. //
  465. // Check for the VIS-Update keypress
  466. //
  467. static DWORD last_vis_gen = 0;
  468. static DWORD last_static_anim_toggle = 0;
  469. DWORD current_ticks = ::GetTickCount ();
  470. if ((::GetAsyncKeyState (VK_RETURN) < 0) && (current_ticks - last_vis_gen) > 1000) {
  471. Vector3 sample_point = m_WalkThruObj->Get_Transform ().Get_Translation () + Vector3 (0, 0, 1);
  472. ::Get_Scene_Editor ()->Record_Vis_Info (m_pCamera->Get_Transform (), sample_point);
  473. last_vis_gen = current_ticks;
  474. } else if (::GetAsyncKeyState (VK_F3) < 0) {
  475. bool points_visible = ::Get_Scene_Editor ()->Are_Manual_Vis_Points_Visible ();
  476. ::Get_Scene_Editor ()->Show_Manual_Vis_Points (!points_visible);
  477. } else if ( (::GetAsyncKeyState (VK_CONTROL) < 0) &&
  478. (::GetAsyncKeyState (VK_SHIFT) < 0) &&
  479. (::GetAsyncKeyState ('T') < 0))
  480. {
  481. //
  482. // Toggle the static anims... (if necessary)
  483. //
  484. if ((current_ticks - last_static_anim_toggle) > 250) {
  485. ((CMainFrame *)::AfxGetMainWnd ())->Toggle_Static_Anims ();
  486. last_static_anim_toggle = current_ticks;
  487. }
  488. }
  489. //
  490. // Allow the game object and physic object to think
  491. //
  492. m_WalkThruObj->Generate_Control ();
  493. m_WalkThruObj->Think ();
  494. PhysClass *phys_obj = m_WalkThruObj->Peek_Physical_Object ();
  495. if (phys_obj != NULL) {
  496. phys_obj->Timestep (TimeManager::Get_Frame_Seconds ());
  497. }
  498. m_WalkThruObj->Post_Think ();
  499. CCameraClass *game_camera = CombatManager::Get_Camera ();
  500. if (game_camera != NULL) {
  501. //
  502. // Update the game camera
  503. //
  504. Input::Update ();
  505. CombatManager::Handle_Input ();
  506. CombatManager::Update_Star ();
  507. Matrix3D tm = m_WalkThruObj->Get_Transform();
  508. game_camera->Update ();
  509. //
  510. // Copy the game camera's transform
  511. //
  512. Matrix3D camera_tm = game_camera->Get_Transform ();
  513. m_pCamera->Set_Transform (camera_tm);
  514. }
  515. }
  516. }
  517. }
  518. if (m_bAutoLevel) {
  519. Auto_Level ();
  520. }
  521. if (m_CameraMode == MODE_FLYTO) {
  522. Update_Fly_To ();
  523. }
  524. return ;
  525. }
  526. ////////////////////////////////////////////////////////////////////////////
  527. //
  528. // Update_Camera_Animation
  529. //
  530. ////////////////////////////////////////////////////////////////////////////
  531. void
  532. CameraMgr::Update_Camera_Animation (void)
  533. {
  534. //
  535. // TODO - move the camera depending on the animation mode
  536. //
  537. return ;
  538. }
  539. //**************************************************************************************************************//
  540. //
  541. // Start of static methods
  542. //
  543. //**************************************************************************************************************//
  544. ////////////////////////////////////////////////////////////////////////////
  545. //
  546. // Update_Camera_MOVE_ZOOM
  547. //
  548. ////////////////////////////////////////////////////////////////////////////
  549. void
  550. CameraMgr::Update_Camera_MOVE_ZOOM
  551. (
  552. CameraClass & camera,
  553. float deltax,
  554. float deltay
  555. )
  556. {
  557. // Get the camera's current position
  558. Vector3 position = camera.Get_Position ();
  559. // Get the current direction the camera is looking at
  560. Matrix3D matrix = camera.Get_Transform ();
  561. Vector3 orientation = matrix.Get_Z_Vector ();
  562. float scale_factor = (position.Z > MIN_MOVE_SCALE) ? position.Z : MIN_MOVE_SCALE;
  563. // Scale the position along the direction vector
  564. position = position - (deltay * orientation * scale_factor);
  565. // Set the camera's new position
  566. ::Get_Camera_Mgr ()->Set_Position (position);
  567. return ;
  568. }
  569. ////////////////////////////////////////////////////////////////////////////
  570. //
  571. // Update_Camera_MOVE_PLANE
  572. //
  573. ////////////////////////////////////////////////////////////////////////////
  574. void
  575. CameraMgr::Update_Camera_MOVE_PLANE
  576. (
  577. CameraClass & camera,
  578. float deltax,
  579. float deltay
  580. )
  581. {
  582. // Get the camera's current position
  583. Vector3 position = camera.Get_Position ();
  584. // Get the vector the camera is 'sliding' up and down on
  585. Matrix3D matrix = camera.Get_Transform ();
  586. Vector3 orientation = matrix.Get_Y_Vector ();
  587. float scale_factor = (position.Z > MIN_MOVE_SCALE) ? position.Z : MIN_MOVE_SCALE;
  588. // Scale the position along the direction vector
  589. position = position + (deltay * orientation * scale_factor);
  590. // Scale the position along the 'left/right' vector
  591. orientation = matrix.Get_X_Vector ();
  592. position = position - (deltax * orientation * scale_factor);
  593. // Set the camera's new position
  594. ::Get_Camera_Mgr ()->Set_Position (position);
  595. return ;
  596. }
  597. ////////////////////////////////////////////////////////////////////////////
  598. //
  599. // Update_Camera_FLY_THROUGH
  600. //
  601. ////////////////////////////////////////////////////////////////////////////
  602. void
  603. CameraMgr::Update_Camera_FLY_THROUGH
  604. (
  605. CameraClass & camera,
  606. float deltax,
  607. float deltay
  608. )
  609. {
  610. //
  611. // TODO - update the camera
  612. //
  613. return ;
  614. }
  615. ////////////////////////////////////////////////////////////////////////////
  616. //
  617. // Update_Camera_WALK_THROUGH
  618. //
  619. ////////////////////////////////////////////////////////////////////////////
  620. void
  621. CameraMgr::Update_Camera_WALK_THROUGH
  622. (
  623. CameraClass & camera,
  624. float deltax,
  625. float deltay
  626. )
  627. {
  628. //
  629. // TODO - update the camera
  630. //
  631. //Matrix3D transform = camera.Get_Transform ();
  632. //transform.Rotate_X (deltay*3.0F);
  633. _XRot += deltay * 2.0F;
  634. //_YRot += deltax * 3.0F;
  635. //camera.Set_Transform (transform);
  636. // Get the camera's current transformation matrix
  637. //CamaraCharPhys *pcharacter = ::Get_Camera_Mgr ()->Get_Walkthru_Character ();
  638. if (0) {//pcharacter != NULL) {
  639. /*Matrix3D transform = pcharacter->Get_Transform ();
  640. transform.Rotate_Z (deltax * 2.0F);
  641. pcharacter->Set_Transform (transform);*/
  642. }
  643. return ;
  644. }
  645. ////////////////////////////////////////////////////////////////////////////
  646. //
  647. // Update_Camera_ROTATE_FREE
  648. //
  649. void
  650. CameraMgr::Update_Camera_ROTATE_FREE
  651. (
  652. CameraClass & camera,
  653. float deltax,
  654. float deltay
  655. )
  656. {
  657. // Rotate the camera's transformation matrix based
  658. // on the deltas
  659. Matrix3D matrix = camera.Get_Transform ();
  660. //matrix.Rotate_X (deltay);
  661. //matrix.Rotate_Y (deltax);
  662. // Restrict the rotation to the current axis
  663. switch (::Get_Current_Document ()->Get_Axis_Restriction ()) {
  664. case CLevelEditDoc::RESTRICT_X:
  665. {
  666. matrix.Rotate_X (deltay);
  667. }
  668. break;
  669. case CLevelEditDoc::RESTRICT_Y:
  670. {
  671. matrix.Rotate_Y (deltax);
  672. }
  673. break;
  674. case CLevelEditDoc::RESTRICT_Z:
  675. {
  676. matrix.Rotate_Z (deltay);
  677. }
  678. break;
  679. default:
  680. {
  681. matrix.Rotate_X (deltay);
  682. matrix.Rotate_Y (deltax);
  683. }
  684. break;
  685. }
  686. // Set the camera's new rotation
  687. ::Get_Camera_Mgr ()->Set_Transform (matrix);
  688. return ;
  689. }
  690. ////////////////////////////////////////////////////////////////////////////
  691. //
  692. // Update_Camera_ROTATE_X
  693. //
  694. void
  695. CameraMgr::Update_Camera_ROTATE_X
  696. (
  697. CameraClass & camera,
  698. float deltax,
  699. float deltay
  700. )
  701. {
  702. // Rotate the camera's transformation matrix based
  703. // on the x delta
  704. Matrix3D matrix = camera.Get_Transform ();
  705. matrix.Rotate_X (deltay);
  706. // Set the camera's new rotation
  707. ::Get_Camera_Mgr ()->Set_Transform (matrix);
  708. return ;
  709. }
  710. ////////////////////////////////////////////////////////////////////////////
  711. //
  712. // Update_Camera_ROTATE_Y
  713. //
  714. void
  715. CameraMgr::Update_Camera_ROTATE_Y
  716. (
  717. CameraClass &camera,
  718. float deltax,
  719. float deltay
  720. )
  721. {
  722. // Rotate the camera's transformation matrix based
  723. // on the y delta
  724. Matrix3D matrix = camera.Get_Transform ();
  725. matrix.Rotate_Y (deltax);
  726. // Set the camera's new rotation
  727. ::Get_Camera_Mgr ()->Set_Transform (matrix);
  728. return ;
  729. }
  730. ////////////////////////////////////////////////////////////////////////////
  731. //
  732. // Update_Camera_ROTATE_Z
  733. //
  734. void
  735. CameraMgr::Update_Camera_ROTATE_Z
  736. (
  737. CameraClass & camera,
  738. float deltax,
  739. float deltay
  740. )
  741. {
  742. // Rotate the camera's transformation matrix based
  743. // on the x and y deltas, but restrict the rotation
  744. // to Z
  745. Matrix3D matrix = camera.Get_Transform ();
  746. Matrix3D temp_matrix = matrix;
  747. temp_matrix.Rotate_X (deltay);
  748. temp_matrix.Rotate_Y (deltax);
  749. /*Matrix3D new_matrix (1);
  750. new_matrix.Set_Translation (matrix.Get_Translation ());
  751. new_matrix.Rotate_X (matrix.Get_X_Rotation ());
  752. new_matrix.Rotate_Y (matrix.Get_Y_Rotation ());
  753. new_matrix.Rotate_Z (temp_matrix.Get_Z_Rotation ());*/
  754. Matrix3D new_matrix = matrix;
  755. new_matrix.Rotate_Z (deltax+deltay);
  756. //new_matrix.Rotate_Z (deltay);
  757. // Set the camera's new rotation
  758. ::Get_Camera_Mgr ()->Set_Transform (matrix);
  759. return ;
  760. }
  761. ////////////////////////////////////////////////////////////////////////////
  762. //
  763. // Update_Camera_FLY_TO
  764. //
  765. ////////////////////////////////////////////////////////////////////////////
  766. void
  767. CameraMgr::Update_Camera_FLY_TO
  768. (
  769. CameraClass &camera,
  770. float /*deltax*/,
  771. float /*deltay*/
  772. )
  773. {
  774. ::Get_Camera_Mgr ()->Update_Fly_To ();
  775. return ;
  776. }
  777. ////////////////////////////////////////////////////////////////////////////
  778. //
  779. // Update_Camera_ORBIT
  780. //
  781. ////////////////////////////////////////////////////////////////////////////
  782. void
  783. CameraMgr::Update_Camera_ORBIT
  784. (
  785. CameraClass &camera,
  786. float /*deltax*/,
  787. float /*deltay*/
  788. )
  789. {
  790. //
  791. // TODO - update the camera
  792. //
  793. MouseMgrClass *pmouse_mgr = ::Get_Mouse_Mgr ();
  794. CPoint point = pmouse_mgr->Get_Mouse_Point ();
  795. CPoint last_point = pmouse_mgr->Get_Last_Mouse_Point ();
  796. Vector3 viewpos;
  797. Vector3 last_viewpos;
  798. camera.Device_To_View_Space (Vector2 (last_point.x, last_point.y), &last_viewpos);
  799. camera.Device_To_View_Space (Vector2 (point.x, point.y), &viewpos);
  800. // Rotate the camara like a trackball
  801. Quaternion rotation = ::Trackball (last_viewpos.X, last_viewpos.Y, viewpos.X, viewpos.Y, 0.4F);
  802. // Rotate this item about its z axis
  803. if (::Get_Selection_Mgr ().Get_Count () > 0) {
  804. //SphereClass bounding_sphere = ::Get_Selection_Mgr ().Get_Bounding_Sphere ();
  805. //bounding_sphere.Center;
  806. Matrix3D matrix = camera.Get_Transform ();
  807. Matrix3D matrix_inv;
  808. matrix.Get_Orthogonal_Inverse (matrix_inv);
  809. Vector3 vector_to_object = matrix_inv * ::Get_Selection_Mgr ().Get_Center ();//bounding_sphere.Center;
  810. matrix.Translate (vector_to_object);
  811. Matrix3D::Multiply (matrix, ::Build_Matrix3D (rotation), &matrix);
  812. matrix.Translate (-vector_to_object);
  813. ::Get_Camera_Mgr ()->Set_Transform (matrix);
  814. }
  815. return ;
  816. }
  817. ////////////////////////////////////////////////////////////////////////////
  818. //
  819. // Set_Camera_Pos
  820. //
  821. ////////////////////////////////////////////////////////////////////////////
  822. void
  823. CameraMgr::Set_Camera_Pos (CAMERA_POS position)
  824. {
  825. SphereClass sp = ::Get_Selection_Mgr ().Get_Bounding_Sphere ();
  826. if (Get_Selection_Mgr ().Get_Count () == 0) {
  827. sp.Center = Vector3 (0, 0, 0);
  828. sp.Radius = 100.00F;
  829. }
  830. float distance = sp.Radius * 3.00F;
  831. distance = (distance < 1.0F) ? 1.0F : distance;
  832. distance = (distance > 400.0F) ? 400.0F : distance;
  833. Matrix3D transform (1);
  834. switch (position)
  835. {
  836. case CAMERA_FRONT:
  837. {
  838. transform.Look_At (sp.Center + Vector3 (distance, 0.00F, 0.00F), sp.Center, 0);
  839. }
  840. break;
  841. case CAMERA_BACK:
  842. {
  843. transform.Look_At (sp.Center + Vector3 (-distance, 0.00F, 0.00F), sp.Center, 0);
  844. }
  845. break;
  846. case CAMERA_LEFT:
  847. {
  848. transform.Look_At (sp.Center + Vector3 (0.00F, -distance, 0.00F), sp.Center, 0);
  849. }
  850. break;
  851. case CAMERA_RIGHT:
  852. {
  853. transform.Look_At (sp.Center + Vector3 (0.00F, distance, 0.00F), sp.Center, 0);
  854. }
  855. break;
  856. case CAMERA_TOP:
  857. {
  858. transform.Look_At (sp.Center + Vector3 (0.00F, 0.00F, distance), sp.Center, 3.1415926535F);
  859. }
  860. break;
  861. case CAMERA_BOTTOM:
  862. {
  863. transform.Look_At (sp.Center + Vector3 (0.00F, 0.00F, -distance), sp.Center, 3.1415926535F);
  864. }
  865. break;
  866. }
  867. // Move the camera back to get a good view of the object
  868. Set_Transform (transform);
  869. return ;
  870. }
  871. ////////////////////////////////////////////////////////////////////////////
  872. //
  873. // Goto_Node
  874. //
  875. ////////////////////////////////////////////////////////////////////////////
  876. void
  877. CameraMgr::Goto_Node (NodeClass *node)
  878. {
  879. RenderObjClass *render_obj = node->Peek_Render_Obj ();
  880. if (render_obj != NULL) {
  881. //
  882. // Get the node's position
  883. //
  884. SphereClass sphere = render_obj->Get_Bounding_Sphere ();
  885. Matrix3D tm = render_obj->Get_Transform ();
  886. tm.Set_Translation (Vector3 (0, 0, 0));
  887. //
  888. // Calculate an appropriate viewing distance
  889. //
  890. float distance = sphere.Radius * 4;
  891. distance = (distance < 1.0F) ? 1.0F : distance;
  892. distance = (distance > 100.0F) ? 100.0F : distance;
  893. Matrix3D transform (1);
  894. //transform.Look_At (sphere.Center + Vector3 (distance, distance, distance), sphere.Center, 0);
  895. Vector3 camera_loc = (sphere.Center + Vector3 (-distance, 0, distance));
  896. transform.Look_At (camera_loc, sphere.Center, 0);
  897. //tranform.Set_Translation (sphere.Center);
  898. //render_obj->
  899. //
  900. // Move the camera back to get a good view of the object
  901. //
  902. Set_Transform (transform);
  903. // Ensure the window is updated
  904. ::Refresh_Main_View ();
  905. }
  906. return ;
  907. }
  908. ////////////////////////////////////////////////////////////////////////////
  909. //
  910. // Goto_Group
  911. //
  912. ////////////////////////////////////////////////////////////////////////////
  913. void
  914. CameraMgr::Goto_Group (GroupMgrClass *group)
  915. {
  916. // State OK?
  917. if (group != NULL) {
  918. // Get the node's position
  919. SphereClass sp = group->Get_Bounding_Sphere ();
  920. float distance = sp.Radius;
  921. distance = (distance < 1.0F) ? 1.0F : distance;
  922. distance = (distance > 400.0F) ? 400.0F : distance;
  923. Matrix3D transform (1);
  924. transform.Look_At (sp.Center + Vector3 (distance, distance, distance), sp.Center, 0);
  925. // Move the camera back to get a good view of the object
  926. Set_Transform (transform);
  927. // Ensure the window is updated
  928. ::Refresh_Main_View ();
  929. }
  930. return ;
  931. }
  932. ////////////////////////////////////////////////////////////////////////////
  933. //
  934. // Set_Transform
  935. //
  936. ////////////////////////////////////////////////////////////////////////////
  937. void
  938. CameraMgr::Set_Transform (const Matrix3D &transform)
  939. {
  940. if (m_pCamera != NULL) {
  941. // Set the camera's new location and orientation
  942. m_pCamera->Set_Transform (transform);
  943. // Update the the UI to reflect this new position
  944. ((CMainFrame *)::AfxGetMainWnd ())->Update_Camera_Location (transform.Get_Translation ());
  945. Set_Modified (true);
  946. // If the camera settings dialog is visible, update its contents
  947. /*FormToolbarClass &toolbar = ((CMainFrame *)::AfxGetMainWnd ())->Get_Camera_Toolbar ();
  948. if (::IsWindow (toolbar) && toolbar.IsVisible ()) {
  949. CameraSettingsFormClass *pform = (CameraSettingsFormClass *)toolbar.Get_Form ();
  950. if (pform != NULL) {
  951. pform->Update_Controls ();
  952. }
  953. }*/
  954. }
  955. return ;
  956. }
  957. ////////////////////////////////////////////////////////////////////////////
  958. //
  959. // Set_Position
  960. //
  961. ////////////////////////////////////////////////////////////////////////////
  962. void
  963. CameraMgr::Set_Position (const Vector3 &position)
  964. {
  965. if (m_pCamera != NULL) {
  966. // Set the camera's new location and orientation
  967. m_pCamera->Set_Position (position);
  968. // Update the the UI to reflect this new position
  969. ((CMainFrame *)::AfxGetMainWnd ())->Update_Camera_Location (position);
  970. Set_Modified (true);
  971. // If the camera settings dialog is visible, update its contents
  972. /*FormToolbarClass &toolbar = ((CMainFrame *)::AfxGetMainWnd ())->Get_Camera_Toolbar ();
  973. if (::IsWindow (toolbar) && toolbar.IsVisible ()) {
  974. CameraSettingsFormClass *pform = (CameraSettingsFormClass *)toolbar.Get_Form ();
  975. if (pform != NULL) {
  976. pform->Update_Controls ();
  977. }
  978. }*/
  979. }
  980. return ;
  981. }
  982. ////////////////////////////////////////////////////////////////////////////
  983. //
  984. // Fly_To_Transform
  985. //
  986. ////////////////////////////////////////////////////////////////////////////
  987. void
  988. CameraMgr::Fly_To_Transform (const Matrix3D &transform)
  989. {
  990. m_FlyToStartTransform = m_pCamera->Get_Transform ();
  991. m_FlyToEndTransform = transform;
  992. Vector3 start_point = m_FlyToStartTransform.Get_Translation ();
  993. Vector3 end_point = m_FlyToEndTransform.Get_Translation ();
  994. Vector3 delta = end_point - start_point;
  995. float len = delta.Length () / 2;
  996. Vector3 middle_point = start_point + Vector3 (delta.X / 2, delta.Y / 2, delta.Z / 2 + len);
  997. // Remove all keys from the spline
  998. while (m_FlyToSpline.Key_Count () > 0) {
  999. m_FlyToSpline.Remove_Key (0);
  1000. }
  1001. Vector3 looking_start = -20.0F * m_FlyToStartTransform.Get_Z_Vector ();
  1002. int index = m_FlyToSpline.Add_Key (start_point, 0);
  1003. m_FlyToSpline.Set_Tangents (index, looking_start, looking_start);
  1004. Vector3 looking_end = -20.0F * m_FlyToEndTransform.Get_Z_Vector ();
  1005. index = m_FlyToSpline.Add_Key (end_point, 1);
  1006. m_FlyToSpline.Set_Tangents (index, looking_end, looking_end);
  1007. m_FlyToStartTime = ::GetTickCount ();
  1008. Set_Camera_Mode (MODE_FLYTO);
  1009. return ;
  1010. }
  1011. ////////////////////////////////////////////////////////////////////////////
  1012. //
  1013. // Update_Fly_To
  1014. //
  1015. ////////////////////////////////////////////////////////////////////////////
  1016. void
  1017. CameraMgr::Update_Fly_To (void)
  1018. {
  1019. const DWORD FLY_TICKS = 5000;
  1020. float fly_ticks = (float)FLY_TICKS;
  1021. if (::GetAsyncKeyState (VK_CONTROL) < 0) {
  1022. fly_ticks = fly_ticks / m_SpeedModifier;
  1023. }
  1024. DWORD current_time = ::GetTickCount ();
  1025. float spline_time = float(current_time - m_FlyToStartTime) / fly_ticks;
  1026. spline_time = min (1.0F, spline_time);
  1027. if (m_FlyToStartTime + ((DWORD)fly_ticks) < current_time) {
  1028. m_pCamera->Set_Transform (m_FlyToEndTransform);
  1029. Set_Camera_Mode (MODE_MOVE_PLANE);
  1030. } else {
  1031. Vector3 position (0,0,0);
  1032. Vector3 next_position (0,0,0);
  1033. m_FlyToSpline.Evaluate (spline_time, &position);
  1034. m_FlyToSpline.Evaluate (spline_time + 0.05F, &next_position);
  1035. Matrix3D rot_90 (1);
  1036. rot_90.Rotate_Z ((float)DEG_TO_RAD (90));
  1037. Vector3 x_axis = next_position - position;
  1038. x_axis.Normalize ();
  1039. Vector3 y_axis = rot_90 * x_axis;
  1040. y_axis.Normalize ();
  1041. Vector3 z_axis;
  1042. Vector3::Cross_Product (x_axis, y_axis, &z_axis);
  1043. Matrix3D orientation (x_axis, y_axis, z_axis, position);
  1044. Matrix3D world_to_cam (Vector3 (0, -1, 0), Vector3 (0, 0, 1), Vector3 (-1, 0, 0), Vector3 (0, 0, 0));
  1045. Matrix3D camera_tm = orientation * world_to_cam;
  1046. if (camera_tm.Is_Orthogonal () == false) {
  1047. TRACE ("Matrix not orthogonal!\r\n");
  1048. camera_tm.Re_Orthogonalize();
  1049. }
  1050. Set_Transform (camera_tm);
  1051. }
  1052. return ;
  1053. }
  1054. ////////////////////////////////////////////////////////////////////////////
  1055. //
  1056. // Get_Character_TM
  1057. //
  1058. ////////////////////////////////////////////////////////////////////////////
  1059. Matrix3D
  1060. CameraMgr::Get_Character_TM (void)
  1061. {
  1062. Matrix3D tm (1);
  1063. if (m_WalkThruObj != NULL) {
  1064. tm = m_WalkThruObj->Get_Transform ();
  1065. } else {
  1066. tm = m_pCamera->Get_Transform ();
  1067. }
  1068. return tm;
  1069. }