SuzanneIsland: An island of Real-time rendering effects!
camera.h
1 #pragma once
2 
3 #define ZOOM_MIN 30.0f
4 #define ZOOM_MAX 80.0f
5 
6 #include <vector>
7 #include <iostream>
8 
9 #include <GL/glew.h>
10 #include <GLFW/glfw3.h>
11 
12 #include "sceneobject.hpp"
13 
14 
20 class Camera : public SceneObject
21 {
22  public:
23  Camera(GLFWwindow *window_, const glm::mat4 &matrix_, float fieldOfView_, float aspectRatio_, float nearPlane_, float farPlane_);
24  Camera(GLFWwindow *window_, const glm::mat4 &matrix_);
25  virtual ~Camera();
26 
28  virtual void update(
29  float timeDelta,
30  float cameraPathSpeed
31  );
32 
38  glm::mat4 getViewMat() const;
39 
45  glm::mat4 getProjMat() const;
46 
49  float getFieldOfView() const;
50 
52  void setFieldOfView(
53  float fieldOfView_
54  );
55 
58  float getAspectRatio() const;
59 
61  void setAspectRatio(
62  float aspectRatio_
63  );
64 
67  float getNearPlane() const;
68 
70  void setNearPlane(
71  float nearPlane_
72  );
73 
76  float getFarPlane() const;
77 
80  void setFarPlane(float farPlane_);
81 
84  void lookAt(
85  const glm::vec3 &target
86  );
87 
94  const glm::vec3 &sphereCenterWorldSpace,
95  const glm::vec3 &sphereFarthestPointWorldSpace,
96  const glm::mat4 &viewMat
97  );
98 
100  void toggleNavMode();
101 
103  inline void clearCameraPath() { cameraPath.clear(); }
104 
108  void appendPath(std::vector<std::vector<glm::vec3> > newPath);
109 
113  inline const std::vector<std::vector<glm::vec3> >& getPath() const { return cameraPath; }
114 
116  inline const glm::vec3& getTargetLookAtPos() const { return targetLookAtPos; }
117 
119  inline void setTargetLookAtPos(const glm::vec3& lookAtPos) { targetLookAtPos = lookAtPos; }
120 
121 private:
122 
123  GLFWwindow *window;
124 
125  float fieldOfView;
126  float aspectRatio;
127  float nearPlane;
128  float farPlane;
129 
130  enum CameraNavigationMode
131  {
132  FOLLOW_PATH,
133  FREE_FLY
134  };
135 
136  static CameraNavigationMode cameraNavMode;
137  CameraNavigationMode lastNavMode;
138  glm::mat4 lastCamTransform; // backup transformation matrix before changing camera mode
139 
140  float timePassed = 0;
141  static double scrollY;
142 
144  void updateBezierPathCamTransform(const double timeDelta, const double speed);
145 
146  std::vector<std::vector<glm::vec3>> cameraPath;
147  int currentPathSegment;
148  float pathSegmentLerpFactor;
149  glm::vec3 targetLookAtPos;
150 
155  void handleNavModeChange();
156 
158  static void onScroll(
159  GLFWwindow *window,
160  double deltaX,
161  double deltaY
162  );
163 
165  void handleInputFreeCamera(
166  GLFWwindow *window,
167  float timeDelta
168  );
169 
170  glm::vec3 mix(const glm::vec3 &a, const glm::vec3 &b, float t);
171 
172 };
173 
void clearCameraPath()
clears the current camera path
Definition: camera.h:103
float getFieldOfView() const
Get field of view of the viewing frustum.
Definition: camera.cpp:63
const std::vector< std::vector< glm::vec3 > > & getPath() const
returns the current camera path (control points of bezier curve segments).
Definition: camera.h:113
void setNearPlane(float nearPlane_)
Set distance from camera to near plane of the viewing frustum.
Definition: camera.cpp:88
float getNearPlane() const
Get distance from camera to near plane of the viewing frustum.
Definition: camera.cpp:83
void appendPath(std::vector< std::vector< glm::vec3 > > newPath)
appends a number of path segments to current camera path (control points of bezier curve segments)...
Definition: camera.cpp:134
void lookAt(const glm::vec3 &target)
Rotate camera to look at a given target point The target should not be the camera location...
Definition: camera.cpp:103
float getFarPlane() const
Get distance from camera to near plane of the viewing frustum.
Definition: camera.cpp:93
void toggleNavMode()
Toggle the camera navigation mode.
Definition: camera.cpp:243
void setTargetLookAtPos(const glm::vec3 &lookAtPos)
set target look at position for FOLLOW_PATH mode
Definition: camera.h:119
void setAspectRatio(float aspectRatio_)
Set the aspect ratio of the viewing frustum (width / height)
Definition: camera.cpp:78
glm::mat4 getProjMat() const
get the current projection matrix defining the view frustum calculated from the parameters stored ...
Definition: camera.cpp:58
const glm::vec3 & getTargetLookAtPos() const
get target look at position for FOLLOW_PATH mode
Definition: camera.h:116
A base class for all scene objects.
Definition: sceneobject.hpp:15
void setFarPlane(float farPlane_)
Set distance from camera to far plane of the viewing frustum.
Definition: camera.cpp:98
glm::mat4 getViewMat() const
get the view matrix, i.e.
Definition: camera.cpp:53
virtual void update(float timeDelta, float cameraPathSpeed)
Update the state of the Camera.
Definition: camera.cpp:36
bool checkSphereInFrustum(const glm::vec3 &sphereCenterWorldSpace, const glm::vec3 &sphereFarthestPointWorldSpace, const glm::mat4 &viewMat)
Determine whether a sphere with given center and farthest point in world space lies completely within...
Definition: camera.cpp:108
A Camera is a SceneObject that maintains a view matrix, as well as parameters defining the projection...
Definition: camera.h:20
float getAspectRatio() const
Get the aspect ratio of the viewing frustum (width/height)
Definition: camera.cpp:73
void setFieldOfView(float fieldOfView_)
Set field of view of the viewing frustum.
Definition: camera.cpp:68