#ifndef __CAMERA_H #define __CAMERA_H #include "Vector3.h" #include "AxisAlignedBox3.h" #include "Plane3.h" namespace CHCDemoEngine { class Matrix4x4; class Polyhedron; /** Class representing a frustum. */ class Frustum { friend class Camera; public: enum {LEFT_PLANE, RIGHT_PLANE, BOTTOM_PLANE, TOP_PLANE, NEAR_PLANE, FAR_PLANE}; Frustum() {}; Frustum(const Matrix4x4 &trafo); /** Resizes the current frustum so that it fully encloses the given polyhedron */ void EnclosePolyhedron(const Polyhedron &polyhedron); void ExtractTransformation(Matrix4x4 &m) const; /// the 6 clip planes Plane3 mClipPlanes[6]; }; /** Classs representing a camera. */ class Camera { public: /** Default constructor. */ Camera(); /** Sets the current camera position. */ void SetPosition(const Vector3 &pos); /** See set. */ inline Vector3 GetPosition() const { return mPosition; } /** Returns view direction. */ Vector3 GetDirection() const; /** Returns up vector. */ Vector3 GetUpVector() const; /** Returns right vector. */ Vector3 GetRightVector() const; Vector3 GetBaseDirection() const; Vector3 GetBaseUpVector() const; Vector3 GetBaseRightVector() const; /** Sets up viewing matrices in for opengl rendering */ void SetupCameraView(); /** Returns the current projection matrix. */ void GetProjectionMatrix(Matrix4x4 &mat) const; /** Returns the current model view matrix. */ void GetModelViewMatrix(Matrix4x4 &mat) const; /** Returns the view orientation (the model view matrix without the translation) */ void GetViewOrientationMatrix(Matrix4x4 &mat) const; /** Returns the near plane. */ inline float GetNear() const { return mNear; } /** Returns the far plane. */ inline float GetFar() const { return mFar; } /** Sets the near plane */ void SetNear(float nearDist); /** Sets the far plane. */ void SetFar(float farDist); /** Set yaw (rotation around vertical axis. */ void Yaw(float angle); /** Set pitch. */ void Pitch(float angle); /** Returns pitch. */ float GetPitch() const { return mPitch; } /** Returns yaw. */ float GetYaw() const { return mYaw; } /** Resets pitch and yaw. */ void ResetPitchAndYaw() { mPitch = 0; mYaw = 0; } /** Sets the view direction. */ void SetDirection(const Vector3 &direction); /** Sets up projection matrix in OpenGl. */ void SetupProjection(); /** Sets up view + projection matrix in OpenGl. */ void SetupViewProjection(); /** Calculates a frustum from the projection and the modelview matrix. */ void CalcFrustum(Frustum &frustum); protected: virtual void UpdateProjectionMatrix() = 0; void Precompute(const Vector3 &direction); void CalculateFromPitchAndYaw(); //////////////// //-- members float mNear; float mFar; Matrix4x4 mBaseOrientation; Matrix4x4 mViewOrientation; Matrix4x4 mProjection; float mPitch; float mYaw; Vector3 mPosition; }; /** Classs representing a perspective camera. */ class PerspectiveCamera: public Camera { friend class ShadowMap; public: /** Default constructor. */ PerspectiveCamera(); /** Camera taking the image width and height and the field of view. */ PerspectiveCamera(float aspect, float fieldOfView = 90.f); /** Returns the field of view. */ inline float GetFov() const { return mFOVy; } /** Returns the aspect ratio. */ inline float GetAspect() const { return mAspect; } /** Computes the extremal points of this frustum. If farthestVisibleDistance is nearer than the far plane, it is used to define the far plane instead. */ void ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr, float farthestVisibleDistance = 1e25f) const; /** Returns frustum as polyhedron. */ Polyhedron *ComputeFrustum(float farthestVisibleDistance = 1e25f) const; protected: /** Calculates the current projection matrix. */ virtual void UpdateProjectionMatrix(); void ComputePointsInternal(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr, const Vector3 &view, const Vector3 &right, const Vector3 &up, const Vector3 &pos, float farthestVisibleDistance = 1e25f) const; //////////////// //-- members float mFOVy; float mAspect; }; /** Classs representing an orthographics camera. */ class OrthoCamera: public Camera { friend class ShadowMap; public: /** Default constructor. */ OrthoCamera(); /** Camera taking the frustum left, right, bottom, top. */ OrthoCamera(float l, float r, float b, float t); /** Camera taking the frustum left, right, bottom, top, near, far. */ OrthoCamera(float l, float r, float b, float t, float n, float f); protected: /** Calculates the current projection matrix. */ virtual void UpdateProjectionMatrix(); ////////// //-- members float mLeft; float mRight; float mBottom; float mTop; }; } #endif