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>
33 #include "eng3d/camera.hpp"
34 #include "eng3d/value_chase.hpp"
45 OrbitCamera(glm::vec2 _screen_size, glm::vec2 _map_size,
float _radius)
61 void move(
float x_dir,
float y_dir,
float z_dir)
override {
76 glm::vec2 radiance_pos;
77 constexpr
float pi = glm::pi<float>();
78 radiance_pos.x = glm::mod(normalized_pos.x * 2.f * pi, 2.f * pi);
79 radiance_pos.y = glm::max(0.f, glm::min(pi, normalized_pos.y * pi));
84 world_position.x = distance * cos(radiance_pos.x) * sin(radiance_pos.y);
85 world_position.y = distance * sin(radiance_pos.x) * sin(radiance_pos.y);
97 glm::vec3 look_at = glm::vec3(0);
98 glm::vec3 up_vector = glm::vec3(0.f, -1.f, 0.f);
101 normalized_pos.x = glm::mod(normalized_pos.x,
map_size.x);
103 glm::vec2 radiance_pos;
104 constexpr
float pi = glm::pi<float>();
105 radiance_pos.x = glm::mod(normalized_pos.x * 2.f * pi, 2.f * pi);
106 radiance_pos.y = glm::max(0.f, glm::min(pi, normalized_pos.y * pi));
107 up_vector.x = -cos(radiance_pos.x) * cos(radiance_pos.y);
108 up_vector.y = -sin(radiance_pos.x) * cos(radiance_pos.y);
109 up_vector.z = sin(radiance_pos.y);
119 out_pos.x = glm::mod(out_pos.x,
map_size.x);
124 float mouse_x = mouse_pos.x;
129 const glm::vec3 world_space_near = glm::unProject(
130 glm::vec3(mouse_x, mouse_y, 0.f),
134 const glm::vec3 world_space_far = glm::unProject(
135 glm::vec3(mouse_x, mouse_y, 1.f),
140 glm::vec3 ray_direction = world_space_far - world_space_near;
141 ray_direction = glm::normalize(ray_direction);
143 float distance = 0.f;
144 bool hit = glm::intersectRaySphere(world_space_near, ray_direction, glm::vec3(0, 0, 0),
radius *
radius, distance);
146 glm::vec3 intersection_point = world_space_near + ray_direction * distance;
147 constexpr
float pi = glm::pi<float>();
148 float y_rad = glm::acos(intersection_point.z /
radius);
149 float x_rad = glm::atan(intersection_point.y, intersection_point.x);
150 x_rad += x_rad < 0 ? 2.f * pi : 0.f;
152 out_pos.x =
map_size.x * x_rad / (2.f * pi);
153 out_pos.y =
map_size.y * y_rad / (pi);
158 constexpr
float pi = glm::pi<float>();
159 const glm::vec2 normalized_pos = tile_pos /
map_size;
160 glm::vec2 radiance_pos;
161 radiance_pos.x = glm::mod(normalized_pos.x * 2.f * pi, 2.f * pi);
162 radiance_pos.y = glm::max(0.f, glm::min(pi, normalized_pos.y * pi));
164 const float distance =
radius;
166 out_pos.x = distance * cos(radiance_pos.x) * sin(radiance_pos.y);
167 out_pos.y = distance * sin(radiance_pos.x) * sin(radiance_pos.y);
168 out_pos.z = distance * cos(radiance_pos.y);
virtual glm::mat4 get_projection() const
Get the projection matrix.
void update() override
Update the movement of the camera. Used for smooth camera movement.
bool get_cursor_map_pos(glm::ivec2 mouse_pos, glm::ivec2 &out_pos) const override
Get the cursors position on the map.
OrbitCamera(const Camera &_camera, float _radius)
void move(float x_dir, float y_dir, float z_dir) override
Move the camera in the specified direction. Uses the map coordinate system, where the Z-axis is the m...
glm::vec3 get_tile_world_pos(glm::vec2 tile_pos) const override
Get the tiles world position.
glm::mat4 get_view() const override
Get the view matrix.
OrbitCamera(glm::vec2 _screen_size, glm::vec2 _map_size, float _radius)
glm::vec3 get_map_pos() const override
Get the map position of the camera.
ValueChase< glm::vec3 > chase
void set_pos(float x, float y) override
Set the map position of the camera.
constexpr T move_towards(T current, T target)