renderer.h 2.3 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2
3
4
5
6

#pragma once

#include <variant>

#include "../lib/bbox.h"
TheNumbat's avatar
TheNumbat committed
7
8
#include "../platform/gl.h"
#include "scene.h"
TheNumbat's avatar
TheNumbat committed
9

TheNumbat's avatar
TheNumbat committed
10
11
12
namespace Gui {
class Model;
}
TheNumbat's avatar
TheNumbat committed
13
14
15
16
17
18

// Singleton
class Renderer {
public:
    static void setup(Vec2 dim);
    static void shutdown();
TheNumbat's avatar
TheNumbat committed
19
20
    static Renderer &get();

TheNumbat's avatar
TheNumbat committed
21
22
23
    void begin();
    void complete();
    void reset_depth();
TheNumbat's avatar
TheNumbat committed
24
25

    void proj(const Mat4 &proj);
TheNumbat's avatar
TheNumbat committed
26
    void update_dim(Vec2 dim);
TheNumbat's avatar
TheNumbat committed
27
    void settings_gui(bool *open);
TheNumbat's avatar
TheNumbat committed
28
29
30
31
32
33
34
35
36
37
38
39
40
    void set_samples(int samples);
    unsigned int read_id(Vec2 pos);

    struct MeshOpt {
        unsigned int id;
        Mat4 modelview;
        Vec3 color, sel_color, hov_color;
        unsigned int sel_id = 0, hov_id = 0;
        float alpha = 1.0f;
        bool wireframe = false;
        bool solid_color = false;
        bool depth_only = false;
        bool per_vert_id = false;
TheNumbat's avatar
TheNumbat committed
41
    };
TheNumbat's avatar
TheNumbat committed
42
43

    struct HalfedgeOpt {
TheNumbat's avatar
TheNumbat committed
44
45
        HalfedgeOpt(Gui::Model &e) : editor(e) {}
        Gui::Model &editor;
TheNumbat's avatar
TheNumbat committed
46
        Mat4 modelview;
TheNumbat's avatar
TheNumbat committed
47
48
49
50
        Vec3 f_color = Vec3{1.0f};
        Vec3 v_color = Vec3{1.0f};
        Vec3 e_color = Vec3{0.8f};
        Vec3 he_color = Vec3{0.6f};
TheNumbat's avatar
TheNumbat committed
51
52
53
54
    };

    // NOTE(max): updates & uses the indices in mesh for selection/traversal
    void halfedge_editor(HalfedgeOpt opt);
TheNumbat's avatar
TheNumbat committed
55
56
57
58
59
    void mesh(GL::Mesh &mesh, MeshOpt opt);
    void lines(const GL::Lines &lines, const Mat4 &view, const Mat4 &model = Mat4::I,
               float alpha = 1.0f);

    void outline(const Mat4 &view, Scene_Item &obj);
TheNumbat's avatar
TheNumbat committed
60
    void begin_outline();
TheNumbat's avatar
TheNumbat committed
61
62
63
64
65
    void end_outline(const Mat4 &view, BBox box);

    void skydome(const Mat4 &rotation, Vec3 color, float cosine);
    void skydome(const Mat4 &rotation, Vec3 color, float cosine, const GL::Tex2D &tex);

TheNumbat's avatar
TheNumbat committed
66
67
    void sphere(MeshOpt opt);
    void capsule(MeshOpt opt, float height, float rad);
TheNumbat's avatar
TheNumbat committed
68
    void capsule(MeshOpt opt, const Mat4 &mdl, float height, float rad, BBox &box);
TheNumbat's avatar
TheNumbat committed
69
70

    GLuint saved() const;
TheNumbat's avatar
TheNumbat committed
71
72
    void saved(std::vector<unsigned char> &data) const;
    void save(Scene &scene, const Camera &cam, int w, int h, int samples);
TheNumbat's avatar
TheNumbat committed
73
74
75
76

private:
    Renderer(Vec2 dim);
    ~Renderer();
TheNumbat's avatar
TheNumbat committed
77
    static inline Renderer *data = nullptr;
TheNumbat's avatar
TheNumbat committed
78

TheNumbat's avatar
TheNumbat committed
79
80
    GL::Framebuffer framebuffer, id_resolve, save_buffer, save_output;
    GL::Shader mesh_shader, line_shader, inst_shader, dome_shader;
TheNumbat's avatar
TheNumbat committed
81
82
83
84
    GL::Mesh _sphere, _cyl, _hemi;

    int samples;
    Vec2 window_dim;
TheNumbat's avatar
TheNumbat committed
85
86
    GLubyte *id_buffer;

TheNumbat's avatar
TheNumbat committed
87
88
    Mat4 _proj;
};