Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
camera.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(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/***********************************************************************************************
20 *** 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 ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WW3D *
24 * *
25 * $Archive:: /Commando/Code/ww3d2/camera.h $*
26 * *
27 * Org Author:: Greg_h *
28 * *
29 * $Author:: Kenny Mitchell *
30 * *
31 * $Modtime:: 06/26/02 4:04p $*
32 * *
33 * $Revision:: 14 $*
34 * *
35 * 06/26/02 KM Matrix name change to avoid MAX conflicts *
36 *---------------------------------------------------------------------------------------------*
37 * Functions: *
38 * CameraClass::Get_Frustum -- returns the frustum of the camera *
39 * CameraClass::Get_Frustum_Planes -- returns pointer to the array of frustum planes *
40 * CameraClass::Get_Frustum_Corners -- returns pointer to the array of frustum corners *
41 * CameraClass::Get_View_Space_Frustum -- returns the view-space frustum for this camera *
42 * CameraClass::Get_View_Space_Frustum_Planes -- returns the view space clip planes for this *
43 * CameraClass::Get_View_Space_Frustum_Corners -- returns the corners of the view space frus *
44 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
45
46#if defined(_MSC_VER)
47#pragma once
48#endif
49
50#ifndef CAMERA_H
51#define CAMERA_H
52
53#include "always.h"
54#include "rendobj.h"
55#include "plane.h"
56#include "frustum.h"
57#include "obbox.h"
58#include "vector2.h"
59#include "matrix4.h"
60#include "colmath.h"
61
62class RenderInfoClass;
63
71{
72public:
73 ViewportClass(void) : Min(0,0), Max(1,1) { }
74 ViewportClass(const Vector2 & min,const Vector2 & max) : Min(min), Max(max) { }
75 ViewportClass(const ViewportClass & vp) : Min(vp.Min), Max(vp.Max) { }
76
77 float Width(void) const { return Max.X - Min.X; }
78 float Height(void) const { return Max.Y - Min.Y; }
79
82};
83
84
108{
109public:
110
116
124
125 CameraClass(void);
126 CameraClass(const CameraClass & src);
128 virtual ~CameraClass(void);
129 virtual RenderObjClass * Clone(void) const;
130 virtual int Class_ID(void) const { return CLASSID_CAMERA; }
131
133 // Render Object Interface - Rendering, cameras don't "render"
135 virtual void Render(RenderInfoClass & rinfo) { }
136
138 // Render Object Interface - "Scene Graph"
139 // Cameras cache their frustum description, this is invalidated whenever
140 // the transform/position is changed
142 virtual void Set_Transform(const Matrix3D &m);
143 virtual void Set_Position(const Vector3 &v);
144
146 // Render Object Interface - Bounding Volumes
148 virtual void Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const;
149 virtual void Get_Obj_Space_Bounding_Box(AABoxClass & box) const;
150
152 // Camera parameter control
154 // Depth of the scene.
155 float Get_Depth(void) const;
156
157 // Setting the projection type
160
161 // Setting the clipping ranges in world space distances
162 void Set_Clip_Planes(float znear,float zfar);
163 void Get_Clip_Planes(float & znear,float & zfar) const;
164
165 // Setting the zbuffer range used during rendering. (Added to allow subdividing the z-buffer. -MW).
166 void Set_Zbuffer_Range(float znear,float zfar) {ZBufferMin = znear;ZBufferMax=zfar;}
167 void Get_Zbuffer_Range(float & znear,float & zfar) const {znear=ZBufferMin;zfar=ZBufferMax;}
168
169 // Methods for setting the View Plane.
170 // NOTE: View plane is always at a distance of 1.0 from the eye.
171 void Set_View_Plane(const Vector2 & min,const Vector2 & max);
172 void Set_View_Plane(float hfov,float vfov = -1);
173 void Set_Aspect_Ratio(float width_to_height);
174
175 // Methods for querying the View Plane settings.
176 void Get_View_Plane(Vector2 & set_min,Vector2 & set_max) const;
177 float Get_Horizontal_FOV(void) const;
178 float Get_Vertical_FOV(void) const;
179 float Get_Aspect_Ratio(void) const;
180
181 // Access to the projection matrices for this camera
182 void Get_Projection_Matrix(Matrix4x4 * set_tm);
184 void Get_View_Matrix(Matrix3D * set_tm);
185 const Matrix4x4 & Get_Projection_Matrix(void);
186 const Matrix3D & Get_View_Matrix(void);
187
188 // Projecting and Un-Projecting a point
189 ProjectionResType Project(Vector3 & dest,const Vector3 & ws_point) const;
190 ProjectionResType Project_Camera_Space_Point(Vector3 & dest,const Vector3 & cam_point) const;
191 void Un_Project(Vector3 & dest,const Vector2 & view_point) const;
192 void Transform_To_View_Space(Vector3 & dest,const Vector3 & ws_point) const;
193 void Rotate_To_View_Space(Vector3 & dest,const Vector3 & ws_vector) const;
194
195 // Viewport control
196 void Set_Viewport(const Vector2 & min,const Vector2 & max);
197 void Get_Viewport(Vector2 & set_min,Vector2 & set_max) const;
198 const ViewportClass & Get_Viewport(void) const;
199
200 void Set_Depth_Range(float zstart = 0.0f,float zend = 1.0f);
201 void Get_Depth_Range(float * set_zstart,float * set_zend) const;
202
203 // Culling for various bounding volumes. These functions will return true if the
204 // given primitive is culled (i.e. it is *outside* the view frustum)
205 bool Cull_Sphere(const SphereClass & sphere) const;
206 bool Cull_Sphere_On_Frustum_Sides(const SphereClass & sphere) const;
207 bool Cull_Box(const AABoxClass & box) const;
208
209 // Various properties of the camera's frustum: These funcitons return a
210 // pointer to the internal storage of the descriptions. there will be
211 // 6 frustum planes, 8 corner points, see the implementations of these
212 // functions for definitions on which points/planes are associated with
213 // each index. Better yet, just use the Frustum object.
214 const FrustumClass & Get_Frustum(void) const;
215 const PlaneClass * Get_Frustum_Planes(void) const;
216 const Vector3 * Get_Frustum_Corners(void) const;
217 const FrustumClass & Get_View_Space_Frustum(void) const;
218 const PlaneClass * Get_View_Space_Frustum_Planes(void) const;
219 const Vector3 * Get_View_Space_Frustum_Corners(void) const;
220 const OBBoxClass & Get_Near_Clip_Bounding_Box(void) const;
221
222 // Methods for transforming/projecting points between various coordinate systems
223 // associated with this camera.
224 // "Device Space" - pixel coordinate
225 // "View Space" - 3D space where the view point is at 0,0,0 and the view plane is at z=-1.0
226 // "World Space" - 3D world coordinate system.
227 void Device_To_View_Space(const Vector2 & device_coord,Vector3 * view_coord);
228 void Device_To_World_Space(const Vector2 & device_coord,Vector3 * world_coord);
229 float Compute_Projected_Sphere_Radius(float dist,float radius);
230
231 // apply this camera's settings into d3d.
232 void Apply(void);
233
234 // utility class to convert to old space of 0..1
235 static void Convert_Old(Vector3 &pos);
236
237protected:
238
239 void Update_Frustum(void) const;
240
241 ProjectionType Projection; // projection type, orthographic or perspective
242 ViewportClass Viewport; // pixel viewport to render into
243 ViewportClass ViewPlane; // corners of a slice through the frustum at z=-1.0
244 float AspectRatio; // aspect ratio of the camera, width / height
245 float ZNear; // near clip plane distance
246 float ZFar; // far clip plane distance
247 float ZBufferMin; // smallest value we'll write into the z-buffer (usually 0.0)
248 float ZBufferMax; // largest value we'll write into the z-buffer (usually 1.0)
249
250 mutable bool FrustumValid;
251 mutable FrustumClass Frustum; // world-space frustum and clip planes
252 mutable FrustumClass ViewSpaceFrustum; // view-space frustum and clip planes
253 mutable OBBoxClass NearClipBBox; // obbox which bounds the near clip plane
256};
257
258
259inline float CameraClass::Get_Depth(void) const
260{
261 return ZFar;
262}
263
265{
266 FrustumValid = false;
267 Projection = ptype;
268}
269
274
275inline void CameraClass::Set_Viewport(const Vector2 & min,const Vector2 & max)
276{
277 Viewport.Min = min; Viewport.Max = max;
278 FrustumValid = false;
279}
280
281inline void CameraClass::Get_Viewport(Vector2 & set_min,Vector2 & set_max) const
282{
283 set_min = Viewport.Min;
284 set_max = Viewport.Max;
285}
286
287inline void CameraClass::Set_Depth_Range(float zmin,float zmax)
288{
289 ZBufferMin = zmin;
290 ZBufferMax = zmax;
291}
292
293inline void CameraClass::Get_Depth_Range(float * set_zmin,float * set_zmax) const
294{
295 if (set_zmin != NULL) {
296 *set_zmin = ZBufferMin;
297 }
298 if (set_zmax != NULL) {
299 *set_zmax = ZBufferMax;
300 }
301}
302
303inline const ViewportClass & CameraClass::Get_Viewport(void) const
304{
305 return Viewport;
306}
307
308inline bool CameraClass::Cull_Sphere(const SphereClass & sphere) const
309{
310 const FrustumClass & frustum = Get_Frustum();
311 return CollisionMath::Overlap_Test(frustum,sphere) == CollisionMath::OUTSIDE;
312}
313
315{
316 const FrustumClass & frustum = Get_Frustum();
317 const PlaneClass * planes = frustum.Planes;
318 bool is_visible = true;
319 for (int i = 1; i < 5; i++) {
320 is_visible = is_visible && (CollisionMath::Overlap_Test(planes[i],sphere) & (CollisionMath::INSIDE|CollisionMath::BOTH));
321 }
322 return !is_visible;
323}
324
325
326/***********************************************************************************************
327 * CameraClass::Get_Frustum -- returns the frustum of the camera *
328 * *
329 * INPUT: *
330 * *
331 * OUTPUT: *
332 * *
333 * WARNINGS: *
334 * *
335 * HISTORY: *
336 * 3/24/99 GTH : Created. *
337 *=============================================================================================*/
338inline const FrustumClass &
340{
342 return Frustum;
343}
344
345/***********************************************************************************************
346 * CameraClass::Get_Frustum_Planes -- returns pointer to the array of frustum planes *
347 * *
348 * INPUT: *
349 * *
350 * OUTPUT: *
351 * *
352 * WARNINGS: *
353 * *
354 * HISTORY: *
355 * 5/29/98 GTH : Created. *
356 *=============================================================================================*/
357inline const PlaneClass *
359{
360 const FrustumClass & frustum = Get_Frustum();
361 return frustum.Planes;
362}
363
364
365/***********************************************************************************************
366 * CameraClass::Get_Frustum_Corners -- returns pointer to the array of frustum corners *
367 * *
368 * The camera frustum corner FrustumCorners are defined in the following order *
369 * The first four points lie on the near clipping plane: *
370 * upper left 0, upper right 1, lower left 2, lower right 3. *
371 * The last four points lie on the far clipping plane, numbered analogous fashion. *
372 * upper left 4, upper right 5, lower left 6, lower right 7. *
373 * (remember: the camera space has x going to the right, y up and z backwards). *
374 * *
375 * INPUT: *
376 * *
377 * OUTPUT: *
378 * *
379 * WARNINGS: *
380 * *
381 * HISTORY: *
382 * 5/29/98 GTH : Created. *
383 *=============================================================================================*/
384inline const Vector3 *
386{
387 const FrustumClass & frustum = Get_Frustum();
388 return frustum.Corners;
389}
390
391
392/***********************************************************************************************
393 * CameraClass::Get_View_Space_Frustum -- returns the view-space frustum for this camera *
394 * *
395 * INPUT: *
396 * *
397 * OUTPUT: *
398 * *
399 * WARNINGS: *
400 * *
401 * HISTORY: *
402 * 5/16/2001 gth : Created. *
403 *=============================================================================================*/
405{
407 return ViewSpaceFrustum;
408}
409
410
411/***********************************************************************************************
412 * CameraClass::Get_View_Space_Frustum_Planes -- returns the view space clip planes for this c *
413 * *
414 * INPUT: *
415 * *
416 * OUTPUT: *
417 * *
418 * WARNINGS: *
419 * *
420 * HISTORY: *
421 * 5/16/2001 gth : Created. *
422 *=============================================================================================*/
424{
425 const FrustumClass & frustum = Get_View_Space_Frustum();
426 return frustum.Planes;
427}
428
429
430/***********************************************************************************************
431 * CameraClass::Get_View_Space_Frustum_Corners -- returns the corners of the view space frustu *
432 * *
433 * The camera frustum corner FrustumCorners are defined in the following order *
434 * The first four points lie on the near clipping plane: *
435 * upper left 0, upper right 1, lower left 2, lower right 3. *
436 * The last four points lie on the far clipping plane, numbered analogous fashion. *
437 * upper left 4, upper right 5, lower left 6, lower right 7. *
438 * (remember: camera space has x going to the right, y up and z backwards). *
439 * *
440 * INPUT: *
441 * *
442 * OUTPUT: *
443 * *
444 * WARNINGS: *
445 * *
446 * HISTORY: *
447 * 5/16/2001 gth : Created. *
448 *=============================================================================================*/
450{
451 const FrustumClass & frustum = Get_View_Space_Frustum();
452 return frustum.Corners;
453}
454
455
456#endif
#define NULL
Definition BaseType.h:92
#define min(x, y)
Definition BaseType.h:101
#define max(x, y)
Definition BaseType.h:105
virtual ~CameraClass(void)
Definition camera.cpp:189
ProjectionType Get_Projection_Type(void)
Definition camera.h:270
const ViewportClass & Get_Viewport(void) const
Definition camera.h:303
virtual void Set_Position(const Vector3 &v)
Definition camera.cpp:285
const Matrix3D & Get_View_Matrix(void)
Definition camera.cpp:814
float Compute_Projected_Sphere_Radius(float dist, float radius)
Definition camera.cpp:826
void Apply(void)
Definition camera.cpp:719
void Set_Depth_Range(float zstart=0.0f, float zend=1.0f)
Definition camera.h:287
void Device_To_World_Space(const Vector2 &device_coord, Vector3 *world_coord)
Definition camera.cpp:699
@ PERSPECTIVE
Definition camera.h:113
void Set_Aspect_Ratio(float width_to_height)
Definition camera.cpp:359
virtual void Get_Obj_Space_Bounding_Box(AABoxClass &box) const
Definition camera.cpp:243
void Set_Clip_Planes(float znear, float zfar)
Definition camera.cpp:742
float ZNear
Definition camera.h:245
void Get_View_Plane(Vector2 &set_min, Vector2 &set_max) const
Definition camera.cpp:380
ProjectionResType Project(Vector3 &dest, const Vector3 &ws_point) const
Definition camera.cpp:403
float ZBufferMin
Definition camera.h:247
Matrix4x4 ProjectionTransform
Definition camera.h:254
void Rotate_To_View_Space(Vector3 &dest, const Vector3 &ws_vector) const
Definition camera.cpp:541
virtual RenderObjClass * Clone(void) const
Definition camera.cpp:206
CameraClass(void)
Definition camera.cpp:92
bool Cull_Box(const AABoxClass &box) const
Definition camera.cpp:580
CameraClass & operator=(const CameraClass &)
Definition camera.cpp:153
ViewportClass ViewPlane
Definition camera.h:243
void Set_Viewport(const Vector2 &min, const Vector2 &max)
Definition camera.h:275
const OBBoxClass & Get_Near_Clip_Bounding_Box(void) const
Definition camera.cpp:561
void Get_Zbuffer_Range(float &znear, float &zfar) const
Definition camera.h:167
virtual void Get_Obj_Space_Bounding_Sphere(SphereClass &sphere) const
Definition camera.cpp:224
ViewportClass Viewport
Definition camera.h:242
const PlaneClass * Get_Frustum_Planes(void) const
Definition camera.h:358
ProjectionResType
Definition camera.h:118
@ OUTSIDE_FRUSTUM
Definition camera.h:120
@ OUTSIDE_FAR_CLIP
Definition camera.h:122
@ OUTSIDE_NEAR_CLIP
Definition camera.h:121
@ INSIDE_FRUSTUM
Definition camera.h:119
void Set_View_Plane(const Vector2 &min, const Vector2 &max)
Definition camera.cpp:306
void Set_Projection_Type(ProjectionType ptype)
Definition camera.h:264
void Un_Project(Vector3 &dest, const Vector2 &view_point) const
Definition camera.cpp:492
bool FrustumValid
Definition camera.h:250
bool Cull_Sphere(const SphereClass &sphere) const
Definition camera.h:308
float Get_Horizontal_FOV(void) const
Definition camera.cpp:755
void Get_Clip_Planes(float &znear, float &zfar) const
Definition camera.cpp:749
OBBoxClass NearClipBBox
Definition camera.h:253
void Get_Depth_Range(float *set_zstart, float *set_zend) const
Definition camera.h:293
float ZBufferMax
Definition camera.h:248
FrustumClass Frustum
Definition camera.h:251
const FrustumClass & Get_View_Space_Frustum(void) const
Definition camera.h:404
virtual void Set_Transform(const Matrix3D &m)
Definition camera.cpp:264
void Set_Zbuffer_Range(float znear, float zfar)
Definition camera.h:166
bool Cull_Sphere_On_Frustum_Sides(const SphereClass &sphere) const
Definition camera.h:314
Matrix3D CameraInvTransform
Definition camera.h:255
static void Convert_Old(Vector3 &pos)
Definition camera.cpp:820
const FrustumClass & Get_Frustum(void) const
Definition camera.h:339
void Transform_To_View_Space(Vector3 &dest, const Vector3 &ws_point) const
Definition camera.cpp:522
void Device_To_View_Space(const Vector2 &device_coord, Vector3 *view_coord)
Definition camera.cpp:661
const Matrix4x4 & Get_Projection_Matrix(void)
Definition camera.cpp:808
const Vector3 * Get_View_Space_Frustum_Corners(void) const
Definition camera.h:449
void Get_D3D_Projection_Matrix(Matrix4x4 *set_tm)
Definition camera.cpp:780
float AspectRatio
Definition camera.h:244
float Get_Depth(void) const
Definition camera.h:259
ProjectionType Projection
Definition camera.h:241
FrustumClass ViewSpaceFrustum
Definition camera.h:252
float ZFar
Definition camera.h:246
void Update_Frustum(void) const
Definition camera.cpp:599
virtual int Class_ID(void) const
Definition camera.h:130
const Vector3 * Get_Frustum_Corners(void) const
Definition camera.h:385
const PlaneClass * Get_View_Space_Frustum_Planes(void) const
Definition camera.h:423
float Get_Aspect_Ratio(void) const
Definition camera.cpp:767
ProjectionResType Project_Camera_Space_Point(Vector3 &dest, const Vector3 &cam_point) const
Definition camera.cpp:449
float Get_Vertical_FOV(void) const
Definition camera.cpp:761
virtual void Render(RenderInfoClass &rinfo)
Definition camera.h:135
static OverlapType Overlap_Test(const AAPlaneClass &plane, const Vector3 &point)
Vector3 Corners[8]
Definition frustum.h:65
PlaneClass Planes[6]
Definition frustum.h:64
RenderObjClass(void)
Definition rendobj.cpp:170
Vector2 Min
Definition camera.h:80
Vector2 Max
Definition camera.h:81
float Height(void) const
Definition camera.h:78
ViewportClass(void)
Definition camera.h:73
float Width(void) const
Definition camera.h:77
ViewportClass(const ViewportClass &vp)
Definition camera.h:75
ViewportClass(const Vector2 &min, const Vector2 &max)
Definition camera.h:74