5507f4a7fc09b01340f33f223c754e05ba57c79c
[mandelwow.git] / main.rs
1 // Wow. Such fractal.
2
3 #[macro_use]
4
5 extern crate glium;
6 extern crate glutin;
7 extern crate image;
8 extern crate libxm;
9 extern crate sdl2;
10
11 use cube::Cube;
12 use glium::{DisplayBuild, Surface};
13 use glutin::ElementState::Pressed;
14 use glutin::Event::KeyboardInput;
15 use glutin::VirtualKeyCode;
16
17 mod bounding_box;
18 mod cube;
19 mod mandelwow;
20 mod sound;
21 mod support;
22
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();
29 }
30
31 fn main() {
32     let _soundplayer = sound::start();
33
34     let display = glium::glutin::WindowBuilder::new()
35         //.with_dimensions(1024, 768)
36         .with_fullscreen(glutin::get_primary_monitor())
37         .with_depth_buffer(24)
38         .with_vsync()
39         .with_title(format!("MandelWow"))
40         .build_glium()
41         .unwrap();
42
43     let mandelwow_program = mandelwow::program(&display);
44     let bounding_box_program = bounding_box::solid_fill_program(&display);
45
46     let mut camera = support::camera::CameraState::new();
47     let mut t: f32 = 0.0;
48     let mut pause = false;
49     let mut bounding_box_enabled = true;
50     let mut fullscreen = true;
51
52     // These are the bounds of the 3D Mandelwow section which we render in 3-space.
53     let bounds = Cube {
54         xmin: -2.0,
55         xmax:  0.7,
56         ymin: -1.0,
57         ymax:  1.0,
58         zmin: -1.1,
59         zmax:  1.1,
60     };
61
62     support::start_loop(|| {
63         camera.update();
64
65         if !pause {
66             // Increment time
67             t += 0.01;
68         }
69
70         // Vary the wow factor to slice the Mandelwow along its 4th dimension.
71         let wmin = -0.8;
72         let wmax =  0.8;
73         let wsize = wmax - wmin;
74         let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
75
76         //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
77
78         let mut frame = display.draw();
79         frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
80
81         let z_trans = -2.0;  // Send the model back a little bit so it fits the screen.
82         let model = [
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]
87         ];
88
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! {
93                 model: model,
94                 view:  camera.get_view(),
95                 perspective: camera.get_perspective(),
96             };
97             bounding_box::draw(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
98         }
99
100         mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
101         frame.finish().unwrap();
102
103         for ev in display.poll_events() {
104             match ev {
105                 glium::glutin::Event::Closed |
106                 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
107                 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
108                     return support::Action::Stop
109                 },
110                 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
111                     bounding_box_enabled ^= true;
112                 },
113                 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
114                     pause ^= true;
115                 },
116                 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
117                     t += 0.01;
118                 },
119                 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
120                     t -= 0.01;
121                 },
122                 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F10)) => {
123                     screenshot(&display);
124                 },
125                 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F11)) => {
126                     fullscreen ^= true;
127                     if fullscreen {
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();
133                     } else {
134                         //glutin::WindowBuilder::new()
135                         //    .rebuild_glium(&display).unwrap();
136                     }
137                 },
138                 ev => camera.process_input(&ev),
139             }
140         }
141
142         support::Action::Continue
143     });
144
145 }