blob: 6ab2c57e8a88e1ff5e63f8b2e91ef211cff67c2e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include "engine/camera.hpp"
void Camera::set_viewport(int width, int height) {
vp_width_ = width;
vp_height_ = height;
}
void Camera::set_position(glm::vec2 pos) {
pos_ = pos;
}
void Camera::set_zoom(float zoom) {
if (zoom < min_zoom) zoom = min_zoom;
if (zoom > max_zoom) zoom = max_zoom;
zoom_ = zoom;
}
void Camera::pan(glm::vec2 delta) {
pos_ += delta;
}
void Camera::zoom_at(float delta, glm::vec2 world_cursor) {
float new_zoom = zoom_ * (1.0f + delta * zoom_speed);
if (new_zoom < min_zoom) new_zoom = min_zoom;
if (new_zoom > max_zoom) new_zoom = max_zoom;
// Adjust position so world_cursor stays under the same screen point
float ratio = zoom_ / new_zoom;
pos_ = world_cursor + (pos_ - world_cursor) * ratio;
zoom_ = new_zoom;
}
void Camera::compute_view_proj() {
float half_w = (vp_width_ / zoom_) * 0.5f;
float half_h = (vp_height_ / zoom_) * 0.5f;
proj_ = glm::ortho(-half_w, half_w, -half_h, half_h, -1.0f, 1.0f);
view_ = glm::translate(glm::mat4(1.0f), glm::vec3(-pos_.x, -pos_.y, 0.0f));
glm::mat4 vp = proj_ * view_;
// Copy to column-major float array for bgfx
const float* src = &vp[0][0];
float* dst = &vp_[0][0];
for (int i = 0; i < 16; i++) {
dst[i] = src[i];
}
}
glm::vec2 Camera::screen_to_world(glm::vec2 screen) const {
// Convert from screen (y-down) to world (y-up)
float world_x = pos_.x + (screen.x - vp_width_ * 0.5f) / zoom_;
float world_y = pos_.y - (screen.y - vp_height_ * 0.5f) / zoom_;
return glm::vec2(world_x, world_y);
}
|