1 extern crate mandelwow_lib;
8 use cgmath::{Euler, Matrix4, Rad, SquareMatrix, Vector3, Vector4, Zero};
9 use cgmath::conv::array4x4;
11 use glium::glutin::WindowEvent::KeyboardInput;
12 use glium::glutin::VirtualKeyCode;
14 use std::f32::consts::PI;
17 #[cfg(target_os = "emscripten")]
18 use std::os::raw::{c_int, c_void};
20 fn gl_info(display : &glium::Display) {
21 if cfg!(feature = "logging") {
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());
31 #[cfg(target_os = "emscripten")]
32 #[allow(non_camel_case_types)]
33 type em_callback_func = unsafe extern fn();
34 #[cfg(target_os = "emscripten")]
36 fn emscripten_set_main_loop(func : em_callback_func, fps : c_int, simulate_infinite_loop : c_int);
39 #[cfg(target_os = "emscripten")]
40 thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> =
41 std::cell::RefCell::new(std::ptr::null_mut()));
43 #[cfg(target_os = "emscripten")]
44 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
45 MAIN_LOOP_CALLBACK.with(|log| {
46 *log.borrow_mut() = &callback as *const _ as *mut c_void;
49 unsafe { emscripten_set_main_loop(wrapper::<F>, 0, 1); }
51 unsafe extern "C" fn wrapper<F>() where F : FnMut() -> support::Action {
52 MAIN_LOOP_CALLBACK.with(|z| {
53 let closure = *z.borrow_mut() as *mut F;
59 #[cfg(not(target_os = "emscripten"))]
60 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
61 support::start_loop(callback);
65 let mut soundplayer = sound::start();
67 let mut events_loop = glium::glutin::EventsLoop::new();
68 let window = glium::glutin::WindowBuilder::new()
69 //.with_dimensions(1280, 720)
70 .with_fullscreen(Some(events_loop.get_primary_monitor()));
71 //.with_title("MandelWow");
72 let context = glium::glutin::ContextBuilder::new()
73 //.with_gl_profile(glutin::GlProfile::Core)
74 //.with_gl(glutin::GlRequest::Specific(glutin::Api::WebGl, (2, 0)))
75 //.with_depth_buffer(24)
77 let display = glium::Display::new(window, context, &events_loop).unwrap();
80 // let mut text = text::Text::new(&display, 'A');
81 let mandelwow_program = mandelwow::program(&display);
82 let bounding_box_program = bounding_box::solid_fill_program(&display);
83 let shaded_program = shaded_cube::shaded_program(&display);
85 let mut timer = Timer::new();
86 let mut camera = support::camera::CameraState::new();
87 let mut bounding_box_enabled = true;
88 let mut fullscreen = true;
90 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
99 let mandelwow_bbox = bounding_box::BoundingBox::new(&display, &bounds, &bounding_box_program);
100 let shaded_cube = ShadedCube::new(&display, &shaded_program);
102 const SEA_XSIZE: usize = 40;
103 const SEA_ZSIZE: usize = 25;
104 let sea_xmin = -20.0f32;
105 let sea_xmax = 20.0f32;
107 let sea_zmin = -2.0f32;
108 let sea_zmax = -27.0f32;
109 let sea_xstep = (sea_xmax - sea_xmin) / (SEA_XSIZE as f32);
110 let sea_zstep = (sea_zmax - sea_zmin) / (SEA_ZSIZE as f32);
111 println!("xstep={} ystep={:?}", sea_xstep, sea_zstep);
113 let mut sea = [[Vector3::zero(); SEA_ZSIZE]; SEA_XSIZE];
114 for x in 0..SEA_XSIZE {
115 for z in 0..SEA_ZSIZE {
116 sea[x][z] = Vector3 {
117 x: sea_xmin + (x as f32) * sea_xstep,
119 z: sea_zmin + (z as f32) * sea_zstep,
124 let mut last_hit = 0.0f32;
125 let mut hit_time = 0.0f32;
126 set_main_loop_callback(|| {
128 let new_hit = sound::hit_event(&mut soundplayer);
129 if new_hit > last_hit {
133 let hit_delta = t - hit_time;
134 let hit_scale = 1. / (1. + hit_delta * hit_delta * 15.0) + 1.;
137 let perspview = camera.get_perspview();
139 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
142 let wsize = wmax - wmin;
143 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
145 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
147 let mut frame = display.draw();
148 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
150 let rotation = Matrix4::from(
151 Euler { x: Rad(t.sin() / 3.), y: Rad(t.sin() / 2.), z: Rad(t / 1.5)});
152 let z_trans = -3.0; // Send the model back a little bit so it fits the screen.
154 Matrix4::from_diagonal(Vector4::new(hit_scale, hit_scale, hit_scale, 1.0));
156 Matrix4::from_translation(Vector3::unit_z() * z_trans) * rotation * scale;
157 let model = array4x4(model2);
159 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
160 // so the lines behind the semi-translucent areas will be drawn.
161 if bounding_box_enabled {
162 let uniforms = uniform! {
164 view: camera.get_view(),
165 perspective: camera.get_perspective(),
167 mandelwow_bbox.draw(&mut frame, &uniforms);
170 let text_rot = Matrix4::from_angle_x(cgmath::Deg(-90.0f32));
171 let text_pos = Matrix4::from_translation(Vector3 { x: 0.0, y: 0.501, z: 0.0f32}) * text_rot;
172 for x in 0..SEA_XSIZE {
173 for z in 0..SEA_ZSIZE {
174 let wave = ((x as f32 / SEA_XSIZE as f32 * PI * 5.0 + t * 2.0).sin() +
175 (z as f32 / SEA_ZSIZE as f32 * PI * 3.0 + t * 3.0).sin()) * 0.3;
176 let model = Matrix4::from_translation(
177 sea[x][z] + Vector3 {x: 0., y: wave, z: 0.});
178 let uniforms = uniform! {
179 model: array4x4(model),
180 perspview: perspview,
181 col: [0., (1. - wave).abs() * 0.5, wave.abs()],
183 shaded_cube.draw(&mut frame, &uniforms);
184 /* text.model = model * text_pos;
185 text.character = (x + z * SEA_XSIZE) as u8 as char;
186 text.draw(&mut frame, &perspview);
190 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
192 frame.finish().unwrap();
194 let mut action = support::Action::Continue;
195 events_loop.poll_events(|event| {
196 if let glium::glutin::Event::WindowEvent { event, .. } = event {
197 camera.process_input(&event);
199 glium::glutin::WindowEvent::CloseRequested => {
200 action = support::Action::Stop
202 KeyboardInput { input, .. } => {
203 if input.state == glium::glutin::ElementState::Pressed {
204 if let Some(key) = input.virtual_keycode {
206 VirtualKeyCode::Escape | VirtualKeyCode::Q => {
207 action = support::Action::Stop;
215 KeyboardInput { input: glutin::KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::Escape), .. } } |
216 KeyboardInput { input: glutin::KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::Q), .. } } => {
217 return support::Action::Stop
219 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::B) } => {
220 bounding_box_enabled ^= true;
222 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::P) } => {
225 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::PageUp) } => {
228 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::PageDown) } => {
231 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::F10) } => {
232 screenshot(&display);
234 KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::F11) } => {
237 // Not implemented on Linux
238 glutin::WindowBuilder::new()
239 .with_fullscreen(glutin::get_primary_monitor())
240 .with_depth_buffer(24)
241 .rebuild_glium(&display).unwrap();
243 glutin::WindowBuilder::new()
244 .rebuild_glium(&display).unwrap();