1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef _CAMERA_H
4 #define _CAMERA_H
5 
6 #include "Rendering/GL/myGL.h"
7 #include "System/float3.h"
8 #include "System/Matrix44f.h"
9 
10 
11 class CCamera
12 {
13 public:
14 	enum {
15 		MOVE_STATE_FWD = 0, // forward
16 		MOVE_STATE_BCK = 1, // back
17 		MOVE_STATE_LFT = 2, // left
18 		MOVE_STATE_RGT = 3, // right
19 		MOVE_STATE_UP  = 4, // up
20 		MOVE_STATE_DWN = 5, // down
21 		MOVE_STATE_FST = 6, // fast
22 		MOVE_STATE_SLW = 7, // slow
23 	};
24 
25 	CCamera();
26 
27 	float3 CalcPixelDir(int x,int y) const;
28 	float3 CalcWindowCoordinates(const float3& objPos) const;
29 
30 	void CopyState(const CCamera*);
31 	// NOTE:
32 	//   only FreeController calls this, others just seem to manipulate
33 	//   azimuth (.x) and zenith (.y) angles for their own (redundant?)
34 	//   copy of Camera::forward (CameraController::dir)
35 	//
36 	//   <forward> is set by CameraHandler::UpdateCam via CamCon::GetDir
37 	//   <right> and <up> are derived from this, never from <rot> directly
UpdateForward(const float3 & fwd)38 	void UpdateForward(const float3& fwd) { forward = fwd; }
39 	void UpdateRightAndUp(bool terrainReflectionPass);
40 
41 	bool InView(const float3& p, float radius = 0) const;
42 	bool InView(const float3& mins, const float3& maxs) const;
43 	void Update(bool terrainReflectionPass = false);
44 
45 	void GetFrustumSides(float miny, float maxy, float scale, bool negSide = false);
46 	void GetFrustumSide(
47 		const float3& zdir,
48 		const float3& offset,
49 		float miny,
50 		float maxy,
51 		float scale,
52 		bool upwardDir,
53 		bool negSide);
ClearFrustumSides()54 	void ClearFrustumSides() {
55 
56 		posFrustumSides.clear();
57 		negFrustumSides.clear();
58 	}
59 	void ClipFrustumLines(bool left, const float zmin, const float zmax);
60 
GetViewMatrix()61 	const CMatrix44f& GetViewMatrix() const { return viewMatrix; }
GetViewMatrixInverse()62 	const CMatrix44f& GetViewMatrixInverse() const { return viewMatrixInverse; }
GetProjectionMatrix()63 	const CMatrix44f& GetProjectionMatrix() const { return projectionMatrix; }
GetProjectionMatrixInverse()64 	const CMatrix44f& GetProjectionMatrixInverse() const { return projectionMatrixInverse; }
GetViewProjectionMatrix()65 	const CMatrix44f& GetViewProjectionMatrix() const { return viewProjectionMatrix; }
GetViewProjectionMatrixInverse()66 	const CMatrix44f& GetViewProjectionMatrixInverse() const { return viewProjectionMatrixInverse; }
GetBillBoardMatrix()67 	const CMatrix44f& GetBillBoardMatrix() const { return billboardMatrix; }
68 
GetPos()69 	const float3& GetPos() const { return pos; }
GetFov()70 	float GetFov() const { return fov; }
GetHalfFov()71 	float GetHalfFov() const { return halfFov; }
GetTanHalfFov()72 	float GetTanHalfFov() const { return tanHalfFov; }
GetLPPScale()73 	float GetLPPScale() const { return lppScale; }
74 
75 	/// @param fov in degree
SetPos(const float3 & p)76 	void SetPos(const float3& p) { pos = p; }
77 	void SetFov(float fov);
78 
79 	float GetMoveDistance(float* time, float* speed, int idx) const;
80 	float3 GetMoveVectorFromState(bool fromKeyState) const;
81 
SetMovState(int idx,bool b)82 	void SetMovState(int idx, bool b) { movState[idx] = b; }
SetRotState(int idx,bool b)83 	void SetRotState(int idx, bool b) { rotState[idx] = b; }
GetMovState()84 	const bool* GetMovState() const { return movState; }
GetRotState()85 	const bool* GetRotState() const { return rotState; }
86 
87 public:
88 	float3 rot;        ///< warning is not always updated
89 
90 	float3 forward;    ///< local z-axis
91 	float3 right;      ///< local x-axis
92 	float3 up;         ///< local y-axis
93 
94 	float3 topFrustumSideDir;
95 	float3 botFrustumSideDir;
96 	float3 rgtFrustumSideDir;
97 	float3 lftFrustumSideDir;
98 
99 	// Lua-controlled parameters, camera-only (not cam2)
100 	float3 posOffset;
101 	float3 tiltOffset;
102 
103 	GLint viewport[4];
104 
105 	struct FrustumLine {
106 		float base;
107 		float dir;
108 		int sign;
109 		float minz;
110 		float maxz;
111 	};
112 
GetNegFrustumSides()113 	const std::vector<FrustumLine> GetNegFrustumSides() const {
114 
115 		return negFrustumSides;
116 	}
GetPosFrustumSides()117 	const std::vector<FrustumLine> GetPosFrustumSides() const {
118 
119 		return posFrustumSides;
120 	}
121 
122 private:
123 	void ComputeViewRange();
124 
125 	void myGluPerspective(float aspect, float zNear, float zFar);
126 	void myGluLookAt(const float3&, const float3&, const float3&);
127 
128 private:
129 	CMatrix44f projectionMatrix;
130 	CMatrix44f projectionMatrixInverse;
131 	CMatrix44f viewMatrix;
132 	CMatrix44f viewMatrixInverse;
133 	CMatrix44f viewProjectionMatrix;
134 	CMatrix44f viewProjectionMatrixInverse;
135 	CMatrix44f billboardMatrix;
136 
137 	std::vector<FrustumLine> posFrustumSides;
138 	std::vector<FrustumLine> negFrustumSides;
139 
140 	float3 pos;
141 	float fov;         ///< in degrees
142 	float halfFov;     ///< half the fov in radians
143 	float tanHalfFov;  ///< math::tan(halfFov)
144 	float lppScale;    ///< length-per-pixel scale
145 
146 	bool movState[8]; // fwd, back, left, right, up, down, fast, slow
147 	bool rotState[4]; // unused
148 };
149 
150 extern CCamera* camera;
151 extern CCamera* cam2;
152 
153 #endif // _CAMERA_H
154