11 use glium::{DisplayBuild, Surface};
12 use glium::index::{IndexBuffer, PrimitiveType};
13 use glutin::ElementState::Pressed;
14 use glutin::Event::KeyboardInput;
15 use glutin::VirtualKeyCode;
22 fn solid_fill_program(display: &glium::Display) -> glium::Program {
23 let vertex_shader_src = r#"
26 uniform mat4 perspective;
31 mat4 modelview = view * model;
32 gl_Position = perspective * modelview * vec4(position, 1.0);
36 let fragment_shader_src = r#"
42 color = vec4(1.0, 1.0, 1.0, 1.0);
46 return glium::Program::from_source(display,
52 fn bounding_box<U>(display: &glium::Display,
53 frame: &mut glium::Frame,
54 program: &glium::Program,
56 cube: &Cube) where U: glium::uniforms::Uniforms {
58 #[derive(Copy, Clone)]
59 struct Vertex { position: [f32; 3] }
60 implement_vertex!(Vertex, position);
63 Vertex { position: [cube.xmin, cube.ymin, cube.zmin] },
64 Vertex { position: [cube.xmax, cube.ymin, cube.zmin] },
65 Vertex { position: [cube.xmax, cube.ymax, cube.zmin] },
66 Vertex { position: [cube.xmin, cube.ymax, cube.zmin] },
67 Vertex { position: [cube.xmin, cube.ymin, cube.zmax] },
68 Vertex { position: [cube.xmax, cube.ymin, cube.zmax] },
69 Vertex { position: [cube.xmax, cube.ymax, cube.zmax] },
70 Vertex { position: [cube.xmin, cube.ymax, cube.zmax] },
72 let vb = glium::VertexBuffer::new(display, &cube).unwrap();
74 let params = glium::DrawParameters {
76 test: glium::draw_parameters::DepthTest::IfLess,
80 blend: glium::Blend::alpha_blending(),
84 let front_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
85 &[0, 1, 2, 3u16]).unwrap();
86 frame.draw(&vb, &front_indices, program, uniforms, ¶ms).unwrap();
88 let back_indices = IndexBuffer::new(display, PrimitiveType::LineLoop,
89 &[4, 5, 6, 7u16]).unwrap();
90 frame.draw(&vb, &back_indices, program, uniforms, ¶ms).unwrap();
92 let sides_indices = IndexBuffer::new(display, PrimitiveType::LinesList,
93 &[0, 4, 1, 5, 2, 6, 3, 7u16]).unwrap();
94 frame.draw(&vb, &sides_indices, program, uniforms, ¶ms).unwrap();
100 let display = glium::glutin::WindowBuilder::new()
101 //.with_dimensions(1024, 768)
102 .with_fullscreen(glutin::get_primary_monitor())
103 .with_depth_buffer(24)
105 .with_title(format!("MandelWow"))
109 let mandelwow_program = mandelwow::program(&display);
110 let bounding_box_program = solid_fill_program(&display);
112 let mut camera = support::camera::CameraState::new();
113 let mut t: f32 = 0.0;
114 let mut pause = false;
115 let mut bounding_box_enabled = true;
116 let mut fullscreen = true;
118 support::start_loop(|| {
126 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
136 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
139 let wsize = wmax - wmin;
140 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
142 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
144 let mut frame = display.draw();
145 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
147 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
149 [ t.cos(), t.sin(), 0.0, 0.0],
150 [-t.sin(), t.cos(), 0.0, 0.0],
151 [ 0.0, 0.0, 1.0, 0.0],
152 [ 0.0, 0.0, z_trans, 1.0f32]
155 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
156 // so the lines behind the semi-translucent areas will be drawn.
157 if bounding_box_enabled {
158 let uniforms = uniform! {
160 view: camera.get_view(),
161 perspective: camera.get_perspective(),
163 bounding_box(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
166 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
167 frame.finish().unwrap();
169 for ev in display.poll_events() {
171 glium::glutin::Event::Closed |
172 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
173 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
174 return support::Action::Stop
176 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
177 bounding_box_enabled ^= true;
179 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F)) => {
182 glutin::WindowBuilder::new()
183 .with_fullscreen(glutin::get_primary_monitor())
184 .rebuild_glium(&display).unwrap();
186 glutin::WindowBuilder::new()
187 .rebuild_glium(&display).unwrap();
190 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
193 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
196 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
199 ev => camera.process_input(&ev),
203 support::Action::Continue