10 use glium::{DisplayBuild, Surface};
11 use glium::index::{IndexBuffer, PrimitiveType};
12 use glutin::ElementState::Pressed;
13 use glutin::Event::KeyboardInput;
14 use glutin::VirtualKeyCode;
16 use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired};
22 #[derive(Copy, Clone)]
33 fn mand(cx: f32, cy: f32) -> [f32; 3] {
35 let mut iter = maxiter;
42 return [iter as f32 / maxiter as f32, 1.0, 1.0];
44 zy = zx * zy * 2.0 + cy;
53 fn mandelwow_program(display: &glium::Display) -> glium::Program {
54 return program!(display,
58 uniform mat4 perspective;
67 mat4 modelview = view * model;
68 gl_Position = perspective * modelview * vec4(position, 1.0);
69 c = vec2(position.x, position.y);
76 precision highp float;
89 if (zx2 * zy2 > 4.0) {
90 float index = 1.0 - float(iter) / float(maxiter);
91 f_color = vec4(index, index * 0.5, index, index * 0.5);
94 zy = zx * zy * 2.0 + c.y;
98 f_color = vec4((sin(z.y) + 1.0) / 2,
100 (sin(c.x) + 1.0) / 2,
107 fn solid_fill_program(display: &glium::Display) -> glium::Program {
108 let vertex_shader_src = r#"
111 uniform mat4 perspective;
116 mat4 modelview = view * model;
117 gl_Position = perspective * modelview * vec4(position, 1.0);
121 let fragment_shader_src = r#"
127 color = vec4(1.0, 1.0, 1.0, 1.0);
131 return glium::Program::from_source(display,
137 fn bounding_box<U>(display: &glium::Display,
138 frame: &mut glium::Frame,
139 program: &glium::Program,
141 cube: &Cube) where U: glium::uniforms::Uniforms {
143 #[derive(Copy, Clone)]
144 struct Vertex { position: [f32; 3] }
145 implement_vertex!(Vertex, position);
148 Vertex { position: [cube.xmin, cube.ymin, cube.zmin] },
149 Vertex { position: [cube.xmax, cube.ymin, cube.zmin] },
150 Vertex { position: [cube.xmax, cube.ymax, cube.zmin] },
151 Vertex { position: [cube.xmin, cube.ymax, cube.zmin] },
152 Vertex { position: [cube.xmin, cube.ymin, cube.zmax] },
153 Vertex { position: [cube.xmax, cube.ymin, cube.zmax] },
154 Vertex { position: [cube.xmax, cube.ymax, cube.zmax] },
155 Vertex { position: [cube.xmin, cube.ymax, cube.zmax] },
157 let vb = glium::VertexBuffer::new(display, &cube).unwrap();
159 let params = glium::DrawParameters {
160 depth: glium::Depth {
161 test: glium::draw_parameters::DepthTest::IfLess,
163 .. Default::default()
165 blend: glium::Blend::alpha_blending(),
166 .. Default::default()
169 let front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
170 &[0, 1, 2, 3u16]).unwrap();
171 frame.draw(&vb, &front_indices, program, uniforms, ¶ms).unwrap();
173 let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
174 &[4, 5, 6, 7u16]).unwrap();
175 frame.draw(&vb, &back_indices, program, uniforms, ¶ms).unwrap();
177 let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
178 &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
179 frame.draw(&vb, &sides_indices, program, uniforms, ¶ms).unwrap();
182 fn mandel<U>(display: &glium::Display,
183 frame: &mut glium::Frame,
184 program: &glium::Program,
187 z: [f32; 2]) where U: glium::uniforms::Uniforms {
189 #[derive(Copy, Clone)]
194 implement_vertex!(Vertex, position, color);
196 let xmin = bounds.xmin;
197 let xmax = bounds.xmax;
198 let ymin = bounds.ymin;
199 let ymax = bounds.ymax;
201 let width = xmax - xmin;
202 let height = ymax - ymin;
205 let xstep = width / (xres as f32);
206 let ystep = height / (yres as f32);
207 let vb_size = (xres * 2 + 4) * yres;
208 let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
209 v.resize(vb_size, Vertex { position: [0.0, 0.0, -1.0], color: [0.0, 0.0, 0.0] });
210 let mut i: usize = 0;
215 let c = [0.0, 0.0, 1.0];
216 v[i] = Vertex { position: [vx, vy+ystep, vz], color: c }; i += 1;
217 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
219 //let c = mand(vx, vy);
220 v[i] = Vertex { position: [vx+xstep, vy+ystep, vz], color: c }; i += 1;
221 v[i] = Vertex { position: [vx+xstep, vy, vz], color: c }; i += 1;
224 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
225 v[i] = Vertex { position: [xmin, vy, vz], color: c }; i += 1;
229 //let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
230 let vb = glium::VertexBuffer::new(display, &v).unwrap();
232 let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
233 //let indices = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
234 //let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
235 // &[0u16, 1, 2]).unwrap();
237 let params = glium::DrawParameters {
238 depth: glium::Depth {
239 test: glium::draw_parameters::DepthTest::IfLess,
241 .. Default::default()
243 blend: glium::Blend::alpha_blending(),
244 .. Default::default()
247 frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap();
250 fn mandelwow(display: &glium::Display,
251 mut frame: &mut glium::Frame,
252 program: &glium::Program,
253 model: [[f32; 4]; 4],
254 camera: &support::camera::CameraState,
257 let mut z0 = [mandel_w, 0f32];
259 let zmin = bounds.zmin;
260 let zmax = bounds.zmax;
261 let zstep = (zmax - zmin) / zres as f32;
267 let uniforms = uniform! {
270 view: camera.get_view(),
271 perspective: camera.get_perspective(),
274 mandel(&display, &mut frame, &program, &uniforms, bounds, z0);
282 impl AudioCallback for XmCallback {
285 fn callback(&mut self, out: &mut [f32]) {
286 self.xm.generate_samples(out);
291 _device: AudioDevice<XmCallback>,
294 fn play_xm(raw_xm: &[u8]) -> SoundPlayer {
295 let sdl_context = sdl2::init().unwrap();
296 let sdl_audio = sdl_context.audio().unwrap();
298 let sample_rate = 48000u32;
299 let desired_spec = AudioSpecDesired {
300 freq: Some(sample_rate as i32),
304 let device = sdl_audio.open_playback(None, &desired_spec, |actual_spec| {
305 let xm = XMContext::new(&raw_xm, actual_spec.freq as u32).unwrap();
320 let mut xm = Vec::new();
321 File::open("flora.xm").unwrap().read_to_end(&mut xm).unwrap();
322 let _sound_player = play_xm(&xm);
324 let display = glium::glutin::WindowBuilder::new()
325 //.with_dimensions(1024, 768)
326 .with_fullscreen(glutin::get_primary_monitor())
327 .with_depth_buffer(24)
329 .with_title(format!("MandelWow"))
333 let program = mandelwow_program(&display);
334 let bounding_box_program = solid_fill_program(&display);
336 let mut camera = support::camera::CameraState::new();
337 let mut t: f32 = 0.0;
338 let mut pause = false;
339 let mut bounding_box_enabled = true;
340 let mut fullscreen = true;
342 support::start_loop(|| {
350 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
360 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
363 let wsize = wmax - wmin;
364 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
366 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
368 let mut frame = display.draw();
369 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
371 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
373 [ t.cos(), t.sin(), 0.0, 0.0],
374 [-t.sin(), t.cos(), 0.0, 0.0],
375 [ 0.0, 0.0, 1.0, 0.0],
376 [ 0.0, 0.0, z_trans, 1.0f32]
379 // Draw the bounding box before the fractal, when the Z-buffer is still clear, so the lines
380 // behind the semi-translucent areas will be drawn.
381 if bounding_box_enabled {
382 let uniforms = uniform! {
384 view: camera.get_view(),
385 perspective: camera.get_perspective(),
387 bounding_box(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
390 mandelwow(&display, &mut frame, &program, model, &camera, &bounds, wow);
391 frame.finish().unwrap();
393 for ev in display.poll_events() {
395 glium::glutin::Event::Closed |
396 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
397 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
398 return support::Action::Stop
400 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
401 bounding_box_enabled ^= true;
403 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F)) => {
406 glutin::WindowBuilder::new()
407 .with_fullscreen(glutin::get_primary_monitor())
408 .rebuild_glium(&display).unwrap();
410 glutin::WindowBuilder::new()
411 .rebuild_glium(&display).unwrap();
414 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
417 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
420 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
423 ev => camera.process_input(&ev),
427 support::Action::Continue