shade 0.0.5

Another graphics library
Documentation
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::mem;
use std::ffi::CString;
use std::num::NonZeroU32;

use glutin::prelude::*;
use shade::cvmath::*;

const FRAGMENT_SHADER: &str = r#"
#version 330

out vec4 o_fragColor;

in vec2 v_pos;

uniform sampler2D u_gradient;

float mandelbrot(vec2 c)
{
	const int maxIter = 100;
	vec2 z = vec2(0.0);
	int iter = 0;

	for (; iter < maxIter; ++iter) {
		if (dot(z, z) > 4.0) break;
		z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;
	}

	return float(iter) / float(maxIter);

	// if (iter == maxIter) {
	// 	// Inside the set
	// 	return 1.0;
	// }

	// // Smooth escape time coloring
	// float zn = length(z);
	// float log_zn = log(zn * zn) / 2.0;
	// float nu = log(log_zn / log(2.0)) / log(2.0);
	// float escape = float(iter) + 1.0 - nu;

	// // Normalize and compress to [0,1)
	// float normalized = log(escape) / log(1.3);
	// return fract(normalized);
}

void main() {
	float s = mandelbrot(v_pos);
	o_fragColor = texture(u_gradient, vec2(s, 0.0));
}
"#;

const VERTEX_SHADER: &str = r#"
#version 330 core

in vec2 a_pos;

out vec2 v_pos;

uniform mat3x2 u_transform;

void main() {
	v_pos = u_transform * vec3(a_pos, 1.0);
	gl_Position = vec4(a_pos, 0.0, 1.0);
}
"#;

//----------------------------------------------------------------

#[derive(Copy, Clone, Default, dataview::Pod)]
#[repr(C)]
struct Vertex {
	position: Vec2f,
}

unsafe impl shade::TVertex for Vertex {
	const LAYOUT: &'static shade::VertexLayout = &shade::VertexLayout {
		size: mem::size_of::<Vertex>() as u16,
		alignment: mem::align_of::<Vertex>() as u16,
		attributes: &[
			shade::VertexAttribute {
				name: "a_pos",
				format: shade::VertexAttributeFormat::F32v2,
				offset: dataview::offset_of!(Vertex.position) as u16,
			},
		],
	};
}

static VERTICES: [Vertex; 6] = [
	Vertex { position: Vec2f(-1.0, -1.0) },
	Vertex { position: Vec2f( 1.0, -1.0) },
	Vertex { position: Vec2f( 1.0,  1.0) },
	Vertex { position: Vec2f(-1.0, -1.0) },
	Vertex { position: Vec2f( 1.0,  1.0) },
	Vertex { position: Vec2f(-1.0,  1.0) },
];

struct Uniforms {
	transform: Transform2f,
	gradient: shade::Texture2D,
}

impl shade::UniformVisitor for Uniforms {
	fn visit(&self, set: &mut dyn shade::UniformSetter) {
		set.value("u_transform", &self.transform);
		set.value("u_gradient", &self.gradient);
	}
}

//----------------------------------------------------------------

#[derive(Clone, Debug)]
struct ZoomView {
	// Complex plane center
	center: Vec2f,
	// Height of the view
	height: f32,
}

static DEFAULT_VIEW: ZoomView = ZoomView {
	center: Vec2f::new(-0.5, 0.0),
	height: 2.0,
};

impl ZoomView {
	fn to_bounds(&self, aspect_ratio: f32) -> Bounds2f {
		let width = self.height * aspect_ratio;
		Bounds2::c(
			self.center.x - width / 2.0,
			self.center.y - self.height / 2.0,
			self.center.x + width / 2.0,
			self.center.y + self.height / 2.0,
		)
	}
}

#[derive(Default)]
struct ZoomViewStack {
	views: Vec<ZoomView>,
}

impl ZoomViewStack {
	fn current(&self) -> &ZoomView {
		self.views.last().unwrap_or(&DEFAULT_VIEW)
	}

	fn zoom(&mut self, pt: Vec2f, screen_size: Vec2f, factor: f32) {
		let current = self.current();

		let pt_frac = (pt - screen_size * 0.5) / screen_size.shuffle(Y, Y);
		let clicked_point = current.center + pt_frac * current.height;

		let center = clicked_point.lerp(current.center, factor);
		let height = current.height * factor;

		let next = ZoomView { center, height };
		self.views.push(next);
	}

