camera.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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.h $*
  25. * *
  26. * Author:: Greg_h *
  27. * *
  28. * $Modtime:: 7/31/01 10:52a $*
  29. * *
  30. * $Revision:: 13 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * CameraClass::Get_Frustum -- returns the frustum of the camera *
  35. * CameraClass::Get_Frustum_Planes -- returns pointer to the array of frustum planes *
  36. * CameraClass::Get_Frustum_Corners -- returns pointer to the array of frustum corners *
  37. * CameraClass::Get_View_Space_Frustum -- returns the view-space frustum for this camera *
  38. * CameraClass::Get_View_Space_Frustum_Planes -- returns the view space clip planes for this *
  39. * CameraClass::Get_View_Space_Frustum_Corners -- returns the corners of the view space frus *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. #if defined(_MSC_VER)
  42. #pragma once
  43. #endif
  44. #ifndef CAMERA_H
  45. #define CAMERA_H
  46. #include "always.h"
  47. #include "rendobj.h"
  48. #include "plane.h"
  49. #include "frustum.h"
  50. #include "obbox.h"
  51. #include "vector2.h"
  52. #include "matrix4.h"
  53. #include "colmath.h"
  54. class RenderInfoClass;
  55. /**
  56. ** ViewportClass
  57. ** This class is used to define a "normalized" screen space rectangle for the
  58. ** camera to render into. A viewport which filled the entire screen would be
  59. ** (0,0) - (1,1) with 0,0 being the upper left and 1,1 being the lower right.
  60. */
  61. class ViewportClass
  62. {
  63. public:
  64. ViewportClass(void) : Min(0,0), Max(1,1) { }
  65. ViewportClass(const Vector2 & min,const Vector2 & max) : Min(min), Max(max) { }
  66. ViewportClass(const ViewportClass & vp) : Min(vp.Min), Max(vp.Max) { }
  67. float Width(void) const { return Max.X - Min.X; }
  68. float Height(void) const { return Max.Y - Min.Y; }
  69. Vector2 Min;
  70. Vector2 Max;
  71. };
  72. /**
  73. ** CameraClass
  74. ** This object controls how vertices are transformed from world-space to view
  75. ** space, the parameters of the perspective projection, and the viewport
  76. ** on screen that the result is mapped into.
  77. **
  78. ** Cameras are not "rendered" and do not need to be "added" to a scene. A
  79. ** CameraClass is passed into the WW3D::Render(...) function. The reason
  80. ** they are render objects is so that they can be inserted onto the bone of
  81. ** some animation and move with the animation...
  82. **
  83. ** For all of the projection functions (Matrix4, ProjectorClass (used by
  84. ** decals and texture projections), and CameraClass) I followed the OpenGL
  85. ** convention of passing positive distances for your clip planes even though
  86. ** in a right-handed coordinate system your z values are negative after
  87. ** transformation to camera space. So Set_Clip_Planes expects positive distances
  88. ** to your near and far clip planes.
  89. **
  90. ** (gth) We should probably separate CameraClass from RenderObjClass... In the
  91. ** case where a camera is using a transform from an animation; the programmer
  92. ** is usually requesting the transform and plugging it into the camera anyway.
  93. */
  94. class CameraClass : public RenderObjClass
  95. {
  96. public:
  97. enum ProjectionType
  98. {
  99. PERSPECTIVE = 0,
  100. ORTHO
  101. };
  102. enum ProjectionResType
  103. {
  104. INSIDE_FRUSTUM,
  105. OUTSIDE_FRUSTUM,
  106. OUTSIDE_NEAR_CLIP,
  107. OUTSIDE_FAR_CLIP,
  108. };
  109. CameraClass(void);
  110. CameraClass(const CameraClass & src);
  111. CameraClass & operator = (const CameraClass &);
  112. virtual ~CameraClass(void);
  113. virtual RenderObjClass * Clone(void) const;
  114. virtual int Class_ID(void) const { return CLASSID_CAMERA; }
  115. /////////////////////////////////////////////////////////////////////////////
  116. // Render Object Interface - Rendering, cameras don't "render"
  117. /////////////////////////////////////////////////////////////////////////////
  118. virtual void Render(RenderInfoClass & rinfo) { }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // Render Object Interface - "Scene Graph"
  121. // Cameras cache their frustum description, this is invalidated whenever
  122. // the transform/position is changed
  123. /////////////////////////////////////////////////////////////////////////////
  124. virtual void Set_Transform(const Matrix3D &m);
  125. virtual void Set_Position(const Vector3 &v);
  126. /////////////////////////////////////////////////////////////////////////////
  127. // Render Object Interface - Bounding Volumes
  128. /////////////////////////////////////////////////////////////////////////////
  129. virtual void Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const;
  130. virtual void Get_Obj_Space_Bounding_Box(AABoxClass & box) const;
  131. ///////////////////////////////////////////////////////////////////////////
  132. // Camera parameter control
  133. ///////////////////////////////////////////////////////////////////////////
  134. // Depth of the scene.
  135. float Get_Depth(void) const;
  136. // Setting the projection type
  137. void Set_Projection_Type(ProjectionType ptype);
  138. ProjectionType Get_Projection_Type(void);
  139. // Setting the clipping ranges in world space distances
  140. void Set_Clip_Planes(float znear,float zfar);
  141. void Get_Clip_Planes(float & znear,float & zfar) const;
  142. // Methods for setting the View Plane.
  143. // NOTE: View plane is always at a distance of 1.0 from the eye.
  144. void Set_View_Plane(const Vector2 & min,const Vector2 & max);
  145. void Set_View_Plane(float hfov,float vfov = -1);
  146. void Set_Aspect_Ratio(float width_to_height);
  147. // Methods for querying the View Plane settings.
  148. void Get_View_Plane(Vector2 & set_min,Vector2 & set_max) const;
  149. float Get_Horizontal_FOV(void) const;
  150. float Get_Vertical_FOV(void) const;
  151. float Get_Aspect_Ratio(void) const;
  152. // Access to the projection matrices for this camera
  153. void Get_Projection_Matrix(Matrix4 * set_tm);
  154. void Get_D3D_Projection_Matrix(Matrix4 * set_tm);
  155. void Get_View_Matrix(Matrix3D * set_tm);
  156. const Matrix4 & Get_Projection_Matrix(void);
  157. const Matrix3D & Get_View_Matrix(void);
  158. // Projecting and Un-Projecting a point
  159. ProjectionResType Project(Vector3 & dest,const Vector3 & ws_point) const;
  160. ProjectionResType Project_Camera_Space_Point(Vector3 & dest,const Vector3 & cam_point) const;
  161. void Un_Project(Vector3 & dest,const Vector2 & view_point) const;
  162. void Transform_To_View_Space(Vector3 & dest,const Vector3 & ws_point) const;
  163. void Rotate_To_View_Space(Vector3 & dest,const Vector3 & ws_vector) const;
  164. // Viewport control
  165. void Set_Viewport(const Vector2 & min,const Vector2 & max);
  166. void Get_Viewport(Vector2 & set_min,Vector2 & set_max) const;
  167. const ViewportClass & Get_Viewport(void) const;
  168. void Set_Depth_Range(float zstart = 0.0f,float zend = 1.0f);
  169. void Get_Depth_Range(float * set_zstart,float * set_zend) const;
  170. // Culling for various bounding volumes. These functions will return true if the
  171. // given primitive is culled (i.e. it is *outside* the view frustum)
  172. bool Cull_Sphere(const SphereClass & sphere) const;
  173. bool Cull_Sphere_On_Frustum_Sides(const SphereClass & sphere) const;
  174. bool Cull_Box(const AABoxClass & box) const;
  175. // Various properties of the camera's frustum: These funcitons return a
  176. // pointer to the internal storage of the descriptions. there will be
  177. // 6 frustum planes, 8 corner points, see the implementations of these
  178. // functions for definitions on which points/planes are associated with
  179. // each index. Better yet, just use the Frustum object.
  180. const FrustumClass & Get_Frustum(void) const;
  181. const PlaneClass * Get_Frustum_Planes(void) const;
  182. const Vector3 * Get_Frustum_Corners(void) const;
  183. const FrustumClass & Get_View_Space_Frustum(void) const;
  184. const PlaneClass * Get_View_Space_Frustum_Planes(void) const;
  185. const Vector3 * Get_View_Space_Frustum_Corners(void) const;
  186. const OBBoxClass & Get_Near_Clip_Bounding_Box(void) const;
  187. // Methods for transforming/projecting points between various coordinate systems
  188. // associated with this camera.
  189. // "Device Space" - pixel coordinate
  190. // "View Space" - 3D space where the view point is at 0,0,0 and the view plane is at z=-1.0
  191. // "World Space" - 3D world coordinate system.
  192. void Device_To_View_Space(const Vector2 & device_coord,Vector3 * view_coord);
  193. void Device_To_World_Space(const Vector2 & device_coord,Vector3 * world_coord);
  194. float Compute_Projected_Sphere_Radius(float dist,float radius);
  195. // apply this camera's settings into d3d.
  196. void Apply(void);
  197. // utility class to convert to old space of 0..1
  198. static void Convert_Old(Vector3 &pos);
  199. protected:
  200. void Update_Frustum(void) const;
  201. ProjectionType Projection; // projection type, orthographic or perspective
  202. ViewportClass Viewport; // pixel viewport to render into
  203. ViewportClass ViewPlane; // corners of a slice through the frustum at z=-1.0
  204. float AspectRatio; // aspect ratio of the camera, width / height
  205. float ZNear; // near clip plane distance
  206. float ZFar; // far clip plane distance
  207. float ZBufferMin; // smallest value we'll write into the z-buffer (usually 0.0)
  208. float ZBufferMax; // largest value we'll write into the z-buffer (usually 1.0)
  209. mutable bool FrustumValid;
  210. mutable FrustumClass Frustum; // world-space frustum and clip planes
  211. mutable FrustumClass ViewSpaceFrustum; // view-space frustum and clip planes
  212. mutable OBBoxClass NearClipBBox; // obbox which bounds the near clip plane
  213. mutable Matrix4 ProjectionTransform;
  214. mutable Matrix3D CameraInvTransform;
  215. };
  216. inline float CameraClass::Get_Depth(void) const
  217. {
  218. return ZFar;
  219. }
  220. inline void CameraClass::Set_Projection_Type(ProjectionType ptype)
  221. {
  222. FrustumValid = false;
  223. Projection = ptype;
  224. }
  225. inline CameraClass::ProjectionType CameraClass::Get_Projection_Type(void)
  226. {
  227. return Projection;
  228. }
  229. inline void CameraClass::Set_Viewport(const Vector2 & min,const Vector2 & max)
  230. {
  231. Viewport.Min = min; Viewport.Max = max;
  232. FrustumValid = false;
  233. }
  234. inline void CameraClass::Get_Viewport(Vector2 & set_min,Vector2 & set_max) const
  235. {
  236. set_min = Viewport.Min;
  237. set_max = Viewport.Max;
  238. }
  239. inline void CameraClass::Set_Depth_Range(float zmin,float zmax)
  240. {
  241. ZBufferMin = zmin;
  242. ZBufferMax = zmax;
  243. }
  244. inline void CameraClass::Get_Depth_Range(float * set_zmin,float * set_zmax) const
  245. {
  246. if (set_zmin != NULL) {
  247. *set_zmin = ZBufferMin;
  248. }
  249. if (set_zmax != NULL) {
  250. *set_zmax = ZBufferMax;
  251. }
  252. }
  253. inline const ViewportClass & CameraClass::Get_Viewport(void) const
  254. {
  255. return Viewport;
  256. }
  257. inline bool CameraClass::Cull_Sphere(const SphereClass & sphere) const
  258. {
  259. const FrustumClass & frustum = Get_Frustum();
  260. return CollisionMath::Overlap_Test(frustum,sphere) == CollisionMath::OUTSIDE;
  261. }
  262. inline bool CameraClass::Cull_Sphere_On_Frustum_Sides(const SphereClass & sphere) const
  263. {
  264. const FrustumClass & frustum = Get_Frustum();
  265. const PlaneClass * planes = frustum.Planes;
  266. bool is_visible = true;
  267. for (int i = 1; i < 5; i++) {
  268. is_visible = is_visible && (CollisionMath::Overlap_Test(planes[i],sphere) & (CollisionMath::INSIDE|CollisionMath::BOTH));
  269. }
  270. return !is_visible;
  271. }
  272. /***********************************************************************************************
  273. * CameraClass::Get_Frustum -- returns the frustum of the camera *
  274. * *
  275. * INPUT: *
  276. * *
  277. * OUTPUT: *
  278. * *
  279. * WARNINGS: *
  280. * *
  281. * HISTORY: *
  282. * 3/24/99 GTH : Created. *
  283. *=============================================================================================*/
  284. inline const FrustumClass &
  285. CameraClass::Get_Frustum(void) const
  286. {
  287. Update_Frustum();
  288. return Frustum;
  289. }
  290. /***********************************************************************************************
  291. * CameraClass::Get_Frustum_Planes -- returns pointer to the array of frustum planes *
  292. * *
  293. * INPUT: *
  294. * *
  295. * OUTPUT: *
  296. * *
  297. * WARNINGS: *
  298. * *
  299. * HISTORY: *
  300. * 5/29/98 GTH : Created. *
  301. *=============================================================================================*/
  302. inline const PlaneClass *
  303. CameraClass::Get_Frustum_Planes(void) const
  304. {
  305. const FrustumClass & frustum = Get_Frustum();
  306. return frustum.Planes;
  307. }
  308. /***********************************************************************************************
  309. * CameraClass::Get_Frustum_Corners -- returns pointer to the array of frustum corners *
  310. * *
  311. * The camera frustum corner FrustumCorners are defined in the following order *
  312. * The first four points lie on the near clipping plane: *
  313. * upper left 0, upper right 1, lower left 2, lower right 3. *
  314. * The last four points lie on the far clipping plane, numbered analogous fashion. *
  315. * upper left 4, upper right 5, lower left 6, lower right 7. *
  316. * (remember: the camera space has x going to the right, y up and z backwards). *
  317. * *
  318. * INPUT: *
  319. * *
  320. * OUTPUT: *
  321. * *
  322. * WARNINGS: *
  323. * *
  324. * HISTORY: *
  325. * 5/29/98 GTH : Created. *
  326. *=============================================================================================*/
  327. inline const Vector3 *
  328. CameraClass::Get_Frustum_Corners(void) const
  329. {
  330. const FrustumClass & frustum = Get_Frustum();
  331. return frustum.Corners;
  332. }
  333. /***********************************************************************************************
  334. * CameraClass::Get_View_Space_Frustum -- returns the view-space frustum for this camera *
  335. * *
  336. * INPUT: *
  337. * *
  338. * OUTPUT: *
  339. * *
  340. * WARNINGS: *
  341. * *
  342. * HISTORY: *
  343. * 5/16/2001 gth : Created. *
  344. *=============================================================================================*/
  345. inline const FrustumClass & CameraClass::Get_View_Space_Frustum(void) const
  346. {
  347. Update_Frustum();
  348. return ViewSpaceFrustum;
  349. }
  350. /***********************************************************************************************
  351. * CameraClass::Get_View_Space_Frustum_Planes -- returns the view space clip planes for this c *
  352. * *
  353. * INPUT: *
  354. * *
  355. * OUTPUT: *
  356. * *
  357. * WARNINGS: *
  358. * *
  359. * HISTORY: *
  360. * 5/16/2001 gth : Created. *
  361. *=============================================================================================*/
  362. inline const PlaneClass * CameraClass::Get_View_Space_Frustum_Planes(void) const
  363. {
  364. const FrustumClass & frustum = Get_View_Space_Frustum();
  365. return frustum.Planes;
  366. }
  367. /***********************************************************************************************
  368. * CameraClass::Get_View_Space_Frustum_Corners -- returns the corners of the view space frustu *
  369. * *
  370. * The camera frustum corner FrustumCorners are defined in the following order *
  371. * The first four points lie on the near clipping plane: *
  372. * upper left 0, upper right 1, lower left 2, lower right 3. *
  373. * The last four points lie on the far clipping plane, numbered analogous fashion. *
  374. * upper left 4, upper right 5, lower left 6, lower right 7. *
  375. * (remember: camera space has x going to the right, y up and z backwards). *
  376. * *
  377. * INPUT: *
  378. * *
  379. * OUTPUT: *
  380. * *
  381. * WARNINGS: *
  382. * *
  383. * HISTORY: *
  384. * 5/16/2001 gth : Created. *
  385. *=============================================================================================*/
  386. inline const Vector3 * CameraClass::Get_View_Space_Frustum_Corners(void) const
  387. {
  388. const FrustumClass & frustum = Get_View_Space_Frustum();
  389. return frustum.Corners;
  390. }
  391. #endif