Tatooine
line.h
Go to the documentation of this file.
1#ifndef TATOOINE_RENDERING_LINE_H
2#define TATOOINE_RENDERING_LINE_H
3//==============================================================================
4#include <tatooine/line.h>
7#include <tatooine/vec.h>
10//==============================================================================
11namespace tatooine::rendering {
12//==============================================================================
15 add_stage<gl::vertexshader>(gl::shadersource{
16 "#version 330 core\n"
17 "layout(location = 0) in vec3 pos;\n"
18 "uniform mat4 projection_matrix;\n"
19 "uniform mat4 modelview_matrix;\n"
20 "//------------------------------------------------------------------\n"
21 "void main() {\n"
22 " gl_Position = projection_matrix * modelview_matrix * vec4(pos, 1);\n"
23 "}\n"});
24 add_stage<gl::fragmentshader>(gl::shadersource{
25 "#version 330 core\n"
26 "uniform vec3 color;\n"
27 "layout(location = 0) out vec4 fragout;\n"
28 "//------------------------------------------------------------------\n"
29 "void main() {\n"
30 " fragout = vec4(color, 1);\n"
31 "}\n"});
32 create();
33 }
35 set_uniform_mat4("projection_matrix", P.data());
36 }
37 auto set_modelview_matrix(mat4f const& MV) {
38 set_uniform_mat4("modelview_matrix", MV.data());
39 }
40 auto set_color(GLfloat const r, GLfloat const g, GLfloat const b) {
41 set_uniform("color", r, g, b);
42 }
43};
44template <typename Real>
45auto to_gpu(line<Real, 3> const& l) {
47 gpu_data.vertexbuffer().resize(l.vertices().size());
48 {
49 auto gpu_mapping = gpu_data.vertexbuffer().wmap();
50 for (auto const v : l.vertices()) {
51 auto const& x = l[v];
52 gpu_mapping[v.i] = x;
53 }
54 }
55 gpu_data.indexbuffer().resize((l.vertices().size() - 1) * 2);
56 {
57 auto gpu_mapping = gpu_data.indexbuffer().wmap();
58 for (size_t i = 0; i < l.vertices().size() - 1; ++i) {
59 gpu_mapping[i * 2] = i;
60 gpu_mapping[i * 2 + 1] = i + 1;
61 }
62 }
63 return gpu_data;
64}
65//------------------------------------------------------------------------------
66template <typename Real>
67auto to_gpu(std::vector<line<Real, 3>> const& lines) {
69 size_t num_vertices = 0;
70 size_t num_indices = 0;
71 for (auto const& l : lines) {
72 num_vertices += l.vertices().size();
73 num_indices += (l.vertices().size() - 1) * 2;
74 }
75 gpu_data.vertexbuffer().resize(num_vertices);
76 gpu_data.indexbuffer().resize(num_indices);
77 {
78 auto gpu_mapping = gpu_data.vertexbuffer().wmap();
79 size_t i = 0;
80 for (auto const& l : lines) {
81 for (auto const v : l.vertices()) {
82 auto const& x = l[v];
83 gpu_mapping[i++] = vec3f{x};
84 }
85 }
86 }
87 gpu_data.indexbuffer().resize(num_indices);
88 {
89 auto gpu_mapping = gpu_data.indexbuffer().wmap();
90 size_t i = 0;
91 size_t k = 0;
92 for (auto const& l : lines) {
93 for (size_t j = 0; j < l.vertices().size() - 1; ++i, ++j) {
94 gpu_mapping[k++] = i;
95 gpu_mapping[k++] = i + 1;
96 }
97 ++i;
98 }
99 }
100 return gpu_data;
101}
102//------------------------------------------------------------------------------
103template <typename Real, size_t N>
104auto interactive(std::vector<line<Real, N>> const& lines) {
105 auto win = first_person_window{};
106 auto shader = line_shader{};
107 shader.bind();
109 for (auto const& l : lines) {
110 for (auto const v : l.vertices()) {
111 aabb += l[v];
112 }
113 }
114 auto gpu_data = to_gpu(lines);
115 auto const center_pos = vec3f{aabb.center()};
116 win.camera_controller().look_at(center_pos + vec3f{2, 2, 2}, center_pos);
117 shader.bind();
118 shader.set_projection_matrix(win.camera_controller().projection_matrix());
119 gl::clear_color(255, 255, 255, 255);
120 vec3f col;
121 win.render_loop([&](auto const /*dt*/) {
123 shader.set_modelview_matrix(win.camera_controller().view_matrix());
124 shader.set_projection_matrix(win.camera_controller().projection_matrix());
125 if (ImGui::ColorEdit3("color", col.data())) {
126 shader.set_color(col(0), col(1), col(2));
127 }
128 gpu_data.draw_lines();
129 });
130}
131//==============================================================================
132template <typename Real, size_t N>
134 auto win = first_person_window{};
135 auto shader = line_shader{};
136 shader.bind();
138 for (auto const v : l.vertices()) {
139 aabb += l[v];
140 }
141 auto gpu_data = to_gpu(l);
142 auto const center_pos = aabb.center();
143 win.camera_controller().look_at(center_pos + vec{2, 2, 2}, center_pos);
144 shader.bind();
145 shader.set_projection_matrix(win.camera_controller().projection_matrix());
146 gl::clear_color(255, 255, 255, 255);
147 bool my_tool_active = true;
148 win.render_loop([&](auto const dt) {
150 shader.set_modelview_matrix(win.camera_controller().view_matrix());
151 shader.set_projection_matrix(win.camera_controller().projection_matrix());
152 gpu_data.draw_lines();
153 });
154}
155//==============================================================================
156} // namespace tatooine::rendering
157//==============================================================================
158#endif
Definition: indexeddata.h:13
auto indexbuffer() -> auto &
Definition: indexeddata.h:145
auto vertexbuffer() -> auto &
Definition: indexeddata.h:148
Definition: shader.h:24
DLL_API void set_uniform(const std::string &, GLfloat)
DLL_API void bind() const
DLL_API void create()
DLL_API void set_uniform_mat4(const std::string &, GLfloat const *)
constexpr auto data() -> ValueType *
Definition: static_multidim_array.h:260
DLL_API auto clear_color(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) -> void
DLL_API auto clear_color_depth_buffer() -> void
Definition: camera.h:12
auto interactive(std::vector< line< Real, N > > const &lines)
Definition: line.h:104
auto to_gpu(line< Real, 3 > const &l)
Definition: line.h:45
auto size(vec< ValueType, N > const &v)
Definition: vec.h:148
Definition: axis_aligned_bounding_box.h:103
auto constexpr center() const
Definition: axis_aligned_bounding_box.h:186
Definition: shadersource.h:8
Definition: line.h:35
auto vertices() const
Definition: line.h:250
Definition: mat.h:14
Definition: first_person_window.h:15
auto set_projection_matrix(mat4f const &P)
Definition: line.h:34
auto set_modelview_matrix(mat4f const &MV)
Definition: line.h:37
auto set_color(GLfloat const r, GLfloat const g, GLfloat const b)
Definition: line.h:40
line_shader()
Definition: line.h:14
Definition: vec.h:12