Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
Camera.hpp
Go to the documentation of this file.
1 #pragma once
2 // Include both standard functionality and the extensions
3 #define GLM_FORCE_DEPTH_ZERO_TO_ONE
4 #include <glm/ext.hpp>
5 #include <glm/glm.hpp>
6 
7 #include <functional>
8 
9 // Forward declarations
10 struct GLFWwindow;
11 
12 // TODO figure out how camera should function with the player target.
13 struct Target
14 {
15  glm::vec3 pos;
16 };
17 // standard cartesian coordinate system
18 constexpr glm::vec3 POSITIVE_X_AXIS(1.f, 0.f, 0.f);
19 constexpr glm::vec3 POSITIVE_Y_AXIS(0.f, 1.f, 0.f);
20 constexpr glm::vec3 POSITIVE_Z_AXIS(0.f, 0.f, 1.f);
21 
22 constexpr float STANDARD_ZOOM(12.f); // zoom used in the constructor. 10 Units away from the target
23 constexpr float MIN_ZOOM(2.f); // minimum zoom
24 constexpr float MAX_ZOOM(25.f); // maximum zoom
25 
31 class Camera
32 {
33 public:
45  Camera(glm::vec3 targetPos, float width, float height, float fovRad = glm::quarter_pi<float>(), float near = 0.1f, float far = 100.f, float sensitivity = 0.01f);
46 
53  glm::mat4 getViewMatrix() const;
54 
59  glm::mat4 getOrientation() const;
60 
66  glm::mat4 getProjectionMatrix() const;
67 
72  glm::vec3 getCameraRay(double x, double y) const;
73 
78  glm::mat4 getWorldToScreenMatrix() const;
79 
84  uint32_t getUboSize();
85 
90  void orbitRotate(glm::vec2 rotationVec);
91 
96  void zoom(float zoomAmount);
97 
102  void registerCameraControls(GLFWwindow* window);
103 
109  void setSensitivity(float newSensitivity, float newZoomSens);
110 
115  void moveLocation(glm::vec3 movement);
116 
120  void move(glm::vec3 movement);
121 
126  void setLocation(glm::vec3 location);
127 
132  void setTargetAndCamera(glm::vec3 location);
133 
138  glm::vec3 getLocation() const;
139 
144  glm::vec3 getForward() const;
145 
150  glm::vec3 getRight() const;
151 
156  glm::vec3 getUp() const;
157 
162  glm::vec2 getSensitivities() const;
163 
164  static bool canMove;
165 
166 private:
171  glm::mat4 getCameraMatrixOrigon() const;
172 
173  // These encapsulate most important aspects of the camera.
174  // cameraMatrixM wastes currently 4 floats, but it's easier to work with.
175  glm::mat4 cameraMatrixM;
176  glm::mat4 projectionM;
177  glm::vec3 targetM;
180  float zoomM;
181  float widthM;
182  float heightM;
183 
184  static double mouseX;
185  static double mouseY;
186  static void cursorPosCallback(GLFWwindow* pWindow, double xpos, double ypos);
187  static void scrollCallback(GLFWwindow* pWindow, double xOffSet, double yOffSet);
188  static std::function<void(glm::vec2)> cameraUpdateCallback;
189  static std::function<void(float)> cameraZoomCallback;
190 };
constexpr glm::vec3 POSITIVE_X_AXIS(1.f, 0.f, 0.f)
constexpr float MIN_ZOOM(2.f)
constexpr float MAX_ZOOM(25.f)
constexpr glm::vec3 POSITIVE_Y_AXIS(0.f, 1.f, 0.f)
constexpr float STANDARD_ZOOM(12.f)
constexpr glm::vec3 POSITIVE_Z_AXIS(0.f, 0.f, 1.f)
Definition: Camera.hpp:32
static double mouseX
Definition: Camera.hpp:184
static void cursorPosCallback(GLFWwindow *pWindow, double xpos, double ypos)
Callback for registering mouse movement.
Definition: Camera.cpp:158
float zoomM
Definition: Camera.hpp:180
glm::mat4 getWorldToScreenMatrix() const
Returns the world to screen matrix.
Definition: Camera.cpp:38
static double mouseY
Definition: Camera.hpp:185
Camera(glm::vec3 targetPos, float width, float height, float fovRad=glm::quarter_pi< float >(), float near=0.1f, float far=100.f, float sensitivity=0.01f)
Constructor for the camera.
Definition: Camera.cpp:10
static std::function< void(float)> cameraZoomCallback
Callback for updating the camera.
Definition: Camera.hpp:189
static bool canMove
Definition: Camera.hpp:164
glm::mat4 getViewMatrix() const
Returns the view matrix of the camera, which is the inverse of the model matrix of the camera....
Definition: Camera.cpp:22
glm::vec3 getCameraRay(double x, double y) const
Returns the camera ray in world space.
Definition: Camera.cpp:43
float widthM
Definition: Camera.hpp:181
glm::mat4 getProjectionMatrix() const
Returns the projection matrix of the camera.
Definition: Camera.cpp:33
static void scrollCallback(GLFWwindow *pWindow, double xOffSet, double yOffSet)
callback for registering mouse scroll
Definition: Camera.cpp:195
glm::mat4 cameraMatrixM
Definition: Camera.hpp:175
void setLocation(glm::vec3 location)
Sets the camera location to the given location.
Definition: Camera.cpp:180
glm::vec3 targetM
Definition: Camera.hpp:177
float heightM
Definition: Camera.hpp:182
void registerCameraControls(GLFWwindow *window)
Registers the camera controls to the given window.
Definition: Camera.cpp:144
void setTargetAndCamera(glm::vec3 location)
Sets the camera target to the given location and moves camera by the same amount.
Definition: Camera.cpp:187
void moveLocation(glm::vec3 movement)
Moves the camera by the given vector.
Definition: Camera.cpp:96
uint32_t getUboSize()
Returns the size of the camera's UBO.
Definition: Camera.cpp:63
glm::vec3 getRight() const
Returns the right direction of the camera.
Definition: Camera.cpp:124
glm::vec3 getLocation() const
Returns the location of the camera.
Definition: Camera.cpp:114
static std::function< void(glm::vec2)> cameraUpdateCallback
Callback for updating the camera.
Definition: Camera.hpp:188
glm::vec3 getUp() const
Returns the up direction of the camera.
Definition: Camera.cpp:129
float zoomSensitivityM
Definition: Camera.hpp:179
glm::mat4 projectionM
Definition: Camera.hpp:176
glm::mat4 getCameraMatrixOrigon() const
returns the camera matrix with the target as the origon.
Definition: Camera.cpp:103
float sensitivityM
Definition: Camera.hpp:178
void orbitRotate(glm::vec2 rotationVec)
rotates the camera around the target.
Definition: Camera.cpp:68
glm::vec2 getSensitivities() const
Returns the sensitivity of the camera.
Definition: Camera.cpp:134
glm::vec3 getForward() const
Returns the forward direction of the camera.
Definition: Camera.cpp:119
void setSensitivity(float newSensitivity, float newZoomSens)
sets the sensitivity of the camera.
Definition: Camera.cpp:90
void zoom(float zoomAmount)
Zooms the camera by the given amount.
Definition: Camera.cpp:81
glm::mat4 getOrientation() const
Returns the orientation of the camera.
Definition: Camera.cpp:28
void move(glm::vec3 movement)
Moves both the camera and the camera target by the given vector.
Definition: Camera.cpp:174
Definition: Camera.hpp:14
glm::vec3 pos
Definition: Camera.hpp:15