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 | /** Classs representing a camera.
|
---|
42 | */
|
---|
43 | class Camera
|
---|
44 | {
|
---|
45 | friend class ShadowMap;
|
---|
46 |
|
---|
47 | public:
|
---|
48 | /** Default constructor.
|
---|
49 | */
|
---|
50 | Camera();
|
---|
51 | /** Camera taking the image width and height and the field of view.
|
---|
52 | */
|
---|
53 | Camera(float aspect, float fieldOfView = 90.f);
|
---|
54 | /** Sets the current camera position.
|
---|
55 | */
|
---|
56 | void SetPosition(const Vector3 &pos);
|
---|
57 | /** See set.
|
---|
58 | */
|
---|
59 | inline Vector3 GetPosition() const { return mPosition; }
|
---|
60 | /** Returns view direction.
|
---|
61 | */
|
---|
62 | Vector3 GetDirection() const;
|
---|
63 | /** Returns up vector.
|
---|
64 | */
|
---|
65 | Vector3 GetUpVector() const;
|
---|
66 | /** Returns right vector.
|
---|
67 | */
|
---|
68 | Vector3 GetRightVector() const;
|
---|
69 |
|
---|
70 | Vector3 GetBaseDirection() const;
|
---|
71 | Vector3 GetBaseUpVector() const;
|
---|
72 | Vector3 GetBaseRightVector() const;
|
---|
73 | /** Returns the field of view.
|
---|
74 | */
|
---|
75 | inline float GetFov() const { return mFOVy; }
|
---|
76 | /** Returns the aspect ratio.
|
---|
77 | */
|
---|
78 | inline float GetAspect() const { return mAspect; }
|
---|
79 | /** Sets up viewing matrices in for opengl rendering
|
---|
80 | */
|
---|
81 | void SetupCameraView();
|
---|
82 | /** Returns the current projection matrix.
|
---|
83 | */
|
---|
84 | void GetProjectionMatrix(Matrix4x4 &mat) const;
|
---|
85 | /** Returns the current model view matrix.
|
---|
86 | */
|
---|
87 | void GetModelViewMatrix(Matrix4x4 &mat) const;
|
---|
88 | /** Returns the view orientation (the model view matrix without the translation)
|
---|
89 | */
|
---|
90 | void GetViewOrientationMatrix(Matrix4x4 &mat) const;
|
---|
91 | /** Calculates a frustum from the projection and the modelview matrix.
|
---|
92 | */
|
---|
93 | void CalcFrustum(Frustum &frustum);
|
---|
94 | /** Computes the extremal points of this frustum.
|
---|
95 | If farthestVisibleDistance is nearer than the far plane,
|
---|
96 | it is used to define the far plane instead.
|
---|
97 | */
|
---|
98 | void ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
|
---|
99 | Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
|
---|
100 | float farthestVisibleDistance = 1e25f) const;
|
---|
101 | /** Returns the near plane.
|
---|
102 | */
|
---|
103 | inline float GetNear() const { return mNear; }
|
---|
104 | /** Returns the far plane.
|
---|
105 | */
|
---|
106 | inline float GetFar() const { return mFar; }
|
---|
107 | /** Sets the near plane
|
---|
108 | */
|
---|
109 | void SetNear(float nearDist);
|
---|
110 | /** Sets the far plane.
|
---|
111 | */
|
---|
112 | void SetFar(float farDist);
|
---|
113 | /** Sets this camera to orthographic projection.
|
---|
114 | */
|
---|
115 | //void SetOrtho(bool ortho);
|
---|
116 | /** Set yaw (rotation around vertical axis.
|
---|
117 | */
|
---|
118 | void Yaw(float angle);
|
---|
119 | /** Set pitch.
|
---|
120 | */
|
---|
121 | void Pitch(float angle);
|
---|
122 | /** Returns pitch.
|
---|
123 | */
|
---|
124 | float GetPitch() const { return mPitch; }
|
---|
125 | /** Returns yaw.
|
---|
126 | */
|
---|
127 | float GetYaw() const { return mYaw; }
|
---|
128 | /** Resets pitch and yaw.
|
---|
129 | */
|
---|
130 | void ResetPitchAndYaw() { mPitch = 0; mYaw = 0; }
|
---|
131 | /** Sets the view direction.
|
---|
132 | */
|
---|
133 | void SetDirection(const Vector3 &direction);
|
---|
134 | /** Returns frustum as polyhedron.
|
---|
135 | */
|
---|
136 | Polyhedron *ComputeFrustum(float farthestVisibleDistance = 1e25f) const;
|
---|
137 | /** Sets up projection matrix in OpenGl.
|
---|
138 | */
|
---|
139 | void SetupProjection();
|
---|
140 | /** Sets up view + projection matrix in OpenGl.
|
---|
141 | */
|
---|
142 | void SetupViewProjection();
|
---|
143 |
|
---|
144 |
|
---|
145 | protected:
|
---|
146 |
|
---|
147 | void Precompute(const Vector3 &direction);
|
---|
148 |
|
---|
149 | void CalculateFromPitchAndYaw();
|
---|
150 |
|
---|
151 | void ComputePointsInternal(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
|
---|
152 | Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
|
---|
153 | const Vector3 &view, const Vector3 &right, const Vector3 &up,
|
---|
154 | const Vector3 &pos,
|
---|
155 | float farthestVisibleDistance = 1e25f) const;
|
---|
156 |
|
---|
157 | ////////////////
|
---|
158 | //-- members
|
---|
159 |
|
---|
160 | float mFOVy;
|
---|
161 | float mNear;
|
---|
162 | float mFar;
|
---|
163 |
|
---|
164 | /// if this camera is orthgraphic or perspective
|
---|
165 | //bool mIsOrtho;
|
---|
166 |
|
---|
167 | Matrix4x4 mBaseOrientation;
|
---|
168 | Matrix4x4 mViewOrientation;
|
---|
169 |
|
---|
170 | float mPitch;
|
---|
171 | float mYaw;
|
---|
172 |
|
---|
173 | Vector3 mPosition;
|
---|
174 |
|
---|
175 | float mAspect;
|
---|
176 | };
|
---|
177 |
|
---|
178 | }
|
---|
179 | #endif
|
---|