10 use glium::{DisplayBuild, Surface};
11 use glium::index::{IndexBuffer, PrimitiveType};
12 use glutin::ElementState::Pressed;
13 use glutin::Event::KeyboardInput;
14 use glutin::VirtualKeyCode;
19 #[derive(Copy, Clone)]
30 fn mand(cx: f32, cy: f32) -> [f32; 3] {
32 let mut iter = maxiter;
39 return [iter as f32 / maxiter as f32, 1.0, 1.0];
41 zy = zx * zy * 2.0 + cy;
50 fn mandelwow_program(display: &glium::Display) -> glium::Program {
51 return program!(display,
55 uniform mat4 perspective;
64 mat4 modelview = view * model;
65 gl_Position = perspective * modelview * vec4(position, 1.0);
66 c = vec2(position.x, position.y);
73 precision highp float;
86 if (zx2 * zy2 > 4.0) {
87 float index = 1.0 - float(iter) / float(maxiter);
88 f_color = vec4(index, index * 0.5, index, index * 0.5);
91 zy = zx * zy * 2.0 + c.y;
95 f_color = vec4((sin(z.y) + 1.0) / 2,
104 fn solid_fill_program(display: &glium::Display) -> glium::Program {
105 let vertex_shader_src = r#"
108 uniform mat4 perspective;
113 mat4 modelview = view * model;
114 gl_Position = perspective * modelview * vec4(position, 1.0);
118 let fragment_shader_src = r#"
124 color = vec4(1.0, 1.0, 1.0, 1.0);
128 return glium::Program::from_source(display,
134 fn bounding_box<U>(display: &glium::Display,
135 frame: &mut glium::Frame,
136 program: &glium::Program,
138 cube: &Cube) where U: glium::uniforms::Uniforms {
140 #[derive(Copy, Clone)]
141 struct Vertex { position: [f32; 3] }
142 implement_vertex!(Vertex, position);
145 Vertex { position: [cube.xmin, cube.ymin, cube.zmin] },
146 Vertex { position: [cube.xmax, cube.ymin, cube.zmin] },
147 Vertex { position: [cube.xmax, cube.ymax, cube.zmin] },
148 Vertex { position: [cube.xmin, cube.ymax, cube.zmin] },
149 Vertex { position: [cube.xmin, cube.ymin, cube.zmax] },
150 Vertex { position: [cube.xmax, cube.ymin, cube.zmax] },
151 Vertex { position: [cube.xmax, cube.ymax, cube.zmax] },
152 Vertex { position: [cube.xmin, cube.ymax, cube.zmax] },
154 let vb = glium::VertexBuffer::new(display, &cube).unwrap();
156 let params = glium::DrawParameters {
157 depth: glium::Depth {
158 test: glium::draw_parameters::DepthTest::IfLess,
162 blend: glium::Blend::alpha_blending(),
166 let front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
167 &[0, 1, 2, 3u16]).unwrap();
168 frame.draw(&vb, &front_indices, program, uniforms, ¶ms).unwrap();
170 let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
171 &[4, 5, 6, 7u16]).unwrap();
172 frame.draw(&vb, &back_indices, program, uniforms, ¶ms).unwrap();
174 let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
175 &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
176 frame.draw(&vb, &sides_indices, program, uniforms, ¶ms).unwrap();
179 fn mandel<U>(display: &glium::Display,
180 frame: &mut glium::Frame,
181 program: &glium::Program,
184 z: [f32; 2]) where U: glium::uniforms::Uniforms {
186 #[derive(Copy, Clone)]
191 implement_vertex!(Vertex, position, color);
193 let xmin = bounds.xmin;
194 let xmax = bounds.xmax;
195 let ymin = bounds.ymin;
196 let ymax = bounds.ymax;
198 let width = xmax - xmin;
199 let height = ymax - ymin;
202 let xstep = width / (xres as f32);
203 let ystep = height / (yres as f32);
204 let vb_size = (xres * 2 + 4) * yres;
205 let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
206 v.resize(vb_size, Vertex { position: [0.0, 0.0, -1.0], color: [0.0, 0.0, 0.0] });
207 let mut i: usize = 0;
212 let c = [0.0, 0.0, 1.0];
213 v[i] = Vertex { position: [vx, vy+ystep, vz], color: c }; i += 1;
214 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
216 //let c = mand(vx, vy);
217 v[i] = Vertex { position: [vx+xstep, vy+ystep, vz], color: c }; i += 1;
218 v[i] = Vertex { position: [vx+xstep, vy, vz], color: c }; i += 1;
221 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
222 v[i] = Vertex { position: [xmin, vy, vz], color: c }; i += 1;
226 //let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
227 let vb = glium::VertexBuffer::new(display, &v).unwrap();
229 let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
230 //let indices = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
231 //let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
232 // &[0u16, 1, 2]).unwrap();
234 let params = glium::DrawParameters {
235 depth: glium::Depth {
236 test: glium::draw_parameters::DepthTest::IfLess,
240 blend: glium::Blend::alpha_blending(),
244 frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap();
247 fn mandelwow(display: &glium::Display,
248 mut frame: &mut glium::Frame,
249 program: &glium::Program,
250 model: [[f32; 4]; 4],
251 camera: &support::camera::CameraState,
254 let mut z0 = [mandel_w, 0f32];
256 let zmin = bounds.zmin;
257 let zmax = bounds.zmax;
258 let zstep = (zmax - zmin) / zres as f32;
264 let uniforms = uniform! {
267 view: camera.get_view(),
268 perspective: camera.get_perspective(),
271 mandel(&display, &mut frame, &program, &uniforms, bounds, z0);
278 let display = glium::glutin::WindowBuilder::new()
279 //.with_dimensions(1024, 768)
280 .with_fullscreen(glutin::get_primary_monitor())
281 .with_depth_buffer(24)
283 .with_title(format!("MandelWow"))
287 let program = mandelwow_program(&display);
288 let bounding_box_program = solid_fill_program(&display);
290 let mut camera = support::camera::CameraState::new();
291 let mut t: f32 = 0.0;
292 let mut pause = false;
293 let mut bounding_box_enabled = true;
294 let mut fullscreen = true;
296 support::start_loop(|| {
304 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
314 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
317 let wsize = wmax - wmin;
318 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
320 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
322 let mut frame = display.draw();
323 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
325 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
327 [ t.cos(), t.sin(), 0.0, 0.0],
328 [-t.sin(), t.cos(), 0.0, 0.0],
329 [ 0.0, 0.0, 1.0, 0.0],
330 [ 0.0, 0.0, z_trans, 1.0f32]
333 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
334 // so the lines behind the semi-translucent areas will be drawn.
335 if bounding_box_enabled {
336 let uniforms = uniform! {
338 view: camera.get_view(),
339 perspective: camera.get_perspective(),
341 bounding_box(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
344 mandelwow(&display, &mut frame, &program, model, &camera, &bounds, wow);
345 frame.finish().unwrap();
347 for ev in display.poll_events() {
349 glium::glutin::Event::Closed |
350 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
351 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
352 return support::Action::Stop
354 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
355 bounding_box_enabled ^= true;
357 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F)) => {
360 glutin::WindowBuilder::new()
361 .with_fullscreen(glutin::get_primary_monitor())
362 .rebuild_glium(&display).unwrap();
364 glutin::WindowBuilder::new()
365 .rebuild_glium(&display).unwrap();
368 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
371 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
374 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
377 ev => camera.process_input(&ev),
381 support::Action::Continue