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