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