6 use glium::DisplayBuild;
8 use glium::index::PrimitiveType;
9 use glium::index::IndexBuffer;
13 #[derive(Copy, Clone)]
18 implement_vertex!(Vertex, position, color);
20 #[derive(Copy, Clone)]
31 fn mand(cx: f32, cy: f32) -> [f32; 3] {
33 let mut iter = maxiter;
40 return [iter as f32 / maxiter as f32, 1.0, 1.0];
42 zy = zx * zy * 2.0 + cy;
51 fn mandelwow_program(display: &glium::Display) -> glium::Program {
52 return program!(display,
56 uniform mat4 perspective;
65 mat4 modelview = view * model;
66 gl_Position = perspective * modelview * vec4(position, 1.0);
67 c = vec2(position.x, position.y);
74 precision highp float;
87 if (zx2 * zy2 > 4.0) {
88 float index = 1.0 - float(iter) / float(maxiter);
89 f_color = vec4(index, index * 0.5, index, index * 0.5);
92 zy = zx * zy * 2.0 + c.y;
96 f_color = vec4((sin(z.y) + 1.0) / 2,
105 fn solid_fill_program(display: &glium::Display) -> glium::Program {
106 let vertex_shader_src = r#"
109 uniform mat4 perspective;
114 mat4 modelview = view * model;
115 gl_Position = perspective * modelview * vec4(position, 1.0);
119 let fragment_shader_src = r#"
125 color = vec4(1.0, 1.0, 1.0, 1.0);
129 return glium::Program::from_source(display,
135 fn bounding_box<U>(display: &glium::Display,
136 frame: &mut glium::Frame,
137 program: &glium::Program,
139 cube: &Cube) where U: glium::uniforms::Uniforms {
140 // Draw the bounding box
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 front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
159 &[0, 1, 2, 3u16]).unwrap();
160 frame.draw(&vb, &front_indices, program, uniforms, &Default::default()).unwrap();
162 let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
163 &[4, 5, 6, 7u16]).unwrap();
164 frame.draw(&vb, &back_indices, program, uniforms, &Default::default()).unwrap();
166 let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
167 &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
168 frame.draw(&vb, &sides_indices, program, uniforms, &Default::default()).unwrap();
171 fn mandel<U>(display: &glium::Display,
172 frame: &mut glium::Frame,
173 program: &glium::Program,
175 z: [f32; 2]) where U: glium::uniforms::Uniforms {
182 let width = xmax - xmin;
183 let height = ymax - ymin;
186 let xstep = width / (xres as f32);
187 let ystep = height / (yres as f32);
188 let vb_size = (xres * 2 + 4) * yres;
189 let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
190 v.resize(vb_size, Vertex { position: [0.0, 0.0, -1.0], color: [0.0, 0.0, 0.0] });
191 let mut i: usize = 0;
196 let c = [0.0, 0.0, 1.0];
197 v[i] = Vertex { position: [vx, vy+ystep, vz], color: c }; i += 1;
198 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
200 //let c = mand(vx, vy);
201 v[i] = Vertex { position: [vx+xstep, vy+ystep, vz], color: c }; i += 1;
202 v[i] = Vertex { position: [vx+xstep, vy, vz], color: c }; i += 1;
205 v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
206 v[i] = Vertex { position: [xmin, vy, vz], color: c }; i += 1;
210 //let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
211 let vb = glium::VertexBuffer::new(display, &v).unwrap();
213 let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
214 //let indices = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
215 //let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
216 // &[0u16, 1, 2]).unwrap();
218 let params = glium::DrawParameters {
219 depth: glium::Depth {
220 test: glium::draw_parameters::DepthTest::IfLess,
222 .. Default::default()
224 blend: glium::Blend::alpha_blending(),
225 .. Default::default()
228 frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap();
231 fn mandelwow(display: &glium::Display,
232 mut frame: &mut glium::Frame,
233 program: &glium::Program,
234 model: [[f32; 4]; 4],
235 camera: &support::camera::CameraState,
238 let mut z0 = [mandel_w, 0f32];
240 let zmin = cube.zmin;
241 let zmax = cube.zmax;
242 let zstep = (zmax - zmin) / zres as f32;
248 let uniforms = uniform! {
251 view: camera.get_view(),
252 perspective: camera.get_perspective(),
255 mandel(&display, &mut frame, &program, &uniforms, z0);
260 let display = glium::glutin::WindowBuilder::new()
261 .with_dimensions(1024, 768)
262 .with_depth_buffer(24)
263 .with_title(format!("MandelWow"))
267 let mut camera = support::camera::CameraState::new();
268 let mut t: f32 = 0.0;
269 let mut z = [ 0.0, 0.0f32 ];
270 let mut pause = false;
272 support::start_loop(|| {
280 // Compute a sine wave slicing the Mandelwow along its 4th dimension.
283 let wsize = wmax - wmin;
284 let mandel_w = ((t.sin() + 1.0) / 2.0) * wsize + wmin;
286 //println!("t={} z={:?} camera={:?}", t, z, camera.get_pos());
288 let mut frame = display.draw();
289 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
291 let z_trans = -2.0; // How far back to move the model
293 [ t.cos(), t.sin(), 0.0, 0.0],
294 [-t.sin(), t.cos(), 0.0, 0.0],
295 [ 0.0, 0.0, 1.0, 0.0],
296 [ 0.0, 0.0, z_trans, 1.0f32]
299 let program = mandelwow_program(&display);
300 let bounding_box_program = solid_fill_program(&display);
311 mandelwow(&display, &mut frame, &program, model, &camera, &bounds, mandel_w);
313 let uniforms = uniform! {
315 view: camera.get_view(),
316 perspective: camera.get_perspective(),
318 bounding_box(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
320 for ev in display.poll_events() {
322 glium::glutin::Event::Closed => {
323 frame.finish().unwrap();
324 return support::Action::Stop
326 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::PageUp)) => {
329 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::PageDown)) => {
332 glutin::Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(glutin::VirtualKeyCode::P)) => {
335 ev => camera.process_input(&ev),
339 frame.finish().unwrap();
340 support::Action::Continue