12 use glium::{DisplayBuild, Surface};
13 use glutin::ElementState::Pressed;
14 use glutin::Event::KeyboardInput;
15 use glutin::VirtualKeyCode;
23 fn screenshot(display : &glium::Display) {
24 let image: glium::texture::RawImage2d<u8> = display.read_front_buffer();
25 let image = image::ImageBuffer::from_raw(image.width, image.height, image.data.into_owned()).unwrap();
26 let image = image::DynamicImage::ImageRgba8(image).flipv();
27 let mut output = std::fs::File::create(&std::path::Path::new("screenshot.png")).unwrap();
28 image.save(&mut output, image::ImageFormat::PNG).unwrap();
32 let _soundplayer = sound::start();
34 let display = glium::glutin::WindowBuilder::new()
35 //.with_dimensions(1024, 768)
36 .with_fullscreen(glutin::get_primary_monitor())
37 .with_depth_buffer(24)
39 .with_title(format!("MandelWow"))
43 let mandelwow_program = mandelwow::program(&display);
44 let bounding_box_program = bounding_box::solid_fill_program(&display);
46 let mut camera = support::camera::CameraState::new();
48 let mut pause = false;
49 let mut bounding_box_enabled = true;
50 let mut fullscreen = true;
52 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
62 support::start_loop(|| {
70 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
73 let wsize = wmax - wmin;
74 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
76 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
78 let mut frame = display.draw();
79 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
81 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
83 [ t.cos(), t.sin(), 0.0, 0.0],
84 [-t.sin(), t.cos(), 0.0, 0.0],
85 [ 0.0, 0.0, 1.0, 0.0],
86 [ 0.0, 0.0, z_trans, 1.0f32]
89 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
90 // so the lines behind the semi-translucent areas will be drawn.
91 if bounding_box_enabled {
92 let uniforms = uniform! {
94 view: camera.get_view(),
95 perspective: camera.get_perspective(),
97 bounding_box::draw(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
100 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
101 frame.finish().unwrap();
103 for ev in display.poll_events() {
105 glium::glutin::Event::Closed |
106 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
107 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
108 return support::Action::Stop
110 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
111 bounding_box_enabled ^= true;
113 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
116 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
119 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
122 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F10)) => {
123 screenshot(&display);
125 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F11)) => {
128 // Not implemented on Linux
129 glutin::WindowBuilder::new()
130 .with_fullscreen(glutin::get_primary_monitor())
131 .with_depth_buffer(24)
132 .rebuild_glium(&display).unwrap();
134 //glutin::WindowBuilder::new()
135 // .rebuild_glium(&display).unwrap();
138 ev => camera.process_input(&ev),
142 support::Action::Continue