	fn pan(&mut self, delta: Vec2f, screen_size: Vec2f) {
		// Pan start clones the current view so that we can modify it
		// This keeps the previous views intact for going back
		let Some(current) = self.views.last_mut() else { return };
		let delta_complex = delta / screen_size.shuffle(Y, Y) * current.height;
		current.center -= delta_complex;
	}

	fn back(&mut self) {
		self.views.pop();
	}
}

/// OpenGL Window wrapper.
struct GlWindow {
	size: winit::dpi::PhysicalSize<u32>,
	window: winit::window::Window,
	surface: glutin::surface::Surface<glutin::surface::WindowSurface>,
	context: glutin::context::PossiblyCurrentContext,
}

impl GlWindow {
	fn new(
		event_loop: &winit::event_loop::ActiveEventLoop,
		size: winit::dpi::PhysicalSize<u32>,
	) -> GlWindow {
		use glutin::config::ConfigTemplateBuilder;
		use glutin::context::{ContextApi, ContextAttributesBuilder, Version};
		use glutin::display::GetGlDisplay;
		use glutin::surface::{SurfaceAttributesBuilder, WindowSurface};
		use raw_window_handle::HasWindowHandle;

		let template_builder = ConfigTemplateBuilder::new()
			.with_alpha_size(8)
			.with_multisampling(4);

		let window_attributes = winit::window::WindowAttributes::default()
			.with_inner_size(size);

		let config_picker = |configs: Box<dyn Iterator<Item = glutin::config::Config> + '_>| {
			configs
				.filter(|c| c.srgb_capable())
				.max_by_key(|c| c.num_samples())
				.expect("No GL configs found")
		};
		let (window, gl_config) = glutin_winit::DisplayBuilder::new()
			.with_window_attributes(Some(window_attributes))
			.build(event_loop, template_builder, config_picker)
			.expect("Failed DisplayBuilder.build");

		let window = window.expect("DisplayBuilder did not build a Window");
		let raw_window_handle = window
			.window_handle()
			.expect("Failed Window.window_handle")
			.as_raw();

		let context_attributes = ContextAttributesBuilder::new()
			.with_context_api(ContextApi::OpenGl(Some(Version::new(3, 3))))
			.build(Some(raw_window_handle));

		let gl_display = gl_config.display();

		let not_current = unsafe { gl_display.create_context(&gl_config, &context_attributes) }
			.expect("Failed Display.create_context");

		let surface_attributes_builder = SurfaceAttributesBuilder::<WindowSurface>::new()
			.with_srgb(Some(true));
		let surface_attributes = surface_attributes_builder.build(
			raw_window_handle,
			NonZeroU32::new(size.width.max(1)).unwrap(),
			NonZeroU32::new(size.height.max(1)).unwrap(),
		);

		let surface = unsafe { gl_display.create_window_surface(&gl_config, &surface_attributes) }
			.expect("Failed Display.create_window_surface");

		let context = not_current.make_current(&surface)
			.expect("Failed NotCurrentContext.make_current");

		shade::gl::capi::load_with(|s| {
			let c = CString::new(s).unwrap();
			gl_display.get_proc_address(&c)
		});

		GlWindow { size, window, surface, context }
	}

	fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
		let width = NonZeroU32::new(new_size.width.max(1)).unwrap();
		let height = NonZeroU32::new(new_size.height.max(1)).unwrap();
		self.size = new_size;
		self.surface.resize(&self.context, width, height);
	}
}

struct MandelbrotDemo {
	vertices: shade::VertexBuffer,
	shader: shade::ShaderProgram,
	gradient: shade::Texture2D,
	pan_start: Point2f,
	panning: bool,
	cursor: Point2f,
	stack: ZoomViewStack,
}

impl MandelbrotDemo {
	fn new(g: &mut shade::Graphics) -> MandelbrotDemo {
		let vertices = g.vertex_buffer(&VERTICES, shade::BufferUsage::Static);
		let shader = g.shader_compile(VERTEX_SHADER, FRAGMENT_SHADER);
		let gradient = {
			let gradient = shade::image::DecodedImage::load_file_png("examples/mandelbrot/gradient.png").unwrap();
			g.image(&gradient)
		};

		MandelbrotDemo {
			vertices,
			shader,
			gradient,
			pan_start: Point2f::ZERO,
			panning: false,
			cursor: Point2f::ZERO,
			stack: ZoomViewStack::default(),
		}
	}

