camera.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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/camera.cpp $*
  25. * *
  26. * Author:: Greg_h *
  27. * *
  28. * $Modtime:: 11/26/01 5:30p $*
  29. * *
  30. * $Revision:: 23 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * CameraClass::CameraClass -- constructor *
  35. * CameraClass::CameraClass -- copy constructor *
  36. * CameraClass::operator == -- assignment operator *
  37. * CameraClass::~CameraClass -- destructor *
  38. * CameraClass::Clone -- virtual copy constructor *
  39. * CameraClass::Add -- add the camera to the world? *
  40. * CameraClass::Remove -- Remove the camera from the world? *
  41. * CameraClass::Get_Obj_Space_Bounding_Sphere -- returns the object space bounding sphere *
  42. * CameraClass::Get_Object_Space_Bounding_Box -- returns the object space bounding box *
  43. * CameraClass::Set_Transform -- set the transform of the camera *
  44. * CameraClass::Set_Position -- Set the position of the camera *
  45. * CameraClass::Set_Animation -- set the animation state of the camera *
  46. * CameraClass::Set_Animation -- Set the animation state of the camera *
  47. * CameraClass::Set_Animation -- set the animation state of the camera *
  48. * CameraClass::Set_Animation -- set the animation state of the camera *
  49. * CameraClass::Set_View_Plane -- control over the view plane *
  50. * CameraClass::Set_View_Plane -- set the viewplane using fov angles *
  51. * CameraClass::Set_Focal_Length -- set the view plane using focal length and aspect ratio *
  52. * CameraClass::Get_View_Plane -- get the corners of the current view plane *
  53. * CameraClass::Project -- project a point from ws to the view plane *
  54. * CameraClass::Project_Camera_Space_Point -- Project a point that is already in camera spac *
  55. * CameraClass::Un_Project -- "unproject" a point from the view plane to world space *
  56. * CameraClass::Transform_To_View_Space -- transforms the given world space point to camera *
  57. * CameraClass::Rotate_To_View_Space -- rotates the given world space vector to camera space *
  58. * CameraClass::Get_Near_Clip_Bounding_Box -- returns an obb that contains near clip plane *
  59. * CameraCLass::Update_Frustum -- updates the frustum parameters *
  60. * CameraClass::Cull_Box -- tests whether the given box can be culled *
  61. * CameraClass::Device_To_View_Space -- converts the given device coordinate to view space *
  62. * CameraClass::Device_To_World_Space -- converts given device coord to world space *
  63. * CameraClass::Camera_Push -- pushes the camera's parameters into the given GERD *
  64. * CameraClass::Camera_Pop -- pops the camera's parameters from the given GERD *
  65. * CameraClass::Set_Aspect_Ratio -- sets the aspect ratio of the camera *
  66. * CameraClass::Apply_D3D_State -- sets the D3D states controlled by the camera *
  67. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  68. #include "camera.h"
  69. #include "ww3d.h"
  70. #include "matrix4.h"
  71. #include "dx8wrapper.h"
  72. /***********************************************************************************************
  73. * CameraClass::CameraClass -- constructor *
  74. * *
  75. * INPUT: *
  76. * *
  77. * OUTPUT: *
  78. * *
  79. * WARNINGS: *
  80. * *
  81. * HISTORY: *
  82. * 3/21/98 GTH : Created. *
  83. *=============================================================================================*/
  84. CameraClass::CameraClass(void) :
  85. Projection(PERSPECTIVE),
  86. Viewport(Vector2(0,0),Vector2(1,1)), // pixel viewport to render into
  87. AspectRatio(4.0f/3.0f),
  88. ZNear(1.0f), // near clip plane distance
  89. ZFar(1000.0f), // far clip plane distance
  90. ZBufferMin(0.0f), // smallest value we'll write into the z-buffer
  91. ZBufferMax(1.0f), // largest value we'll write into the z-buffer
  92. FrustumValid(false)
  93. {
  94. Set_Transform(Matrix3D(1));
  95. Set_View_Plane(DEG_TO_RADF(50.0f));
  96. }
  97. /***********************************************************************************************
  98. * CameraClass::CameraClass -- copy constructor *
  99. * *
  100. * INPUT: *
  101. * *
  102. * OUTPUT: *
  103. * *
  104. * WARNINGS: *
  105. * *
  106. * HISTORY: *
  107. * 3/21/98 GTH : Created. *
  108. * 4/13/2001 hy : added in copy code for new member functions *
  109. *=============================================================================================*/
  110. CameraClass::CameraClass(const CameraClass & src) :
  111. RenderObjClass(src),
  112. Projection(src.Projection),
  113. Viewport(src.Viewport),
  114. ViewPlane(src.ViewPlane),
  115. ZNear(src.ZNear),
  116. ZFar(src.ZFar),
  117. FrustumValid(src.FrustumValid),
  118. Frustum(src.Frustum),
  119. NearClipBBox(src.NearClipBBox),
  120. ProjectionTransform(src.ProjectionTransform),
  121. CameraInvTransform(src.CameraInvTransform),
  122. AspectRatio(src.AspectRatio),
  123. ZBufferMin(src.ZBufferMin),
  124. ZBufferMax(src.ZBufferMax)
  125. {
  126. // just being paraniod in case any parent class doesn't completely copy the entire state...
  127. FrustumValid = false;
  128. }
  129. /***********************************************************************************************
  130. * CameraClass::operator == -- assignment operator *
  131. * *
  132. * INPUT: *
  133. * *
  134. * OUTPUT: *
  135. * *
  136. * WARNINGS: *
  137. * *
  138. * HISTORY: *
  139. * 3/21/98 GTH : Created. *
  140. *=============================================================================================*/
  141. CameraClass & CameraClass::operator = (const CameraClass & that)
  142. {
  143. if (this != &that) {
  144. RenderObjClass::operator = (that);
  145. Projection = that.Projection;
  146. Viewport = that.Viewport;
  147. ViewPlane = that.ViewPlane;
  148. ZNear = that.ZNear;
  149. ZFar = that.ZFar;
  150. FrustumValid = that.FrustumValid;
  151. Frustum = that.Frustum;
  152. NearClipBBox = that.NearClipBBox;
  153. ProjectionTransform = that.ProjectionTransform;
  154. CameraInvTransform = that.CameraInvTransform;
  155. // just being paraniod in case any parent class doesn't completely copy the entire state...
  156. FrustumValid = false;
  157. }
  158. return * this;
  159. }
  160. /***********************************************************************************************
  161. * CameraClass::~CameraClass -- destructor *
  162. * *
  163. * INPUT: *
  164. * *
  165. * OUTPUT: *
  166. * *
  167. * WARNINGS: *
  168. * *
  169. * HISTORY: *
  170. * 3/21/98 GTH : Created. *
  171. *=============================================================================================*/
  172. CameraClass::~CameraClass(void)
  173. {
  174. }
  175. /***********************************************************************************************
  176. * CameraClass::Clone -- virtual copy constructor *
  177. * *
  178. * INPUT: *
  179. * *
  180. * OUTPUT: *
  181. * *
  182. * WARNINGS: *
  183. * *
  184. * HISTORY: *
  185. * 3/21/98 GTH : Created. *
  186. *=============================================================================================*/
  187. RenderObjClass * CameraClass::Clone(void) const
  188. {
  189. return NEW_REF( CameraClass, (*this) );
  190. }
  191. /***********************************************************************************************
  192. * CameraClass::Get_Obj_Space_Bounding_Sphere -- returns the object space bounding sphere *
  193. * *
  194. * INPUT: *
  195. * *
  196. * OUTPUT: *
  197. * *
  198. * WARNINGS: *
  199. * *
  200. * HISTORY: *
  201. * 9/29/98 GTH : Created. *
  202. *=============================================================================================*/
  203. void CameraClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  204. {
  205. sphere.Center.Set(0,0,0);
  206. sphere.Radius = ZFar; // could optimize this but its not really used.
  207. }
  208. /***********************************************************************************************
  209. * CameraClass::Get_Object_Space_Bounding_Box -- returns the object space bounding box *
  210. * *
  211. * INPUT: *
  212. * *
  213. * OUTPUT: *
  214. * *
  215. * WARNINGS: *
  216. * *
  217. * HISTORY: *
  218. * 9/29/98 GTH : Created. *
  219. *=============================================================================================*/
  220. void CameraClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  221. {
  222. box.Center.Set(0,0,0);
  223. box.Extent.Set(ZFar,ZFar,ZFar); // could optimize this but its not really used.
  224. }
  225. /***********************************************************************************************
  226. * CameraClass::Set_Transform -- set the transform of the camera *
  227. * *
  228. * This is over-ridden to invalidate the cached frustum parameters *
  229. * *
  230. * INPUT: *
  231. * *
  232. * OUTPUT: *
  233. * *
  234. * WARNINGS: *
  235. * *
  236. * HISTORY: *
  237. * 5/29/98 GTH : Created. *
  238. *=============================================================================================*/
  239. void CameraClass::Set_Transform(const Matrix3D &m)
  240. {
  241. RenderObjClass::Set_Transform(m);
  242. FrustumValid = false;
  243. }
  244. /***********************************************************************************************
  245. * CameraClass::Set_Position -- Set the position of the camera *
  246. * *
  247. * This is overriden to invalidate the cached frustum parameters *
  248. * *
  249. * INPUT: *
  250. * *
  251. * OUTPUT: *
  252. * *
  253. * WARNINGS: *
  254. * *
  255. * HISTORY: *
  256. * 5/29/98 GTH : Created. *
  257. *=============================================================================================*/
  258. void CameraClass::Set_Position(const Vector3 &v)
  259. {
  260. RenderObjClass::Set_Position(v);
  261. FrustumValid = false;
  262. }
  263. /***********************************************************************************************
  264. * CameraClass::Set_View_Plane -- control over the view plane *
  265. * *
  266. * INPUT: *
  267. * min - x,y of the upper left corner of the view rectangle (dist is assumed to be 1.0) *
  268. * max - x,y of the lower right corner of the view rectangle *
  269. * *
  270. * OUTPUT: *
  271. * *
  272. * WARNINGS: *
  273. * *
  274. * HISTORY: *
  275. * 3/21/98 GTH : Created. *
  276. *=============================================================================================*/
  277. void CameraClass::Set_View_Plane(const Vector2 & vmin,const Vector2 & vmax)
  278. {
  279. ViewPlane.Min = vmin;
  280. ViewPlane.Max = vmax;
  281. AspectRatio = (vmax.X - vmin.X) / (vmax.Y - vmin.Y);
  282. FrustumValid = false;
  283. }
  284. /***********************************************************************************************
  285. * CameraClass::Set_View_Plane -- set the viewplane using fov angles *
  286. * *
  287. * INPUT: *
  288. * *
  289. * OUTPUT: *
  290. * *
  291. * WARNINGS: *
  292. * *
  293. * HISTORY: *
  294. * 3/21/98 GTH : Created. *
  295. *=============================================================================================*/
  296. void CameraClass::Set_View_Plane(float hfov,float vfov)
  297. {
  298. float width_half = tan(hfov/2.0);
  299. float height_half = 0.0f;
  300. if (vfov == -1) {
  301. height_half = (1.0f / AspectRatio) * width_half; // use the aspect ratio
  302. } else {
  303. height_half = tan(vfov/2.0);
  304. AspectRatio = width_half / height_half; // or, initialize the aspect ratio
  305. }
  306. ViewPlane.Min.Set(-width_half,-height_half);
  307. ViewPlane.Max.Set(width_half,height_half);
  308. FrustumValid = false;
  309. }
  310. /***********************************************************************************************
  311. * CameraClass::Set_Aspect_Ratio -- sets the aspect ratio of the camera *
  312. * *
  313. * INPUT: *
  314. * *
  315. * OUTPUT: *
  316. * *
  317. * WARNINGS: *
  318. * *
  319. * HISTORY: *
  320. * 1/29/2001 gth : Created. *
  321. *=============================================================================================*/
  322. void CameraClass::Set_Aspect_Ratio(float width_to_height)
  323. {
  324. AspectRatio = width_to_height;
  325. ViewPlane.Min.Y = ViewPlane.Min.X / AspectRatio;
  326. ViewPlane.Max.Y = ViewPlane.Max.X / AspectRatio;
  327. FrustumValid = false;
  328. }
  329. /***********************************************************************************************
  330. * CameraClass::Get_View_Plane -- get the corners of the current view plane *
  331. * *
  332. * INPUT: *
  333. * *
  334. * OUTPUT: *
  335. * *
  336. * WARNINGS: *
  337. * *
  338. * HISTORY: *
  339. * 3/21/98 GTH : Created. *
  340. *=============================================================================================*/
  341. void CameraClass::Get_View_Plane(Vector2 & set_min,Vector2 & set_max) const
  342. {
  343. set_min = ViewPlane.Min;
  344. set_max = ViewPlane.Max;
  345. }
  346. /***********************************************************************************************
  347. * CameraClass::Project -- project a point from ws to the view plane *
  348. * *
  349. * INPUT: *
  350. * dest - will be set to a point on the normalized view plane. x,y, and z range from -1 to 1 *
  351. * ws_point - input point in world space *
  352. * *
  353. * OUTPUT: *
  354. * ProjectionResType indicating whether the point was in the frustum *
  355. * *
  356. * WARNINGS: *
  357. * When the input is behind the near clip plane, 0,0,0 is returned. *
  358. * *
  359. * HISTORY: *
  360. * 3/21/98 GTH : Created. *
  361. *=============================================================================================*/
  362. CameraClass::ProjectionResType CameraClass::Project(Vector3 & dest,const Vector3 & ws_point) const
  363. {
  364. Update_Frustum();
  365. Vector3 cam_point;
  366. Matrix3D::Transform_Vector(CameraInvTransform,ws_point,&cam_point);
  367. if (cam_point.Z > -ZNear) {
  368. dest.Set(0,0,0);
  369. return OUTSIDE_NEAR_CLIP;
  370. }
  371. Vector4 view_point = ProjectionTransform * cam_point;
  372. float oow = 1.0f / view_point.W;
  373. dest.X = view_point.X * oow;
  374. dest.Y = view_point.Y * oow;
  375. dest.Z = view_point.Z * oow;
  376. if (dest.Z > 1.0f) {
  377. return OUTSIDE_FAR_CLIP;
  378. }
  379. if ((dest.X < -1.0f) || (dest.X > 1.0f) || (dest.Y < -1.0f) || (dest.Y > 1.0f)) {
  380. return OUTSIDE_FRUSTUM;
  381. }
  382. return INSIDE_FRUSTUM;
  383. }
  384. /***********************************************************************************************
  385. * CameraClass::Project_Camera_Space_Point -- Project a point that is already in camera space *
  386. * *
  387. * INPUT: *
  388. * dest - will be set to a point on the normalized view plane. x,y, and z range from -1 to 1 *
  389. * cam_point - input point in camera space *
  390. * *
  391. * OUTPUT: *
  392. * ProjectionResType indicating whether the point was in the frustum *
  393. * *
  394. * WARNINGS: *
  395. * When the input is behind the near clip plane, 0,0,0 is returned. *
  396. * *
  397. * HISTORY: *
  398. * 11/17/2000 gth : Created. *
  399. *=============================================================================================*/
  400. CameraClass::ProjectionResType
  401. CameraClass::Project_Camera_Space_Point(Vector3 & dest,const Vector3 & cam_point) const
  402. {
  403. Update_Frustum();
  404. if ( cam_point.Z > -ZNear + WWMATH_EPSILON) {
  405. dest.Set(0,0,0);
  406. return OUTSIDE_NEAR_CLIP;
  407. }
  408. Vector4 view_point = ProjectionTransform * cam_point;
  409. float oow = 1.0f / view_point.W;
  410. dest.X = view_point.X * oow;
  411. dest.Y = view_point.Y * oow;
  412. dest.Z = view_point.Z * oow;
  413. if (dest.Z > 1.0f) {
  414. return OUTSIDE_FAR_CLIP;
  415. }
  416. if ((dest.X < -1.0f) || (dest.X > 1.0f) || (dest.Y < -1.0f) || (dest.Y > 1.0f)) {
  417. return OUTSIDE_FRUSTUM;
  418. }
  419. return INSIDE_FRUSTUM;
  420. }
  421. /***********************************************************************************************
  422. * CameraClass::Un_Project -- "unproject" a point from the view plane to world space *
  423. * *
  424. * The given point is assumed to be on the view_plane at z=-1. The 3D world space point that *
  425. * this represents will be returned in dest. *
  426. * *
  427. * INPUT: *
  428. * dest - will be filled in with the 3D world space point that the given point represents *
  429. * view_point - point on the view_plane to be un-projected *
  430. * *
  431. * OUTPUT: *
  432. * *
  433. * WARNINGS: *
  434. * *
  435. * HISTORY: *
  436. * 3/21/98 GTH : Created. *
  437. *=============================================================================================*/
  438. void CameraClass::Un_Project(Vector3 & dest,const Vector2 & view_point) const
  439. {
  440. /*
  441. ** map view_point.X from -1..1 to ViewPlaneMin.X..ViewPlaneMax.X
  442. ** map view_point.Y from -1..1 to ViewPlaneMin.X..ViewPlaneMax.X
  443. */
  444. float vpdx = ViewPlane.Max.X - ViewPlane.Min.X;
  445. float vpdy = ViewPlane.Max.Y - ViewPlane.Min.Y;
  446. Vector3 point;
  447. point.X = ViewPlane.Min.X + vpdx * (view_point.X + 1.0f) * 0.5f;
  448. point.Y = ViewPlane.Min.Y + vpdy * (view_point.Y + 1.0f) * 0.5f;
  449. point.Z = -1.0f;
  450. Matrix3D::Transform_Vector(Transform,point,&dest);
  451. }
  452. /***********************************************************************************************
  453. * CameraClass::Transform_To_View_Space -- transforms the given world space point to camera sp *
  454. * *
  455. * INPUT: *
  456. * *
  457. * OUTPUT: *
  458. * *
  459. * WARNINGS: *
  460. * *
  461. * HISTORY: *
  462. * 2/22/2001 gth : Created. *
  463. *=============================================================================================*/
  464. void CameraClass::Transform_To_View_Space(Vector3 & dest,const Vector3 & ws_point) const
  465. {
  466. Update_Frustum();
  467. Matrix3D::Transform_Vector(CameraInvTransform,ws_point,&dest);
  468. }
  469. /***********************************************************************************************
  470. * CameraClass::Rotate_To_View_Space -- rotates the given world space vector to camera space *
  471. * *
  472. * INPUT: *
  473. * *
  474. * OUTPUT: *
  475. * *
  476. * WARNINGS: *
  477. * *
  478. * HISTORY: *
  479. * 2/22/2001 gth : Created. *
  480. *=============================================================================================*/
  481. void CameraClass::Rotate_To_View_Space(Vector3 & dest,const Vector3 & ws_vector) const
  482. {
  483. Update_Frustum();
  484. Matrix3D::Rotate_Vector(CameraInvTransform,ws_vector,&dest);
  485. }
  486. /***********************************************************************************************
  487. * CameraClass::Get_Near_Clip_Bounding_Box -- returns an obb that contains near clip plane *
  488. * *
  489. * INPUT: *
  490. * *
  491. * OUTPUT: *
  492. * *
  493. * WARNINGS: *
  494. * *
  495. * HISTORY: *
  496. * 8/25/99 GTH : Created. *
  497. *=============================================================================================*/
  498. const OBBoxClass & CameraClass::Get_Near_Clip_Bounding_Box(void) const
  499. {
  500. Update_Frustum();
  501. return NearClipBBox;
  502. }
  503. /***********************************************************************************************
  504. * CameraClass::Cull_Box -- tests whether the given box can be culled *
  505. * *
  506. * INPUT: *
  507. * *
  508. * OUTPUT: *
  509. * *
  510. * WARNINGS: *
  511. * *
  512. * HISTORY: *
  513. * 12/8/98 GTH : Created. *
  514. *=============================================================================================*/
  515. bool CameraClass::Cull_Box(const AABoxClass & box) const
  516. {
  517. const FrustumClass & frustum = Get_Frustum();
  518. return CollisionMath::Overlap_Test(frustum,box) == CollisionMath::OUTSIDE;
  519. }
  520. /***********************************************************************************************
  521. * CameraClass::Update_Frustum -- updates the frustum parameters *
  522. * *
  523. * INPUT: *
  524. * *
  525. * OUTPUT: *
  526. * *
  527. * WARNINGS: *
  528. * *
  529. * HISTORY: *
  530. * 5/29/98 GTH : Created. *
  531. *=============================================================================================*/
  532. void CameraClass::Update_Frustum(void) const
  533. {
  534. if (FrustumValid) return;
  535. Vector2 vpmin,vpmax;
  536. float znear,zfar;
  537. float znear_dist,zfar_dist;
  538. Matrix3D cam_mat = Get_Transform();
  539. Get_View_Plane(vpmin, vpmax); // Normalized view plane at a depth of 1.0
  540. Get_Clip_Planes(znear_dist, zfar_dist);
  541. // Forward is negative Z in our viewspace coordinate system.
  542. znear = -znear_dist;
  543. zfar = -zfar_dist;
  544. // Update the frustum
  545. FrustumValid = true;
  546. Frustum.Init(cam_mat,vpmin,vpmax,znear,zfar);
  547. ViewSpaceFrustum.Init(Matrix3D(1),vpmin,vpmax,znear,zfar);
  548. // Update the OBB around the near clip rectangle
  549. NearClipBBox.Center = cam_mat * Vector3(0,0,znear);
  550. NearClipBBox.Extent.X = (vpmax.X - vpmin.X) * (-znear) * 0.5f; // (near_clip_x / |znear|) == (vpmin.X / 1.0f)...
  551. NearClipBBox.Extent.Y = (vpmax.Y - vpmin.Y) * (-znear) * 0.5f;
  552. NearClipBBox.Extent.Z = 0.01f;
  553. NearClipBBox.Basis.Set(cam_mat);
  554. // Update the inverse camera matrix
  555. Transform.Get_Inverse(CameraInvTransform);
  556. // Update the projection matrix
  557. if (Projection == PERSPECTIVE) {
  558. ProjectionTransform.Init_Perspective( vpmin.X*znear_dist, vpmax.X*znear_dist,
  559. vpmin.Y*znear_dist, vpmax.Y*znear_dist,
  560. znear_dist, zfar_dist );
  561. } else {
  562. ProjectionTransform.Init_Ortho( vpmin.X,vpmax.X,vpmin.Y,vpmax.Y,znear_dist,zfar_dist);
  563. }
  564. }
  565. /***********************************************************************************************
  566. * CameraClass::Device_To_View_Space -- converts the given device coordinate to view space *
  567. * *
  568. * INPUT: *
  569. * *
  570. * OUTPUT: *
  571. * *
  572. * WARNINGS: *
  573. * *
  574. * HISTORY: *
  575. * 12/8/98 GTH : Created. *
  576. *=============================================================================================*/
  577. void CameraClass::Device_To_View_Space(const Vector2 & device_coord,Vector3 * set_view)
  578. {
  579. int res_width;
  580. int res_height;
  581. int res_bits;
  582. bool windowed;
  583. WW3D::Get_Render_Target_Resolution(res_width,res_height,res_bits,windowed);
  584. // convert the device coordinates into normalized device coordinates:
  585. Vector2 ndev;
  586. ndev.X = device_coord.X / (float)res_width;
  587. ndev.Y = device_coord.Y / (float)res_height;
  588. // view space rectangle which corresponds to the viewport
  589. Vector2 vs_min;
  590. Vector2 vs_max;
  591. Get_View_Plane(vs_min,vs_max);
  592. // mapping from the viewport coordinates to view space coordinates
  593. set_view->X = vs_min.X + (ndev.X - Viewport.Min.X) * (vs_max.X - vs_min.X) / (Viewport.Width());
  594. set_view->Y = vs_max.Y - (ndev.Y - Viewport.Min.Y) * (vs_max.Y - vs_min.Y) / (Viewport.Height());
  595. set_view->Z = -1.0f;
  596. }
  597. /***********************************************************************************************
  598. * CameraClass::Device_To_World_Space -- converts given device coord to world space *
  599. * *
  600. * INPUT: *
  601. * *
  602. * OUTPUT: *
  603. * *
  604. * WARNINGS: *
  605. * *
  606. * HISTORY: *
  607. * 12/8/98 GTH : Created. *
  608. *=============================================================================================*/
  609. void CameraClass::Device_To_World_Space(const Vector2 & device_coord,Vector3 * world_coord)
  610. {
  611. Vector3 vs;
  612. Device_To_View_Space(device_coord,&vs);
  613. Matrix3D::Transform_Vector(Transform,vs,world_coord);
  614. }
  615. /***********************************************************************************************
  616. * CameraClass::Apply -- sets the D3D states controlled by the camera *
  617. * *
  618. * INPUT: *
  619. * *
  620. * OUTPUT: *
  621. * *
  622. * WARNINGS: *
  623. * *
  624. * HISTORY: *
  625. * 1/29/2001 gth : Created. *
  626. *=============================================================================================*/
  627. void CameraClass::Apply(void)
  628. {
  629. Update_Frustum();
  630. int width,height,bits;
  631. bool windowed;
  632. WW3D::Get_Render_Target_Resolution(width,height,bits,windowed);
  633. D3DVIEWPORT8 vp;
  634. vp.X = (DWORD)(Viewport.Min.X * (float)width);
  635. vp.Y = (DWORD)(Viewport.Min.Y * (float)height);
  636. vp.Width = (DWORD)((Viewport.Max.X - Viewport.Min.X) * (float)width);
  637. vp.Height = (DWORD)((Viewport.Max.Y - Viewport.Min.Y) * (float)height);
  638. vp.MinZ = ZBufferMin;
  639. vp.MaxZ = ZBufferMax;
  640. DX8Wrapper::Set_Viewport(&vp);
  641. Matrix4 d3dprojection;
  642. Get_D3D_Projection_Matrix(&d3dprojection);
  643. DX8Wrapper::Set_Projection_Transform_With_Z_Bias(d3dprojection,ZNear,ZFar);
  644. DX8Wrapper::Set_Transform(D3DTS_VIEW,CameraInvTransform);
  645. }
  646. void CameraClass::Set_Clip_Planes(float znear,float zfar)
  647. {
  648. FrustumValid = false;
  649. ZNear = znear;
  650. ZFar = zfar;
  651. }
  652. void CameraClass::Get_Clip_Planes(float & znear,float & zfar) const
  653. {
  654. znear = ZNear;
  655. zfar = ZFar;
  656. }
  657. float CameraClass::Get_Horizontal_FOV(void) const
  658. {
  659. float width = ViewPlane.Max.X - ViewPlane.Min.X;
  660. return 2*WWMath::Atan2(width,2.0);
  661. }
  662. float CameraClass::Get_Vertical_FOV(void) const
  663. {
  664. float height = ViewPlane.Max.Y - ViewPlane.Min.Y;
  665. return 2*WWMath::Atan2(height,2.0);
  666. }
  667. float CameraClass::Get_Aspect_Ratio(void) const
  668. {
  669. return AspectRatio;
  670. }
  671. void CameraClass::Get_Projection_Matrix(Matrix4 * set_tm)
  672. {
  673. WWASSERT(set_tm != NULL);
  674. Update_Frustum();
  675. *set_tm = ProjectionTransform;
  676. }
  677. void CameraClass::Get_D3D_Projection_Matrix(Matrix4 * set_tm)
  678. {
  679. WWASSERT(set_tm != NULL);
  680. Update_Frustum();
  681. *set_tm = ProjectionTransform;
  682. /*
  683. ** We need to flip the handed-ness of the projection matrix and
  684. ** move the z-range to 0<z<1 rather than -1<z<1
  685. */
  686. float oozdiff = 1.0 / (ZFar - ZNear);
  687. if (Projection == PERSPECTIVE) {
  688. (*set_tm)[2][2] = -(ZFar) * oozdiff;
  689. (*set_tm)[2][3] = -(ZFar*ZNear) * oozdiff;
  690. } else {
  691. (*set_tm)[2][2] = -oozdiff;
  692. (*set_tm)[2][3] = -ZNear * oozdiff;
  693. }
  694. }
  695. void CameraClass::Get_View_Matrix(Matrix3D * set_tm)
  696. {
  697. WWASSERT(set_tm != NULL);
  698. Update_Frustum();
  699. *set_tm = CameraInvTransform;
  700. }
  701. const Matrix4 & CameraClass::Get_Projection_Matrix(void)
  702. {
  703. Update_Frustum();
  704. return ProjectionTransform;
  705. }
  706. const Matrix3D & CameraClass::Get_View_Matrix(void)
  707. {
  708. Update_Frustum();
  709. return CameraInvTransform;
  710. }
  711. void CameraClass::Convert_Old(Vector3 &pos)
  712. {
  713. pos.X=(pos.X+1)/2;
  714. pos.Y=(pos.Y+1)/2;
  715. }
  716. float CameraClass::Compute_Projected_Sphere_Radius(float dist,float radius)
  717. {
  718. Vector4 result = ProjectionTransform * Vector4(radius,0.0f,dist,1.0f);
  719. return result.X / result.W;
  720. }