aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-render/src/designer_lighting.rs
blob: 7103f77b4afcc847e2e0337f7c3986d4d996da9c (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
//! The constructor's local lamps, evaluated in the unrotated model's coordinates.

/// Elevation of the preview camera, equivalent to the original model pitch.
pub const ELEVATION: f32 = 0.5;
/// RGB of each original directional lamp.
pub const LIGHT_RGB: [f32; 3] = [2.0; 3];

/// Returns the two lamp directions with the inverse of the model's spin and pitch applied.
#[must_use]
pub fn light_directions(yaw: f64) -> [[f32; 3]; 2] {
    let (sin_yaw, cos_yaw) = yaw.sin_cos();
    let (sin_elevation, cos_elevation) = ELEVATION.sin_cos();
    let diagonal = std::f32::consts::FRAC_1_SQRT_2;
    // iron3d+0x9e780 supplies normalized (-1, 0, -1) and (1, 0, -1).
    [-diagonal, diagonal].map(|x| {
        let tilted_x = cos_elevation * x - sin_elevation * diagonal;
        let z = -sin_elevation * x - cos_elevation * diagonal;
        [tilted_x * cos_yaw as f32, -tilted_x * sin_yaw as f32, z]
    })
}

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

    #[test]
    fn lamps_match_the_native_light_setter_before_and_after_rotation() {
        // Terrain.dll+0x4c4d0 with the preview's mode-2 transform.
        for (yaw, expected) in [
            (
                0.0,
                [[-0.959550, 0.0, -0.281539], [0.281539, 0.0, -0.959550]],
            ),
            (
                0.75,
                [
                    [-0.702092, 0.654066, -0.281539],
                    [0.205999, -0.191908, -0.959550],
                ],
            ),
        ] {
            for (actual, expected) in light_directions(yaw)
                .into_iter()
                .flatten()
                .zip(expected.into_iter().flatten())
            {
                assert!((actual - expected).abs() < 1.0e-6, "{actual} != {expected}");
            }
        }
    }
}