aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-render/src/cockpit_course.rs
blob: 713c9ca64be879ee5b3a8db6e08ef3a0b7c78291 (plain) (blame)
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
//! Camera bearings drawn by the original cockpit instrument at 640×480.

use std::f32::consts::{FRAC_PI_2, PI};

pub(crate) const CENTER: [f32; 2] = [320.0, 396.0];
const HALF_MARKER_ANGLE: f32 = 0.08726647;

/// Front and rear marker corners in the original triangle-strip order.
#[must_use]
pub fn bearing_markers([x, y]: [f32; 2]) -> [[[f32; 2]; 4]; 2] {
    let front = y.atan2(x) - FRAC_PI_2;
    [front, front + PI].map(|bearing| {
        // Unlike the sector, the marker projects sine onto X and cosine onto Y.
        let left = FRAC_PI_2 - (bearing - HALF_MARKER_ANGLE);
        let right = FRAC_PI_2 - (bearing + HALF_MARKER_ANGLE);
        [
            radial_point(CENTER, left, 68.0),
            radial_point(CENTER, left, 56.0),
            radial_point(CENTER, right, 68.0),
            radial_point(CENTER, right, 56.0),
        ]
    })
}

/// Positions and page7 UVs of the sector that represents the view aperture.
#[must_use]
pub fn view_sector(horizontal_fov: f32) -> [([f32; 2], [f32; 2]); 3] {
    let half = horizontal_fov * 0.5;
    let edge = |side: f32| {
        let point = radial_point(CENTER, FRAC_PI_2 + side, 75.0);
        // The original uses -4.71228 here for the atlas angle.
        let uv = radial_point([192.0, 64.0], side - 4.71228, 64.0).map(|texel| texel / 256.0);
        (point, uv)
    };
    [edge(-half), (CENTER, [0.75, 0.25]), edge(half)]
}

fn radial_point([x, y]: [f32; 2], angle: f32, radius: f32) -> [f32; 2] {
    let (sin, cos) = angle.sin_cos();
    [x + cos * radius, y - sin * radius]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn matches_native_rear_marker_and_sector_in_the_stock_cockpit() {
        // The camera and last presented TL vertices are sampled outside the draw call.
        let [front, rear] = bearing_markers([0.5369722, -0.4331754]);
        let expected_rear = [
            [461.82346, 436.666],
            [450.91342, 446.96024],
            [471.0138, 448.28775],
            [458.482, 456.5311],
        ];
        for ((front, rear), native) in front.into_iter().zip(rear).zip(expected_rear) {
            near(rear.map(|axis| axis * 1.25), native, 1.0);
            near(
                [front[0] + rear[0], front[1] + rear[1]],
                [2.0 * CENTER[0], 2.0 * CENTER[1]],
                0.001,
            );
        }
        let native_sector = [
            ([456.7362, 420.36716], [0.9012749, 0.050962552]),
            ([400.0, 495.00006], [0.75, 0.25]),
            ([343.2638, 420.36716], [0.5986816, 0.05099559]),
        ];
        for ((point, uv), (native_point, native_uv)) in
            view_sector(1.3).into_iter().zip(native_sector)
        {
            near(point.map(|axis| axis * 1.25), native_point, 0.001);
            near(uv, native_uv, 0.000001);
        }
    }

    #[test]
    fn front_marker_follows_quadrant_and_ignores_view_pitch() {
        for (forward, expected) in [
            ([1.0, 0.0], [252.25876, 396.0]),
            ([0.0, 1.0], [320.0, 328.25876]),
            ([-1.0, 0.0], [387.74124, 396.0]),
            ([0.0, -1.0], [320.0, 463.74124]),
        ] {
            let [front, _] = bearing_markers(forward);
            let midpoint = [
                (front[0][0] + front[2][0]) * 0.5,
                (front[0][1] + front[2][1]) * 0.5,
            ];
            near(midpoint, expected, 0.001);
            assert_eq!(
                bearing_markers(forward.map(|axis| axis * 0.5)),
                bearing_markers(forward)
            );
        }
    }

    fn near(actual: [f32; 2], expected: [f32; 2], tolerance: f32) {
        assert!(
            actual
                .into_iter()
                .zip(expected)
                .all(|(a, b)| (a - b).abs() < tolerance),
            "{actual:?} vs {expected:?}"
        );
    }
}