Symphony Of Empires
camera.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // camera.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <glm/vec3.hpp>
28 #include <glm/glm.hpp>
29 #include <glm/gtc/matrix_transform.hpp>
30 #include <glm/gtc/type_ptr.hpp>
31 #include <glm/gtx/intersect.hpp>
32 
33 namespace Eng3D {
34  class Camera {
35  protected:
36  glm::vec3 map_position; // Z-axis is the zoom
37  glm::vec3 world_position;
38  glm::vec2 screen_size;
39  glm::vec2 map_size;
40  public:
41  float fov = 45.0f, near_plane = 1.0f, far_plane = 20000.0f;
42 
43  constexpr Camera(glm::vec2 _screen_size, glm::vec2 _map_size)
44  : map_position{ glm::vec3(1.f) },
45  world_position{ glm::vec3(1.f) },
46  screen_size{ _screen_size },
47  map_size{ _map_size }
48  {
49 
50  }
51 
52  Camera(const Camera& camera)
53  : map_position{ camera.get_map_pos() },
54  world_position{ glm::vec3(1.f) },
55  screen_size{ camera.screen_size },
56  map_size{ camera.map_size }
57  {
58 
59  }
60 
61  virtual ~Camera() = default;
62 
64  inline void set_screen(const int width, const int height) {
65  screen_size = glm::vec2(width, height);
66  }
67 
69  virtual void move(float x_dir, float y_dir, float z_dir) = 0;
70 
72  virtual void set_pos(float x, float y) = 0;
73 
75  virtual glm::vec3 get_map_pos() const = 0;
76 
78  inline glm::vec3 get_world_pos() const {
79  return world_position;
80  }
81 
83  inline glm::vec2 get_map_size() const {
84  return map_size;
85  }
86 
88  virtual void update() = 0;
89 
91  virtual glm::mat4 get_projection() const {
92  const float aspect_ratio = screen_size.x / screen_size.y;
93  return glm::perspective(glm::radians(fov), aspect_ratio, near_plane, far_plane);
94  }
95 
97  virtual glm::mat4 get_view() const = 0;
98 
103  virtual bool get_cursor_map_pos(glm::ivec2 mouse_pos, glm::ivec2& out_pos) const = 0;
104 
106  virtual glm::vec3 get_tile_world_pos(glm::vec2 tile_pos) const = 0;
107 
109  virtual glm::vec2 get_tile_screen_pos(glm::vec2 tile_pos) const {
110  auto world_pos = get_tile_world_pos(tile_pos);
111  auto v = get_projection() * get_view() * glm::vec4(world_pos, 1);
112  auto screen_pos = glm::vec2(v) / v.w;
113  screen_pos.y *= -1;
114  screen_pos = (0.5f * (screen_pos + 1.f)) * screen_size;
115  return screen_pos;
116  }
117  };
118 }
Camera(const Camera &camera)
Definition: camera.hpp:52
virtual ~Camera()=default
glm::vec2 screen_size
Definition: camera.hpp:38
glm::vec2 get_map_size() const
Get the size of the map.
Definition: camera.hpp:83
virtual glm::mat4 get_view() const =0
Get the view matrix.
virtual void set_pos(float x, float y)=0
Set the map position of the camera.
virtual glm::vec3 get_map_pos() const =0
Get the map position of the camera.
void set_screen(const int width, const int height)
Set the width and height of the screen.
Definition: camera.hpp:64
virtual glm::mat4 get_projection() const
Get the projection matrix.
Definition: camera.hpp:91
virtual bool get_cursor_map_pos(glm::ivec2 mouse_pos, glm::ivec2 &out_pos) const =0
Get the cursors position on the map.
glm::vec3 get_world_pos() const
Get the world positions of the camera.
Definition: camera.hpp:78
glm::vec3 world_position
Definition: camera.hpp:37
glm::vec2 map_size
Definition: camera.hpp:39
glm::vec3 map_position
Definition: camera.hpp:36
float near_plane
Definition: camera.hpp:41
virtual glm::vec2 get_tile_screen_pos(glm::vec2 tile_pos) const
Get the tiles position on the screen.
Definition: camera.hpp:109
virtual void update()=0
Update the movement of the camera. Used for smooth camera movement.
virtual glm::vec3 get_tile_world_pos(glm::vec2 tile_pos) const =0
Get the tiles world position.
constexpr Camera(glm::vec2 _screen_size, glm::vec2 _map_size)
Definition: camera.hpp:43
virtual void move(float x_dir, float y_dir, float z_dir)=0
Move the camera in the specified direction. Uses the map coordinate system, where the Z-axis is the m...
float far_plane
Definition: camera.hpp:41
float fov
Definition: camera.hpp:41