	fn draw(&mut self, g: &mut shade::Graphics, viewport: Bounds2i) {
		g.begin(&shade::BeginArgs::BackBuffer { viewport });

		shade::clear!(g, color: Vec4(0.2, 0.5, 0.2, 1.0));

		let aspect_ratio = viewport.width() as f32 / viewport.height() as f32;
		let zoom_view = self.stack.current();
		let view_bounds = zoom_view.to_bounds(aspect_ratio);
		let transform = Transform2f::ortho(view_bounds).inverse();

		let uniforms = Uniforms { transform, gradient: self.gradient };

		g.draw(&shade::DrawArgs {
			scissor: None,
			blend_mode: shade::BlendMode::Solid,
			depth_test: None,
			cull_mode: None,
			mask: shade::DrawMask::COLOR,
			prim_type: shade::PrimType::Triangles,
			shader: self.shader,
			vertices: &[shade::DrawVertexBuffer {
				buffer: self.vertices,
				divisor: shade::VertexDivisor::PerVertex,
			}],
			uniforms: &[&uniforms],
			vertex_start: 0,
			vertex_end: 6,
			instances: -1,
		});

		g.end();
	}
}

struct App {
	window: GlWindow,
	opengl: shade::gl::GlGraphics,
	demo: MandelbrotDemo,
}

impl App {
	fn new(event_loop: &winit::event_loop::ActiveEventLoop, size: winit::dpi::PhysicalSize<u32>) -> Box<App> {
		let window = GlWindow::new(event_loop, size);
		let mut opengl = shade::gl::GlGraphics::new(shade::gl::GlConfig { srgb: true });
		let demo = MandelbrotDemo::new(opengl.as_graphics());
		Box::new(App { window, opengl, demo })
	}
	fn draw(&mut self) {
		let viewport = Bounds2::c(0, 0, self.window.size.width as i32, self.window.size.height as i32);
		self.demo.draw(self.opengl.as_graphics(), viewport);
	}
}

fn main() {
	let event_loop = winit::event_loop::EventLoop::new().expect("Failed to create event loop");
	let size = winit::dpi::PhysicalSize::new(800, 600);

	let mut app: Option<Box<App>> = None;

	#[allow(deprecated)]
	let _ = event_loop.run(move |event, event_loop| {
		use winit::event::{ElementState, Event, MouseButton, WindowEvent};

		match event {
			Event::Resumed => {
				if app.is_none() {
					app = Some(App::new(event_loop, size));
				}
			}
			Event::WindowEvent { event, .. } => match event {
				WindowEvent::Resized(new_size) => {
					if let Some(app) = app.as_deref_mut() {
						app.window.resize(new_size);
					}
				}
				WindowEvent::CursorMoved { position, .. } => {
					if let Some(app) = app.as_deref_mut() {
						app.demo.cursor.x = position.x as f32;
						app.demo.cursor.y = position.y as f32;
						if app.demo.panning {
							let size_vec = Vec2::new(app.window.size.width as f32, app.window.size.height as f32);
							app.demo.stack.pan(app.demo.cursor - app.demo.pan_start, size_vec);
							app.demo.pan_start = app.demo.cursor;
							app.window.window.request_redraw();
						}
					}
				}
				WindowEvent::MouseInput { state, button, .. } => {
					if let Some(app) = app.as_deref_mut() {
						match button {
							MouseButton::Left => {
								if matches!(state, ElementState::Pressed) {
									let size_vec = Vec2::new(app.window.size.width as f32, app.window.size.height as f32);
									app.demo.stack.zoom(app.demo.cursor, size_vec, 0.5);
									app.window.window.request_redraw();
								}
							}
							MouseButton::Right => {
								if matches!(state, ElementState::Pressed) {
									app.demo.pan_start = app.demo.cursor;
									app.demo.panning = true;
									app.demo.stack.views.push(app.demo.stack.current().clone());
								} else {
									app.demo.panning = false;
								}
							}
							MouseButton::Middle => {
								if matches!(state, ElementState::Pressed) {
									app.demo.stack.back();
									app.window.window.request_redraw();
								}
							}
							_ => {}
						}
					}
				}
				WindowEvent::CloseRequested => event_loop.exit(),
				WindowEvent::RedrawRequested => {
					if let Some(app) = app.as_deref_mut() {
						app.draw();
						app.window.surface.swap_buffers(&app.window.context).unwrap();
					}
				}
				_ => {}
			},
			_ => {}
		}
	});
}