1 | #ifndef __CAMERA_H
|
---|
2 | #define __CAMERA_H
|
---|
3 |
|
---|
4 | #include "Vector3.h"
|
---|
5 | #include "AxisAlignedBox3.h"
|
---|
6 | #include "Plane3.h"
|
---|
7 |
|
---|
8 |
|
---|
9 | namespace CHCDemoEngine
|
---|
10 | {
|
---|
11 |
|
---|
12 | class Matrix4x4;
|
---|
13 | class Polyhedron;
|
---|
14 |
|
---|
15 | /** Class representing a frustum.
|
---|
16 | */
|
---|
17 | class Frustum
|
---|
18 | {
|
---|
19 | friend class Camera;
|
---|
20 |
|
---|
21 | public:
|
---|
22 |
|
---|
23 | Frustum() {};
|
---|
24 |
|
---|
25 | Frustum(const Matrix4x4 &trafo);
|
---|
26 |
|
---|
27 | enum { LEFT_PLANE, RIGHT_PLANE, BOTTOM_PLANE, TOP_PLANE, NEAR_PLANE, FAR_PLANE};
|
---|
28 |
|
---|
29 | /** Resizes the current frustum so that it fully encloses the given polyhedron
|
---|
30 | */
|
---|
31 | void EnclosePolyhedron(const Polyhedron &polyhedron);
|
---|
32 |
|
---|
33 | void ExtractTransformation(Matrix4x4 &m) const;
|
---|
34 |
|
---|
35 |
|
---|
36 | /// the 6 clip planes
|
---|
37 | Plane3 mClipPlanes[6];
|
---|
38 | };
|
---|
39 |
|
---|
40 |
|
---|
41 | class Camera
|
---|
42 | {
|
---|
43 | friend class ShadowMap;
|
---|
44 | public:
|
---|
45 |
|
---|
46 | Camera();
|
---|
47 |
|
---|
48 | Camera(int width, int height, float fieldOfView = 90.f);
|
---|
49 |
|
---|
50 | /** Sets the current camera position.
|
---|
51 | */
|
---|
52 | void SetPosition(const Vector3 &pos);
|
---|
53 |
|
---|
54 | inline Vector3 GetPosition() const { return mPosition; }
|
---|
55 |
|
---|
56 | Vector3 GetDirection() const;
|
---|
57 | Vector3 GetUpVector() const;
|
---|
58 | Vector3 GetRightVector() const;
|
---|
59 |
|
---|
60 | Vector3 GetBaseDirection() const;
|
---|
61 | Vector3 GetBaseUpVector() const;
|
---|
62 | Vector3 GetBaseRightVector() const;
|
---|
63 |
|
---|
64 | inline float GetFov() const { return mFovy; }
|
---|
65 | inline float GetAspect() const { return (float) mWidth / mHeight; }
|
---|
66 | inline int GetWidth() const { return mWidth; }
|
---|
67 | inline int GetHeight() const { return mHeight; }
|
---|
68 |
|
---|
69 | /** Sets up viewing in gl
|
---|
70 | */
|
---|
71 | void SetupCameraView();
|
---|
72 | /** Returns the current projection matrix.
|
---|
73 | */
|
---|
74 | void GetProjectionMatrix(Matrix4x4 &mat);
|
---|
75 | /** Returns the current model view matrix.
|
---|
76 | */
|
---|
77 | void GetModelViewMatrix(Matrix4x4 &mat);
|
---|
78 | /** Calculates a frustum from the projection and the modelview matrix.
|
---|
79 | */
|
---|
80 | void CalcFrustum(Frustum &frustum);
|
---|
81 | /** Computes the extremal points of this frustum.
|
---|
82 | If farthestVisibleDistance is nearer than the far plane,
|
---|
83 | it is used to define the far plane instead.
|
---|
84 | */
|
---|
85 | void ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
|
---|
86 | Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
|
---|
87 | float farthestVisibleDistance = 1e25f) const;
|
---|
88 | /** Returns the near plane.
|
---|
89 | */
|
---|
90 | inline float GetNear() const { return mNear; }
|
---|
91 | /** Returns the far plane.
|
---|
92 | */
|
---|
93 | inline float GetFar() const { return mFar; }
|
---|
94 | /** Sets the near plane
|
---|
95 | */
|
---|
96 | void SetNear(float nearDist);
|
---|
97 | /** Sets the far plane.
|
---|
98 | */
|
---|
99 | void SetFar(float farDist);
|
---|
100 |
|
---|
101 | void SetOrtho(bool ortho);
|
---|
102 |
|
---|
103 | void Yaw(float angle);
|
---|
104 | void Pitch(float angle);
|
---|
105 |
|
---|
106 | float GetPitch() const { return mPitch; }
|
---|
107 | float GetYaw() const { return mYaw; }
|
---|
108 | void ResetPitchAndYaw() { mPitch = 0; mYaw = 0; }
|
---|
109 | /** Sets the camera direction.
|
---|
110 | */
|
---|
111 | void SetDirection(const Vector3 &direction);
|
---|
112 | /** Returns frustum as polyhedron.
|
---|
113 | */
|
---|
114 | Polyhedron *ComputeFrustum(float farthestVisibleDistance = 1e25f) const;
|
---|
115 |
|
---|
116 | protected:
|
---|
117 |
|
---|
118 | void Precompute(const Vector3 &direction);
|
---|
119 |
|
---|
120 | void CalculateFromPitchAndYaw();
|
---|
121 |
|
---|
122 | void ComputePointsInternal(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
|
---|
123 | Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
|
---|
124 | const Vector3 &view, const Vector3 &right, const Vector3 &up,
|
---|
125 | const Vector3 &pos,
|
---|
126 | float farthestVisibleDistance = 1e25f) const;
|
---|
127 |
|
---|
128 | ////////////////
|
---|
129 | //-- members
|
---|
130 |
|
---|
131 | float mFovy;
|
---|
132 | int mWidth;
|
---|
133 | int mHeight;
|
---|
134 |
|
---|
135 | float mNear;
|
---|
136 |
|
---|
137 | float mFar;
|
---|
138 |
|
---|
139 | bool mIsOrtho;
|
---|
140 |
|
---|
141 | Matrix4x4 mBaseOrientation;
|
---|
142 | Matrix4x4 mViewOrientation;
|
---|
143 |
|
---|
144 | float mPitch;
|
---|
145 | float mYaw;
|
---|
146 |
|
---|
147 | Vector3 mPosition;
|
---|
148 | };
|
---|
149 |
|
---|
150 | }
|
---|
151 | #endif
|
---|