source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h @ 3102

Revision 3102, 5.1 KB checked in by mattausch, 16 years ago (diff)

orthocam not working yet

Line 
1#ifndef __CAMERA_H
2#define __CAMERA_H
3
4#include "Vector3.h"
5#include "AxisAlignedBox3.h"
6#include "Plane3.h"
7
8
9namespace CHCDemoEngine
10{
11
12class Matrix4x4;
13class Polyhedron;
14
15
16/** Class representing a frustum.
17*/
18class Frustum
19{
20        friend class Camera;
21
22public:
23
24        Frustum() {};
25
26        Frustum(const Matrix4x4 &trafo);
27
28        enum { LEFT_PLANE, RIGHT_PLANE, BOTTOM_PLANE, TOP_PLANE, NEAR_PLANE, FAR_PLANE};
29
30        /** Resizes the current frustum so that it fully encloses the given polyhedron
31        */
32        void EnclosePolyhedron(const Polyhedron &polyhedron);
33
34        void ExtractTransformation(Matrix4x4 &m) const;
35
36
37        /// the 6 clip planes
38        Plane3 mClipPlanes[6];
39};
40
41
42/** Classs representing a camera.
43*/
44class Camera
45{
46public:
47        /** Default constructor.
48        */
49        Camera();
50        /** Sets the current camera position.
51        */
52        void SetPosition(const Vector3 &pos);
53        /** See set.
54        */
55        inline Vector3 GetPosition() const { return mPosition; }
56        /** Returns view direction.
57        */
58        Vector3 GetDirection() const;
59        /** Returns up vector.
60        */
61        Vector3 GetUpVector() const;
62        /** Returns right vector.
63        */
64        Vector3 GetRightVector() const;
65
66        Vector3 GetBaseDirection() const;
67        Vector3 GetBaseUpVector() const;
68        Vector3 GetBaseRightVector() const;
69        /** Sets up viewing matrices in for opengl rendering
70        */
71        void SetupCameraView();
72        /** Returns the current projection matrix.
73        */
74        void GetProjectionMatrix(Matrix4x4 &mat) const;
75        /** Returns the current model view matrix.
76        */
77        void GetModelViewMatrix(Matrix4x4 &mat) const;
78        /** Returns the view orientation (the model view matrix without the translation)
79        */
80        void GetViewOrientationMatrix(Matrix4x4 &mat) const;
81        /** Returns the near plane.
82        */
83        inline float GetNear() const { return mNear; }
84        /** Returns the far plane.
85        */
86        inline float GetFar() const { return mFar; }
87        /** Sets the near plane
88        */
89        void SetNear(float nearDist);
90        /** Sets the far plane.
91        */
92        void SetFar(float farDist);
93        /** Set yaw (rotation around vertical axis.
94        */
95        void Yaw(float angle);
96        /** Set pitch.
97        */
98        void Pitch(float angle);
99        /** Returns pitch.
100        */
101        float GetPitch() const { return mPitch; }
102        /** Returns yaw.
103        */
104        float GetYaw() const { return mYaw; }
105        /** Resets pitch and yaw.
106        */
107        void ResetPitchAndYaw() { mPitch = 0; mYaw = 0; }
108        /** Sets the view direction.
109        */
110        void SetDirection(const Vector3 &direction);
111        /** Sets up projection matrix in OpenGl.
112        */
113        void SetupProjection();
114        /** Sets up view + projection matrix in OpenGl.
115        */
116        void SetupViewProjection();
117        /** Calculates a frustum from the projection and the modelview matrix.
118        */
119        void CalcFrustum(Frustum &frustum);
120
121protected:
122
123        virtual void UpdateProjectionMatrix() = 0;
124
125        void Precompute(const Vector3 &direction);
126
127        void CalculateFromPitchAndYaw();
128
129
130        ////////////////
131        //-- members
132
133        float mNear;
134        float mFar;
135       
136        Matrix4x4 mBaseOrientation;
137        Matrix4x4 mViewOrientation;
138
139        Matrix4x4 mProjection;
140
141        float mPitch;
142        float mYaw;
143
144        Vector3 mPosition;
145};
146
147
148/** Classs representing a perspective camera.
149*/
150class PerspectiveCamera: public Camera
151{
152        friend class ShadowMap;
153
154public:
155        /** Default constructor.
156        */
157        PerspectiveCamera();
158        /** Camera taking the image width and height and the field of view.
159        */
160        PerspectiveCamera(float aspect, float fieldOfView = 90.f);
161        /** Returns the field of view.
162        */
163        inline float GetFov() const { return mFOVy; }
164        /** Returns the aspect ratio.
165        */
166        inline float GetAspect() const { return mAspect; }
167        /** Computes the extremal points of this frustum.
168                If farthestVisibleDistance is nearer than the far plane,
169                it is used to define the far plane instead.
170        */
171        void ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
172                               Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
173                                           float farthestVisibleDistance = 1e25f) const;
174        /** Returns frustum as polyhedron.
175        */
176        Polyhedron *ComputeFrustum(float farthestVisibleDistance = 1e25f) const;
177
178
179protected:
180        /** Calculates the current projection matrix.
181        */
182        virtual void UpdateProjectionMatrix();
183
184        void ComputePointsInternal(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
185                                                           Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
186                                                           const Vector3 &view, const Vector3 &right, const Vector3 &up,
187                                                           const Vector3 &pos,
188                                                           float farthestVisibleDistance = 1e25f) const;
189
190        ////////////////
191        //-- members
192
193        float mFOVy;
194        float mAspect;
195};
196
197/** Classs representing an orthographics camera.
198*/
199class OrthoCamera: public Camera
200{
201        friend class ShadowMap;
202
203public:
204        /** Default constructor.
205        */
206        OrthoCamera();
207        /** Camera taking the frustum left, right, bottom, top.
208        */
209        OrthoCamera(float l, float r, float b, float t);
210
211        /** Camera taking the frustum left, right, bottom, top, near, far.
212        */
213        OrthoCamera(float l, float r, float b, float t, float n, float f);
214
215
216protected:
217        /** Calculates the current projection matrix.
218        */
219        virtual void UpdateProjectionMatrix();
220
221
222        //////////
223        //-- members
224
225        float mLeft;
226        float mRight;
227        float mBottom;
228        float mTop;
229};
230}
231#endif
Note: See TracBrowser for help on using the repository browser.