2 #[macro_use(uniform,program,implement_vertex)]
9 //use cgmath::prelude::*;
10 use cgmath::{Euler, Matrix4, Rad, Vector3};
12 use glium::{DisplayBuild, Surface};
13 use glutin::ElementState::Pressed;
14 use glutin::Event::KeyboardInput;
15 use glutin::VirtualKeyCode;
17 #[cfg(target_os = "emscripten")]
18 use std::os::raw::{c_int, c_void};
26 fn screenshot(display : &glium::Display) {
27 let image: glium::texture::RawImage2d<u8> = display.read_front_buffer();
28 let image = image::ImageBuffer::from_raw(image.width, image.height, image.data.into_owned()).unwrap();
29 let image = image::DynamicImage::ImageRgba8(image).flipv();
30 let mut output = std::fs::File::create(&std::path::Path::new("screenshot.png")).unwrap();
31 image.save(&mut output, image::ImageFormat::PNG).unwrap();
34 fn gl_info(display : &glium::Display) {
35 let version = *display.get_opengl_version();
36 let api = match version {
37 glium::Version(glium::Api::Gl, _, _) => "OpenGL",
38 glium::Version(glium::Api::GlEs, _, _) => "OpenGL ES"
40 println!("{} context verson: {}", api, display.get_opengl_version_string());
43 #[cfg(target_os = "emscripten")]
44 #[allow(non_camel_case_types)]
45 type em_callback_func = unsafe extern fn();
46 #[cfg(target_os = "emscripten")]
48 fn emscripten_set_main_loop(func : em_callback_func, fps : c_int, simulate_infinite_loop : c_int);
51 #[cfg(target_os = "emscripten")]
52 thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> =
53 std::cell::RefCell::new(std::ptr::null_mut()));
55 #[cfg(target_os = "emscripten")]
56 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
57 MAIN_LOOP_CALLBACK.with(|log| {
58 *log.borrow_mut() = &callback as *const _ as *mut c_void;
61 unsafe { emscripten_set_main_loop(wrapper::<F>, 0, 1); }
63 unsafe extern "C" fn wrapper<F>() where F : FnMut() -> support::Action {
64 MAIN_LOOP_CALLBACK.with(|z| {
65 let closure = *z.borrow_mut() as *mut F;
71 #[cfg(not(target_os = "emscripten"))]
72 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
73 support::start_loop(callback);
77 let _soundplayer = sound::start();
79 let display = glutin::WindowBuilder::new()
80 .with_dimensions(600, 600)
81 //.with_fullscreen(glutin::get_primary_monitor())
82 .with_depth_buffer(24)
84 .with_title(format!("MandelWow"))
90 let mandelwow_program = mandelwow::program(&display);
91 let bounding_box_program = bounding_box::solid_fill_program(&display);
93 let mut camera = support::camera::CameraState::new();
95 let mut pause = false;
96 let mut bounding_box_enabled = true;
97 let mut fullscreen = true;
99 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
109 set_main_loop_callback(|| {
117 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
120 let wsize = wmax - wmin;
121 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
123 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
125 let mut frame = display.draw();
126 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
128 let rotation = cgmath::Matrix4::from(
129 Euler { x: Rad(t.sin() / 3.), y: Rad(t.sin() / 2.), z: Rad(t)});
130 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
132 Matrix4::from_translation(Vector3::unit_z() * z_trans) * rotation;
133 let model = cgmath::conv::array4x4(model2);
135 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
136 // so the lines behind the semi-translucent areas will be drawn.
137 if bounding_box_enabled {
138 let uniforms = uniform! {
140 view: camera.get_view(),
141 perspective: camera.get_perspective(),
143 bounding_box::draw(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
146 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
147 frame.finish().unwrap();
149 for ev in display.poll_events() {
151 glutin::Event::Closed |
152 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
153 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
154 return support::Action::Stop
156 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
157 bounding_box_enabled ^= true;
159 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
162 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
165 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
168 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F10)) => {
169 screenshot(&display);
171 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F11)) => {
174 // Not implemented on Linux
175 glutin::WindowBuilder::new()
176 .with_fullscreen(glutin::get_primary_monitor())
177 .with_depth_buffer(24)
178 .rebuild_glium(&display).unwrap();
180 //glutin::WindowBuilder::new()
181 // .rebuild_glium(&display).unwrap();
184 ev => camera.process_input(&ev),
188 support::Action::Continue