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;
18 #[cfg(target_os = "emscripten")]
19 use std::os::raw::{c_int, c_void};
21 fn gl_info(display : &glium::Display) {
22 if cfg!(feature = "logging") {
23 let version = *display.get_opengl_version();
24 let api = match version {
25 glium::Version(glium::Api::Gl, _, _) => "OpenGL",
26 glium::Version(glium::Api::GlEs, _, _) => "OpenGL ES"
28 println!("{} context verson: {}", api, display.get_opengl_version_string());
32 #[cfg(target_os = "emscripten")]
33 #[allow(non_camel_case_types)]
34 type em_callback_func = unsafe extern fn();
35 #[cfg(target_os = "emscripten")]
37 fn emscripten_set_main_loop(func : em_callback_func, fps : c_int, simulate_infinite_loop : c_int);
40 #[cfg(target_os = "emscripten")]
41 thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> =
42 std::cell::RefCell::new(std::ptr::null_mut()));
44 #[cfg(target_os = "emscripten")]
45 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
46 MAIN_LOOP_CALLBACK.with(|log| {
47 *log.borrow_mut() = &callback as *const _ as *mut c_void;
50 unsafe { emscripten_set_main_loop(wrapper::<F>, 0, 1); }
52 unsafe extern "C" fn wrapper<F>() where F : FnMut() -> support::Action {
53 MAIN_LOOP_CALLBACK.with(|z| {
54 let closure = *z.borrow_mut() as *mut F;
60 #[cfg(not(target_os = "emscripten"))]
61 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
62 support::start_loop(callback);
66 let mut soundplayer = sound::start();
68 let display = glutin::WindowBuilder::new()
69 .with_dimensions(1280, 720)
70 .with_gl_profile(glutin::GlProfile::Core)
71 //.with_fullscreen(glutin::get_primary_monitor())
72 .with_depth_buffer(24)
74 .with_srgb(Some(true))
75 .with_title("MandelWow")
77 //.build_glium_debug(glium::debug::DebugCallbackBehavior::PrintAll)
82 let mut text = text::Text::new(&display, 'A');
83 let mandelwow_program = mandelwow::program(&display);
84 let bounding_box_program = bounding_box::solid_fill_program(&display);
85 let shaded_program = shaded_cube::shaded_program(&display);
87 let mut timer = Timer::new();
88 let mut camera = support::camera::CameraState::new();
89 let mut bounding_box_enabled = true;
90 let mut fullscreen = true;
92 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
101 let mandelwow_bbox = bounding_box::BoundingBox::new(&display, &bounds, &bounding_box_program);
102 let shaded_cube = ShadedCube::new(&display, &shaded_program);
104 const SEA_XSIZE: usize = 40;
105 const SEA_ZSIZE: usize = 25;
106 let sea_xmin = -20.0f32;
107 let sea_xmax = 20.0f32;
109 let sea_zmin = -2.0f32;
110 let sea_zmax = -27.0f32;
111 let sea_xstep = (sea_xmax - sea_xmin) / (SEA_XSIZE as f32);
112 let sea_zstep = (sea_zmax - sea_zmin) / (SEA_ZSIZE as f32);
113 println!("xstep={} ystep={:?}", sea_xstep, sea_zstep);
115 let mut sea = [[Vector3::zero(); SEA_ZSIZE]; SEA_XSIZE];
116 for x in 0..SEA_XSIZE {
117 for z in 0..SEA_ZSIZE {
118 sea[x][z] = Vector3 {
119 x: sea_xmin + (x as f32) * sea_xstep,
121 z: sea_zmin + (z as f32) * sea_zstep,
126 let mut last_hit = 0.0f32;
127 let mut hit_time = 0.0f32;
128 set_main_loop_callback(|| {
130 let new_hit = sound::hit_event(&mut soundplayer);
131 if new_hit > last_hit {
135 let hit_delta = t - hit_time;
136 let hit_scale = 1. / (1. + hit_delta * hit_delta * 15.0) + 1.;
139 let perspview = camera.get_perspview();
141 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
144 let wsize = wmax - wmin;
145 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
147 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
149 let mut frame = display.draw();
150 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
152 let rotation = Matrix4::from(
153 Euler { x: Rad(t.sin() / 3.), y: Rad(t.sin() / 2.), z: Rad(t / 1.5)});
154 let z_trans = -3.0; // Send the model back a little bit so it fits the screen.
156 Matrix4::from_diagonal(Vector4::new(hit_scale, hit_scale, hit_scale, 1.0));
158 Matrix4::from_translation(Vector3::unit_z() * z_trans) * rotation * scale;
159 let model = array4x4(model2);
161 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
162 // so the lines behind the semi-translucent areas will be drawn.
163 if bounding_box_enabled {
164 let uniforms = uniform! {
166 view: camera.get_view(),
167 perspective: camera.get_perspective(),
169 mandelwow_bbox.draw(&mut frame, &uniforms);
172 let text_rot = Matrix4::from_angle_x(cgmath::Deg(-90.0f32));
173 let text_pos = Matrix4::from_translation(Vector3 { x: 0.0, y: 0.501, z: 0.0f32}) * text_rot;
174 for x in 0..SEA_XSIZE {
175 for z in 0..SEA_ZSIZE {
176 let wave = ((x as f32 / SEA_XSIZE as f32 * PI * 5.0 + t * 2.0).sin() +
177 (z as f32 / SEA_ZSIZE as f32 * PI * 3.0 + t * 3.0).sin()) * 0.3;
178 let model = Matrix4::from_translation(
179 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);
186 text.model = model * text_pos;
187 text.character = (x + z * SEA_XSIZE) as u8 as char;
188 text.draw(&mut frame, &perspview);
192 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
194 frame.finish().unwrap();
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),
237 support::Action::Continue