1 extern crate mandelwow_lib;
8 use cgmath::{Euler, Matrix4, Rad, SquareMatrix, Vector3, Vector4, Zero};
9 use cgmath::conv::array4x4;
10 use glium::{DisplayBuild, Surface};
11 use glutin::ElementState::Pressed;
12 use glutin::Event::KeyboardInput;
13 use glutin::VirtualKeyCode;
15 use std::f32::consts::PI;
16 use std::time::{Duration, Instant};
18 #[cfg(target_os = "emscripten")]
19 use std::os::raw::{c_int, c_void};
21 fn gl_info(display : &glium::Display) {
22 let version = *display.get_opengl_version();
23 let api = match version {
24 glium::Version(glium::Api::Gl, _, _) => "OpenGL",
25 glium::Version(glium::Api::GlEs, _, _) => "OpenGL ES"
27 println!("{} context verson: {}", api, display.get_opengl_version_string());
30 #[cfg(target_os = "emscripten")]
31 #[allow(non_camel_case_types)]
32 type em_callback_func = unsafe extern fn();
33 #[cfg(target_os = "emscripten")]
35 fn emscripten_set_main_loop(func : em_callback_func, fps : c_int, simulate_infinite_loop : c_int);
38 #[cfg(target_os = "emscripten")]
39 thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> =
40 std::cell::RefCell::new(std::ptr::null_mut()));
42 #[cfg(target_os = "emscripten")]
43 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
44 MAIN_LOOP_CALLBACK.with(|log| {
45 *log.borrow_mut() = &callback as *const _ as *mut c_void;
48 unsafe { emscripten_set_main_loop(wrapper::<F>, 0, 1); }
50 unsafe extern "C" fn wrapper<F>() where F : FnMut() -> support::Action {
51 MAIN_LOOP_CALLBACK.with(|z| {
52 let closure = *z.borrow_mut() as *mut F;
58 #[cfg(not(target_os = "emscripten"))]
59 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
60 support::start_loop(callback);
64 let mut soundplayer = sound::start();
66 let display = glutin::WindowBuilder::new()
67 .with_dimensions(1280, 720)
68 .with_gl_profile(glutin::GlProfile::Core)
69 //.with_fullscreen(glutin::get_primary_monitor())
70 .with_depth_buffer(24)
72 .with_title(format!("MandelWow"))
78 let text = text::Text::new(&display);
79 let mandelwow_program = mandelwow::program(&display);
80 let bounding_box_program = bounding_box::solid_fill_program(&display);
81 let shaded_program = shaded_cube::shaded_program(&display);
83 let mut camera = support::camera::CameraState::new();
85 let mut pause = false;
86 let mut bounding_box_enabled = true;
87 let mut fullscreen = true;
89 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
98 let mandelwow_bbox = bounding_box::BoundingBox::new(&display, &bounds, &bounding_box_program);
99 let shaded_cube = ShadedCube::new(&display, &Cube::default(), &shaded_program);
101 const SEA_XSIZE: usize = 24;
102 const SEA_ZSIZE: usize = 20;
103 let sea_xmin = -14.0f32;
104 let sea_xmax = 14.0f32;
106 let sea_zmin = -2.0f32;
107 let sea_zmax = -26.0f32;
108 let sea_xstep = (sea_xmax - sea_xmin) / (SEA_XSIZE as f32);
109 let sea_zstep = (sea_zmax - sea_zmin) / (SEA_ZSIZE as f32);
111 let mut sea = [[Vector3::zero(); SEA_ZSIZE]; SEA_XSIZE];
112 for x in 0..SEA_XSIZE {
113 for z in 0..SEA_ZSIZE {
114 sea[x][z] = Vector3 {
115 x: sea_xmin + (x as f32) * sea_xstep,
117 z: sea_zmin + (z as f32) * sea_zstep,
122 let mut frame_cnt = 0;
123 let mut last_report_time = Instant::now();
124 let mut last_report_frame_cnt = 0;
125 let mut accum_draw_time = Duration::new(0, 0);
126 let mut accum_idle_time = Duration::new(0, 0);
128 let mut last_hit = 0.0f32;
129 let mut hit_time = 0.0f32;
130 set_main_loop_callback(|| {
131 let new_hit = sound::hit_event(&mut soundplayer);
132 if new_hit != last_hit {
136 let hit_delta = t - hit_time;
137 let hit_scale = 1. / (1. + hit_delta * hit_delta * 15.0) + 1.;
140 let perspview = camera.get_perspview();
142 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
145 let wsize = wmax - wmin;
146 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
148 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
150 let time_before_swap = Instant::now();
151 let mut frame = display.draw();
152 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
153 let time_before_draw = Instant::now();
155 let rotation = Matrix4::from(
156 Euler { x: Rad(t.sin() / 3.), y: Rad(t.sin() / 2.), z: Rad(t / 1.5)});
157 let z_trans = -3.0; // Send the model back a little bit so it fits the screen.
159 Matrix4::from_diagonal(Vector4::new(hit_scale, hit_scale, hit_scale, 1.0));
161 Matrix4::from_translation(Vector3::unit_z() * z_trans) * rotation * scale;
162 let model = array4x4(model2);
164 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
165 // so the lines behind the semi-translucent areas will be drawn.
166 if bounding_box_enabled {
167 let uniforms = uniform! {
169 view: camera.get_view(),
170 perspective: camera.get_perspective(),
172 mandelwow_bbox.draw(&mut frame, &uniforms);
175 for x in 0..SEA_XSIZE {
176 for z in 0..SEA_ZSIZE {
177 let wave = (((x as f32 / SEA_XSIZE as f32 * PI * 5.0 + t * 2.0)).sin() +
178 ((z as f32 / SEA_ZSIZE as f32 * PI * 3.0 + t * 3.0)).sin()) * 0.3;
179 let model = Matrix4::from_translation(sea[x][z] + Vector3 {x: 0., y: wave, z: 0.});
180 let uniforms = uniform! {
181 model: array4x4(model),
182 perspview: perspview,
183 col: [0., (1. - wave).abs() * 0.5, wave.abs()],
185 shaded_cube.draw(&mut frame, &uniforms);
189 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
191 text.draw(&mut frame, &perspview);
193 frame.finish().unwrap();
194 let time_after_draw = Instant::now();
196 for ev in display.poll_events() {
198 glutin::Event::Closed |
199 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
200 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
201 return support::Action::Stop
203 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
204 bounding_box_enabled ^= true;
206 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
209 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
212 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
215 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F10)) => {
216 screenshot(&display);
218 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F11)) => {
221 // Not implemented on Linux
222 glutin::WindowBuilder::new()
223 .with_fullscreen(glutin::get_primary_monitor())
224 .with_depth_buffer(24)
225 .rebuild_glium(&display).unwrap();
227 //glutin::WindowBuilder::new()
228 // .rebuild_glium(&display).unwrap();
231 ev => camera.process_input(&ev),
235 let now = Instant::now();
237 accum_idle_time += time_before_draw - time_before_swap;
238 accum_draw_time += time_after_draw - time_before_draw;
239 if now - last_report_time > Duration::from_secs(5) {
240 fn millis(d : Duration) -> f32 {
241 d.as_secs() as f32 * 1e3 + d.subsec_nanos() as f32 / 1e6
243 let frames_done = frame_cnt - last_report_frame_cnt;
244 let fps = frames_done as f32 / (now - last_report_time).as_secs() as f32;
245 let avg_draw_time = millis(accum_draw_time / frames_done);
246 let avg_idle_time = millis(accum_idle_time / frames_done);
247 println!("fps={:.1} draw={:.1}ms idle={:.1}ms", fps, avg_draw_time, avg_idle_time);
249 last_report_time = now;
250 last_report_frame_cnt = frame_cnt;
251 accum_draw_time = Duration::new(0, 0);
252 accum_idle_time = Duration::new(0, 0);
260 support::Action::Continue