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;
16 use std::os::raw::{c_int, c_void};
24 fn screenshot(display : &glium::Display) {
25 let image: glium::texture::RawImage2d<u8> = display.read_front_buffer();
26 let image = image::ImageBuffer::from_raw(image.width, image.height, image.data.into_owned()).unwrap();
27 let image = image::DynamicImage::ImageRgba8(image).flipv();
28 let mut output = std::fs::File::create(&std::path::Path::new("screenshot.png")).unwrap();
29 image.save(&mut output, image::ImageFormat::PNG).unwrap();
32 fn gl_info(display : &glium::Display) {
33 let version = *display.get_opengl_version();
34 let api = match version {
35 glium::Version(glium::Api::Gl, _, _) => "OpenGL",
36 glium::Version(glium::Api::GlEs, _, _) => "OpenGL ES"
38 println!("{} context verson: {}", api, display.get_opengl_version_string());
41 #[cfg(target_os = "emscripten")]
42 #[allow(non_camel_case_types)]
43 type em_callback_func = unsafe extern fn();
44 #[cfg(target_os = "emscripten")]
46 fn emscripten_set_main_loop(func : em_callback_func, fps : c_int, simulate_infinite_loop : c_int);
49 #[cfg(target_os = "emscripten")]
50 thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> =
51 std::cell::RefCell::new(std::ptr::null_mut()));
53 #[cfg(target_os = "emscripten")]
54 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
55 MAIN_LOOP_CALLBACK.with(|log| {
56 *log.borrow_mut() = &callback as *const _ as *mut c_void;
59 unsafe { emscripten_set_main_loop(wrapper::<F>, 0, 1); }
61 unsafe extern "C" fn wrapper<F>() where F : FnMut() -> support::Action {
62 MAIN_LOOP_CALLBACK.with(|z| {
63 let closure = *z.borrow_mut() as *mut F;
69 #[cfg(not(target_os = "emscripten"))]
70 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() -> support::Action {
71 support::start_loop(callback);
75 let _soundplayer = sound::start();
77 let display = glutin::WindowBuilder::new()
78 .with_dimensions(600, 600)
79 //.with_fullscreen(glutin::get_primary_monitor())
80 .with_depth_buffer(24)
82 .with_title(format!("MandelWow"))
88 let mandelwow_program = mandelwow::program(&display);
89 let bounding_box_program = bounding_box::solid_fill_program(&display);
91 let mut camera = support::camera::CameraState::new();
93 let mut pause = false;
94 let mut bounding_box_enabled = true;
95 let mut fullscreen = true;
97 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
107 set_main_loop_callback(|| {
115 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
118 let wsize = wmax - wmin;
119 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
121 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
123 let mut frame = display.draw();
124 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
126 let rotation = cgmath::Matrix4::from(
127 Euler { x: Rad(t.sin() / 3.), y: Rad(t.sin() / 2.), z: Rad(t)});
128 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
130 Matrix4::from_translation(Vector3::unit_z() * z_trans) * rotation;
131 let model = cgmath::conv::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 bounding_box_enabled {
136 let uniforms = uniform! {
138 view: camera.get_view(),
139 perspective: camera.get_perspective(),
141 bounding_box::draw(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
144 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
145 frame.finish().unwrap();
147 for ev in display.poll_events() {
149 glutin::Event::Closed |
150 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
151 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
152 return support::Action::Stop
154 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
155 bounding_box_enabled ^= true;
157 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
160 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
163 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
166 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F10)) => {
167 screenshot(&display);
169 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F11)) => {
172 // Not implemented on Linux
173 glutin::WindowBuilder::new()
174 .with_fullscreen(glutin::get_primary_monitor())
175 .with_depth_buffer(24)
176 .rebuild_glium(&display).unwrap();
178 //glutin::WindowBuilder::new()
179 // .rebuild_glium(&display).unwrap();
182 ev => camera.process_input(&ev),
186 support::Action::Continue