use crate::RenderError;
#[derive(Clone, Debug)]
pub struct MeshGeometry {
pub(crate) gx: u32,
pub(crate) gy: u32,
pub(crate) aspect_x: f32,
pub(crate) aspect_y: f32,
pub(crate) x: Vec<f32>,
pub(crate) y: Vec<f32>,
pub(crate) rad: Vec<f32>,
pub(crate) ang: Vec<f32>,
}
pub fn aspect_xy(tex_w: u32, tex_h: u32) -> (f32, f32) {
let ax = if tex_h > tex_w {
tex_w as f32 / tex_h as f32
} else {
1.0
};
let ay = if tex_w > tex_h {
tex_h as f32 / tex_w as f32
} else {
1.0
};
(ax, ay)
}
impl MeshGeometry {
pub fn new(gx: u32, gy: u32, tex_w: u32, tex_h: u32) -> Result<Self, RenderError> {
if gx < 2 || gy < 2 || !gx.is_multiple_of(2) || !gy.is_multiple_of(2) {
return Err(RenderError::Invalid(format!(
"mesh dimensions must be even and >= 2, got {gx}x{gy}"
)));
}
let (ax, ay) = aspect_xy(tex_w, tex_h);
let n = ((gx + 1) * (gy + 1)) as usize;
let mut g = MeshGeometry {
gx,
gy,
aspect_x: ax,
aspect_y: ay,
x: Vec::with_capacity(n),
y: Vec::with_capacity(n),
rad: Vec::with_capacity(n),
ang: Vec::with_capacity(n),
};
for y in 0..=gy {
for x in 0..=gx {
let vx = x as f32 / gx as f32 * 2.0 - 1.0;
let vy = y as f32 / gy as f32 * 2.0 - 1.0;
let rad = (vx * ax).hypot(vy * ay);
let ang = if y == gy / 2 && x == gx / 2 {
0.0
} else {
(vy * ay).atan2(vx * ax)
};
g.x.push(vx);
g.y.push(vy);
g.rad.push(rad);
g.ang.push(ang);
}
}
Ok(g)
}
pub fn gx(&self) -> u32 {
self.gx
}
pub fn gy(&self) -> u32 {
self.gy
}
pub fn aspect_x(&self) -> f32 {
self.aspect_x
}
pub fn aspect_y(&self) -> f32 {
self.aspect_y
}
pub fn x(&self) -> &[f32] {
&self.x
}
pub fn y(&self) -> &[f32] {
&self.y
}
pub fn rad(&self) -> &[f32] {
&self.rad
}
pub fn ang(&self) -> &[f32] {
&self.ang
}
pub fn vertex_count(&self) -> usize {
((self.gx + 1) * (self.gy + 1)) as usize
}
#[inline]
pub fn index(&self, x: u32, y: u32) -> usize {
(y * (self.gx + 1) + x) as usize
}
pub fn pv_x(&self, n: usize) -> f32 {
self.x[n] * 0.5 * self.aspect_x + 0.5
}
pub fn pv_y(&self, n: usize) -> f32 {
self.y[n] * -0.5 * self.aspect_y + 0.5
}
pub fn projectm_vertex(&self, x: u32, y: u32) -> ([f32; 2], f32, f32) {
let row_pm = self.gy - y;
let vx = x as f32 / self.gx as f32 * 2.0 - 1.0;
let vy = row_pm as f32 / self.gy as f32 * 2.0 - 1.0;
let rad = (vx * self.aspect_x).hypot(vy * self.aspect_y);
let ang = if row_pm == self.gy / 2 && x == self.gx / 2 {
0.0
} else {
(vy * self.aspect_y).atan2(vx * self.aspect_x)
};
([vx, vy], rad, ang)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct VertexVars {
pub zoom: f32,
pub zoomexp: f32,
pub rot: f32,
pub warp: f32,
pub cx: f32,
pub cy: f32,
pub dx: f32,
pub dy: f32,
pub sx: f32,
pub sy: f32,
}
impl Default for VertexVars {
fn default() -> Self {
Self {
zoom: 1.0,
zoomexp: 1.0,
rot: 0.0,
warp: 1.0,
cx: 0.5,
cy: 0.5,
dx: 0.0,
dy: 0.0,
sx: 1.0,
sy: 1.0,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct WarpAnim {
pub y_flip: bool,
pub gpu_pow: bool,
pub warp_time: f32,
pub scale_inv: f32,
pub f: [f32; 4],
}
impl WarpAnim {
pub fn new(time: f32, warp_anim_speed: f32, warp_scale: f32) -> Self {
let warp_time = time * warp_anim_speed;
let f = [
11.68 + 4.0 * (warp_time * 1.413 + 10.0).cos(),
8.77 + 3.0 * (warp_time * 1.113 + 7.0).cos(),
10.54 + 3.0 * (warp_time * 1.233 + 3.0).cos(),
11.49 + 4.0 * (warp_time * 0.933 + 5.0).cos(),
];
Self {
y_flip: false,
gpu_pow: false,
warp_time,
scale_inv: 1.0 / warp_scale,
f,
}
}
pub fn projectm(time: f32, warp_anim_speed: f32, warp_scale: f32) -> Self {
Self {
y_flip: true,
gpu_pow: true,
..Self::new(time, warp_anim_speed, warp_scale)
}
}
}
pub fn warp_vertex_uv(
g: &MeshGeometry,
n: usize,
v: &VertexVars,
anim: &WarpAnim,
texel_offset_uv: [f32; 2],
) -> [f32; 2] {
let vx = g.x[n];
let vy = g.y[n];
let wy = if anim.y_flip { -vy } else { vy };
let rad = g.rad[n];
let f = &anim.f;
let zoom2 = if anim.gpu_pow && v.zoom < 0.0 {
f32::NAN
} else {
v.zoom.powf(v.zoomexp.powf(rad * 2.0 - 1.0))
};
let zoom2_inv = 1.0 / zoom2;
let mut u = vx * g.aspect_x * 0.5 * zoom2_inv + 0.5;
let mut vv = -vy * g.aspect_y * 0.5 * zoom2_inv + 0.5;
u = (u - v.cx) / v.sx + v.cx;
vv = (vv - v.cy) / v.sy + v.cy;
let wt = anim.warp_time;
let si = anim.scale_inv;
u += v.warp * 0.0035 * (wt * 0.333 + si * (vx * f[0] - wy * f[3])).sin();
vv += v.warp * 0.0035 * (wt * 0.375 - si * (vx * f[2] + wy * f[1])).cos();
u += v.warp * 0.0035 * (wt * 0.753 - si * (vx * f[1] - wy * f[2])).cos();
vv += v.warp * 0.0035 * (wt * 0.825 + si * (vx * f[0] + wy * f[3])).sin();
let u2 = u - v.cx;
let v2 = vv - v.cy;
let cos_rot = v.rot.cos();
let sin_rot = v.rot.sin();
u = u2 * cos_rot - v2 * sin_rot + v.cx;
vv = u2 * sin_rot + v2 * cos_rot + v.cy;
u -= v.dx;
vv -= v.dy;
u = (u - 0.5) * (1.0 / g.aspect_x) + 0.5;
vv = (vv - 0.5) * (1.0 / g.aspect_y) + 0.5;
u += texel_offset_uv[0];
vv += texel_offset_uv[1];
[u, vv]
}
pub fn warp_mesh_uniform(
g: &MeshGeometry,
v: &VertexVars,
anim: &WarpAnim,
texel_offset_uv: [f32; 2],
) -> Vec<[f32; 2]> {
(0..g.vertex_count())
.map(|n| warp_vertex_uv(g, n, v, anim, texel_offset_uv))
.collect()
}
pub fn texel_offset_uv(texel_offset: [f32; 2], tex_w: u32, tex_h: u32) -> [f32; 2] {
[
texel_offset[0] / tex_w as f32,
texel_offset[1] / tex_h as f32,
]
}
#[cfg(test)]
mod tests {
use super::*;
fn close(a: f32, b: f32) -> bool {
(a - b).abs() < 1e-5
}
#[test]
fn geometry_matches_md2_setup() {
let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
assert_eq!(g.vertex_count(), 49 * 37);
assert!(close(g.aspect_x, 1.0));
assert!(close(g.aspect_y, 360.0 / 640.0));
assert!(close(g.x[0], -1.0) && close(g.y[0], -1.0));
assert!(close(g.x[48], 1.0) && close(g.y[48], -1.0));
let last = g.vertex_count() - 1;
assert!(close(g.x[last], 1.0) && close(g.y[last], 1.0));
let c = g.index(24, 18);
assert!(close(g.rad[c], 0.0) && g.ang[c] == 0.0);
let corner = (1.0f32 + (360.0f32 / 640.0).powi(2)).sqrt();
assert!(close(g.rad[0], corner));
assert!(close(g.pv_y(0), 0.5 + 0.5 * g.aspect_y));
assert!(close(g.pv_x(0), 0.0));
}
#[test]
fn identity_motion_maps_to_screen_uv() {
let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
let anim = WarpAnim::new(3.0, 1.0, 1.0);
let v = VertexVars {
warp: 0.0,
..Default::default()
};
let uv = warp_mesh_uniform(&g, &v, &anim, [0.0, 0.0]);
for n in 0..g.vertex_count() {
assert!(close(uv[n][0], g.x[n] * 0.5 + 0.5), "u {n}");
assert!(close(uv[n][1], -g.y[n] * 0.5 + 0.5), "v {n}");
}
assert!(close(uv[0][1], 1.0));
}
#[test]
fn zoom_rot_translate_hand_computed() {
let g = MeshGeometry::new(2, 2, 100, 100).unwrap(); let anim = WarpAnim::new(0.0, 1.0, 1.0);
let v = VertexVars {
zoom: 2.0,
warp: 0.0,
..Default::default()
};
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));
let v = VertexVars {
rot: std::f32::consts::FRAC_PI_2,
warp: 0.0,
..Default::default()
};
let uv = warp_vertex_uv(&g, g.index(2, 2), &v, &anim, [0.0, 0.0]);
assert!(close(uv[0], 1.0) && close(uv[1], 1.0));
let v = VertexVars {
dx: 0.1,
dy: -0.2,
warp: 0.0,
..Default::default()
};
let uv = warp_vertex_uv(&g, g.index(1, 1), &v, &anim, [0.001, 0.002]);
assert!(close(uv[0], 0.5 - 0.1 + 0.001) && close(uv[1], 0.5 + 0.2 + 0.002));
}
#[test]
fn warp_terms_are_applied() {
let g = MeshGeometry::new(4, 4, 64, 64).unwrap();
let anim = WarpAnim::new(1.0, 1.0, 1.0);
assert!(close(anim.f[0], 11.68 + 4.0 * (1.413f32 + 10.0).cos()));
let a = warp_vertex_uv(
&g,
7,
&VertexVars {
warp: 0.0,
..Default::default()
},
&anim,
[0.0; 2],
);
let b = warp_vertex_uv(
&g,
7,
&VertexVars {
warp: 1.0,
..Default::default()
},
&anim,
[0.0; 2],
);
let (du, dv) = ((b[0] - a[0]).abs(), (b[1] - a[1]).abs());
assert!(
du > 0.0 && du < 0.0141 && dv < 0.0141,
"warp displacement bounded by 2*0.0035: {du} {dv}"
);
}
#[test]
fn projectm_vertex_follows_initialize_mesh() {
let g = MeshGeometry::new(48, 36, 640, 360).unwrap();
let (p0, rad0, _) = g.projectm_vertex(0, 0);
assert_eq!(p0, [-1.0, 1.0]);
assert!(close(rad0, (1.0f32 + (360.0f32 / 640.0).powi(2)).sqrt()));
let (pc, radc, angc) = g.projectm_vertex(24, 18);
assert_eq!((pc, radc, angc), ([0.0, 0.0], 0.0, 0.0));
let (p, rad, ang) = g.projectm_vertex(24, 34);
assert_eq!(p[1], 2.0f32 / 36.0 * 2.0 - 1.0);
assert_eq!(rad, (p[1] * g.aspect_y).abs());
assert!(ang < 0.0, "top half has negative atan2(y, x)");
assert_eq!(g.projectm_vertex(3, 18).2, std::f32::consts::PI);
}
#[test]
fn negative_zoom_is_nan_on_the_gpu_but_finite_in_md2() {
let g = MeshGeometry::new(4, 4, 64, 64).unwrap();
let v = VertexVars {
zoom: -0.5,
..Default::default()
};
let uv = warp_vertex_uv(&g, 7, &v, &WarpAnim::projectm(1.0, 1.0, 1.0), [0.0; 2]);
assert!(uv[0].is_nan() && uv[1].is_nan());
let uv = warp_vertex_uv(&g, 7, &v, &WarpAnim::new(1.0, 1.0, 1.0), [0.0; 2]);
assert!(uv[0].is_finite() && uv[1].is_finite());
let v = VertexVars {
zoom: 1.5,
..Default::default()
};
let uv = warp_vertex_uv(&g, 7, &v, &WarpAnim::projectm(1.0, 1.0, 1.0), [0.0; 2]);
assert!(uv[0].is_finite() && uv[1].is_finite());
}
}