Horizon
Loading...
Searching...
No Matches
triangle_renderer.hpp
1#pragma once
2#include "triangle.hpp"
3#include "util/gl_inc.h"
4#include <map>
5#include <vector>
6#include <tuple>
7#include "util/vector_pair.hpp"
8
9namespace horizon {
11 friend class CanvasGL;
12
13public:
14 TriangleRenderer(const class CanvasGL &c, const std::map<int, vector_pair<Triangle, TriangleInfo>> &tris);
15 void realize();
16 void render();
17 void push();
18
19private:
20 const CanvasGL &ca;
21 enum class Type { TRIANGLE, LINE, LINE0, LINE_BUTT, GLYPH, CIRCLE, ARC, ARC0, N_TYPES };
22 static_assert(static_cast<int>(Type::N_TYPES) <= 8);
23 const std::map<int, vector_pair<Triangle, TriangleInfo>> &triangles;
24
25 struct BatchKey {
26 Type type;
27 bool highlight;
28 bool stencil;
29
30 unsigned int hash() const
31 {
32 return (static_cast<unsigned int>(type) & 0x7) | (highlight << 3) | (stencil << 4);
33 }
34
35 static BatchKey unhash(unsigned int h)
36 {
37 return BatchKey{static_cast<Type>(h & 0x7), !!(h & (1 << 3)), !!(h & (1 << 4))};
38 }
39
40 static constexpr unsigned int hash_max = 0x7 | (1 << 3) | (1 << 4);
41 static_assert(hash_max == 0b11'111);
42
43 private:
44 auto tie() const
45 {
46 return std::tie(type, highlight, stencil);
47 }
48
49 public:
50 bool operator<(const BatchKey &other) const
51 {
52 return tie() < other.tie();
53 }
54 bool operator==(const BatchKey &other) const
55 {
56 return tie() == other.tie();
57 }
58 };
59
60 struct Span {
61 size_t offset;
62 size_t count;
63 };
64
65 std::map<int, std::map<BatchKey, Span>> layer_offsets;
66 size_t n_tris = 0;
67
68 GLuint program_line0;
69 GLuint program_line;
70 GLuint program_line_butt;
71 GLuint program_triangle;
72 GLuint program_circle;
73 GLuint program_glyph;
74 GLuint program_arc;
75 GLuint program_arc0;
76 GLuint vao;
77 GLuint vbo;
78 GLuint ubo;
79 GLuint ebo;
80 GLuint texture_glyph;
81
82 enum class HighlightMode { SKIP, ONLY };
83 void render_layer(int layer, HighlightMode highlight_mode, bool ignore_flip = false);
84 using Batch = std::vector<decltype(layer_offsets)::mapped_type::value_type>;
85 void render_layer_batch(int layer, HighlightMode highlight_mode, bool ignore_flip, const Batch &batch,
86 bool use_stencil, bool stencil_mode);
87 void render_annotations(bool top);
88 std::array<float, 4> apply_highlight(const class Color &color, HighlightMode mode, int layer) const;
89 int stencil = 0;
90
91 std::array<std::vector<unsigned int>, BatchKey::hash_max + 1> type_indices;
92};
93} // namespace horizon
Represent basic circle geometry with utility geometry functions.
Definition circle.h:33
Definition canvas_gl.hpp:20
Definition common.hpp:278
Definition triangle_renderer.hpp:10
Definition vector_pair.hpp:8