forked from igorbologovv/lodtree
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glium.rs
246 lines (213 loc) · 7.66 KB
/
glium.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* Generic tree structures for storage of spatial data.
* Copyright (C) 2023 Alexander Pyattaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use glium::glutin::event_loop::EventLoop;
use glium::index::PrimitiveType;
use glium::{
glutin, implement_vertex, program, uniform, Display, IndexBuffer, Program, Surface,
VertexBuffer,
};
use spatialtree::*;
// the chunk struct for the tree
#[allow(dead_code)]
struct Chunk {
visible: bool,
cache_state: i32,
// 0 is new, 1 is merged, 2 is cached, 3 is both
selected: bool,
in_bounds: bool,
}
fn make_shaders(display: &Display) -> Program {
program!(display,
140 => {
vertex: "
#version 140
uniform vec2 offset;
uniform float scale;
in vec2 position;
void main() {
vec2 local_position = position * scale + 0.005;
local_position.x = min(local_position.x, scale) - 0.0025;
local_position.y = min(local_position.y, scale) - 0.0025;
gl_Position = vec4(local_position + (offset + scale * 0.5) * 2.0 - 1.0, 0.0, 1.0);
}
",
fragment: "
#version 140
uniform int state;
uniform int selected;
uniform int in_bounds;
void main() {
if (state == 0) gl_FragColor = vec4(0.2, 0.2, 0.2, 1.0); // new, white
if (state == 1) gl_FragColor = vec4(0.0, 0.2, 0.0, 1.0); // merged, green
if (state == 2) gl_FragColor = vec4(0.2, 0.0, 0.0, 1.0); // from cache, red
if (state == 3) gl_FragColor = vec4(0.4, 0.2, 0.0, 1.0); // both, yellow
if (selected != 0) gl_FragColor = vec4(0.0, 0.2, 0.2, 1.0); // selected, so blue
if (in_bounds != 0) gl_FragColor = vec4(0.0, 0.2, 0.2, 1.0); // in bounds, so purple
}
"
}
)
.unwrap()
}
#[derive(Copy, Clone)]
struct Vertex {
// only need a 2d position
position: [f32; 2],
}
implement_vertex!(Vertex, position);
struct RenderContext {
display: Display,
vertex_buffer: VertexBuffer<Vertex>,
shaders: Program,
index_buffer: IndexBuffer<u16>,
}
impl RenderContext {
pub fn new(event_loop: &EventLoop<()>) -> Self {
let wb = glutin::window::WindowBuilder::new().with_title("Quadtree demo");
let cb = glutin::ContextBuilder::new().with_vsync(true);
let display = Display::new(wb, cb, event_loop).unwrap();
// make a vertex buffer
// we'll reuse it as we only need to draw one quad multiple times anyway
let vertex_buffer = {
VertexBuffer::new(
&display,
&[
Vertex {
position: [-1.0, -1.0],
},
Vertex {
position: [-1.0, 1.0],
},
Vertex {
position: [1.0, -1.0],
},
Vertex {
position: [1.0, 1.0],
},
],
)
.unwrap()
};
// and the index buffer to form the triangle
let index_buffer = IndexBuffer::new(
&display,
PrimitiveType::TrianglesList,
&[0_u16, 1, 2, 1, 2, 3],
)
.unwrap();
let shaders = make_shaders(&display);
Self {
display,
vertex_buffer,
index_buffer,
shaders,
}
}
}
fn draw(mouse_pos: (f32, f32), tree: &mut QuadTree<Chunk, QuadVec>, ctx: &RenderContext) {
//function for adding chunks to their respective position, and also set their properties
fn chunk_creator(_position: QuadVec) -> Chunk {
Chunk {
visible: true,
cache_state: 0,
selected: false,
in_bounds: false,
}
}
let qv = QuadVec::from_float_coords([mouse_pos.0, (1.0 - mouse_pos.1)], 6);
tree.lod_update(&[qv], 2, chunk_creator, |_, _| {});
// make sure there are no holes in chunks array for fast iteration
tree.defragment_chunks();
// and select the chunk at the mouse position
if let Some(chunk) = tree.get_chunk_by_position_mut(qv) {
chunk.selected = true;
}
// and select a number of chunks in a region when the mouse buttons are selected
// and, Redraw!
let mut target = ctx.display.draw();
target.clear_color(0.6, 0.6, 0.6, 1.0);
// go over all chunks, iterator version
for (_, container) in tree.iter_chunks() {
let (chunk, position) = (&container.chunk, container.position());
if chunk.visible {
// draw it if it's visible
// here we get the chunk position and size
let uniforms = uniform! {
offset: position.float_coords(),
scale: position.float_size(),
state: chunk.cache_state,
selected: chunk.selected as i32,
};
// draw it with glium
target
.draw(
&ctx.vertex_buffer,
&ctx.index_buffer,
&ctx.shaders,
&uniforms,
&Default::default(),
)
.unwrap();
}
}
target.finish().unwrap();
// deselect the chunk at the mouse position
if let Some(chunk) = tree.get_chunk_by_position_mut(qv) {
chunk.selected = false;
}
}
fn main() {
// set up the tree
let mut tree = QuadTree::<Chunk, QuadVec>::with_capacity(32, 32);
// start the glium event loop
let event_loop = glutin::event_loop::EventLoop::new();
let context = RenderContext::new(&event_loop);
draw((0.5, 0.5), &mut tree, &context);
// the mouse cursor position
let mut mouse_pos = (0.5, 0.5);
// last time redraw was done
let mut last_redraw = std::time::Instant::now();
// run the main loop
event_loop.run(move |event, _, control_flow| {
*control_flow = match event {
glutin::event::Event::RedrawRequested(_) => {
// and draw, if enough time elapses
if last_redraw.elapsed().as_millis() > 16 {
draw(mouse_pos, &mut tree, &context);
last_redraw = std::time::Instant::now();
}
glutin::event_loop::ControlFlow::Wait
}
glutin::event::Event::WindowEvent { event, .. } => match event {
// stop if the window is closed
glutin::event::WindowEvent::CloseRequested => glutin::event_loop::ControlFlow::Exit,
glutin::event::WindowEvent::CursorMoved { position, .. } => {
// get the mouse position
mouse_pos = (
position.x as f32 / context.display.get_framebuffer_dimensions().0 as f32,
position.y as f32 / context.display.get_framebuffer_dimensions().1 as f32,
);
// request a redraw
context.display.gl_window().window().request_redraw();
glutin::event_loop::ControlFlow::Wait
}
_ => glutin::event_loop::ControlFlow::Wait,
},
_ => glutin::event_loop::ControlFlow::Wait,
}
});
}