Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Camera.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2015-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "Camera.h"
22 
23 #include <assert.h>
24 #include "util/MathUtil.h"
25 #include "platform/Environment.h"
26 
27 using namespace HFCore;
28 
29 #include "debug/CrtDbgNew.h"
30 
32 {
33  reset();
34 }
35 
37 {
38 }
39 
41 {
42  // TODO:
43 }
44 
46 {
47  if (mResizeCamera)
48  {
49  resize(HFCORE_ENV->getScreenWidth(),
50  HFCORE_ENV->getScreenHeight());
51  }
52 
53  // set camera lookAt:
54  g->setLookAt(mEyePos, mEyePos + mLookAtPos, mUpDirection);
55 
56  // handle rotation of Camera:
57  g->rotateZDeg(mRotationZDeg);
58 }
59 
60 void Camera::resize(int32_hf screenWidth, int32_hf screenHeight)
61 {
62  assert(screenWidth > 0 && screenHeight > 0);
63 
64  Graphics *g = HFCORE_ENV->getGraphics();
65  assert(g != NULL);
66 
67  // update viewport:
68  g->setViewport(0, 0, screenWidth, screenHeight);
69 
70  // setup perspective projection:
71  fp32_hf aspect = (fp32_hf)screenWidth / (fp32_hf)screenHeight;
72  g->setPerspectiveProjection(mFieldOfViewYDeg,
73  aspect,
74  mZNear,
75  mZFar);
76 
77  // reset flag:
78  mResizeCamera = false;
79 }
80 
82 {
83  mResizeCamera = true;
84  mFieldOfViewYDeg = 90.0f;
85  mEyePos = glm::vec3(0.0f, 0.0f, 10.0f);
86  mLookAtPos = glm::vec3(0.0f, 0.0f, -10.0f);
87  mUpDirection = glm::vec3(0.0f, 1.0f, 0.0f);
88  mZNear = 0.001f;
89  mZFar = 200.0f;
90  mRotationZRad = 0.0f;
91  mRotationZDeg = 0.0f;
92 }
93 
95 {
96  fp32_hf sideAngle = MathUtil::deg2rad(mFieldOfViewYDeg / 2.0f);
97  return tan(sideAngle) * fabs(mEyePos.z);
98 }
99 
101 {
102  glm::vec2 retval = MathUtil::mapScreenToWorld((fp32_hf)HFCORE_ENV->getScreenWidth(), 0);
103  return retval;
104 }
105 
107 {
108  glm::vec2 retval = MathUtil::mapScreenToWorld(0, (fp32_hf)HFCORE_ENV->getScreenHeight());
109  return retval;
110 }
111 
113 {
114  fp32_hf worldWidth = 0.0f;
115  fp32_hf worldHeight = 0.0f;
116  fp32_hf screenWidth = (fp32_hf)HFCORE_ENV->getScreenWidth();
117  fp32_hf screenHeight = (fp32_hf)HFCORE_ENV->getScreenHeight();
118  float aspect = screenWidth / screenHeight;
119 
120  if (screenWidth > screenHeight)
121  {
122  // window is wider than it is tall; radius goes with height
123  worldHeight = getViewRadius() * 2.0f;
124  worldWidth = worldHeight * aspect;
125  }
126  else
127  {
128  // window is taller than it is wide; radius goes with width
129  worldWidth = getViewRadius() * 2.0f;
130  worldHeight = worldWidth / aspect;
131  }
132 
133  return glm::vec2(worldWidth, worldHeight);
134 }
135 
137 {
138  mEyePos = glm::vec3(x, y, mEyePos.z);
139 }
140 
142 {
143  mEyePos = glm::vec3(x, y, z);
144 }
145 
146 void Camera::setPosition(const glm::vec2 &pos)
147 {
148  mEyePos = glm::vec3(pos.x, pos.y, mEyePos.z);
149 }
150 
151 void Camera::setPosition(const glm::vec3 &pos)
152 {
153  mEyePos = pos;
154 }
155 
157 {
158  if (x != NULL)
159  *x = mEyePos.x;
160  if (y != NULL)
161  *y = mEyePos.y;
162 }
163 
165 {
166  if (x != NULL)
167  *x = mEyePos.x;
168  if (y != NULL)
169  *y = mEyePos.y;
170  if (z != NULL)
171  *z = mEyePos.z;
172 }
173 
175 {
176  return glm::vec2(mEyePos.x, mEyePos.y);
177 }
178 
180 {
181  return mEyePos;
182 }
183 
185 {
186  mRotationZRad = angle;
187  mRotationZDeg = MathUtil::rad2deg(angle);
188 }
189 
191 {
192  mRotationZDeg = angle;
193  mRotationZRad = MathUtil::deg2rad(angle);
194 }
195 
197 {
198  return mRotationZRad;
199 }
200 
202 {
203  return mRotationZDeg;
204 }
205 
207 {
208  mEyePos.z = z;
209 }
210 
212 {
213  return mEyePos.z;
214 }
215 
217 {
218  fp32_hf sideAngle = MathUtil::deg2rad(mFieldOfViewYDeg / 2.0f);
219  return radius / tan(sideAngle);
220 }
221 
223 {
224  mEyePos.z = getZForViewRadius(radius);
225 }
226 
228 {
229  return mZNear;
230 }
231 
233 {
234  return mZFar;
235 }
236 
238 {
239  mZNear = zNear;
240  mResizeCamera = true;
241 }
242 
244 {
245  mZFar = zFar;
246  mResizeCamera = true;
247 }
248 
250 {
251  mLookAtPos = glm::vec3(x, y, z);
252 }
253 
254 void Camera::setViewCenter(const glm::vec3 &view)
255 {
256  mLookAtPos = view;
257 }
258 
260 {
261  return mLookAtPos;
262 }
263 
265 {
266  mUpDirection = glm::vec3(x, y, z);
267 }
268 
269 void Camera::setUpDirection(const glm::vec3 &upDirection)
270 {
271  mUpDirection = upDirection;
272 }
273 
275 {
276  return mUpDirection;
277 }
278 
280 {
281  return mFieldOfViewYDeg;
282 }
283 
285 {
286  mFieldOfViewYDeg = fovyDeg;
287  mResizeCamera = true;
288 }
virtual fp32_hf getRotationZRad()
Get the rotation angle of the Camera in Radians about the Z-axis.
Definition: Camera.cpp:196
virtual void setLookAt(const glm::vec3 &eye, const glm::vec3 &lookAt, const glm::vec3 &upDirection)=0
Set viewing matrix.
Interface useful for controlling the Camera.
virtual void setFieldOfViewYDeg(fp32_hf fovyDeg)
Set the field of view angle, in degrees, in the y direction.
Definition: Camera.cpp:284
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
virtual glm::vec3 getPositionV3()
Get the position of the Camera.
Definition: Camera.cpp:179
virtual void setZByViewRadius(fp32_hf radius)
Set the Z value necessary to achieve the requested view radius.
Definition: Camera.cpp:222
virtual void setUpDirection(fp32_hf x, fp32_hf y, fp32_hf z)
Set the up direction.
Definition: Camera.cpp:264
static fp32_hf rad2deg(fp32_hf rad)
Convert radians to degrees.
Definition: MathUtil.cpp:101
virtual fp32_hf getZNear()
Get the Z near clip distance.
Definition: Camera.cpp:227
virtual ~Camera()
Destructor.
Definition: Camera.cpp:36
static glm::vec2 mapScreenToWorld(fp32_hf x, fp32_hf y)
Convert screen (pixel) coordinates to world coordinates.
Definition: MathUtil.cpp:33
virtual fp32_hf getRotationZDeg()
Get the rotation angle of the Camera in Degrees about the Z-axis.
Definition: Camera.cpp:201
virtual glm::vec2 getWorldMinVertex()
Get the world coordinate of the bottom-left point of the window.
Definition: Camera.cpp:106
Camera()
Constructor.
Definition: Camera.cpp:31
#define NULL
Convenient define for 0.
Definition: HFDataTypes.h:42
virtual void reset()
Resets the Camera/Viewport to its default.
Definition: Camera.cpp:81
virtual void setZNear(fp32_hf zNear)
Set the Z near clip distance.
Definition: Camera.cpp:237
virtual void setViewCenter(fp32_hf x, fp32_hf y, fp32_hf z)
Set the point towards which the camera should aim.
Definition: Camera.cpp:249
Interface for math related helper methods.
virtual glm::vec2 getPositionV2()
Get the position of the Camera.
Definition: Camera.cpp:174
virtual void setZ(fp32_hf z)
Set the position of the Camera on the Z-axis.
Definition: Camera.cpp:206
virtual void setViewport(int32_hf x, int32_hf y, int32_hf w, int32_hf h)=0
Setup viewport.
virtual void setZFar(fp32_hf zFar)
Set the Z far clip distance.
Definition: Camera.cpp:243
Definition: Actor.h:34
Singleton interface for platform specific things.
virtual void getPosition(fp32_hf *x, fp32_hf *y)
Get the position of the Camera.
Definition: Camera.cpp:156
static fp32_hf deg2rad(fp32_hf deg)
Convert degrees to radians.
Definition: MathUtil.cpp:106
virtual fp32_hf getZForViewRadius(fp32_hf radius)
Get the Z value necessary to achieve the requested view radius.
Definition: Camera.cpp:216
virtual void setRotationZRad(fp32_hf angle)
Set the rotation of the Camera about the Z-axis.
Definition: Camera.cpp:184
virtual void resize(int32_hf screenWidth, int32_hf screenHeight)
This method should be called by the game loop when window is resized. It will setup the perspective p...
Definition: Camera.cpp:60
#define HFCORE_ENV
Get HFCore::Environment instance shortcut macro.
Definition: Environment.h:68
Interface for all rendering related things.
Definition: Graphics.h:120
virtual void rotateZDeg(fp32_hf angle)=0
Multiply the current matrix by a rotation matrix.
virtual glm::vec3 getViewCenter()
Get the current look-at target of the camera.
Definition: Camera.cpp:259
virtual fp32_hf getZ()
Get the position of the camera on the Z-axis.
Definition: Camera.cpp:211
virtual void update(fp32_hf dt)
This method should be called by the game loop once for every iteration of the game loop...
Definition: Camera.cpp:40
virtual void setPosition(fp32_hf x, fp32_hf y)
Set the position of the Camera.
Definition: Camera.cpp:136
virtual glm::vec2 getWorldMaxVertex()
Get the world coordinate of the top-right point of the window.
Definition: Camera.cpp:100
virtual void draw(Graphics *g)
This method should be called by the game loop once for every iteration of the game loop...
Definition: Camera.cpp:45
virtual void setRotationZDeg(fp32_hf angle)
Set the rotation of the Camera about the Z-axis.
Definition: Camera.cpp:190
virtual glm::vec2 getWorldDimensions()
Find out the dimensions of the area currently displayed in the window.
Definition: Camera.cpp:112
virtual glm::vec3 getUpDirection()
Get the current up-direction of the camera.
Definition: Camera.cpp:274
virtual fp32_hf getFieldOfViewYDeg()
Get the field of view angle, in degrees, in the y direction.
Definition: Camera.cpp:279
virtual fp32_hf getZFar()
Get the Z far clip distance.
Definition: Camera.cpp:232
float fp32_hf
A type definition for float.
Definition: HFDataTypes.h:359
virtual fp32_hf getViewRadius()
If you were to draw a circle inscribed in the viewport, this function will let you know the size of i...
Definition: Camera.cpp:94
virtual void setPerspectiveProjection(fp32_hf fovyDeg, fp32_hf aspect, fp32_hf zNear, fp32_hf zFar)=0
Set Perspective projection matrix.