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}
35
36pub fn aspect_xy(tex_w: u32, tex_h: u32) -> (f32, f32) {
38 let ax = if tex_h > tex_w {
39 tex_w as f32 / tex_h as f32
40 } else {
41 1.0
42 };
43 let ay = if tex_w > tex_h {
44 tex_h as f32 / tex_w as f32
45 } else {
46 1.0
47 };
48 (ax, ay)
49}
50
51impl MeshGeometry {
52 pub fn new(gx: u32, gy: u32, tex_w: u32, tex_h: u32) -> Result<Self, RenderError> {
57 if gx < 2 || gy < 2 || !gx.is_multiple_of(2) || !gy.is_multiple_of(2) {
58 return Err(RenderError::Invalid(format!(
59 "mesh dimensions must be even and >= 2, got {gx}x{gy}"
60 )));
61 }
62 let (ax, ay) = aspect_xy(tex_w, tex_h);
63 let n = ((gx + 1) * (gy + 1)) as usize;
64 let mut g = MeshGeometry {
65 gx,
66 gy,
67 aspect_x: ax,
68 aspect_y: ay,
69 x: Vec::with_capacity(n),
70 y: Vec::with_capacity(n),
71 rad: Vec::with_capacity(n),
72 ang: Vec::with_capacity(n),
73 };
74 for y in 0..=gy {
75 for x in 0..=gx {
76 let vx = x as f32 / gx as f32 * 2.0 - 1.0;
77 let vy = y as f32 / gy as f32 * 2.0 - 1.0;
78 let rad = (vx * ax).hypot(vy * ay);
79 let ang = if y == gy / 2 && x == gx / 2 {
80 0.0
81 } else {
82 (vy * ay).atan2(vx * ax)
83 };
84 g.x.push(vx);
85 g.y.push(vy);
86 g.rad.push(rad);
87 g.ang.push(ang);
88 }
89 }
90 Ok(g)
91 }
92
93 pub fn gx(&self) -> u32 {
95 self.gx
96 }
97
98 pub fn gy(&self) -> u32 {
100 self.gy
101 }
102
103 pub fn aspect_x(&self) -> f32 {
105 self.aspect_x
106 }
107
108 pub fn aspect_y(&self) -> f32 {
110 self.aspect_y
111 }
112
113 pub fn x(&self) -> &[f32] {
115 &self.x
116 }
117
118 pub fn y(&self) -> &[f32] {
120 &self.y
121 }
122
123 pub fn rad(&self) -> &[f32] {
125 &self.rad
126 }
127
128 pub fn ang(&self) -> &[f32] {
130 &self.ang
131 }
132
133 pub fn vertex_count(&self) -> usize {
135 ((self.gx + 1) * (self.gy + 1)) as usize
136 }
137
138 #[inline]
140 pub fn index(&self, x: u32, y: u32) -> usize {
141 (y * (self.gx + 1) + x) as usize
142 }
143
144 pub fn pv_x(&self, n: usize) -> f32 {
147 self.x[n] * 0.5 * self.aspect_x + 0.5
148 }
149 pub fn pv_y(&self, n: usize) -> f32 {
151 self.y[n] * -0.5 * self.aspect_y + 0.5
152 }
153
154 pub fn projectm_vertex(&self, x: u32, y: u32) -> ([f32; 2], f32, f32) {
160 let row_pm = self.gy - y;
161 let vx = x as f32 / self.gx as f32 * 2.0 - 1.0;
162 let vy = row_pm as f32 / self.gy as f32 * 2.0 - 1.0;
163 let rad = (vx * self.aspect_x).hypot(vy * self.aspect_y);
164 let ang = if row_pm == self.gy / 2 && x == self.gx / 2 {
165 0.0
166 } else {
167 (vy * self.aspect_y).atan2(vx * self.aspect_x)
168 };
169 ([vx, vy], rad, ang)
170 }
171}
172
173#[derive(Clone, Copy, Debug, PartialEq)]
176pub struct VertexVars {
177 pub zoom: f32,
179 pub zoomexp: f32,
181 pub rot: f32,
183 pub warp: f32,
185 pub cx: f32,
187 pub cy: f32,
189 pub dx: f32,
191 pub dy: f32,
193 pub sx: f32,
195 pub sy: f32,
197}
198
199impl Default for VertexVars {
200 fn default() -> Self {
202 Self {
203 zoom: 1.0,
204 zoomexp: 1.0,
205 rot: 0.0,
206 warp: 1.0,
207 cx: 0.5,
208 cy: 0.5,
209 dx: 0.0,
210 dy: 0.0,
211 sx: 1.0,
212 sy: 1.0,
213 }
214 }
215}
216
217#[derive(Clone, Copy, Debug, PartialEq)]
219pub struct WarpAnim {
220 pub y_flip: bool,
224 pub gpu_pow: bool,
227 pub warp_time: f32,
229 pub scale_inv: f32,
231 pub f: [f32; 4],
233}
234
235impl WarpAnim {
236 pub fn new(time: f32, warp_anim_speed: f32, warp_scale: f32) -> Self {
238 let warp_time = time * warp_anim_speed;
239 let f = [
240 11.68 + 4.0 * (warp_time * 1.413 + 10.0).cos(),
241 8.77 + 3.0 * (warp_time * 1.113 + 7.0).cos(),
242 10.54 + 3.0 * (warp_time * 1.233 + 3.0).cos(),
243 11.49 + 4.0 * (warp_time * 0.933 + 5.0).cos(),
244 ];
245 Self {
246 y_flip: false,
247 gpu_pow: false,
248 warp_time,
249 scale_inv: 1.0 / warp_scale,
250 f,
251 }
252 }
253
254 pub fn projectm(time: f32, warp_anim_speed: f32, warp_scale: f32) -> Self {
256 Self {
257 y_flip: true,
258 gpu_pow: true,
259 ..Self::new(time, warp_anim_speed, warp_scale)
260 }
261 }
262}
263
264pub fn warp_vertex_uv(
269 g: &MeshGeometry,
270 n: usize,
271 v: &VertexVars,
272 anim: &WarpAnim,
273 texel_offset_uv: [f32; 2],
274) -> [f32; 2] {
275 let vx = g.x[n];
276 let vy = g.y[n];
277 let wy = if anim.y_flip { -vy } else { vy };
279 let rad = g.rad[n];
280 let f = &anim.f;
281
282 let zoom2 = if anim.gpu_pow && v.zoom < 0.0 {
283 f32::NAN
284 } else {
285 v.zoom.powf(v.zoomexp.powf(rad * 2.0 - 1.0))
286 };
287 let zoom2_inv = 1.0 / zoom2;
288
289 let mut u = vx * g.aspect_x * 0.5 * zoom2_inv + 0.5;
290 let mut vv = -vy * g.aspect_y * 0.5 * zoom2_inv + 0.5;
291
292 u = (u - v.cx) / v.sx + v.cx;
294 vv = (vv - v.cy) / v.sy + v.cy;
295
296 let wt = anim.warp_time;
298 let si = anim.scale_inv;
299 u += v.warp * 0.0035 * (wt * 0.333 + si * (vx * f[0] - wy * f[3])).sin();
300 vv += v.warp * 0.0035 * (wt * 0.375 - si * (vx * f[2] + wy * f[1])).cos();
301 u += v.warp * 0.0035 * (wt * 0.753 - si * (vx * f[1] - wy * f[2])).cos();
302 vv += v.warp * 0.0035 * (wt * 0.825 + si * (vx * f[0] + wy * f[3])).sin();
303
304 let u2 = u - v.cx;
306 let v2 = vv - v.cy;
307 let cos_rot = v.rot.cos();
308 let sin_rot = v.rot.sin();
309 u = u2 * cos_rot - v2 * sin_rot + v.cx;
310 vv = u2 * sin_rot + v2 * cos_rot + v.cy;
311
312 u -= v.dx;
314 vv -= v.dy;
315
316 u = (u - 0.5) * (1.0 / g.aspect_x) + 0.5;
318 vv = (vv - 0.5) * (1.0 / g.aspect_y) + 0.5;
319
320 u += texel_offset_uv[0];
322 vv += texel_offset_uv[1];
323 [u, vv]
324}
325
326pub fn warp_mesh_uniform(
328 g: &MeshGeometry,
329 v: &VertexVars,
330 anim: &WarpAnim,
331 texel_offset_uv: [f32; 2],
332) -> Vec<[f32; 2]> {
333 (0..g.vertex_count())
334 .map(|n| warp_vertex_uv(g, n, v, anim, texel_offset_uv))
335 .collect()
336}
337
338pub fn texel_offset_uv(texel_offset: [f32; 2], tex_w: u32, tex_h: u32) -> [f32; 2] {
340 [
341 texel_offset[0] / tex_w as f32,
342 texel_offset[1] / tex_h as f32,
343 ]
344}
345
346#[cfg(test)]
347mod tests {
348 use super::*;
349
350 fn close(a: f32, b: f32) -> bool {
351 (a - b).abs() < 1e-5
352 }
353
354 #[test]
355 fn geometry_matches_md2_setup() {
356 let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
357 assert_eq!(g.vertex_count(), 49 * 37);
358 assert!(close(g.aspect_x, 1.0));
359 assert!(close(g.aspect_y, 360.0 / 640.0));
360 assert!(close(g.x[0], -1.0) && close(g.y[0], -1.0));
362 assert!(close(g.x[48], 1.0) && close(g.y[48], -1.0));
363 let last = g.vertex_count() - 1;
364 assert!(close(g.x[last], 1.0) && close(g.y[last], 1.0));
365 let c = g.index(24, 18);
367 assert!(close(g.rad[c], 0.0) && g.ang[c] == 0.0);
368 let corner = (1.0f32 + (360.0f32 / 640.0).powi(2)).sqrt();
370 assert!(close(g.rad[0], corner));
371 assert!(close(g.pv_y(0), 0.5 + 0.5 * g.aspect_y));
373 assert!(close(g.pv_x(0), 0.0));
374 }
375
376 #[test]
377 fn identity_motion_maps_to_screen_uv() {
378 let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
380 let anim = WarpAnim::new(3.0, 1.0, 1.0);
381 let v = VertexVars {
382 warp: 0.0,
383 ..Default::default()
384 };
385 let uv = warp_mesh_uniform(&g, &v, &anim, [0.0, 0.0]);
386 for n in 0..g.vertex_count() {
387 assert!(close(uv[n][0], g.x[n] * 0.5 + 0.5), "u {n}");
388 assert!(close(uv[n][1], -g.y[n] * 0.5 + 0.5), "v {n}");
389 }
390 assert!(close(uv[0][1], 1.0));
392 }
393
394 #[test]
395 fn zoom_rot_translate_hand_computed() {
396 let g = MeshGeometry::new(2, 2, 100, 100).unwrap(); let anim = WarpAnim::new(0.0, 1.0, 1.0);
398 let v = VertexVars {
400 zoom: 2.0,
401 warp: 0.0,
402 ..Default::default()
403 };
404 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));
406 let v = VertexVars {
409 rot: std::f32::consts::FRAC_PI_2,
410 warp: 0.0,
411 ..Default::default()
412 };
413 let uv = warp_vertex_uv(&g, g.index(2, 2), &v, &anim, [0.0, 0.0]);
414 assert!(close(uv[0], 1.0) && close(uv[1], 1.0));
415 let v = VertexVars {
417 dx: 0.1,
418 dy: -0.2,
419 warp: 0.0,
420 ..Default::default()
421 };
422 let uv = warp_vertex_uv(&g, g.index(1, 1), &v, &anim, [0.001, 0.002]);
423 assert!(close(uv[0], 0.5 - 0.1 + 0.001) && close(uv[1], 0.5 + 0.2 + 0.002));
424 }
425
426 #[test]
427 fn warp_terms_are_applied() {
428 let g = MeshGeometry::new(4, 4, 64, 64).unwrap();
429 let anim = WarpAnim::new(1.0, 1.0, 1.0);
430 assert!(close(anim.f[0], 11.68 + 4.0 * (1.413f32 + 10.0).cos()));
431 let a = warp_vertex_uv(
432 &g,
433 7,
434 &VertexVars {
435 warp: 0.0,
436 ..Default::default()
437 },
438 &anim,
439 [0.0; 2],
440 );
441 let b = warp_vertex_uv(
442 &g,
443 7,
444 &VertexVars {
445 warp: 1.0,
446 ..Default::default()
447 },
448 &anim,
449 [0.0; 2],
450 );
451 let (du, dv) = ((b[0] - a[0]).abs(), (b[1] - a[1]).abs());
452 assert!(
453 du > 0.0 && du < 0.0141 && dv < 0.0141,
454 "warp displacement bounded by 2*0.0035: {du} {dv}"
455 );
456 }
457
458 #[test]
462 fn projectm_vertex_follows_initialize_mesh() {
463 let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
464 let (p0, rad0, _) = g.projectm_vertex(0, 0);
465 assert_eq!(p0, [-1.0, 1.0]);
466 assert!(close(rad0, (1.0f32 + (360.0f32 / 640.0).powi(2)).sqrt()));
467 let (pc, radc, angc) = g.projectm_vertex(24, 18);
468 assert_eq!((pc, radc, angc), ([0.0, 0.0], 0.0, 0.0));
469 let (p, rad, ang) = g.projectm_vertex(24, 34);
471 assert_eq!(p[1], 2.0f32 / 36.0 * 2.0 - 1.0);
472 assert_eq!(rad, (p[1] * g.aspect_y).abs());
473 assert!(ang < 0.0, "top half has negative atan2(y, x)");
474 assert_eq!(g.projectm_vertex(3, 18).2, std::f32::consts::PI);
476 }
477
478 #[test]
479 fn negative_zoom_is_nan_on_the_gpu_but_finite_in_md2() {
480 let g = MeshGeometry::new(4, 4, 64, 64).unwrap();
481 let v = VertexVars {
482 zoom: -0.5,
483 ..Default::default()
484 };
485 let uv = warp_vertex_uv(&g, 7, &v, &WarpAnim::projectm(1.0, 1.0, 1.0), [0.0; 2]);
486 assert!(uv[0].is_nan() && uv[1].is_nan());
487 let uv = warp_vertex_uv(&g, 7, &v, &WarpAnim::new(1.0, 1.0, 1.0), [0.0; 2]);
488 assert!(uv[0].is_finite() && uv[1].is_finite());
489 let v = VertexVars {
491 zoom: 1.5,
492 ..Default::default()
493 };
494 let uv = warp_vertex_uv(&g, 7, &v, &WarpAnim::projectm(1.0, 1.0, 1.0), [0.0; 2]);
495 assert!(uv[0].is_finite() && uv[1].is_finite());
496 }
497}