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 #[allow(non_camel_case_types)]
42 type em_callback_func = unsafe extern fn();
44 fn emscripten_set_main_loop(func : em_callback_func, fps : c_int, simulate_infinite_loop : c_int);
47 thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> =
48 std::cell::RefCell::new(std::ptr::null_mut()));
50 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() {
51 MAIN_LOOP_CALLBACK.with(|log| {
52 *log.borrow_mut() = &callback as *const _ as *mut c_void;
55 unsafe { emscripten_set_main_loop(wrapper::<F>, 0, 1); }
57 unsafe extern "C" fn wrapper<F>() where F : FnMut() {
58 MAIN_LOOP_CALLBACK.with(|z| {
59 let closure = *z.borrow_mut() as *mut F;
66 let _soundplayer = sound::start();
68 let display = glutin::WindowBuilder::new()
69 .with_dimensions(600, 600)
70 //.with_fullscreen(glutin::get_primary_monitor())
71 .with_depth_buffer(24)
73 .with_title(format!("MandelWow"))
79 let mandelwow_program = mandelwow::program(&display);
80 let bounding_box_program = bounding_box::solid_fill_program(&display);
82 let mut camera = support::camera::CameraState::new();
84 let mut pause = false;
85 let mut bounding_box_enabled = true;
86 let mut fullscreen = true;
88 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
98 //support::start_loop(|| {
99 set_main_loop_callback(|| {
107 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
110 let wsize = wmax - wmin;
111 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
113 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
115 let mut frame = display.draw();
116 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
118 let rotation = cgmath::Matrix4::from(
119 Euler { x: Rad(t.sin() / 3.), y: Rad(t.sin() / 2.), z: Rad(t)});
120 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
122 Matrix4::from_translation(Vector3::unit_z() * z_trans) * rotation;
123 let model = cgmath::conv::array4x4(model2);
125 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
126 // so the lines behind the semi-translucent areas will be drawn.
127 if bounding_box_enabled {
128 let uniforms = uniform! {
130 view: camera.get_view(),
131 perspective: camera.get_perspective(),
133 bounding_box::draw(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
136 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
137 frame.finish().unwrap();
139 for ev in display.poll_events() {
141 glutin::Event::Closed |
142 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
143 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
144 //return support::Action::Stop
146 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
147 bounding_box_enabled ^= true;
149 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
152 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
155 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
158 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F10)) => {
159 screenshot(&display);
161 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F11)) => {
164 // Not implemented on Linux
165 glutin::WindowBuilder::new()
166 .with_fullscreen(glutin::get_primary_monitor())
167 .with_depth_buffer(24)
168 .rebuild_glium(&display).unwrap();
170 //glutin::WindowBuilder::new()
171 // .rebuild_glium(&display).unwrap();
174 ev => camera.process_input(&ev),
178 //support::Action::Continue