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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use adi_gpu;
use Window;
use ami::{ Mat4, IDENTITY, Vec4 };
#[macro_export] macro_rules! models {
($window:expr, $( $x:expr ),*) => { {
use $crate::ModelBuilder as model;
use $crate::IDENTITY;
let a = vec![ $( include!($x).finish($window) ),* ];
$window.models(a);
} }
}
pub struct ModelBuilder {
vertices: Vec<f32>,
ts: Vec<[f32; 4]>,
colors: Vec<f32>,
colors_ts: Vec<[f32; 4]>,
tcs: Vec<f32>,
tcs_ts: Vec<[f32; 4]>,
color: Option<[f32; 4]>,
opacity: Option<f32>,
fans: Vec<(u32, u32)>,
mat4: Mat4,
}
impl ModelBuilder {
#[doc(hidden)]
pub fn new() -> Self {
ModelBuilder {
vertices: vec![],
ts: vec![],
colors: vec![],
colors_ts: vec![],
tcs: vec![],
tcs_ts: vec![],
color: None,
opacity: None,
fans: vec![],
mat4: IDENTITY,
}
}
pub fn m(mut self, mat4: Mat4) -> Self {
self.mat4 = mat4;
self
}
pub fn c(mut self, color: [f32;4]) -> Self {
self.color = Some(color);
self
}
pub fn o(mut self, opacity: f32) -> Self {
self.opacity = Some(opacity);
self
}
pub fn g(mut self, vertices: &[[f32;4]]) -> Self {
self.colors_ts = vec![];
self.colors_ts.extend(vertices);
self
}
pub fn t(mut self, vertices: &[[f32;4]]) -> Self {
self.tcs_ts = vec![];
self.tcs_ts.extend(vertices);
self
}
pub fn v(mut self, vertices: &[[f32;4]]) -> Self {
self.ts = vec![];
self.ts.extend(vertices);
self
}
pub fn d(mut self) -> Self {
self = self.shape();
self.ts.reverse();
let origin = self.ts.pop().unwrap();
self.ts.insert(0, origin);
self = self.shape();
self.mat4 = IDENTITY;
self
}
pub fn f(mut self) -> Self {
self = self.shape();
self.mat4 = IDENTITY;
self
}
pub fn shape(mut self) -> Self {
if self.ts.len() == 0 { return self; }
let start = self.vertices.len() / 4;
let length = self.ts.len();
self.fans.push((start as u32, length as u32));
for i in &self.ts {
let v = self.mat4 * Vec4::new(i[0], i[1], i[2], i[3]);
self.vertices.push(v.x as f32);
self.vertices.push(v.y as f32);
self.vertices.push(v.z as f32);
self.vertices.push(v.w as f32);
}
for i in &self.colors_ts {
self.colors.push(i[0] as f32);
self.colors.push(i[1] as f32);
self.colors.push(i[2] as f32);
self.colors.push(i[3] as f32);
}
for i in &self.tcs_ts {
self.tcs.push(i[0] as f32);
self.tcs.push(i[1] as f32);
self.tcs.push(i[2] as f32);
self.tcs.push(i[3] as f32);
}
self
}
pub fn finish(self, window: &mut Window) -> Model {
Model(window.window.model(self.vertices.as_slice(), self.fans),
if self.colors.is_empty() {
None
} else {
assert!(self.colors.len() == self.vertices.len());
Some(window.window.gradient(self.colors.as_slice()))
},
if self.tcs.is_empty() {
None
} else {
assert_eq!(self.tcs.len(), self.vertices.len());
Some(window.window.texcoords(self.tcs.as_slice()))
}, self.color, self.opacity)
}
}
pub struct Model(pub(crate) adi_gpu::Model,
pub(crate) Option<adi_gpu::Gradient>,
pub(crate) Option<adi_gpu::TexCoords>,
pub(crate) Option<[f32; 4]>,
pub(crate) Option<f32>);