1 use cgmath::conv::array4x4;
2 use cgmath::{Euler, Matrix4, Rad, SquareMatrix, Vector3, Vector4, Zero};
3 use glium::glutin::event::{ self, Event, VirtualKeyCode, WindowEvent };
4 use glium::glutin::event_loop::{ ControlFlow };
5 use glium::{Display, Program, Surface, uniform};
7 use std::f32::consts::PI;
9 use std::time::{Duration, Instant};
11 #[cfg(target_os = "emscripten")]
12 use std::os::raw::{c_int, c_void};
14 fn gl_info(display: &glium::Display) {
15 if cfg!(feature = "logging") {
16 let version = *display.get_opengl_version();
17 let api = match version {
18 glium::Version(glium::Api::Gl, _, _) => "OpenGL",
19 glium::Version(glium::Api::GlEs, _, _) => "OpenGL ES",
22 "{} context verson: {}",
24 display.get_opengl_version_string()
29 const SEA_XSIZE: usize = 40;
30 const SEA_ZSIZE: usize = 25;
33 mandelwow_program: Rc<Program>,
34 mandelwow_bounds: Cube,
35 mandelwow_bbox: BoundingBox,
36 bounding_box_enabled: bool,
38 shaded_cube: ShadedCube,
41 sea: [[Vector3<f32>; SEA_ZSIZE]; SEA_XSIZE],
43 // For the zoom animation synchronized to the drum-hits
49 pub fn new(display: &glium::Display) -> World {
50 let mandelwow_program = Rc::new(mandelwow::program(display));
51 let bounding_box_program = Rc::new(bounding_box::solid_fill_program(display));
52 let shaded_program = Rc::new(shaded_cube::shaded_program(display));
54 // These are the bounds for the 3D slice of the 4D Mandelwow
55 let mandelwow_bounds = Cube {
64 // Generate a wavy sea made of cubes
65 let sea_xmin = -20.0f32;
66 let sea_xmax = 20.0f32;
68 let sea_zmin = -2.0f32;
69 let sea_zmax = -27.0f32;
70 let sea_xstep = (sea_xmax - sea_xmin) / (SEA_XSIZE as f32);
71 let sea_zstep = (sea_zmax - sea_zmin) / (SEA_ZSIZE as f32);
72 println!("xstep={} ystep={:?}", sea_xstep, sea_zstep);
74 let mut sea = [[Vector3::zero(); SEA_ZSIZE]; SEA_XSIZE];
75 for x in 0..SEA_XSIZE {
76 for z in 0..SEA_ZSIZE {
78 x: sea_xmin + (x as f32) * sea_xstep,
80 z: sea_zmin + (z as f32) * sea_zstep,
87 mandelwow_bbox: BoundingBox::new(
88 display, &mandelwow_bounds, bounding_box_program.clone()),
90 bounding_box_enabled: true,
92 shaded_cube: ShadedCube::new(display, shaded_program.clone()),
93 text: text::Text::new(display),
104 camera: &support::camera::CameraState,
107 let perspview = camera.get_perspview();
109 let hit_delta = t - self.hit_time;
110 let hit_scale = 1. / (1. + hit_delta * hit_delta * 15.0) + 1.;
112 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
115 let wsize = wmax - wmin;
116 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
118 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
120 let mut frame = display.draw();
121 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
123 let rotation = Matrix4::from(Euler {
124 x: Rad(t.sin() / 3.),
125 y: Rad(t.sin() / 2.),
128 let z_trans = -3.0; // Send the model back a little bit so it fits the screen.
129 let scale = Matrix4::from_diagonal(Vector4::new(hit_scale, hit_scale, hit_scale, 1.0));
130 let model2 = Matrix4::from_translation(Vector3::unit_z() * z_trans) * rotation * scale;
131 let model = array4x4(model2);
133 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
134 // so the lines behind the semi-translucent areas will be drawn.
135 if self.bounding_box_enabled {
136 let uniforms = uniform! {
138 view: camera.get_view(),
139 perspective: camera.get_perspective(),
141 self.mandelwow_bbox.draw(&mut frame, &uniforms);
144 let text_rot = Matrix4::from_angle_x(cgmath::Deg(-90.0f32));
145 let text_pos = Matrix4::from_translation(Vector3 {
150 for x in 0..SEA_XSIZE {
151 for z in 0..SEA_ZSIZE {
152 let wave = ((x as f32 / SEA_XSIZE as f32 * PI * 5.0 + t * 2.0).sin()
153 + (z as f32 / SEA_ZSIZE as f32 * PI * 3.0 + t * 3.0).sin())
155 let model = Matrix4::from_translation(
163 let uniforms = uniform! {
164 model: array4x4(model),
165 perspview: perspview,
166 col: [0., (1. - wave).abs() * 0.5, wave.abs()],
168 self.shaded_cube.draw(&mut frame, &uniforms);
169 let model = model * text_pos;
170 let c = (x + z * SEA_XSIZE) as u8 as char;
171 self.text.draw(&mut frame, c, &model, &perspview);
178 &self.mandelwow_program,
181 &self.mandelwow_bounds,
185 frame.finish().unwrap();
190 #[cfg(target_os = "emscripten")]
191 #[allow(non_camel_case_types)]
192 type em_callback_func = unsafe extern "C" fn();
193 #[cfg(target_os = "emscripten")]
195 fn emscripten_set_main_loop(func: em_callback_func, fps: c_int, simulate_infinite_loop: c_int);
198 #[cfg(target_os = "emscripten")]
199 thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> =
200 std::cell::RefCell::new(std::ptr::null_mut()));
202 #[cfg(target_os = "emscripten")]
203 pub fn set_main_loop_callback<F>(callback: F)
205 F: FnMut() -> support::Action,
207 MAIN_LOOP_CALLBACK.with(|log| {
208 *log.borrow_mut() = &callback as *const _ as *mut c_void;
212 emscripten_set_main_loop(wrapper::<F>, 0, 1);
215 unsafe extern "C" fn wrapper<F>()
217 F: FnMut() -> support::Action,
219 MAIN_LOOP_CALLBACK.with(|z| {
220 let closure = *z.borrow_mut() as *mut F;
226 #[cfg(not(target_os = "emscripten"))]
227 pub fn set_main_loop_callback<F>(callback: F)
229 F: FnMut() -> support::Action,
231 support::start_loop(callback);
234 //extern crate gleam;
238 fn emscripten_GetProcAddress(
239 name: *const ::std::os::raw::c_char,
240 ) -> *const ::std::os::raw::c_void;
246 let gl = gleam::gl::GlesFns::load_with(|addr| {
247 let addr = std::ffi::CString::new(addr).unwrap();
248 emscripten_GetProcAddress(addr.into_raw() as *const _) as *const _
250 gl.glGetInternalformativ(0, 0, 0, 0, 0);
253 let mut soundplayer = sound::start();
255 let event_loop = glutin::event_loop::EventLoop::new();
256 //let fullscreen = Some(glutin::window::Fullscreen::Borderless(event_loop.primary_monitor()));
257 let window = glutin::window::WindowBuilder::new()
258 //.with_dimensions(1280, 720)
259 //.with_fullscreen(fullscreen);
261 //.with_title("MandelWow");
262 let context = glutin::ContextBuilder::new()
263 //.with_gl_profile(glutin::GlProfile::Core)
264 //.with_gl(glutin::GlRequest::Specific(glutin::Api::WebGl, (2, 0)))
265 .with_gl(glutin::GlRequest::Specific(
266 glutin::Api::OpenGlEs,
269 //.with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (4, 0)))
270 //.with_depth_buffer(24)
273 let display = glium::Display::new(window, context, &event_loop).unwrap();
275 let mut world = World::new(&display);
277 let mut timer = Timer::new();
278 let mut camera = support::camera::CameraState::new();
279 let _fullscreen = true;
281 event_loop.run(move |event, _, control_flow| {
283 let new_hit = sound::hit_event(&mut soundplayer);
284 if new_hit > world.last_hit {
287 world.last_hit = new_hit;
291 *control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::from_nanos(16666667));
293 Event::NewEvents(cause) => {
295 event::StartCause::ResumeTimeReached { .. } | event::StartCause::Init => {
296 world.draw_frame(&display, &camera, t);
302 if let Event::WindowEvent { event, .. } = event {
303 camera.process_input(&event);
305 WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
306 WindowEvent::KeyboardInput { input, .. } => {
307 if input.state == event::ElementState::Pressed {
308 if let Some(key) = input.virtual_keycode {
310 VirtualKeyCode::Escape | VirtualKeyCode::Q => {
311 *control_flow = ControlFlow::Exit;
319 KeyboardInput { input: glutin::KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::Escape), .. } } |
320 KeyboardInput { input: glutin::KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::Q), .. } } => {
321 *control_flow = ControlFlow::Exit;
323 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::B) } => {
324 bounding_box_enabled ^= true;
326 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::P) } => {
329 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::PageUp) } => {
332 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::PageDown) } => {
335 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::F10) } => {
336 screenshot(&display);
338 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::F11) } => {
341 // Not implemented on Linux
342 glutin::WindowBuilder::new()
343 .with_fullscreen(glutin::get_primary_monitor())
344 .with_depth_buffer(24)
345 .rebuild_glium(&display).unwrap();
347 glutin::WindowBuilder::new()
348 .rebuild_glium(&display).unwrap();