Tatooine
ellipse.h
Go to the documentation of this file.
1#ifndef TATOOINE_RENDERING_INTERACTIVE_ELLIPSE_H
2#define TATOOINE_RENDERING_INTERACTIVE_ELLIPSE_H
3//==============================================================================
8#include <tatooine/linspace.h>
11//==============================================================================
13//==============================================================================
14template <typename Ellipse>
15requires(is_derived_from_hyper_ellipse<Ellipse> &&
16 Ellipse::num_dimensions() == 2)
18 using renderable_type = Ellipse;
19 using real_type = typename Ellipse::real_type;
20 struct geometry : gl::indexeddata<Vec2<GLfloat>> {
21 static auto get() -> auto& {
22 static auto instance = geometry{};
23 return instance;
24 }
25 explicit geometry(std::size_t const num_vertices = 32) {
26 vertexbuffer().resize(num_vertices);
27 {
28 auto ts = linspace<float>{0, 2 * M_PI, num_vertices + 1};
29 ts.pop_back();
30 auto vb_map = vertexbuffer().wmap();
31 auto i = std::size_t{};
32 for (auto const t : ts) {
33 vb_map[i++] = Vec2<GLfloat>{std::cos(t), std::sin(t)};
34 }
35 }
36 indexbuffer().resize(num_vertices);
37 {
38 auto data = indexbuffer().wmap();
39 for (std::size_t i = 0; i < num_vertices; ++i) {
40 data[i] = i;
41 }
42 }
43 }
44 };
45 //==============================================================================
47 //==============================================================================
48 int line_width = 1;
49 Vec4<GLfloat> color = {0, 0, 0, 1};
50 //==============================================================================
51 renderer(renderable_type const& /*ell*/) {}
52 //------------------------------------------------------------------------------
53 static auto set_projection_matrix(Mat4<GLfloat> const& P) {
54 shader::get().set_projection_matrix(P);
55 }
56 //------------------------------------------------------------------------------
57 auto properties(renderable_type const& /*ell*/) {
58 ImGui::Text("Ellipse");
59 ImGui::DragInt("Line width", &line_width, 1, 1, 20);
60 ImGui::ColorEdit4("Color", color.data());
61 }
62 //==============================================================================
64 Vec2<real_type> const& center) {
65 auto constexpr O = GLfloat(0);
66 auto constexpr I = GLfloat(1);
67 static auto constexpr ell_is_float = is_same<GLfloat, real_type>;
68 if constexpr (ell_is_float) {
69 return Mat4<GLfloat>{{S(0, 0), S(0, 1), O, center(0)},
70 {S(1, 0), S(1, 1), O, center(1)},
71 {O, O, I, O},
72 {O, O, O, I}};
73 } else {
74 return Mat4<GLfloat>{
75 {GLfloat(S(0, 0)), GLfloat(S(0, 1)), O, GLfloat(center(0))},
76 {GLfloat(S(1, 0)), GLfloat(S(1, 1)), O, GLfloat(center(1))},
77 {O, O, I, O},
78 {O, O, O, I}};
79 }
80 }
81 //==============================================================================
82 auto update(auto const /*dt*/, auto& ell, camera auto const& cam) {
83 auto& shader = shader::get();
84 using cam_real_type = typename std::decay_t<decltype(cam)>::real_type;
85 static auto constexpr ell_is_float = is_same<GLfloat, real_type>;
86 static auto constexpr cam_is_float = is_same<GLfloat, cam_real_type>;
87
88 auto V = [&] {
89 if constexpr (cam_is_float) {
90 return cam.view_matrix();
91 } else {
92 return Mat4<GLfloat>{cam.view_matrix()};
93 }
94 }();
96 construct_model_matrix(ell.S(), ell.center()));
97 shader.set_color(color(0), color(1), color(2), color(3));
98 }
99 //----------------------------------------------------------------------------
100 auto render() {
101 shader::get().bind();
102 gl::line_width(line_width);
103 geometry::get().draw_line_loop();
104 }
105};
106//==============================================================================
107template <range EllipseRange>
109 std::ranges::range_value_t<EllipseRange>> &&
110 std::ranges::range_value_t<EllipseRange>::num_dimensions() == 2)
112 using renderable_type = EllipseRange;
113 using ellipse_type = std::ranges::range_value_t<EllipseRange>;
114 using real_type =
115 typename ellipse_type::real_type;
116 //==============================================================================
119 //==============================================================================
120 int line_width = 1;
121 Vec4<GLfloat> color = {0, 0, 0, 1};
122 //==============================================================================
123 renderer(renderable_type const& /*ell*/) {}
124 //------------------------------------------------------------------------------
125 static auto set_projection_matrix(Mat4<GLfloat> const& P) {
126 shader::get().set_projection_matrix(P);
127 }
128 //------------------------------------------------------------------------------
129 auto properties(renderable_type const& /*ell*/) {
130 ImGui::Text("Ellipse");
131 ImGui::DragInt("Line width", &line_width, 1, 1, 20);
132 ImGui::ColorEdit4("Color", color.data());
133 }
134 //==============================================================================
136 Vec2<real_type> const& center) {
137 auto constexpr O = GLfloat(0);
138 auto constexpr I = GLfloat(1);
139 static auto constexpr ell_is_float = is_same<GLfloat, real_type>;
140 if constexpr (ell_is_float) {
141 return Mat4<GLfloat>{{S(0, 0), S(0, 1), O, center(0)},
142 {S(1, 0), S(1, 1), O, center(1)},
143 {O, O, I, O},
144 {O, O, O, I}};
145 } else {
146 return Mat4<GLfloat>{
147 {GLfloat(S(0, 0)), GLfloat(S(0, 1)), O, GLfloat(center(0))},
148 {GLfloat(S(1, 0)), GLfloat(S(1, 1)), O, GLfloat(center(1))},
149 {O, O, I, O},
150 {O, O, O, I}};
151 }
152 }
153 //==============================================================================
154 auto render(auto const& ellipses, camera auto const& cam) {
155 auto& shader = shader::get();
156 shader.bind();
157 using cam_real_type = typename std::decay_t<decltype(cam)>::real_type;
158 static auto constexpr cam_is_float = is_same<GLfloat, cam_real_type>;
159
160 gl::line_width(line_width);
161 shader.set_color(color(0), color(1), color(2), color(3));
162 auto const V = [&] {
163 if constexpr (cam_is_float) {
164 return cam.view_matrix();
165 } else {
166 return Mat4<GLfloat>{cam.view_matrix()};
167 }
168 }();
169 for (auto const& ell : ellipses) {
170 shader.set_model_view_matrix(
171 V * construct_model_matrix(ell.S(), ell.center()));
172 geometry::get().draw_line_loop();
173 }
174 }
175};
176//==============================================================================
177} // namespace tatooine::rendering::interactive
178//==============================================================================
179#endif
Definition: indexeddata.h:13
Definition: shader.h:24
constexpr auto data() -> ValueType *
Definition: static_multidim_array.h:260
Definition: camera.h:312
DLL_API auto line_width(GLfloat width) -> void
Definition: interactive.h:15
typename get_impl< Container, I >::type get
Definition: get.h:11
static auto constexpr is_derived_from_hyper_ellipse
Definition: hyper_ellipse.h:306
Definition: linspace.h:26
constexpr auto pop_back()
Definition: linspace.h:107
Definition: mat.h:14
std::ranges::range_value_t< EllipseRange > ellipse_type
Definition: ellipse.h:113
static auto set_projection_matrix(Mat4< GLfloat > const &P)
Definition: ellipse.h:125
typename renderer< ellipse_type >::geometry geometry
Definition: ellipse.h:117
static auto construct_model_matrix(Mat2< real_type > const &S, Vec2< real_type > const &center)
Definition: ellipse.h:135
renderer(renderable_type const &)
Definition: ellipse.h:123
EllipseRange renderable_type
Definition: ellipse.h:112
auto render(auto const &ellipses, camera auto const &cam)
Definition: ellipse.h:154
auto properties(renderable_type const &)
Definition: ellipse.h:129
typename renderer< ellipse_type >::shader shader
Definition: ellipse.h:118
typename ellipse_type::real_type real_type
Definition: ellipse.h:115
geometry(std::size_t const num_vertices=32)
Definition: ellipse.h:25
static auto get() -> auto &
Definition: ellipse.h:21
auto update(auto const, auto &ell, camera auto const &cam)
Definition: ellipse.h:82
typename Ellipse::real_type real_type
Definition: ellipse.h:19
auto properties(renderable_type const &)
Definition: ellipse.h:57
Ellipse renderable_type
Definition: ellipse.h:18
renderer(renderable_type const &)
Definition: ellipse.h:51
static auto set_projection_matrix(Mat4< GLfloat > const &P)
Definition: ellipse.h:53
static auto construct_model_matrix(Mat2< real_type > const &S, Vec2< real_type > const &center)
Definition: ellipse.h:63
auto set_model_view_matrix(Mat4< GLfloat > const &MV) -> void
Definition: shaders.h:89
auto set_color(GLfloat r, GLfloat g, GLfloat b, GLfloat a=1) -> void
Definition: shaders.h:85
Definition: vec.h:12