8 use glium::DisplayBuild;
10 use glium::index::PrimitiveType;
11 use glium::index::IndexBuffer;
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 = Default::default();
154 let front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
155 &[0, 1, 2, 3u16]).unwrap();
156 frame.draw(&vb, &front_indices, program, uniforms, ¶ms).unwrap();
158 let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
159 &[4, 5, 6, 7u16]).unwrap();
160 frame.draw(&vb, &back_indices, program, uniforms, ¶ms).unwrap();
162 let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
163 &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
164 frame.draw(&vb, &sides_indices, program, uniforms, ¶ms).unwrap();
167 fn mandel<U>(display: &glium::Display,
168 frame: &mut glium::Frame,
169 program: &glium::Program,
172 z: [f32; 2]) where U: glium::uniforms::Uniforms {
174 #[derive(Copy, Clone)]
179 implement_vertex!(Vertex, position, color);
181 let xmin = bounds.xmin;
182 let xmax = bounds.xmax;
183 let ymin = bounds.ymin;
184 let ymax = bounds.ymax;
186 let width = xmax - xmin;
187 let height = ymax - ymin;
190 let xstep = width / (xres as f32);
191 let ystep = height / (yres as f32);
192 let vb_size = (xres * 2 + 4) * yres;
193 let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
194 v.resize(vb_size, Vertex { position: [0.0, 0.0, -1.0], color: [0.0, 0.0, 0.0] });
195 let mut i: usize = 0;
200 let c = [0.0, 0.0, 1.0];
201 v[i] = Vertex { position: [vx, vy+ystep, vz], color: c }; i += 1;
202 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
204 //let c = mand(vx, vy);
205 v[i] = Vertex { position: [vx+xstep, vy+ystep, vz], color: c }; i += 1;
206 v[i] = Vertex { position: [vx+xstep, vy, vz], color: c }; i += 1;
209 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
210 v[i] = Vertex { position: [xmin, vy, vz], color: c }; i += 1;
214 //let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
215 let vb = glium::VertexBuffer::new(display, &v).unwrap();
217 let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
218 //let indices = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
219 //let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
220 // &[0u16, 1, 2]).unwrap();
222 let params = glium::DrawParameters {
223 depth: glium::Depth {
224 test: glium::draw_parameters::DepthTest::IfLess,
226 .. Default::default()
228 blend: glium::Blend::alpha_blending(),
229 .. Default::default()
232 frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap();
235 fn mandelwow(display: &glium::Display,
236 mut frame: &mut glium::Frame,
237 program: &glium::Program,
238 model: [[f32; 4]; 4],
239 camera: &support::camera::CameraState,
242 let mut z0 = [mandel_w, 0f32];
244 let zmin = bounds.zmin;
245 let zmax = bounds.zmax;
246 let zstep = (zmax - zmin) / zres as f32;
252 let uniforms = uniform! {
255 view: camera.get_view(),
256 perspective: camera.get_perspective(),
259 mandel(&display, &mut frame, &program, &uniforms, bounds, z0);
264 let display = glium::glutin::WindowBuilder::new()
265 .with_dimensions(1024, 768)
266 .with_depth_buffer(24)
267 .with_title(format!("MandelWow"))
271 let mut camera = support::camera::CameraState::new();
272 let mut t: f32 = 0.0;
273 let mut pause = false;
275 support::start_loop(|| {
283 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
293 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
296 let wsize = wmax - wmin;
297 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
299 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
301 let mut frame = display.draw();
302 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
304 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
306 [ t.cos(), t.sin(), 0.0, 0.0],
307 [-t.sin(), t.cos(), 0.0, 0.0],
308 [ 0.0, 0.0, 1.0, 0.0],
309 [ 0.0, 0.0, z_trans, 1.0f32]
312 let program = mandelwow_program(&display);
313 let bounding_box_program = solid_fill_program(&display);
315 mandelwow(&display, &mut frame, &program, model, &camera, &bounds, wow);
317 let uniforms = uniform! {
319 view: camera.get_view(),
320 perspective: camera.get_perspective(),
322 bounding_box(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
324 for ev in display.poll_events() {
326 glium::glutin::Event::Closed => {
327 frame.finish().unwrap();
328 return support::Action::Stop
330 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::PageUp)) => {
333 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::PageDown)) => {
336 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::P)) => {
339 ev => camera.process_input(&ev),
343 frame.finish().unwrap();
344 support::Action::Continue