model.h 3.29 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2
3
4
5
6
7
8
9
10
11
12

#pragma once

#include "widgets.h"

#include <SDL2/SDL.h>
#include <optional>
#include <unordered_map>

#include "../geometry/halfedge.h"
#include "../platform/gl.h"
#include "../scene/scene.h"
TheNumbat's avatar
TheNumbat committed
13
#include "../util/camera.h"
TheNumbat's avatar
TheNumbat committed
14
15
16
17
18
19
20
21

namespace Gui {

class Model {
public:
    Model();

    // Gui view API
TheNumbat's avatar
TheNumbat committed
22
    bool keydown(Widgets &widgets, SDL_Keysym key, Camera &cam);
TheNumbat's avatar
TheNumbat committed
23
    void unset_mesh();
TheNumbat's avatar
TheNumbat committed
24
25
26
27

    std::string UIsidebar(Undo &undo, Widgets &widgets, Scene_Maybe obj, Camera &cam);
    void render(Scene_Maybe obj_opt, Widgets &widgets, Camera &cam);

TheNumbat's avatar
TheNumbat committed
28
29
    Vec3 selected_pos();
    void clear_select();
TheNumbat's avatar
TheNumbat committed
30
    void erase_selected(Undo& undo, Scene_Maybe obj_opt);
TheNumbat's avatar
TheNumbat committed
31
32
33
    std::string end_transform(Widgets &widgets, Undo &undo, Scene_Object &obj);
    void apply_transform(Widgets &widgets);
    std::string select(Widgets &widgets, Scene_ID click, Vec3 cam, Vec2 spos, Vec3 dir);
TheNumbat's avatar
TheNumbat committed
34

TheNumbat's avatar
TheNumbat committed
35
    std::tuple<GL::Mesh &, GL::Instances &, GL::Instances &, GL::Instances &> shapes();
TheNumbat's avatar
TheNumbat committed
36
37
38
39
40
    unsigned int select_id() const;
    unsigned int hover_id() const;
    void set_hover(unsigned int id);

private:
TheNumbat's avatar
TheNumbat committed
41
42
43
44
45
46
47
48
    template <typename T>
    std::string update_mesh(Undo &undo, Scene_Object &obj, Halfedge_Mesh &&before,
                            Halfedge_Mesh::ElementRef ref, T &&op);
    template <typename T>
    std::string update_mesh_global(Undo &undo, Scene_Object &obj, Halfedge_Mesh &&before, T &&op);

    void zoom_to(Halfedge_Mesh::ElementRef ref, Camera &cam);
    void set_mesh(Halfedge_Mesh &mesh);
TheNumbat's avatar
TheNumbat committed
49
    void begin_transform();
TheNumbat's avatar
TheNumbat committed
50
    bool begin_bevel(std::string &err);
TheNumbat's avatar
TheNumbat committed
51
    void set_selected(Halfedge_Mesh::ElementRef elem);
TheNumbat's avatar
TheNumbat committed
52
    std::optional<std::reference_wrapper<Scene_Object>> is_my_obj(Scene_Maybe obj_opt);
TheNumbat's avatar
TheNumbat committed
53
54
    std::optional<Halfedge_Mesh::ElementRef> selected_element();
    void rebuild();
TheNumbat's avatar
TheNumbat committed
55

TheNumbat's avatar
TheNumbat committed
56
    void update_vertex(Halfedge_Mesh::VertexRef vert);
TheNumbat's avatar
TheNumbat committed
57
58
59
60
61
    void vertex_viz(Halfedge_Mesh::VertexRef v, float &size, Mat4 &transform);
    void edge_viz(Halfedge_Mesh::EdgeRef e, Mat4 &transform);
    void halfedge_viz(Halfedge_Mesh::HalfedgeRef h, Mat4 &transform);
    void face_viz(Halfedge_Mesh::FaceRef face, std::vector<GL::Mesh::Vert> &verts,
                  std::vector<GL::Mesh::Index> &idxs, size_t insert_at);
TheNumbat's avatar
TheNumbat committed
62

TheNumbat's avatar
TheNumbat committed
63
    Halfedge_Mesh *my_mesh = nullptr;
TheNumbat's avatar
TheNumbat committed
64
65
    Halfedge_Mesh old_mesh;

TheNumbat's avatar
TheNumbat committed
66
    enum class Bevel { face, edge, vert };
TheNumbat's avatar
TheNumbat committed
67
    Bevel beveling;
TheNumbat's avatar
TheNumbat committed
68

TheNumbat's avatar
TheNumbat committed
69
70
71
72
73
74
75
76
    struct Transform_Data {
        std::vector<Vec3> verts;
        Vec3 center;
    };

    Transform_Data trans_begin;
    GL::Instances spheres, cylinders, arrows;
    GL::Mesh face_mesh;
TheNumbat's avatar
TheNumbat committed
77
    Vec3 f_col = Vec3{1.0f}, v_col = Vec3{1.0f}, e_col = Vec3{0.8f}, he_col = Vec3{0.6f};
TheNumbat's avatar
TheNumbat committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

    // This all needs to be updated when the mesh connectivity changes
    unsigned int selected_elem_id = UINT32_MAX, hovered_elem_id = UINT32_MAX;

    // This is a kind of bad design and would be un-necessary if we used
    // a halfedge implementation with contiguous iterators. For now this map must
    // be updated (along with the instance data) by build_halfedge whenever
    // the mesh changes its connectivity. Note that build_halfedge also
    // re-indexes the mesh elements in the provided half-edge mesh.
    struct ElemInfo {
        Halfedge_Mesh::ElementRef ref;
        size_t instance = 0;
    };
    std::unordered_map<unsigned int, ElemInfo> id_to_info;
    std::unordered_map<unsigned int, float> vert_sizes;
};

TheNumbat's avatar
TheNumbat committed
95
} // namespace Gui