8 use glium::DisplayBuild;
10 use glium::index::IndexBuffer;
11 use glium::index::PrimitiveType;
15 #[derive(Copy, Clone)]
26 fn mand(cx: f32, cy: f32) -> [f32; 3] {
28 let mut iter = maxiter;
35 return [iter as f32 / maxiter as f32, 1.0, 1.0];
37 zy = zx * zy * 2.0 + cy;
46 fn mandelwow_program(display: &glium::Display) -> glium::Program {
47 return program!(display,
51 uniform mat4 perspective;
60 mat4 modelview = view * model;
61 gl_Position = perspective * modelview * vec4(position, 1.0);
62 c = vec2(position.x, position.y);
69 precision highp float;
82 if (zx2 * zy2 > 4.0) {
83 float index = 1.0 - float(iter) / float(maxiter);
84 f_color = vec4(index, index * 0.5, index, index * 0.5);
87 zy = zx * zy * 2.0 + c.y;
91 f_color = vec4((sin(z.y) + 1.0) / 2,
100 fn solid_fill_program(display: &glium::Display) -> glium::Program {
101 let vertex_shader_src = r#"
104 uniform mat4 perspective;
109 mat4 modelview = view * model;
110 gl_Position = perspective * modelview * vec4(position, 1.0);
114 let fragment_shader_src = r#"
120 color = vec4(1.0, 1.0, 1.0, 1.0);
124 return glium::Program::from_source(display,
130 fn bounding_box<U>(display: &glium::Display,
131 frame: &mut glium::Frame,
132 program: &glium::Program,
134 cube: &Cube) where U: glium::uniforms::Uniforms {
136 #[derive(Copy, Clone)]
137 struct Vertex { position: [f32; 3] }
138 implement_vertex!(Vertex, position);
141 Vertex { position: [cube.xmin, cube.ymin, cube.zmin] },
142 Vertex { position: [cube.xmax, cube.ymin, cube.zmin] },
143 Vertex { position: [cube.xmax, cube.ymax, cube.zmin] },
144 Vertex { position: [cube.xmin, cube.ymax, cube.zmin] },
145 Vertex { position: [cube.xmin, cube.ymin, cube.zmax] },
146 Vertex { position: [cube.xmax, cube.ymin, cube.zmax] },
147 Vertex { position: [cube.xmax, cube.ymax, cube.zmax] },
148 Vertex { position: [cube.xmin, cube.ymax, cube.zmax] },
150 let vb = glium::VertexBuffer::new(display, &cube).unwrap();
152 let params = glium::DrawParameters {
153 depth: glium::Depth {
154 test: glium::draw_parameters::DepthTest::IfLess,
156 .. Default::default()
158 blend: glium::Blend::alpha_blending(),
159 .. Default::default()
162 let front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
163 &[0, 1, 2, 3u16]).unwrap();
164 frame.draw(&vb, &front_indices, program, uniforms, ¶ms).unwrap();
166 let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
167 &[4, 5, 6, 7u16]).unwrap();
168 frame.draw(&vb, &back_indices, program, uniforms, ¶ms).unwrap();
170 let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
171 &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
172 frame.draw(&vb, &sides_indices, program, uniforms, ¶ms).unwrap();
175 fn mandel<U>(display: &glium::Display,
176 frame: &mut glium::Frame,
177 program: &glium::Program,
180 z: [f32; 2]) where U: glium::uniforms::Uniforms {
182 #[derive(Copy, Clone)]
187 implement_vertex!(Vertex, position, color);
189 let xmin = bounds.xmin;
190 let xmax = bounds.xmax;
191 let ymin = bounds.ymin;
192 let ymax = bounds.ymax;
194 let width = xmax - xmin;
195 let height = ymax - ymin;
198 let xstep = width / (xres as f32);
199 let ystep = height / (yres as f32);
200 let vb_size = (xres * 2 + 4) * yres;
201 let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
202 v.resize(vb_size, Vertex { position: [0.0, 0.0, -1.0], color: [0.0, 0.0, 0.0] });
203 let mut i: usize = 0;
208 let c = [0.0, 0.0, 1.0];
209 v[i] = Vertex { position: [vx, vy+ystep, vz], color: c }; i += 1;
210 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
212 //let c = mand(vx, vy);
213 v[i] = Vertex { position: [vx+xstep, vy+ystep, vz], color: c }; i += 1;
214 v[i] = Vertex { position: [vx+xstep, vy, vz], color: c }; i += 1;
217 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
218 v[i] = Vertex { position: [xmin, vy, vz], color: c }; i += 1;
222 //let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
223 let vb = glium::VertexBuffer::new(display, &v).unwrap();
225 let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
226 //let indices = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
227 //let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
228 // &[0u16, 1, 2]).unwrap();
230 let params = glium::DrawParameters {
231 depth: glium::Depth {
232 test: glium::draw_parameters::DepthTest::IfLess,
234 .. Default::default()
236 blend: glium::Blend::alpha_blending(),
237 .. Default::default()
240 frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap();
243 fn mandelwow(display: &glium::Display,
244 mut frame: &mut glium::Frame,
245 program: &glium::Program,
246 model: [[f32; 4]; 4],
247 camera: &support::camera::CameraState,
250 let mut z0 = [mandel_w, 0f32];
252 let zmin = bounds.zmin;
253 let zmax = bounds.zmax;
254 let zstep = (zmax - zmin) / zres as f32;
260 let uniforms = uniform! {
263 view: camera.get_view(),
264 perspective: camera.get_perspective(),
267 mandel(&display, &mut frame, &program, &uniforms, bounds, z0);
272 let display = glium::glutin::WindowBuilder::new()
273 .with_dimensions(1024, 768)
274 .with_depth_buffer(24)
275 .with_title(format!("MandelWow"))
279 let program = mandelwow_program(&display);
280 let bounding_box_program = solid_fill_program(&display);
282 let mut camera = support::camera::CameraState::new();
283 let mut t: f32 = 0.0;
284 let mut pause = false;
285 let mut bounding_box_enabled = true;
287 support::start_loop(|| {
295 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
305 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
308 let wsize = wmax - wmin;
309 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
311 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
313 let mut frame = display.draw();
314 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
316 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
318 [ t.cos(), t.sin(), 0.0, 0.0],
319 [-t.sin(), t.cos(), 0.0, 0.0],
320 [ 0.0, 0.0, 1.0, 0.0],
321 [ 0.0, 0.0, z_trans, 1.0f32]
324 // Draw the bounding box before the fractal, when the Z-buffer is still clear, so the lines
325 // behind the semi-translucent areas will be drawn.
326 if bounding_box_enabled {
327 let uniforms = uniform! {
329 view: camera.get_view(),
330 perspective: camera.get_perspective(),
332 bounding_box(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
335 mandelwow(&display, &mut frame, &program, model, &camera, &bounds, wow);
337 for ev in display.poll_events() {
339 glium::glutin::Event::Closed => {
340 frame.finish().unwrap();
341 return support::Action::Stop
343 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::PageUp)) => {
346 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::PageDown)) => {
349 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::P)) => {
352 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::B)) => {
353 bounding_box_enabled ^= true;
355 ev => camera.process_input(&ev),
359 frame.finish().unwrap();
360 support::Action::Continue