5 use glium::DisplayBuild;
7 //use glium::index::PrimitiveType;
11 #[derive(Copy, Clone)]
16 implement_vertex!(Vertex, position, color);
18 fn mand(cx: f32, cy: f32) -> [f32; 3] {
20 let mut iter = maxiter;
27 return [iter as f32 / maxiter as f32, 1.0, 1.0];
29 zy = zx * zy * 2.0 + cy;
37 fn mandel<U>(display: &glium::Display,
38 frame: &mut glium::Frame,
40 t: f32) where U: glium::uniforms::Uniforms {
41 let program = program!(display,
45 uniform mat4 perspective;
52 mat4 modelview = view * model;
53 gl_Position = perspective * modelview * vec4(position, 1.0);
54 c = vec2(gl_Position.x, gl_Position.y);
60 precision mediump float;
71 if (zx2 * zy2 > 4.0) {
72 f_color = vec4(0, 0, 0, 0);
75 zy = zx * zy * 2.0 + c.y;
79 f_color = vec4(1.0, 1.0, 1.0, 1.0);
80 //f_color = vec4(vColor, 1.0);
89 let width = xmax - xmin;
90 let height = ymax - ymin;
91 let xres: usize = 100;
92 let yres: usize = 100;
93 let xstep = width / xres as f32;
94 let ystep = height / yres as f32;
95 let vb_size = (xres * 2 + 4) * yres;
96 let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
97 v.resize(vb_size, Vertex { position: [0.0, 0.0, 1.0], color: [0.0, 0.0, 0.0] });
102 v[i] = Vertex { position: [vx, vy+ystep, 1.0], color: [0.0, 0.0, 0.0] }; i+=1;
103 v[i] = Vertex { position: [vx, vy, 1.0], color: [vx, vy, 0.0] }; i+=1;
105 let c = mand(vx, vy);
106 v[i] = Vertex { position: [vx+xstep, vy+ystep, 1.0], color: c }; i += 1;
107 v[i] = Vertex { position: [vx+xstep, vy , 1.0], color: c }; i += 1;
110 v[i] = Vertex { position: [vx+xstep, vy, 1.0], color: [0.0, 0.0, 0.0] }; i+=1;
111 v[i] = Vertex { position: [vx+xstep, vy, 1.0], color: [0.0, 0.0, 0.0] }; i+=1;
114 //let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
115 let vb = glium::VertexBuffer::new(display, &v).unwrap();
117 //let indices = glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip);
118 let indices = glium::index::NoIndices(glium::index::PrimitiveType::LineStrip);
119 //let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
120 // &[0u16, 1, 2]).unwrap();
122 let params = glium::DrawParameters {
123 depth: glium::Depth {
124 test: glium::draw_parameters::DepthTest::IfLess,
126 .. Default::default()
128 .. Default::default()
131 //let mut s = display.draw();
132 frame.draw(&vb, &indices, &program, uniforms, ¶ms).unwrap();
137 let display = glium::glutin::WindowBuilder::new()
138 .with_dimensions(1024, 768)
139 .with_depth_buffer(24)
140 .with_title(format!("Mandel"))
144 let mut camera = support::camera::CameraState::new();
146 //let mut t: f32 = -0.5;
147 let mut t: f32 = 0.0;
148 support::start_loop(|| {
152 //println!("t={}", t);
154 let mut frame = display.draw();
155 frame.clear_color_and_depth((0.0, 0.0, 0.5, 1.0), 1.0);
158 [ (t*5.0).cos(), (t*3.0).sin(), 0.0, 0.0],
159 [-t.sin(), -t.cos(), 0.0, 0.0],
160 [ 0.0, 0.0, 1.0, 0.0],
161 [ t, 0.0, 0.0, 1.0f32]
164 let uniforms = uniform! {
166 view: camera.get_view(), // view_matrix(&[2.0, -1.0, 1.0], &[-2.0, 1.0, 1.0], &[0.0, 1.0, 0.0]),
167 perspective: camera.get_perspective(), // perspective,
170 mandel(&display, &mut frame, &uniforms, t);
172 for ev in display.poll_events() {
174 glium::glutin::Event::Closed => return support::Action::Stop,
175 ev => camera.process_input(&ev),
179 frame.finish().unwrap();
180 support::Action::Continue