8 use glium::{DisplayBuild, Surface};
9 use glium::index::{IndexBuffer, PrimitiveType};
10 use glutin::ElementState::Pressed;
11 use glutin::Event::KeyboardInput;
12 use glutin::VirtualKeyCode;
16 #[derive(Copy, Clone)]
27 fn mand(cx: f32, cy: f32) -> [f32; 3] {
29 let mut iter = maxiter;
36 return [iter as f32 / maxiter as f32, 1.0, 1.0];
38 zy = zx * zy * 2.0 + cy;
47 fn mandelwow_program(display: &glium::Display) -> glium::Program {
48 return program!(display,
52 uniform mat4 perspective;
61 mat4 modelview = view * model;
62 gl_Position = perspective * modelview * vec4(position, 1.0);
63 c = vec2(position.x, position.y);
70 precision highp float;
83 if (zx2 * zy2 > 4.0) {
84 float index = 1.0 - float(iter) / float(maxiter);
85 f_color = vec4(index, index * 0.5, index, index * 0.5);
88 zy = zx * zy * 2.0 + c.y;
92 f_color = vec4((sin(z.y) + 1.0) / 2,
101 fn solid_fill_program(display: &glium::Display) -> glium::Program {
102 let vertex_shader_src = r#"
105 uniform mat4 perspective;
110 mat4 modelview = view * model;
111 gl_Position = perspective * modelview * vec4(position, 1.0);
115 let fragment_shader_src = r#"
121 color = vec4(1.0, 1.0, 1.0, 1.0);
125 return glium::Program::from_source(display,
131 fn bounding_box<U>(display: &glium::Display,
132 frame: &mut glium::Frame,
133 program: &glium::Program,
135 cube: &Cube) where U: glium::uniforms::Uniforms {
137 #[derive(Copy, Clone)]
138 struct Vertex { position: [f32; 3] }
139 implement_vertex!(Vertex, position);
142 Vertex { position: [cube.xmin, cube.ymin, cube.zmin] },
143 Vertex { position: [cube.xmax, cube.ymin, cube.zmin] },
144 Vertex { position: [cube.xmax, cube.ymax, cube.zmin] },
145 Vertex { position: [cube.xmin, cube.ymax, cube.zmin] },
146 Vertex { position: [cube.xmin, cube.ymin, cube.zmax] },
147 Vertex { position: [cube.xmax, cube.ymin, cube.zmax] },
148 Vertex { position: [cube.xmax, cube.ymax, cube.zmax] },
149 Vertex { position: [cube.xmin, cube.ymax, cube.zmax] },
151 let vb = glium::VertexBuffer::new(display, &cube).unwrap();
153 let params = glium::DrawParameters {
154 depth: glium::Depth {
155 test: glium::draw_parameters::DepthTest::IfLess,
157 .. Default::default()
159 blend: glium::Blend::alpha_blending(),
160 .. Default::default()
163 let front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
164 &[0, 1, 2, 3u16]).unwrap();
165 frame.draw(&vb, &front_indices, program, uniforms, ¶ms).unwrap();
167 let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
168 &[4, 5, 6, 7u16]).unwrap();
169 frame.draw(&vb, &back_indices, program, uniforms, ¶ms).unwrap();
171 let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
172 &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
173 frame.draw(&vb, &sides_indices, program, uniforms, ¶ms).unwrap();
176 fn mandel<U>(display: &glium::Display,
177 frame: &mut glium::Frame,
178 program: &glium::Program,
181 z: [f32; 2]) where U: glium::uniforms::Uniforms {
183 #[derive(Copy, Clone)]
188 implement_vertex!(Vertex, position, color);
190 let xmin = bounds.xmin;
191 let xmax = bounds.xmax;
192 let ymin = bounds.ymin;
193 let ymax = bounds.ymax;
195 let width = xmax - xmin;
196 let height = ymax - ymin;
199 let xstep = width / (xres as f32);
200 let ystep = height / (yres as f32);
201 let vb_size = (xres * 2 + 4) * yres;
202 let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
203 v.resize(vb_size, Vertex { position: [0.0, 0.0, -1.0], color: [0.0, 0.0, 0.0] });
204 let mut i: usize = 0;
209 let c = [0.0, 0.0, 1.0];
210 v[i] = Vertex { position: [vx, vy+ystep, vz], color: c }; i += 1;
211 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
213 //let c = mand(vx, vy);
214 v[i] = Vertex { position: [vx+xstep, vy+ystep, vz], color: c }; i += 1;
215 v[i] = Vertex { position: [vx+xstep, vy, vz], color: c }; i += 1;
218 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
219 v[i] = Vertex { position: [xmin, vy, vz], color: c }; i += 1;
223 //let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
224 let vb = glium::VertexBuffer::new(display, &v).unwrap();
226 let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
227 //let indices = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
228 //let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
229 // &[0u16, 1, 2]).unwrap();
231 let params = glium::DrawParameters {
232 depth: glium::Depth {
233 test: glium::draw_parameters::DepthTest::IfLess,
235 .. Default::default()
237 blend: glium::Blend::alpha_blending(),
238 .. Default::default()
241 frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap();
244 fn mandelwow(display: &glium::Display,
245 mut frame: &mut glium::Frame,
246 program: &glium::Program,
247 model: [[f32; 4]; 4],
248 camera: &support::camera::CameraState,
251 let mut z0 = [mandel_w, 0f32];
253 let zmin = bounds.zmin;
254 let zmax = bounds.zmax;
255 let zstep = (zmax - zmin) / zres as f32;
261 let uniforms = uniform! {
264 view: camera.get_view(),
265 perspective: camera.get_perspective(),
268 mandel(&display, &mut frame, &program, &uniforms, bounds, z0);
273 let display = glium::glutin::WindowBuilder::new()
274 //.with_dimensions(1024, 768)
275 .with_fullscreen(glutin::get_primary_monitor())
276 .with_depth_buffer(24)
278 .with_title(format!("MandelWow"))
282 let program = mandelwow_program(&display);
283 let bounding_box_program = solid_fill_program(&display);
285 let mut camera = support::camera::CameraState::new();
286 let mut t: f32 = 0.0;
287 let mut pause = false;
288 let mut bounding_box_enabled = true;
289 let mut fullscreen = true;
291 support::start_loop(|| {
299 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
309 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
312 let wsize = wmax - wmin;
313 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
315 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
317 let mut frame = display.draw();
318 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
320 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
322 [ t.cos(), t.sin(), 0.0, 0.0],
323 [-t.sin(), t.cos(), 0.0, 0.0],
324 [ 0.0, 0.0, 1.0, 0.0],
325 [ 0.0, 0.0, z_trans, 1.0f32]
328 // Draw the bounding box before the fractal, when the Z-buffer is still clear, so the lines
329 // behind the semi-translucent areas will be drawn.
330 if bounding_box_enabled {
331 let uniforms = uniform! {
333 view: camera.get_view(),
334 perspective: camera.get_perspective(),
336 bounding_box(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
339 mandelwow(&display, &mut frame, &program, model, &camera, &bounds, wow);
340 frame.finish().unwrap();
342 for ev in display.poll_events() {
344 glium::glutin::Event::Closed |
345 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
346 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
347 return support::Action::Stop
349 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
350 bounding_box_enabled ^= true;
352 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F)) => {
355 glutin::WindowBuilder::new()
356 .with_fullscreen(glutin::get_primary_monitor())
357 .rebuild_glium(&display).unwrap();
359 glutin::WindowBuilder::new()
360 .rebuild_glium(&display).unwrap();
363 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
366 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
369 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
372 ev => camera.process_input(&ev),
376 support::Action::Continue