1use crate::RenderError;
19
20#[derive(Clone, Debug)]
25pub struct MeshGeometry {
26 pub(crate) gx: u32,
27 pub(crate) gy: u32,
28 pub(crate) aspect_x: f32,
29 pub(crate) aspect_y: f32,
30 pub(crate) x: Vec<f32>,
31 pub(crate) y: Vec<f32>,
32 pub(crate) rad: Vec<f32>,
33 pub(crate) ang: Vec<f32>,
34 pm: Vec<([f32; 2], f32, f32)>,
36}
37
38pub fn aspect_xy(tex_w: u32, tex_h: u32) -> (f32, f32) {
40 let ax = if tex_h > tex_w {
41 tex_w as f32 / tex_h as f32
42 } else {
43 1.0
44 };
45 let ay = if tex_w > tex_h {
46 tex_h as f32 / tex_w as f32
47 } else {
48 1.0
49 };
50 (ax, ay)
51}
52
53impl MeshGeometry {
54 pub fn new(gx: u32, gy: u32, tex_w: u32, tex_h: u32) -> Result<Self, RenderError> {
59 if gx < 2 || gy < 2 || !gx.is_multiple_of(2) || !gy.is_multiple_of(2) {
60 return Err(RenderError::Invalid(format!(
61 "mesh dimensions must be even and >= 2, got {gx}x{gy}"
62 )));
63 }
64 let (ax, ay) = aspect_xy(tex_w, tex_h);
65 let n = ((gx + 1) * (gy + 1)) as usize;
66 let mut g = MeshGeometry {
67 gx,
68 gy,
69 aspect_x: ax,
70 aspect_y: ay,
71 x: Vec::with_capacity(n),
72 y: Vec::with_capacity(n),
73 rad: Vec::with_capacity(n),
74 ang: Vec::with_capacity(n),
75 pm: Vec::with_capacity(n),
76 };
77 for y in 0..=gy {
78 for x in 0..=gx {
79 let vx = x as f32 / gx as f32 * 2.0 - 1.0;
80 let vy = y as f32 / gy as f32 * 2.0 - 1.0;
81 let rad = (vx * ax).hypot(vy * ay);
82 let ang = if y == gy / 2 && x == gx / 2 {
83 0.0
84 } else {
85 (vy * ay).atan2(vx * ax)
86 };
87 g.x.push(vx);
88 g.y.push(vy);
89 g.rad.push(rad);
90 g.ang.push(ang);
91 g.pm.push(projectm_vertex(gx, gy, ax, ay, x, gy - y));
92 }
93 }
94 Ok(g)
95 }
96
97 pub fn gx(&self) -> u32 {
99 self.gx
100 }
101
102 pub fn gy(&self) -> u32 {
104 self.gy
105 }
106
107 pub fn aspect_x(&self) -> f32 {
109 self.aspect_x
110 }
111
112 pub fn aspect_y(&self) -> f32 {
114 self.aspect_y
115 }
116
117 pub fn x(&self) -> &[f32] {
119 &self.x
120 }
121
122 pub fn y(&self) -> &[f32] {
124 &self.y
125 }
126
127 pub fn rad(&self) -> &[f32] {
129 &self.rad
130 }
131
132 pub fn ang(&self) -> &[f32] {
134 &self.ang
135 }
136
137 pub fn vertex_count(&self) -> usize {
139 ((self.gx + 1) * (self.gy + 1)) as usize
140 }
141
142 #[inline]
144 pub fn index(&self, x: u32, y: u32) -> usize {
145 (y * (self.gx + 1) + x) as usize
146 }
147
148 pub fn pv_x(&self, n: usize) -> f32 {
151 self.x[n] * 0.5 * self.aspect_x + 0.5
152 }
153 pub fn pv_y(&self, n: usize) -> f32 {
155 self.y[n] * -0.5 * self.aspect_y + 0.5
156 }
157
158 #[inline]
164 pub fn projectm_vertex(&self, x: u32, y: u32) -> ([f32; 2], f32, f32) {
165 if x <= self.gx && y <= self.gy {
166 self.pm[self.index(x, y)]
167 } else {
168 projectm_vertex(
169 self.gx,
170 self.gy,
171 self.aspect_x,
172 self.aspect_y,
173 x,
174 self.gy.wrapping_sub(y),
175 )
176 }
177 }
178}
179
180fn projectm_vertex(
182 gx: u32,
183 gy: u32,
184 ax: f32,
185 ay: f32,
186 x: u32,
187 row_pm: u32,
188) -> ([f32; 2], f32, f32) {
189 let vx = x as f32 / gx as f32 * 2.0 - 1.0;
190 let vy = row_pm as f32 / gy as f32 * 2.0 - 1.0;
191 let rad = (vx * ax).hypot(vy * ay);
192 let ang = if row_pm == gy / 2 && x == gx / 2 {
193 0.0
194 } else {
195 (vy * ay).atan2(vx * ax)
196 };
197 ([vx, vy], rad, ang)
198}
199
200#[derive(Clone, Copy, Debug, PartialEq)]
203pub struct VertexVars {
204 pub zoom: f32,
206 pub zoomexp: f32,
208 pub rot: f32,
210 pub warp: f32,
212 pub cx: f32,
214 pub cy: f32,
216 pub dx: f32,
218 pub dy: f32,
220 pub sx: f32,
222 pub sy: f32,
224}
225
226impl Default for VertexVars {
227 fn default() -> Self {
229 Self {
230 zoom: 1.0,
231 zoomexp: 1.0,
232 rot: 0.0,
233 warp: 1.0,
234 cx: 0.5,
235 cy: 0.5,
236 dx: 0.0,
237 dy: 0.0,
238 sx: 1.0,
239 sy: 1.0,
240 }
241 }
242}
243
244#[derive(Clone, Copy, Debug, PartialEq)]
246pub struct WarpAnim {
247 pub y_flip: bool,
251 pub gpu_pow: bool,
254 pub skip_tiny_warp: bool,
257 pub warp_time: f32,
259 pub scale_inv: f32,
261 pub f: [f32; 4],
263}
264
265impl WarpAnim {
266 pub fn new(time: f32, warp_anim_speed: f32, warp_scale: f32) -> Self {
268 let warp_time = time * warp_anim_speed;
269 let f = [
270 11.68 + 4.0 * (warp_time * 1.413 + 10.0).cos(),
271 8.77 + 3.0 * (warp_time * 1.113 + 7.0).cos(),
272 10.54 + 3.0 * (warp_time * 1.233 + 3.0).cos(),
273 11.49 + 4.0 * (warp_time * 0.933 + 5.0).cos(),
274 ];
275 Self {
276 y_flip: false,
277 gpu_pow: false,
278 skip_tiny_warp: false,
279 warp_time,
280 scale_inv: 1.0 / warp_scale,
281 f,
282 }
283 }
284
285 pub fn projectm(time: f32, warp_anim_speed: f32, warp_scale: f32) -> Self {
287 Self {
288 y_flip: true,
289 gpu_pow: true,
290 ..Self::new(time, warp_anim_speed, warp_scale)
291 }
292 }
293}
294
295pub fn warp_vertex_uv(
300 g: &MeshGeometry,
301 n: usize,
302 v: &VertexVars,
303 anim: &WarpAnim,
304 texel_offset_uv: [f32; 2],
305) -> [f32; 2] {
306 let vx = g.x[n];
307 let vy = g.y[n];
308 let wy = if anim.y_flip { -vy } else { vy };
310 let rad = g.rad[n];
311 let f = &anim.f;
312
313 let zoom2 = if anim.gpu_pow && v.zoom < 0.0 {
314 f32::NAN
315 } else {
316 v.zoom.powf(v.zoomexp.powf(rad * 2.0 - 1.0))
317 };
318 let zoom2_inv = 1.0 / zoom2;
319
320 let mut u = vx * g.aspect_x * 0.5 * zoom2_inv + 0.5;
321 let mut vv = -vy * g.aspect_y * 0.5 * zoom2_inv + 0.5;
322
323 u = (u - v.cx) / v.sx + v.cx;
325 vv = (vv - v.cy) / v.sy + v.cy;
326
327 let wt = anim.warp_time;
329 let si = anim.scale_inv;
330 if !anim.skip_tiny_warp || v.warp.abs() > 0.0001 {
331 u += v.warp * 0.0035 * (wt * 0.333 + si * (vx * f[0] - wy * f[3])).sin();
332 vv += v.warp * 0.0035 * (wt * 0.375 - si * (vx * f[2] + wy * f[1])).cos();
333 u += v.warp * 0.0035 * (wt * 0.753 - si * (vx * f[1] - wy * f[2])).cos();
334 vv += v.warp * 0.0035 * (wt * 0.825 + si * (vx * f[0] + wy * f[3])).sin();
335 }
336
337 let u2 = u - v.cx;
339 let v2 = vv - v.cy;
340 let cos_rot = v.rot.cos();
341 let sin_rot = v.rot.sin();
342 u = u2 * cos_rot - v2 * sin_rot + v.cx;
343 vv = u2 * sin_rot + v2 * cos_rot + v.cy;
344
345 u -= v.dx;
347 vv -= v.dy;
348
349 u = (u - 0.5) * (1.0 / g.aspect_x) + 0.5;
351 vv = (vv - 0.5) * (1.0 / g.aspect_y) + 0.5;
352
353 u += texel_offset_uv[0];
355 vv += texel_offset_uv[1];
356 [u, vv]
357}
358
359pub fn warp_mesh_uniform(
361 g: &MeshGeometry,
362 v: &VertexVars,
363 anim: &WarpAnim,
364 texel_offset_uv: [f32; 2],
365) -> Vec<[f32; 2]> {
366 (0..g.vertex_count())
367 .map(|n| warp_vertex_uv(g, n, v, anim, texel_offset_uv))
368 .collect()
369}
370
371pub fn texel_offset_uv(texel_offset: [f32; 2], tex_w: u32, tex_h: u32) -> [f32; 2] {
373 [
374 texel_offset[0] / tex_w as f32,
375 texel_offset[1] / tex_h as f32,
376 ]
377}
378
379#[cfg(test)]
380mod tests {
381 use super::*;
382
383 fn close(a: f32, b: f32) -> bool {
384 (a - b).abs() < 1e-5
385 }
386
387 #[test]
388 fn geometry_matches_md2_setup() {
389 let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
390 assert_eq!(g.vertex_count(), 49 * 37);
391 assert!(close(g.aspect_x, 1.0));
392 assert!(close(g.aspect_y, 360.0 / 640.0));
393 assert!(close(g.x[0], -1.0) && close(g.y[0], -1.0));
395 assert!(close(g.x[48], 1.0) && close(g.y[48], -1.0));
396 let last = g.vertex_count() - 1;
397 assert!(close(g.x[last], 1.0) && close(g.y[last], 1.0));
398 let c = g.index(24, 18);
400 assert!(close(g.rad[c], 0.0) && g.ang[c] == 0.0);
401 let corner = (1.0f32 + (360.0f32 / 640.0).powi(2)).sqrt();
403 assert!(close(g.rad[0], corner));
404 assert!(close(g.pv_y(0), 0.5 + 0.5 * g.aspect_y));
406 assert!(close(g.pv_x(0), 0.0));
407 }
408
409 #[test]
410 fn identity_motion_maps_to_screen_uv() {
411 let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
413 let anim = WarpAnim::new(3.0, 1.0, 1.0);
414 let v = VertexVars {
415 warp: 0.0,
416 ..Default::default()
417 };
418 let uv = warp_mesh_uniform(&g, &v, &anim, [0.0, 0.0]);
419 for n in 0..g.vertex_count() {
420 assert!(close(uv[n][0], g.x[n] * 0.5 + 0.5), "u {n}");
421 assert!(close(uv[n][1], -g.y[n] * 0.5 + 0.5), "v {n}");
422 }
423 assert!(close(uv[0][1], 1.0));
425 }
426
427 #[test]
428 fn zoom_rot_translate_hand_computed() {
429 let g = MeshGeometry::new(2, 2, 100, 100).unwrap(); let anim = WarpAnim::new(0.0, 1.0, 1.0);
431 let v = VertexVars {
433 zoom: 2.0,
434 warp: 0.0,
435 ..Default::default()
436 };
437 let uv = warp_vertex_uv(&g, g.index(2, 0), &v, &anim, [0.0, 0.0]); assert!(close(uv[0], 0.75) && close(uv[1], 0.75));
439 let v = VertexVars {
442 rot: std::f32::consts::FRAC_PI_2,
443 warp: 0.0,
444 ..Default::default()
445 };
446 let uv = warp_vertex_uv(&g, g.index(2, 2), &v, &anim, [0.0, 0.0]);
447 assert!(close(uv[0], 1.0) && close(uv[1], 1.0));
448 let v = VertexVars {
450 dx: 0.1,
451 dy: -0.2,
452 warp: 0.0,
453 ..Default::default()
454 };
455 let uv = warp_vertex_uv(&g, g.index(1, 1), &v, &anim, [0.001, 0.002]);
456 assert!(close(uv[0], 0.5 - 0.1 + 0.001) && close(uv[1], 0.5 + 0.2 + 0.002));
457 }
458
459 #[test]
460 fn warp_terms_are_applied() {
461 let g = MeshGeometry::new(4, 4, 64, 64).unwrap();
462 let anim = WarpAnim::new(1.0, 1.0, 1.0);
463 assert!(close(anim.f[0], 11.68 + 4.0 * (1.413f32 + 10.0).cos()));
464 let a = warp_vertex_uv(
465 &g,
466 7,
467 &VertexVars {
468 warp: 0.0,
469 ..Default::default()
470 },
471 &anim,
472 [0.0; 2],
473 );
474 let b = warp_vertex_uv(
475 &g,
476 7,
477 &VertexVars {
478 warp: 1.0,
479 ..Default::default()
480 },
481 &anim,
482 [0.0; 2],
483 );
484 let (du, dv) = ((b[0] - a[0]).abs(), (b[1] - a[1]).abs());
485 assert!(
486 du > 0.0 && du < 0.0141 && dv < 0.0141,
487 "warp displacement bounded by 2*0.0035: {du} {dv}"
488 );
489 }
490
491 #[test]
494 fn tiny_warp_skips_the_terms_under_md3() {
495 let g = MeshGeometry::new(4, 4, 64, 64).unwrap();
496 let md2 = WarpAnim::new(1.0, 1.0, 1.0);
497 let md3 = WarpAnim {
498 skip_tiny_warp: true,
499 ..md2
500 };
501 let uv = |warp: f32, anim: &WarpAnim| {
502 warp_vertex_uv(
503 &g,
504 7,
505 &VertexVars {
506 warp,
507 ..Default::default()
508 },
509 anim,
510 [0.0; 2],
511 )
512 };
513 let base = uv(0.0, &md3);
514 assert_eq!(uv(0.0001, &md3), base, "|warp| <= 1e-4: skipped");
515 assert_eq!(uv(-0.0001, &md3), base);
516 assert_ne!(uv(0.0001, &md2), base, "MD2 adds the terms");
517 let big = uv(0.0002, &md3);
518 assert_eq!(big, uv(0.0002, &md2), "above the threshold both add them");
519 assert_ne!(big, base);
520 assert_eq!(uv(1.0, &md3), uv(1.0, &md2));
521 }
522
523 #[test]
527 fn projectm_vertex_follows_initialize_mesh() {
528 let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
529 let (p0, rad0, _) = g.projectm_vertex(0, 0);
530 assert_eq!(p0, [-1.0, 1.0]);
531 assert!(close(rad0, (1.0f32 + (360.0f32 / 640.0).powi(2)).sqrt()));
532 let (pc, radc, angc) = g.projectm_vertex(24, 18);
533 assert_eq!((pc, radc, angc), ([0.0, 0.0], 0.0, 0.0));
534 let (p, rad, ang) = g.projectm_vertex(24, 34);
536 assert_eq!(p[1], 2.0f32 / 36.0 * 2.0 - 1.0);
537 assert_eq!(rad, (p[1] * g.aspect_y).abs());
538 assert!(ang < 0.0, "top half has negative atan2(y, x)");
539 assert_eq!(g.projectm_vertex(3, 18).2, std::f32::consts::PI);
541 }
542
543 #[test]
544 fn negative_zoom_is_nan_on_the_gpu_but_finite_in_md2() {
545 let g = MeshGeometry::new(4, 4, 64, 64).unwrap();
546 let v = VertexVars {
547 zoom: -0.5,
548 ..Default::default()
549 };
550 let uv = warp_vertex_uv(&g, 7, &v, &WarpAnim::projectm(1.0, 1.0, 1.0), [0.0; 2]);
551 assert!(uv[0].is_nan() && uv[1].is_nan());
552 let uv = warp_vertex_uv(&g, 7, &v, &WarpAnim::new(1.0, 1.0, 1.0), [0.0; 2]);
553 assert!(uv[0].is_finite() && uv[1].is_finite());
554 let v = VertexVars {
556 zoom: 1.5,
557 ..Default::default()
558 };
559 let uv = warp_vertex_uv(&g, 7, &v, &WarpAnim::projectm(1.0, 1.0, 1.0), [0.0; 2]);
560 assert!(uv[0].is_finite() && uv[1].is_finite());
561 }
562}