camera.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. ** Command & Conquer Generals(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. // Setting the zbuffer range used during rendering. (Added to allow subdividing the z-buffer. -MW).
  143. void Set_Zbuffer_Range(float znear,float zfar) {ZBufferMin = znear;ZBufferMax=zfar;}
  144. void Get_Zbuffer_Range(float & znear,float & zfar) const {znear=ZBufferMin;zfar=ZBufferMax;}
  145. // Methods for setting the View Plane.
  146. // NOTE: View plane is always at a distance of 1.0 from the eye.
  147. void Set_View_Plane(const Vector2 & min,const Vector2 & max);
  148. void Set_View_Plane(float hfov,float vfov = -1);
  149. void Set_Aspect_Ratio(float width_to_height);
  150. // Methods for querying the View Plane settings.
  151. void Get_View_Plane(Vector2 & set_min,Vector2 & set_max) const;
  152. float Get_Horizontal_FOV(void) const;
  153. float Get_Vertical_FOV(void) const;
  154. float Get_Aspect_Ratio(void) const;
  155. // Access to the projection matrices for this camera
  156. void Get_Projection_Matrix(Matrix4 * set_tm);
  157. void Get_D3D_Projection_Matrix(Matrix4 * set_tm);
  158. void Get_View_Matrix(Matrix3D * set_tm);
  159. const Matrix4 & Get_Projection_Matrix(void);
  160. const Matrix3D & Get_View_Matrix(void);
  161. // Projecting and Un-Projecting a point
  162. ProjectionResType Project(Vector3 & dest,const Vector3 & ws_point) const;
  163. ProjectionResType Project_Camera_Space_Point(Vector3 & dest,const Vector3 & cam_point) const;
  164. void Un_Project(Vector3 & dest,const Vector2 & view_point) const;
  165. void Transform_To_View_Space(Vector3 & dest,const Vector3 & ws_point) const;
  166. void Rotate_To_View_Space(Vector3 & dest,const Vector3 & ws_vector) const;
  167. // Viewport control
  168. void Set_Viewport(const Vector2 & min,const Vector2 & max);
  169. void Get_Viewport(Vector2 & set_min,Vector2 & set_max) const;
  170. const ViewportClass & Get_Viewport(void) const;
  171. void Set_Depth_Range(float zstart = 0.0f,float zend = 1.0f);
  172. void Get_Depth_Range(float * set_zstart,float * set_zend) const;
  173. // Culling for various bounding volumes. These functions will return true if the
  174. // given primitive is culled (i.e. it is *outside* the view frustum)
  175. bool Cull_Sphere(const SphereClass & sphere) const;
  176. bool Cull_Sphere_On_Frustum_Sides(const SphereClass & sphere) const;
  177. bool Cull_Box(const AABoxClass & box) const;
  178. // Various properties of the camera's frustum: These funcitons return a
  179. // pointer to the internal storage of the descriptions. there will be
  180. // 6 frustum planes, 8 corner points, see the implementations of these
  181. // functions for definitions on which points/planes are associated with
  182. // each index. Better yet, just use the Frustum object.
  183. const FrustumClass & Get_Frustum(void) const;
  184. const PlaneClass * Get_Frustum_Planes(void) const;
  185. const Vector3 * Get_Frustum_Corners(void) const;
  186. const FrustumClass & Get_View_Space_Frustum(void) const;
  187. const PlaneClass * Get_View_Space_Frustum_Planes(void) const;
  188. const Vector3 * Get_View_Space_Frustum_Corners(void) const;
  189. const OBBoxClass & Get_Near_Clip_Bounding_Box(void) const;
  190. // Methods for transforming/projecting points between various coordinate systems
  191. // associated with this camera.
  192. // "Device Space" - pixel coordinate
  193. // "View Space" - 3D space where the view point is at 0,0,0 and the view plane is at z=-1.0
  194. // "World Space" - 3D world coordinate system.
  195. void Device_To_View_Space(const Vector2 & device_coord,Vector3 * view_coord);
  196. void Device_To_World_Space(const Vector2 & device_coord,Vector3 * world_coord);
  197. float Compute_Projected_Sphere_Radius(float dist,float radius);
  198. // apply this camera's settings into d3d.
  199. void Apply(void);
  200. // utility class to convert to old space of 0..1
  201. static void Convert_Old(Vector3 &pos);
  202. protected:
  203. void Update_Frustum(void) const;
  204. ProjectionType Projection; // projection type, orthographic or perspective
  205. ViewportClass Viewport; // pixel viewport to render into
  206. ViewportClass ViewPlane; // corners of a slice through the frustum at z=-1.0
  207. float AspectRatio; // aspect ratio of the camera, width / height
  208. float ZNear; // near clip plane distance
  209. float ZFar; // far clip plane distance
  210. float ZBufferMin; // smallest value we'll write into the z-buffer (usually 0.0)
  211. float ZBufferMax; // largest value we'll write into the z-buffer (usually 1.0)
  212. mutable bool FrustumValid;
  213. mutable FrustumClass Frustum; // world-space frustum and clip planes
  214. mutable FrustumClass ViewSpaceFrustum; // view-space frustum and clip planes
  215. mutable OBBoxClass NearClipBBox; // obbox which bounds the near clip plane
  216. mutable Matrix4 ProjectionTransform;
  217. mutable Matrix3D CameraInvTransform;
  218. };
  219. inline float CameraClass::Get_Depth(void) const
  220. {
  221. return ZFar;
  222. }
  223. inline void CameraClass::Set_Projection_Type(ProjectionType ptype)
  224. {
  225. FrustumValid = false;
  226. Projection = ptype;
  227. }
  228. inline CameraClass::ProjectionType CameraClass::Get_Projection_Type(void)
  229. {
  230. return Projection;
  231. }
  232. inline void CameraClass::Set_Viewport(const Vector2 & min,const Vector2 & max)
  233. {
  234. Viewport.Min = min; Viewport.Max = max;
  235. FrustumValid = false;
  236. }
  237. inline void CameraClass::Get_Viewport(Vector2 & set_min,Vector2 & set_max) const
  238. {
  239. set_min = Viewport.Min;
  240. set_max = Viewport.Max;
  241. }
  242. inline void CameraClass::Set_Depth_Range(float zmin,float zmax)
  243. {
  244. ZBufferMin = zmin;
  245. ZBufferMax = zmax;
  246. }
  247. inline void CameraClass::Get_Depth_Range(float * set_zmin,float * set_zmax) const
  248. {
  249. if (set_zmin != NULL) {
  250. *set_zmin = ZBufferMin;
  251. }
  252. if (set_zmax != NULL) {
  253. *set_zmax = ZBufferMax;
  254. }
  255. }
  256. inline const ViewportClass & CameraClass::Get_Viewport(void) const
  257. {
  258. return Viewport;
  259. }
  260. inline bool CameraClass::Cull_Sphere(const SphereClass & sphere) const
  261. {
  262. const FrustumClass & frustum = Get_Frustum();
  263. return CollisionMath::Overlap_Test(frustum,sphere) == CollisionMath::OUTSIDE;
  264. }
  265. inline bool CameraClass::Cull_Sphere_On_Frustum_Sides(const SphereClass & sphere) const
  266. {
  267. const FrustumClass & frustum = Get_Frustum();
  268. const PlaneClass * planes = frustum.Planes;
  269. bool is_visible = true;
  270. for (int i = 1; i < 5; i++) {
  271. is_visible = is_visible && (CollisionMath::Overlap_Test(planes[i],sphere) & (CollisionMath::INSIDE|CollisionMath::BOTH));
  272. }
  273. return !is_visible;
  274. }
  275. /***********************************************************************************************
  276. * CameraClass::Get_Frustum -- returns the frustum of the camera *
  277. * *
  278. * INPUT: *
  279. * *
  280. * OUTPUT: *
  281. * *
  282. * WARNINGS: *
  283. * *
  284. * HISTORY: *
  285. * 3/24/99 GTH : Created. *
  286. *=============================================================================================*/
  287. inline const FrustumClass &
  288. CameraClass::Get_Frustum(void) const
  289. {
  290. Update_Frustum();
  291. return Frustum;
  292. }
  293. /***********************************************************************************************
  294. * CameraClass::Get_Frustum_Planes -- returns pointer to the array of frustum planes *
  295. * *
  296. * INPUT: *
  297. * *
  298. * OUTPUT: *
  299. * *
  300. * WARNINGS: *
  301. * *
  302. * HISTORY: *
  303. * 5/29/98 GTH : Created. *
  304. *=============================================================================================*/
  305. inline const PlaneClass *
  306. CameraClass::Get_Frustum_Planes(void) const
  307. {
  308. const FrustumClass & frustum = Get_Frustum();
  309. return frustum.Planes;
  310. }
  311. /***********************************************************************************************
  312. * CameraClass::Get_Frustum_Corners -- returns pointer to the array of frustum corners *
  313. * *
  314. * The camera frustum corner FrustumCorners are defined in the following order *
  315. * The first four points lie on the near clipping plane: *
  316. * upper left 0, upper right 1, lower left 2, lower right 3. *
  317. * The last four points lie on the far clipping plane, numbered analogous fashion. *
  318. * upper left 4, upper right 5, lower left 6, lower right 7. *
  319. * (remember: the camera space has x going to the right, y up and z backwards). *
  320. * *
  321. * INPUT: *
  322. * *
  323. * OUTPUT: *
  324. * *
  325. * WARNINGS: *
  326. * *
  327. * HISTORY: *
  328. * 5/29/98 GTH : Created. *
  329. *=============================================================================================*/
  330. inline const Vector3 *
  331. CameraClass::Get_Frustum_Corners(void) const
  332. {
  333. const FrustumClass & frustum = Get_Frustum();
  334. return frustum.Corners;
  335. }
  336. /***********************************************************************************************
  337. * CameraClass::Get_View_Space_Frustum -- returns the view-space frustum for this camera *
  338. * *
  339. * INPUT: *
  340. * *
  341. * OUTPUT: *
  342. * *
  343. * WARNINGS: *
  344. * *
  345. * HISTORY: *
  346. * 5/16/2001 gth : Created. *
  347. *=============================================================================================*/
  348. inline const FrustumClass & CameraClass::Get_View_Space_Frustum(void) const
  349. {
  350. Update_Frustum();
  351. return ViewSpaceFrustum;
  352. }
  353. /***********************************************************************************************
  354. * CameraClass::Get_View_Space_Frustum_Planes -- returns the view space clip planes for this c *
  355. * *
  356. * INPUT: *
  357. * *
  358. * OUTPUT: *
  359. * *
  360. * WARNINGS: *
  361. * *
  362. * HISTORY: *
  363. * 5/16/2001 gth : Created. *
  364. *=============================================================================================*/
  365. inline const PlaneClass * CameraClass::Get_View_Space_Frustum_Planes(void) const
  366. {
  367. const FrustumClass & frustum = Get_View_Space_Frustum();
  368. return frustum.Planes;
  369. }
  370. /***********************************************************************************************
  371. * CameraClass::Get_View_Space_Frustum_Corners -- returns the corners of the view space frustu *
  372. * *
  373. * The camera frustum corner FrustumCorners are defined in the following order *
  374. * The first four points lie on the near clipping plane: *
  375. * upper left 0, upper right 1, lower left 2, lower right 3. *
  376. * The last four points lie on the far clipping plane, numbered analogous fashion. *
  377. * upper left 4, upper right 5, lower left 6, lower right 7. *
  378. * (remember: camera space has x going to the right, y up and z backwards). *
  379. * *
  380. * INPUT: *
  381. * *
  382. * OUTPUT: *
  383. * *
  384. * WARNINGS: *
  385. * *
  386. * HISTORY: *
  387. * 5/16/2001 gth : Created. *
  388. *=============================================================================================*/
  389. inline const Vector3 * CameraClass::Get_View_Space_Frustum_Corners(void) const
  390. {
  391. const FrustumClass & frustum = Get_View_Space_Frustum();
  392. return frustum.Corners;
  393. }
  394. #endif