-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelwow.rs
133 lines (118 loc) · 3.95 KB
/
mandelwow.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Wow. Such fractal.
use crate::cube::Cube;
use glium;
use glium::index::PrimitiveType;
use glium::{Display, Program, Surface, implement_vertex, uniform};
use crate::support;
/*
fn mand(cx: f32, cy: f32) -> [f32; 3] {
let maxiter = 64;
let mut iter = maxiter;
let mut zx = cx;
let mut zy = cy;
while iter > 0 {
let zx2 = zx * zx;
let zy2 = zy * zy;
if zx2 + zy2 > 4.0 {
return [iter as f32 / maxiter as f32, 1.0, 1.0];
}
zy = zx * zy * 2.0 + cy;
zx = zx2 - zy2 + cx;
iter -= 1;
}
[0.0, 0.0, 0.0]
}
*/
pub fn program(display: &Display) -> Program {
Program::from_source(
display,
include_str!("shaders/mandelwow.vert"),
include_str!("shaders/mandelwow.frag"), None)
.unwrap()
}
fn mandel<U>(display: &Display,
frame: &mut glium::Frame,
program: &Program,
uniforms: &U,
bounds: &Cube,
z: [f32; 2]) where U: glium::uniforms::Uniforms {
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 3],
color: [f32; 3],
}
implement_vertex!(Vertex, position, color);
let xmin = bounds.xmin;
let xmax = bounds.xmax;
let ymin = bounds.ymin;
let ymax = bounds.ymax;
let width = xmax - xmin;
let height = ymax - ymin;
let xres: usize = 1;
let yres: usize = 1;
let xstep = width / (xres as f32);
let ystep = height / (yres as f32);
let vb_size = (xres * 2 + 4) * yres;
let mut v : Vec<Vertex> = Vec::with_capacity(vb_size);
v.resize(vb_size, Vertex { position: [0.0, 0.0, -1.0], color: [0.0, 0.0, 0.0] });
let mut i: usize = 0;
let mut vy = ymin;
let vz = z[1];
for _ in 0..yres {
let mut vx = xmin;
let c = [0.0, 0.0, 1.0];
v[i] = Vertex { position: [vx, vy+ystep, vz], color: c }; i += 1;
v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
for _ in 0..xres {
//let c = mand(vx, vy);
v[i] = Vertex { position: [vx+xstep, vy+ystep, vz], color: c }; i += 1;
v[i] = Vertex { position: [vx+xstep, vy, vz], color: c }; i += 1;
vx += xstep;
}
v[i] = Vertex { position: [vx, vy, vz], color: c }; i += 1;
v[i] = Vertex { position: [xmin, vy, vz], color: c }; i += 1;
vy += ystep;
}
//let vb = glium::VertexBuffer::empty_persistent(display, width*height*3).unwrap();
let vb = glium::VertexBuffer::new(display, &v).unwrap();
let indices = glium::index::NoIndices(PrimitiveType::TriangleStrip);
//let indices = glium::index::NoIndices(PrimitiveType::LineStrip);
//let indices = glium::IndexBuffer::new(display, PrimitiveType::TrianglesList,
// &[0u16, 1, 2]).unwrap();
let params = glium::DrawParameters {
depth: glium::Depth {
test: glium::draw_parameters::DepthTest::IfLess,
write: true,
..Default::default()
},
blend: glium::Blend::alpha_blending(),
..Default::default()
};
frame.draw(&vb, &indices, program, uniforms, ¶ms).unwrap();
}
pub fn draw(display: &Display,
mut frame: &mut glium::Frame,
program: &Program,
model: [[f32; 4]; 4],
camera: &support::camera::CameraState,
bounds: &Cube,
mandel_w: f32) {
let mut z0 = [mandel_w, 0f32];
let zres = 30;
let zmin = bounds.zmin;
let zmax = bounds.zmax;
let zstep = (zmax - zmin) / zres as f32;
let mut zy = zmin;
// zres + 1 to reach the other face of the cube (fencepost error)
for _ in 0..(zres + 1) {
z0[1] = zy;
zy += zstep;
let uniforms = uniform! {
z0: z0,
model: model,
view: camera.get_view(),
perspective: camera.get_perspective(),
};
mandel(display, &mut frame, program, &uniforms, bounds, z0);
}
}