Skip to main content

lactea_render/
wavegen_md3.rs

1// SPDX-FileCopyrightText: 2026 Gianluca Boiano
2// SPDX-FileCopyrightText: 2005-2013 Nullsoft, Inc.
3// SPDX-License-Identifier: (MIT OR Apache-2.0) AND BSD-3-Clause
4
5//! Main waveform of MilkDrop 3 (3.37): the 17 modes `0..=16` (`docs/md3.md`, "Main waveform").
6//!
7//! MD3 keeps the BeatDrop `DrawWave` structure (per-pass set-up, blend of the old and the new mode, colour,
8//! `SmoothWave`, thick / dots passes) and differs in the modes themselves: every formula reads *one* channel (like the
9//! original MilkDrop 2 and unlike BeatDrop, which averages left and right), mode 14 really halves `rad` and `ang`,
10//! mode 13 reads past the end of the right channel into the spectrum, mode 16 is BeatDrop's triangle spinning the other
11//! way, and BeatDrop's mode 17 does not exist. The shared tail (`WaveGeom` consumers in the renderer) is the MD2 one.
12//!
13//! Output convention as in [`crate::wavegen`]: clip space, x right, y up, before `SmoothWave`.
14
15use crate::frame::MainWave;
16use crate::wavegen::{
17    NUM_WAVE_INPUT, NUM_WAVEFORM_SAMPLES, WaveGeom, finish_alpha, line_edges, line_strip,
18    size_gain, wave_rgb, wrap_param2,
19};
20
21/// `NUM_WAVES` of MilkDrop 3: valid modes are `0..=16`; the drawn mode is `(int)wave_mode % 17`.
22pub const MD3_NUM_WAVES: i32 = 17;
23
24/// The mode MilkDrop 3 draws for the per-frame variable `wave_mode`: C truncation to `int`
25/// (`cvttsd2si`: NaN and out-of-range values give `INT_MIN`), then the C remainder by 17, which keeps the sign of the
26/// dividend. A negative result is not a mode: [`main_wave_geometry_md3_raw`] then yields 480 zero vertices.
27pub fn md3_wave_mode(wave_mode: f64) -> i32 {
28    let t = wave_mode.trunc();
29    let i = if t.is_nan() || !(-2147483648.0..2147483648.0).contains(&t) {
30        i32::MIN
31    } else {
32        t as i32
33    };
34    i % MD3_NUM_WAVES
35}
36
37/// Samples read by mode 13 beyond the right channel: MD3's `fWave[1]` is followed in memory by `fSpecLeft`, so
38/// index `k >= 576` of the right channel is `spec_l[k - 576]`.
39fn extended_right(wr: &[f32], spec_l: &[f32], k: usize) -> f32 {
40    match wr.get(k) {
41        Some(&v) => v,
42        None => spec_l.get(k - wr.len()).copied().unwrap_or(0.0),
43    }
44}
45
46/// One pass of MD3's `DrawWave` loop: the vertices and the alpha of the wave number `mw.mode` *before* the
47/// "alpha < 0.004" test (which applies to the possibly blended result).
48///
49/// * `wl`, `wr`: the output of [`crate::wavegen::prepare_main_waveform`]; `spec_l`: `fSpecLeft` (modes 8 and 13).
50/// * `mw.mode` is used as given: the caller applies [`md3_wave_mode`] for the new wave and passes the old preset's
51///   mode unchanged for the second pass of a blend. A mode outside `0..=16` gives 480 zero vertices with the plain
52///   `wave_a` alpha, like MD3 (`docs/md3.md`).
53/// * `blending` is `m_bBlending`: the modes that close their loop by repeating the first vertex (0, 13, 14, 16) do
54///   not while blending.
55///
56/// `None` only when a strip would have no vertices (texture narrower than 3 pixels).
57pub fn main_wave_geometry_md3_raw(
58    mw: &MainWave,
59    wl: &[f32],
60    wr: &[f32],
61    spec_l: &[f32],
62    time: f32,
63    imm_rel: [f32; 3],
64    imm_abs: [f32; 3],
65    tex_w: u32,
66    aspect: (f32, f32),
67    blending: bool,
68) -> Option<WaveGeom> {
69    let (asp_x, asp_y) = aspect;
70    if wl.len() < NUM_WAVE_INPUT || wr.len() < NUM_WAVE_INPUT {
71        return None;
72    }
73    let (fl, fr) = (wl, wr);
74    let rgb = wave_rgb(mw);
75    let pos_x = mw.x * 2.0 - 1.0;
76    let pos_y = mw.y * 2.0 - 1.0;
77    let param = mw.mystery;
78    let treble = imm_abs[2];
79    let wave = mw.mode;
80    let t = time;
81
82    // `param2` differs from `param` only for the modes that wrap it into [-1, 1)
83    let param2 = wrap_param2(wave, param);
84
85    let mut alpha = mw.rgba[3];
86    let finish = |alpha: f32| finish_alpha(mw, imm_rel, alpha);
87    // modes 6..=11: vertices per strip (`min(240, W/3)`) and the sample offset that centres them in the 480 samples
88    let line_n = (NUM_WAVEFORM_SAMPLES / 2).min(tex_w as usize / 3);
89    let line_off = (NUM_WAVEFORM_SAMPLES - line_n) / 2;
90
91    let mut brk: Option<usize> = None;
92    let mut v: Vec<[f32; 2]>;
93    match wave {
94        // circle, right channel only; the first 24 vertices cross-fade into the next 240 samples
95        0 | 13 | 14 => {
96            alpha = finish(alpha);
97            let o = 120usize;
98            v = Vec::with_capacity(241);
99            for i in 0..240usize {
100                let fi = i as f32;
101                let ang = 0.2 * t + fi * (1.0 / 239.0) * 6.28;
102                let (x, y) = match wave {
103                    0 => {
104                        let mut rad = 0.5 + 0.4 * fr[o + i] + param2;
105                        if i < 24 {
106                            let m = 0.5 - 0.5 * (fi / 24.0 * 3.1416).cos();
107                            let rad2 = 0.5 + 0.4 * fr[o + 240 + i] + param2;
108                            rad = rad2 * (1.0 - m) + rad * m;
109                        }
110                        (
111                            rad * ang.cos() * asp_y + pos_x,
112                            rad * ang.sin() * asp_x + pos_y,
113                        )
114                    }
115                    13 => {
116                        let mut rad = 0.7 + 0.4 * extended_right(fr, spec_l, o + i) + param;
117                        if fi < 240.0 / rad {
118                            let m = 0.5 - 0.5 * (fi / 24.0 * 3.1416).cos();
119                            // index 360 + i runs past the 576 samples for i >= 216 (into the spectrum)
120                            let rad2 = 0.5 + 0.4 * extended_right(fr, spec_l, 360 + i) + param;
121                            rad = rad2 * (1.0 - m) + rad * m;
122                        }
123                        (
124                            rad * ang.cos() * asp_y + pos_x,
125                            rad * ang.sin() * asp_x + pos_y,
126                        )
127                    }
128                    _ => {
129                        // MD3 really halves `rad` and `ang`; the cross-fade reuses the same sample
130                        let rad0 = 0.7 + 0.7 * fr[o + i] + param;
131                        let mut rad = rad0 / 2.0;
132                        let ang = ang / 2.0;
133                        if fi < 240.0 / rad {
134                            let m = 0.7 - 0.7 * (fi / 24.0 * 3.1416).cos();
135                            rad = rad0 * (1.0 - m) + 0.25 * m * rad;
136                        }
137                        let c = 3.1416f32.cos();
138                        (
139                            rad * (ang * 3.1416).cos() * asp_y / 1.5 + pos_x * c,
140                            rad * (ang - t / 3.0).sin() * asp_x / 1.5 + pos_y * c,
141                        )
142                    }
143                };
144                v.push([x, y]);
145            }
146            if !blending {
147                v.push(v[0]);
148            }
149        }
150        // x-y oscillator spiral; mode 12 is the skewed variant whose angle also carries the final alpha
151        1 | 12 => {
152            alpha = finish(alpha * 1.25);
153            v = (0..240usize)
154                .map(|i| {
155                    if wave == 1 {
156                        let rad = 0.53 + 0.43 * fr[i] + param2;
157                        let ang = 1.57 * fl[i + 32] + 2.3 * t;
158                        [
159                            rad * ang.cos() * asp_y + pos_x,
160                            rad * ang.sin() * asp_x + pos_y,
161                        ]
162                    } else {
163                        let rad = 0.63 + 0.23 * fr[i] + param;
164                        let ang = 0.9 * fl[i + 32] + 3.3 * t;
165                        [
166                            rad * (ang + alpha).cos() * asp_y + pos_x,
167                            rad * ang.sin() * asp_x + pos_y,
168                        ]
169                    }
170                })
171                .collect();
172        }
173        // spiro: faint (2) / treble-driven (3)
174        2 | 3 => {
175            if wave == 2 {
176                alpha *= size_gain(tex_w);
177            } else {
178                alpha = match tex_w {
179                    256 => 0.075,
180                    512 => 0.15,
181                    1024 => 0.22,
182                    2048 => 0.33,
183                    _ => alpha,
184                };
185                alpha *= 1.3 * treble * treble;
186            }
187            alpha = finish(alpha);
188            v = (0..NUM_WAVEFORM_SAMPLES)
189                .map(|i| [fr[i] * asp_y + pos_x, fl[i + 32] * asp_x + pos_y])
190                .collect();
191        }
192        // script with momentum, left channel for y
193        4 => {
194            alpha = finish(alpha);
195            let n = NUM_WAVEFORM_SAMPLES.min(tex_w as usize / 3);
196            let o = (NUM_WAVEFORM_SAMPLES - n) / 2;
197            let w1 = (param2 * 0.5 + 0.5) * 0.5 + 0.45;
198            v = vec![[0.0; 2]; n];
199            for i in 0..n {
200                let x0 = -1.0 + 2.0 * i as f32 / n as f32 + pos_x + 0.44 * fr[o + i + 25];
201                let y0 = 0.47 * fl[o + i] + pos_y;
202                if i > 1 {
203                    v[i] = [
204                        w1 * (2.0 * v[i - 1][0] - v[i - 2][0]) + (1.0 - w1) * x0,
205                        w1 * (2.0 * v[i - 1][1] - v[i - 2][1]) + (1.0 - w1) * y0,
206                    ];
207                } else {
208                    v[i] = [x0, y0];
209                }
210            }
211        }
212        // explosive hash
213        5 => {
214            alpha = finish(alpha * size_gain(tex_w));
215            let (c, s) = ((0.3 * t).cos(), (0.3 * t).sin());
216            v = (0..NUM_WAVEFORM_SAMPLES)
217                .map(|i| {
218                    let x0 = fr[i] * fl[i + 32] + fl[i] * fr[i + 32];
219                    let y0 = fr[i] * fr[i] - fl[i + 32] * fl[i + 32];
220                    [
221                        (x0 * c - y0 * s) * asp_y + pos_x,
222                        (x0 * s + y0 * c) * asp_x + pos_y,
223                    ]
224                })
225                .collect();
226        }
227        // angled lines: 6 left only, 7 left over right, 8 spectrum (left bins only), 9 "large" line (second strip
228        // stays zero), 10 X, 11 two vertical lines
229        6..=11 => {
230            alpha = finish(alpha);
231            let n = if wave == 8 { 256 } else { line_n };
232            if n == 0 {
233                return None;
234            }
235            let o = line_off;
236            let (ex, ey) = line_edges(pos_x, 1.57 * param2);
237            v = match wave {
238                6 => line_strip(ex, ey, n, 0.0, |i| 0.25 * fl[o + i]),
239                7 => {
240                    let sep = (pos_y * 0.5 + 0.5).powf(2.0);
241                    let mut a = line_strip(ex, ey, n, 0.0, |i| 0.25 * fl[o + i] + sep);
242                    a.extend(line_strip(ex, ey, n, 0.0, |i| 0.25 * fr[o + i] - sep));
243                    brk = Some(n);
244                    a
245                }
246                8 => line_strip(ex, ey, n, 0.0, |i| {
247                    let s = |k: usize| spec_l.get(k).copied().unwrap_or(0.0);
248                    0.1 * (s(2 * i) + s(2 * i + 1)).ln()
249                }),
250                9 => {
251                    let mut a = line_strip(ex, ey, n, 0.0, |i| fl[o + i]);
252                    a.resize(2 * n, [0.0; 2]);
253                    brk = Some(n);
254                    a
255                }
256                10 => {
257                    // fixed angles: `wave_mystery` has no effect
258                    let (ax, ay) = line_edges(pos_x, -0.75);
259                    let (bx, by) = line_edges(pos_x, 0.75);
260                    let mut a = line_strip(ax, ay, n, 0.0, |i| 0.35 * fl[o + i]);
261                    a.extend(line_strip(bx, by, n, 0.0, |i| 0.35 * fr[o + i]));
262                    brk = Some(n);
263                    a
264                }
265                _ => {
266                    let (ex, ey) = line_edges(pos_x, 1.57);
267                    let mut a = line_strip(ex, ey, n, -0.45, |i| 0.35 * fl[o + i]);
268                    a.extend(line_strip(ex, ey, n, 0.45, |i| 0.35 * fr[o + i]));
269                    brk = Some(n);
270                    a
271                }
272            };
273        }
274        // lasso: left channel only, no zero guard on the angle, no posX / aspect in x
275        15 => {
276            alpha = finish(alpha * 1.25);
277            v = (0..240usize)
278                .map(|i| {
279                    let ang = 2.0 * t + 1.57 * fl[i + 32];
280                    [
281                        0.5 * t.cos() + (2.0 * ang + (t / ang).tan()).cos(),
282                        2.0 * t.sin() * (3.14 * ang).sin() * asp_x / 2.8 + pos_y,
283                    ]
284                })
285                .collect();
286        }
287        // triangle (BeatDrop's mode 16 with the spin reversed), right channel only, no clamps
288        16 => {
289            alpha = finish(alpha);
290            v = (0..240usize)
291                .map(|i| {
292                    let p = i as f32 / 240.0;
293                    let angle = 6.28 * p - 0.2 * t;
294                    let phi = ((3.0 * p).floor() + 0.5) / 3.0 * 6.28 - 0.2 * t;
295                    let e = (angle - phi).cos();
296                    let r = (0.7 + e * fr[i] + param) / (2.0 * e);
297                    [
298                        r * angle.cos() * asp_y + pos_x,
299                        r * angle.sin() * asp_x + pos_y,
300                    ]
301                })
302                .collect();
303            if !blending {
304                v.push(v[0]);
305            }
306        }
307        // not a mode (negative after the C remainder, or an old preset's value >= 17): MD3 still tessellates and
308        // draws 480 zero vertices
309        _ => {
310            v = vec![[0.0; 2]; NUM_WAVEFORM_SAMPLES];
311        }
312    }
313    Some(WaveGeom {
314        verts: v,
315        brk,
316        alpha,
317        rgb,
318    })
319}
320
321#[cfg(test)]
322mod tests {
323    use super::*;
324
325    const TOL: f32 = 1e-4;
326
327    /// Annex-B-style fixed input: 576 samples per channel, a synthetic left-spectrum ramp.
328    fn input() -> (Vec<f32>, Vec<f32>, Vec<f32>) {
329        let fl = (0..576)
330            .map(|i| (i as f32 - 288.0) / 576.0 + 0.1 * (0.37 * i as f32).sin())
331            .collect();
332        let fr = (0..576)
333            .map(|i| 0.1 + (i as f32 - 288.0) / 1152.0 + 0.05 * (0.23 * i as f32).cos())
334            .collect();
335        let sp = (0..512).map(|k| 1.0 + 0.01 * k as f32).collect();
336        (fl, fr, sp)
337    }
338
339    fn geom(mode: i32, time: f32, tweak: impl FnOnce(&mut MainWave), blending: bool) -> WaveGeom {
340        let (fl, fr, sp) = input();
341        let mut mw = MainWave::default();
342        mw.mode = mode;
343        mw.rgba = [1.0, 1.0, 1.0, 0.8];
344        mw.x = 0.5;
345        mw.y = 0.5;
346        mw.mystery = 0.0;
347        mw.mod_alpha_by_volume = false;
348        tweak(&mut mw);
349        main_wave_geometry_md3_raw(
350            &mw,
351            &fl,
352            &fr,
353            &sp,
354            time,
355            [1.0; 3],
356            [1.0; 3],
357            1024,
358            (1.0, 0.75),
359            blending,
360        )
361        .unwrap()
362    }
363
364    fn near(g: &WaveGeom, i: usize, want: [f32; 2], tol: f32) {
365        let p = g.verts[i];
366        assert!(
367            (p[0] - want[0]).abs() < tol && (p[1] - want[1]).abs() < tol,
368            "v{i} = {p:?}, want {want:?}"
369        );
370    }
371
372    #[test]
373    fn mode_wraps_modulo_17_with_c_semantics() {
374        for (x, want) in [
375            (0.0, 0),
376            (5.0, 5),
377            (16.0, 16),
378            (17.0, 0),
379            (20.0, 3),
380            (33.0, 16),
381            (34.0, 0),
382            (16.9, 16),
383            (17.9, 0),
384            (-1.0, -1),
385            (-17.0, 0),
386            (-18.0, -1),
387        ] {
388            assert_eq!(md3_wave_mode(x), want, "wave_mode {x}");
389        }
390        // cvttsd2si overflow / NaN: INT_MIN, a negative remainder, so no mode
391        assert!(md3_wave_mode(1e12) < 0);
392        assert!(md3_wave_mode(f64::NAN) < 0);
393    }
394
395    #[test]
396    fn circle_modes_0_13_14_16() {
397        let g = geom(0, 0.0, |_| {}, false);
398        assert_eq!(g.verts.len(), 241);
399        assert_eq!(g.brk, None);
400        assert!((g.alpha - 0.8).abs() < TOL);
401        near(&g, 0, [0.43030, 0.0], TOL);
402        near(&g, 1, [0.42683, 0.01496], TOL);
403        near(&g, 120, [-0.39578, -0.00609], TOL);
404        near(&g, 239, [0.43294, -0.00184], TOL);
405        assert_eq!(g.verts[240], g.verts[0]);
406        let g = geom(0, 1.0, |_| {}, false);
407        near(&g, 0, [0.42173, 0.11398], TOL);
408        near(&g, 120, [-0.38698, -0.11081], TOL);
409        // blending: no closing vertex
410        assert_eq!(geom(0, 0.0, |_| {}, true).verts.len(), 240);
411
412        let g = geom(13, 0.0, |_| {}, false);
413        assert_eq!(g.verts.len(), 241);
414        near(&g, 0, [0.43030, 0.0], TOL);
415        near(&g, 1, [0.42747, 0.01498], TOL);
416        near(&g, 120, [-0.54577, -0.00840], TOL);
417        // the tail reads the spectrum (indices >= 576)
418        near(&g, 239, [0.74331, -0.00316], TOL);
419        let g = geom(13, 1.0, |_| {}, false);
420        near(&g, 120, [-0.53364, -0.15280], TOL);
421        near(&g, 239, [0.72896, 0.19380], TOL);
422
423        let g = geom(14, 0.0, |_| {}, false);
424        near(&g, 0, [0.32029, 0.0], TOL);
425        near(&g, 1, [0.31652, 0.00555], TOL);
426        near(&g, 120, [-0.02006, -0.11228], TOL);
427        near(&g, 239, [-0.37588, 0.00088], TOL);
428        let g = geom(14, 1.0, |_| {}, false);
429        near(&g, 0, [0.30461, -0.09874], TOL);
430        near(&g, 239, [-0.30282, 0.12893], TOL);
431
432        let g = geom(16, 0.0, |_| {}, false);
433        assert_eq!(g.verts.len(), 241);
434        near(&g, 0, [0.48702, 0.0], 1e-3);
435        near(&g, 1, [0.46414, 0.01620], 1e-3);
436        near(&g, 120, [-0.23067, 0.00049], 1e-3);
437        near(&g, 239, [0.52315, -0.02048], 1e-3);
438        let g = geom(16, 1.0, |_| {}, false);
439        // the spin term is negative: BeatDrop's +0.2t gives another first vertex
440        near(&g, 0, [0.47731, -0.12901], 1e-3);
441        near(&g, 120, [-0.22600, 0.06158], 1e-3);
442    }
443
444    #[test]
445    fn spirals_and_spiros_1_2_3_4_5_12_15() {
446        let g = geom(1, 0.0, |_| {}, false);
447        assert_eq!(g.verts.len(), 240);
448        assert!((g.alpha - 1.0).abs() < TOL);
449        near(&g, 0, [0.25393, -0.35005], TOL);
450        near(&g, 120, [0.33820, -0.20050], TOL);
451        near(&g, 239, [0.41434, -0.04810], TOL);
452
453        let g = geom(2, 0.0, |_| {}, false);
454        assert_eq!(g.verts.len(), 480);
455        assert!((g.alpha - 0.088).abs() < TOL);
456        near(&g, 0, [-0.07500, -0.51086], TOL);
457        near(&g, 240, [0.05201, -0.01690], TOL);
458        near(&g, 479, [0.16271, 0.44149], TOL);
459        // mode 3: alpha = table * 1.3 * treble^2, same vertices
460        let g3 = geom(3, 0.0, |_| {}, false);
461        assert!((g3.alpha - 0.286).abs() < TOL);
462        assert_eq!(g3.verts, g.verts);
463
464        let g = geom(4, 0.0, |_| {}, false);
465        assert_eq!(g.verts.len(), 341);
466        near(&g, 0, [-1.05060, -0.16051], TOL);
467        near(&g, 170, [0.01836, -0.02024], TOL);
468        near(&g, 340, [1.11230, 0.12270], TOL);
469
470        let g = geom(5, 0.0, |_| {}, false);
471        assert_eq!(g.verts.len(), 480);
472        near(&g, 0, [0.07526, -0.25098], TOL);
473        near(&g, 479, [0.16163, -0.14785], TOL);
474        let g = geom(5, 1.0, |_| {}, false);
475        near(&g, 0, [0.12752, -0.21012], TOL);
476
477        let g = geom(12, 0.0, |_| {}, false);
478        assert!((g.alpha - 1.0).abs() < TOL);
479        near(&g, 0, [0.39042, -0.26935], TOL);
480        near(&g, 239, [0.28049, -0.03200], TOL);
481        let g = geom(12, 1.0, |_| {}, false);
482        near(&g, 0, [-0.34860, 0.18017], TOL);
483
484        let g = geom(15, 0.0, |_| {}, false);
485        assert_eq!(g.verts.len(), 240, "no closing vertex");
486        near(&g, 0, [0.46670, 0.0], 1e-3);
487        near(&g, 120, [1.16986, 0.0], 1e-3);
488        let g = geom(15, 1.0, |_| {}, false);
489        near(&g, 0, [-0.66636, -0.34922], 1e-3);
490        near(&g, 239, [-0.03484, -0.16372], 1e-3);
491    }
492
493    #[test]
494    fn angled_line_modes_6_to_11() {
495        let g = geom(6, 0.0, |_| {}, false);
496        assert_eq!(g.verts.len(), 240);
497        near(&g, 0, [-1.10005, -0.06278], TOL);
498        near(&g, 120, [0.0, -0.00229], TOL);
499        near(&g, 239, [1.09087, 0.05013], TOL);
500
501        let g = geom(7, 0.0, |_| {}, false);
502        assert_eq!((g.verts.len(), g.brk), (480, Some(240)));
503        near(&g, 0, [-1.09985, 0.18722], TOL);
504        near(&g, 240, [-1.10022, -0.27122], TOL);
505        near(&g, 479, [1.09067, -0.20171], TOL);
506
507        let g = geom(8, 0.0, |_| {}, false);
508        assert_eq!((g.verts.len(), g.brk), (256, None));
509        near(&g, 0, [-1.09994, 0.06981], TOL);
510        near(&g, 255, [1.09161, 0.25023], TOL);
511
512        // mode 9: only strip A is written, the second strip stays at the origin
513        let g = geom(9, 0.0, |_| {}, false);
514        assert_eq!((g.verts.len(), g.brk), (480, Some(240)));
515        near(&g, 0, [-1.10020, -0.25110], TOL);
516        near(&g, 239, [1.09099, 0.20054], TOL);
517        assert!(g.verts[240..].iter().all(|p| *p == [0.0, 0.0]));
518
519        let g = geom(10, 0.0, |_| {}, false);
520        assert_eq!((g.verts.len(), g.brk), (480, Some(240)));
521        near(&g, 0, [-1.15996, 0.96050], 1e-3);
522        near(&g, 240, [-1.07976, -1.04651], 1e-3);
523        near(&g, 479, [1.04479, 1.06572], 1e-3);
524        // ignores wave_mystery
525        let g2 = geom(10, 0.0, |m| m.mystery = 0.5, false);
526        assert_eq!(g.verts, g2.verts);
527
528        let g = geom(11, 0.0, |_| {}, false);
529        assert_eq!((g.verts.len(), g.brk), (480, Some(240)));
530        near(&g, 0, [-0.36299, -1.10014], TOL);
531        near(&g, 240, [0.47884, -1.10005], TOL);
532        near(&g, 479, [0.38326, 1.09094], TOL);
533    }
534
535    #[test]
536    fn single_point_checks_and_param_wrap() {
537        // mode 9 with wave_x/y moved: the line start is clipped on the left edge, y does not move the line
538        let (fl, fr, sp) = input();
539        let ramp: Vec<f32> = (0..576).map(|i| (i as f32 - 288.0) / 576.0).collect();
540        let mut mw = MainWave::default();
541        mw.mode = 9;
542        mw.rgba = [1.0, 1.0, 1.0, 0.8];
543        mw.x = 0.7;
544        mw.y = 0.4;
545        let run = |mw: &MainWave| {
546            main_wave_geometry_md3_raw(
547                mw,
548                &ramp,
549                &fr,
550                &sp,
551                0.0,
552                [1.0; 3],
553                [1.0; 3],
554                1024,
555                (1.0, 0.75),
556                false,
557            )
558            .unwrap()
559        };
560        near(&run(&mw), 0, [-1.10023, 0.10833], TOL);
561        mw.mode = 6;
562        near(&run(&mw), 0, [-1.10006, 0.32708], TOL);
563        mw.mode = 15;
564        let g = main_wave_geometry_md3_raw(
565            &mw,
566            &fl,
567            &fr,
568            &sp,
569            0.0,
570            [1.0; 3],
571            [1.0; 3],
572            1024,
573            (1.0, 0.75),
574            false,
575        )
576        .unwrap();
577        assert!(g.verts.iter().all(|p| (p[1] + 0.2).abs() < 1e-6));
578        // param 0.5 on a ramp-only input
579        mw.mode = 6;
580        mw.mystery = 0.5;
581        mw.x = 0.5;
582        mw.y = 0.5;
583        near(&run(&mw), 0, [-1.04850, -1.15075], 1e-3);
584        mw.mode = 9;
585        near(&run(&mw), 0, [-0.89401, -1.30561], 1e-3);
586
587        // wrap of `wave_mystery` into [-1, 1) applies to modes 0, 1, 4 only
588        let r = |p: f32, mode: i32| geom(mode, 0.0, |m| m.mystery = p, false).verts[0];
589        let base = geom(1, 0.0, |m| m.mystery = -1.0, false).verts[0];
590        // -3 wraps to -1 (mode 1): same first vertex as param -1
591        assert_eq!(r(-3.0, 1), base);
592        // mode 2 does not use the parameter at all
593        assert_eq!(r(-3.0, 2), r(0.0, 2));
594        // mode 13 uses the raw value: 2.0 is not wrapped to 0.0
595        assert_ne!(r(2.0, 13), r(0.0, 13));
596    }
597
598    #[test]
599    fn alpha_tables_volume_and_cut() {
600        // mode 3, treble 0.5, 1024 wide: 0.22 * 1.3 * 0.25
601        let (fl, fr, sp) = input();
602        let mut mw = MainWave::default();
603        mw.mode = 3;
604        mw.rgba = [1.0, 1.0, 1.0, 0.8];
605        let alpha = |mw: &MainWave, w: u32, imm_rel: [f32; 3], treb: f32| {
606            main_wave_geometry_md3_raw(
607                mw,
608                &fl,
609                &fr,
610                &sp,
611                0.0,
612                imm_rel,
613                [1.0, 1.0, treb],
614                w,
615                (1.0, 0.75),
616                false,
617            )
618            .unwrap()
619            .alpha
620        };
621        assert!((alpha(&mw, 1024, [1.0; 3], 0.5) - 0.0715).abs() < 1e-5);
622        // a width off the table keeps wave_a: 0.8 * 1.3 * 0.25
623        assert!((alpha(&mw, 1920, [1.0; 3], 0.5) - 0.26).abs() < 1e-5);
624        mw.mode = 2;
625        assert!((alpha(&mw, 512, [1.0; 3], 1.0) - 0.072).abs() < 1e-5);
626        assert!((alpha(&mw, 1920, [1.0; 3], 1.0) - 0.8).abs() < 1e-5);
627        // alpha by volume: 0.8 * (0.999 - 0.75) / 0.5
628        mw.mode = 0;
629        mw.mod_alpha_by_volume = true;
630        mw.mod_alpha_start = 0.75;
631        mw.mod_alpha_end = 1.25;
632        assert!((alpha(&mw, 1024, [1.2, 1.0, 0.8], 1.0) - 0.3984).abs() < 1e-4);
633        // below Start: clamped to 0
634        assert_eq!(alpha(&mw, 1024, [0.1; 3], 1.0), 0.0);
635    }
636
637    #[test]
638    fn invalid_mode_draws_480_zero_vertices() {
639        for mode in [-1, 17, 40] {
640            let g = geom(mode, 0.0, |_| {}, false);
641            assert_eq!(g.verts.len(), 480);
642            assert!(g.verts.iter().all(|p| *p == [0.0, 0.0]));
643            assert_eq!(g.brk, None);
644            assert!((g.alpha - 0.8).abs() < TOL);
645        }
646    }
647}