aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-terrain/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-01 23:31:29 +0300
committerValentin Popov <valentin@popov.link>2026-12-01 23:31:29 +0300
commitb3d2b95b8dd5e4578e2889845809fe7c21c5886b (patch)
tree97c106a587c350cb82f05fc20c477c3e89f6aba9 /crates/fparkan-terrain/src
parentc3e2add9a189879b5e800344e07645af591a2cbd (diff)
downloadfparkan-b3d2b95b8dd5e4578e2889845809fe7c21c5886b.tar.xz
fparkan-b3d2b95b8dd5e4578e2889845809fe7c21c5886b.zip
feat(ui): show cockpit altitude above mission sea level
Diffstat (limited to 'crates/fparkan-terrain/src')
-rw-r--r--crates/fparkan-terrain/src/lib.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/fparkan-terrain/src/lib.rs b/crates/fparkan-terrain/src/lib.rs
index fd4a28a2..7e2ae588 100644
--- a/crates/fparkan-terrain/src/lib.rs
+++ b/crates/fparkan-terrain/src/lib.rs
@@ -33,6 +33,7 @@ pub struct TerrainWorld {
surfaces: Vec<RuntimeTriangle>,
surface_bvh: SurfaceBvh,
source_mesh: Option<LandMeshDocument>,
+ sea_level: Option<f32>,
basement_foundation: Option<BasementFoundation>,
}
@@ -299,6 +300,7 @@ impl TerrainWorld {
surfaces: Vec::new(),
surface_bvh: SurfaceBvh::default(),
source_mesh: None,
+ sea_level: None,
basement_foundation: None,
})
}
@@ -314,6 +316,7 @@ impl TerrainWorld {
surfaces: build_surfaces(mesh)?,
surface_bvh: SurfaceBvh::default(),
source_mesh: Some(mesh.clone()),
+ sea_level: authored_sea_level(mesh),
..Self::default()
}
.with_surface_bvh())
@@ -333,6 +336,7 @@ impl TerrainWorld {
world.surfaces = build_surfaces(mesh)?;
world.surface_bvh = SurfaceBvh::from_surfaces(&world.surfaces);
world.source_mesh = Some(mesh.clone());
+ world.sea_level = authored_sea_level(mesh);
Ok(world)
}
@@ -460,6 +464,12 @@ impl TerrainWorld {
self.source_mesh.as_ref()
}
+ /// The first vertex of the first authored water face, or −1 on a dry map.
+ #[must_use]
+ pub fn sea_level(&self) -> f32 {
+ self.sea_level.unwrap_or(-1.0)
+ }
+
/// Returns source terrain positions without exposing parser ownership to apps.
#[must_use]
pub fn source_positions(&self) -> Option<&[[f32; 3]]> {
@@ -1090,6 +1100,15 @@ impl RuntimeGrid {
}
}
+fn authored_sea_level(mesh: &LandMeshDocument) -> Option<f32> {
+ // Terrain+0xf2f7 scans the original faces before building its surface index.
+ let water = mesh
+ .faces
+ .iter()
+ .find(|face| face.flags.0 & 0x0002_0000 != 0)?;
+ Some(mesh.positions.get(usize::from(water.vertices[0]))?[2])
+}
+
fn build_surfaces(mesh: &LandMeshDocument) -> Result<Vec<RuntimeTriangle>, TerrainError> {
let assignments = mesh
.face_lod_assignments()
@@ -1879,6 +1898,19 @@ mod tests {
}
}
+ #[test]
+ fn sea_level_uses_the_first_water_face_in_source_order() {
+ let mut mesh = synthetic_land_mesh();
+ assert_eq!(
+ TerrainWorld::from_land_msh(&mesh).unwrap().sea_level(),
+ -1.0
+ );
+ mesh.faces[1].flags.0 |= 0x0002_0000;
+ assert_eq!(TerrainWorld::from_land_msh(&mesh).unwrap().sea_level(), 1.0);
+ mesh.faces[0].flags.0 |= 0x0002_0000;
+ assert_eq!(TerrainWorld::from_land_msh(&mesh).unwrap().sea_level(), 0.0);
+ }
+
fn synthetic_land_mesh() -> LandMeshDocument {
use fparkan_terrain_format::{TerrainSlotTable, TerrainStream};