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;
19 #[derive(Copy, Clone)]
30 fn mand(cx: f32, cy: f32) -> [f32; 3] {
32 let mut iter = maxiter;
39 return [iter as f32 / maxiter as f32, 1.0, 1.0];
41 zy = zx * zy * 2.0 + cy;
50 fn mandelwow_program(display: &glium::Display) -> glium::Program {
51 return program!(display,
55 uniform mat4 perspective;
64 mat4 modelview = view * model;
65 gl_Position = perspective * modelview * vec4(position, 1.0);
66 c = vec2(position.x, position.y);
73 precision highp float;
86 if (zx2 * zy2 > 4.0) {
87 float index = float(iter) / float(maxiter);
88 f_color = vec4(index, 0.1, 0.5 - index / 2, 0.8 - index);
91 zy = zx * zy * 2.0 + c.y;
95 f_color = vec4((sin(z.y) + 1.0) / 2,
104 fn solid_fill_program(display: &glium::Display) -> glium::Program {
105 let vertex_shader_src = r#"
108 uniform mat4 perspective;
113 mat4 modelview = view * model;
114 gl_Position = perspective * modelview * vec4(position, 1.0);
118 let fragment_shader_src = r#"
124 color = vec4(1.0, 1.0, 1.0, 1.0);
128 return glium::Program::from_source(display,
134 fn bounding_box<U>(display: &glium::Display,
135 frame: &mut glium::Frame,
136 program: &glium::Program,
138 cube: &Cube) where U: glium::uniforms::Uniforms {
140 #[derive(Copy, Clone)]
141 struct Vertex { position: [f32; 3] }
142 implement_vertex!(Vertex, position);
145 Vertex { position: [cube.xmin, cube.ymin, cube.zmin] },
146 Vertex { position: [cube.xmax, cube.ymin, cube.zmin] },
147 Vertex { position: [cube.xmax, cube.ymax, cube.zmin] },
148 Vertex { position: [cube.xmin, cube.ymax, cube.zmin] },
149 Vertex { position: [cube.xmin, cube.ymin, cube.zmax] },
150 Vertex { position: [cube.xmax, cube.ymin, cube.zmax] },
151 Vertex { position: [cube.xmax, cube.ymax, cube.zmax] },
152 Vertex { position: [cube.xmin, cube.ymax, cube.zmax] },
154 let vb = glium::VertexBuffer::new(display, &cube).unwrap();
156 let params = glium::DrawParameters {
157 depth: glium::Depth {
158 test: glium::draw_parameters::DepthTest::IfLess,
162 blend: glium::Blend::alpha_blending(),
166 let front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
167 &[0, 1, 2, 3u16]).unwrap();
168 frame.draw(&vb, &front_indices, program, uniforms, ¶ms).unwrap();
170 let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
171 &[4, 5, 6, 7u16]).unwrap();
172 frame.draw(&vb, &back_indices, program, uniforms, ¶ms).unwrap();
174 let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
175 &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
176 frame.draw(&vb, &sides_indices, program, uniforms, ¶ms).unwrap();
179 fn mandel<U>(display: &glium::Display,
180 frame: &mut glium::Frame,
181 program: &glium::Program,
184 z: [f32; 2]) where U: glium::uniforms::Uniforms {
186 #[derive(Copy, Clone)]
191 implement_vertex!(Vertex, position, color);
193 let xmin = bounds.xmin;
194 let xmax = bounds.xmax;
195 let ymin = bounds.ymin;
196 let ymax = bounds.ymax;
198 let width = xmax - xmin;
199 let height = ymax - ymin;
202 let xstep = width / (xres as f32);
203 let ystep = height / (yres as f32);
204 let vb_size = (xres * 2 + 4) * yres;
205 let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
206 v.resize(vb_size, Vertex { position: [0.0, 0.0, -1.0], color: [0.0, 0.0, 0.0] });
207 let mut i: usize = 0;
212 let c = [0.0, 0.0, 1.0];
213 v[i] = Vertex { position: [vx, vy+ystep, vz], color: c }; i += 1;
214 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
216 //let c = mand(vx, vy);
217 v[i] = Vertex { position: [vx+xstep, vy+ystep, vz], color: c }; i += 1;
218 v[i] = Vertex { position: [vx+xstep, vy, vz], color: c }; i += 1;
221 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
222 v[i] = Vertex { position: [xmin, vy, vz], color: c }; i += 1;
226 //let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
227 let vb = glium::VertexBuffer::new(display, &v).unwrap();
229 let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
230 //let indices = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
231 //let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
232 // &[0u16, 1, 2]).unwrap();
234 let params = glium::DrawParameters {
235 depth: glium::Depth {
236 test: glium::draw_parameters::DepthTest::IfLess,
240 blend: glium::Blend::alpha_blending(),
244 frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap();
247 fn mandelwow(display: &glium::Display,
248 mut frame: &mut glium::Frame,
249 program: &glium::Program,
250 model: [[f32; 4]; 4],
251 camera: &support::camera::CameraState,
254 let mut z0 = [mandel_w, 0f32];
256 let zmin = bounds.zmin;
257 let zmax = bounds.zmax;
258 let zstep = (zmax - zmin) / zres as f32;
260 // zres + 1 to reach the other face of the cube (fencepost error)
261 for _ in 0..(zres + 1) {
265 let uniforms = uniform! {
268 view: camera.get_view(),
269 perspective: camera.get_perspective(),
272 mandel(&display, &mut frame, &program, &uniforms, bounds, z0);
279 let display = glium::glutin::WindowBuilder::new()
280 //.with_dimensions(1024, 768)
281 .with_fullscreen(glutin::get_primary_monitor())
282 .with_depth_buffer(24)
284 .with_title(format!("MandelWow"))
288 let program = mandelwow_program(&display);
289 let bounding_box_program = solid_fill_program(&display);
291 let mut camera = support::camera::CameraState::new();
292 let mut t: f32 = 0.0;
293 let mut pause = false;
294 let mut bounding_box_enabled = true;
295 let mut fullscreen = true;
297 support::start_loop(|| {
305 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
315 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
318 let wsize = wmax - wmin;
319 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
321 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
323 let mut frame = display.draw();
324 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
326 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
328 [ t.cos(), t.sin(), 0.0, 0.0],
329 [-t.sin(), t.cos(), 0.0, 0.0],
330 [ 0.0, 0.0, 1.0, 0.0],
331 [ 0.0, 0.0, z_trans, 1.0f32]
334 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
335 // so the lines behind the semi-translucent areas will be drawn.
336 if bounding_box_enabled {
337 let uniforms = uniform! {
339 view: camera.get_view(),
340 perspective: camera.get_perspective(),
342 bounding_box(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
345 mandelwow(&display, &mut frame, &program, model, &camera, &bounds, wow);
346 frame.finish().unwrap();
348 for ev in display.poll_events() {
350 glium::glutin::Event::Closed |
351 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
352 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
353 return support::Action::Stop
355 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
356 bounding_box_enabled ^= true;
358 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F)) => {
361 glutin::WindowBuilder::new()
362 .with_fullscreen(glutin::get_primary_monitor())
363 .rebuild_glium(&display).unwrap();
365 glutin::WindowBuilder::new()
366 .rebuild_glium(&display).unwrap();
369 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
372 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
375 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
378 ev => camera.process_input(&ev),
382 support::Action::Continue