9 use glium::{DisplayBuild, Surface};
10 use glium::index::{IndexBuffer, PrimitiveType};
11 use glutin::ElementState::Pressed;
12 use glutin::Event::KeyboardInput;
13 use glutin::VirtualKeyCode;
21 #[derive(Copy, Clone)]
32 fn mand(cx: f32, cy: f32) -> [f32; 3] {
34 let mut iter = maxiter;
41 return [iter as f32 / maxiter as f32, 1.0, 1.0];
43 zy = zx * zy * 2.0 + cy;
52 fn mandelwow_program(display: &glium::Display) -> glium::Program {
53 return program!(display,
57 uniform mat4 perspective;
66 mat4 modelview = view * model;
67 gl_Position = perspective * modelview * vec4(position, 1.0);
68 c = vec2(position.x, position.y);
75 precision highp float;
88 if (zx2 * zy2 > 4.0) {
89 float index = 1.0 - float(iter) / float(maxiter);
90 f_color = vec4(index, index * 0.5, index, index * 0.5);
93 zy = zx * zy * 2.0 + c.y;
97 f_color = vec4((sin(z.y) + 1.0) / 2,
106 fn solid_fill_program(display: &glium::Display) -> glium::Program {
107 let vertex_shader_src = r#"
110 uniform mat4 perspective;
115 mat4 modelview = view * model;
116 gl_Position = perspective * modelview * vec4(position, 1.0);
120 let fragment_shader_src = r#"
126 color = vec4(1.0, 1.0, 1.0, 1.0);
130 return glium::Program::from_source(display,
136 fn bounding_box<U>(display: &glium::Display,
137 frame: &mut glium::Frame,
138 program: &glium::Program,
140 cube: &Cube) where U: glium::uniforms::Uniforms {
142 #[derive(Copy, Clone)]
143 struct Vertex { position: [f32; 3] }
144 implement_vertex!(Vertex, position);
147 Vertex { position: [cube.xmin, cube.ymin, cube.zmin] },
148 Vertex { position: [cube.xmax, cube.ymin, cube.zmin] },
149 Vertex { position: [cube.xmax, cube.ymax, cube.zmin] },
150 Vertex { position: [cube.xmin, cube.ymax, cube.zmin] },
151 Vertex { position: [cube.xmin, cube.ymin, cube.zmax] },
152 Vertex { position: [cube.xmax, cube.ymin, cube.zmax] },
153 Vertex { position: [cube.xmax, cube.ymax, cube.zmax] },
154 Vertex { position: [cube.xmin, cube.ymax, cube.zmax] },
156 let vb = glium::VertexBuffer::new(display, &cube).unwrap();
158 let params = glium::DrawParameters {
159 depth: glium::Depth {
160 test: glium::draw_parameters::DepthTest::IfLess,
162 .. Default::default()
164 blend: glium::Blend::alpha_blending(),
165 .. Default::default()
168 let front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
169 &[0, 1, 2, 3u16]).unwrap();
170 frame.draw(&vb, &front_indices, program, uniforms, ¶ms).unwrap();
172 let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
173 &[4, 5, 6, 7u16]).unwrap();
174 frame.draw(&vb, &back_indices, program, uniforms, ¶ms).unwrap();
176 let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
177 &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
178 frame.draw(&vb, &sides_indices, program, uniforms, ¶ms).unwrap();
181 fn mandel<U>(display: &glium::Display,
182 frame: &mut glium::Frame,
183 program: &glium::Program,
186 z: [f32; 2]) where U: glium::uniforms::Uniforms {
188 #[derive(Copy, Clone)]
193 implement_vertex!(Vertex, position, color);
195 let xmin = bounds.xmin;
196 let xmax = bounds.xmax;
197 let ymin = bounds.ymin;
198 let ymax = bounds.ymax;
200 let width = xmax - xmin;
201 let height = ymax - ymin;
204 let xstep = width / (xres as f32);
205 let ystep = height / (yres as f32);
206 let vb_size = (xres * 2 + 4) * yres;
207 let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
208 v.resize(vb_size, Vertex { position: [0.0, 0.0, -1.0], color: [0.0, 0.0, 0.0] });
209 let mut i: usize = 0;
214 let c = [0.0, 0.0, 1.0];
215 v[i] = Vertex { position: [vx, vy+ystep, vz], color: c }; i += 1;
216 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
218 //let c = mand(vx, vy);
219 v[i] = Vertex { position: [vx+xstep, vy+ystep, vz], color: c }; i += 1;
220 v[i] = Vertex { position: [vx+xstep, vy, vz], color: c }; i += 1;
223 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
224 v[i] = Vertex { position: [xmin, vy, vz], color: c }; i += 1;
228 //let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
229 let vb = glium::VertexBuffer::new(display, &v).unwrap();
231 let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
232 //let indices = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
233 //let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
234 // &[0u16, 1, 2]).unwrap();
236 let params = glium::DrawParameters {
237 depth: glium::Depth {
238 test: glium::draw_parameters::DepthTest::IfLess,
240 .. Default::default()
242 blend: glium::Blend::alpha_blending(),
243 .. Default::default()
246 frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap();
249 fn mandelwow(display: &glium::Display,
250 mut frame: &mut glium::Frame,
251 program: &glium::Program,
252 model: [[f32; 4]; 4],
253 camera: &support::camera::CameraState,
256 let mut z0 = [mandel_w, 0f32];
258 let zmin = bounds.zmin;
259 let zmax = bounds.zmax;
260 let zstep = (zmax - zmin) / zres as f32;
266 let uniforms = uniform! {
269 view: camera.get_view(),
270 perspective: camera.get_perspective(),
273 mandel(&display, &mut frame, &program, &uniforms, bounds, z0);
276 fn play_xm(raw_xm: &[u8]) {
278 let mut xm = XMContext::new(&raw_xm, freq).unwrap();
282 let mut xm = Vec::new();
283 File::open("flora.xm").unwrap().read_to_end(&mut xm).unwrap();
286 let display = glium::glutin::WindowBuilder::new()
287 //.with_dimensions(1024, 768)
288 .with_fullscreen(glutin::get_primary_monitor())
289 .with_depth_buffer(24)
291 .with_title(format!("MandelWow"))
295 let program = mandelwow_program(&display);
296 let bounding_box_program = solid_fill_program(&display);
298 let mut camera = support::camera::CameraState::new();
299 let mut t: f32 = 0.0;
300 let mut pause = false;
301 let mut bounding_box_enabled = true;
302 let mut fullscreen = true;
304 support::start_loop(|| {
312 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
322 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
325 let wsize = wmax - wmin;
326 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
328 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
330 let mut frame = display.draw();
331 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
333 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
335 [ t.cos(), t.sin(), 0.0, 0.0],
336 [-t.sin(), t.cos(), 0.0, 0.0],
337 [ 0.0, 0.0, 1.0, 0.0],
338 [ 0.0, 0.0, z_trans, 1.0f32]
341 // Draw the bounding box before the fractal, when the Z-buffer is still clear, so the lines
342 // behind the semi-translucent areas will be drawn.
343 if bounding_box_enabled {
344 let uniforms = uniform! {
346 view: camera.get_view(),
347 perspective: camera.get_perspective(),
349 bounding_box(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
352 mandelwow(&display, &mut frame, &program, model, &camera, &bounds, wow);
353 frame.finish().unwrap();
355 for ev in display.poll_events() {
357 glium::glutin::Event::Closed |
358 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
359 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
360 return support::Action::Stop
362 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
363 bounding_box_enabled ^= true;
365 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F)) => {
368 glutin::WindowBuilder::new()
369 .with_fullscreen(glutin::get_primary_monitor())
370 .rebuild_glium(&display).unwrap();
372 glutin::WindowBuilder::new()
373 .rebuild_glium(&display).unwrap();
376 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
379 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
382 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
385 ev => camera.process_input(&ev),
389 support::Action::Continue