aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/camera.hpp
blob: ecaadc10971fe7632e55fe232cbd80bc8f53a930 (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
56
57
58
59
#pragma once

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

// 2D orthographic camera. Works in world-space coordinates (Box2D space).
// x+ = right, y+ = up.

class Camera {
public:
    Camera() = default;

    // Set the viewport size in pixels (window size).
    void set_viewport(int width, int height);

    // World-space position the camera is looking at.
    void set_position(glm::vec2 pos);
    glm::vec2 position() const { return pos_; }

    // Zoom: higher = more zoomed in. Clamped to [min_zoom, max_zoom].
    void set_zoom(float zoom);
    float zoom() const { return zoom_; }

    // Pan by delta in world units (not pixels).
    void pan(glm::vec2 delta);

    // Zoom by delta at the given screen position (world-space under cursor).
    void zoom_at(float delta, glm::vec2 world_cursor);

    // Build view-projection matrix. Call once per frame.
    void compute_view_proj();

    const float* view_proj() const { return &vp_[0][0]; }
    glm::mat4    view() const       { return view_; }
    glm::mat4    proj() const       { return proj_; }

    // Convert screen coordinates (pixels, y-down) to world coordinates.
    glm::vec2 screen_to_world(glm::vec2 screen) const;

    int viewport_width()  const { return vp_width_; }
    int viewport_height() const { return vp_height_; }

    // Config
    float min_zoom   = 0.1f;
    float max_zoom   = 200.0f;
    float zoom_speed = 0.1f;
    float pan_speed  = 500.0f;  // pixels per second

private:
    glm::vec2 pos_   = {0.0f, 5.0f};     // world center at start
    float     zoom_  = 10.0f;            // pixels per world unit

    int vp_width_  = 1280;
    int vp_height_ = 720;

    glm::mat4 view_ = glm::mat4(1.0f);
    glm::mat4 proj_ = glm::mat4(1.0f);
    float     vp_[4][4] = {};   // column-major for bgfx
};