diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 20:44:01 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 20:44:01 +0300 |
| commit | d8166352578d9305f15c9fb1ea9d686f4640d970 (patch) | |
| tree | 0b5a248cf52b247f08aaeca265517a842abb72ff /adapters/fparkan-render-vulkan/src | |
| parent | 78a63745f471c028c0ed67f133de9034e901c905 (diff) | |
| download | fparkan-d8166352578d9305f15c9fb1ea9d686f4640d970.tar.xz fparkan-d8166352578d9305f15c9fb1ea9d686f4640d970.zip | |
feat(camera): render the authored cockpit geometry
Sample state 0, group 4 from the active camera component. Keep its moving model attached to the unit and apply the CTLD camera projection only to those draw ranges.
Checked in the Constructor at the stock start and while looking sideways, plus the first cockpit in AutoDemo with Vulkan validation. The native depth ordering and matched warbot comparison remain open.
Diffstat (limited to 'adapters/fparkan-render-vulkan/src')
| -rw-r--r-- | adapters/fparkan-render-vulkan/src/asset_mesh.rs | 100 | ||||
| -rw-r--r-- | adapters/fparkan-render-vulkan/src/ffi.rs | 4 | ||||
| -rw-r--r-- | adapters/fparkan-render-vulkan/src/ffi/resources.rs | 2 | ||||
| -rw-r--r-- | adapters/fparkan-render-vulkan/src/ffi/smoke.rs | 53 | ||||
| -rw-r--r-- | adapters/fparkan-render-vulkan/src/ffi/smoke_types.rs | 1 |
5 files changed, 153 insertions, 7 deletions
diff --git a/adapters/fparkan-render-vulkan/src/asset_mesh.rs b/adapters/fparkan-render-vulkan/src/asset_mesh.rs index 06bd3be6..dffbe710 100644 --- a/adapters/fparkan-render-vulkan/src/asset_mesh.rs +++ b/adapters/fparkan-render-vulkan/src/asset_mesh.rs @@ -418,6 +418,7 @@ pub fn project_msh_to_static_mesh_in_world_space_with_node_fallback_poses( node38_fallback_hierarchy(model), None, false, + Group(0), ) } @@ -445,6 +446,7 @@ pub fn project_msh_to_static_mesh_in_world_space_with_node_sampled_poses( node38_sampled_hierarchy(model, animation_frame), None, false, + Group(0), ) } @@ -470,6 +472,7 @@ pub fn project_msh_to_static_mesh_in_world_space_with_node_pose_buffer( Some(node_poses.clone()), None, false, + Group(0), ) } @@ -497,6 +500,29 @@ pub fn project_msh_to_static_mesh_in_world_space_with_node_pose_buffer_and_mount rotation: mount_pose.rotation, }), true, + Group(0), + ) +} + +/// Projects the detail group authored for an in-cockpit view. +pub fn project_cockpit_msh( + model: &ModelAsset, + transform: LegacyIron3dEulerTransform, + scale: [f32; 3], + node_poses: &NodePoseBuffer, + mount_pose: Option<VulkanNodePose>, +) -> Result<VulkanStaticMesh, VulkanAssetMeshError> { + project_msh_to_static_mesh_in_world_space_with_node_poses( + model, + transform, + scale, + Some(node_poses.clone()), + mount_pose.map(|mount| Pose { + translation: mount.translation, + rotation: mount.rotation, + }), + mount_pose.is_some(), + Group(4), ) } @@ -615,6 +641,7 @@ pub fn project_msh_to_static_mesh_in_world_space_with_node_sampled_poses_and_mou rotation: mount_pose.rotation, }), true, + Group(0), ) } @@ -640,6 +667,7 @@ pub fn project_msh_to_static_mesh_in_world_space_with_node_fallback_poses_and_mo rotation: mount_pose.rotation, }), true, + Group(0), ) } @@ -650,7 +678,13 @@ fn project_msh_to_static_mesh_in_world_space_with_node_poses( hierarchy: Option<fparkan_animation::NodePoseBuffer>, mount_pose: Option<Pose>, hide_root_node: bool, + group: Group, ) -> Result<VulkanStaticMesh, VulkanAssetMeshError> { + let empty = || VulkanStaticMesh { + vertices: Vec::new(), + indices: Vec::new(), + draw_ranges: Vec::new(), + }; if let Some(hierarchy) = hierarchy.as_ref() { if hierarchy.poses.len() != model.node_count { return Err(VulkanAssetMeshError::NodePoseCountMismatch { @@ -669,6 +703,9 @@ fn project_msh_to_static_mesh_in_world_space_with_node_poses( return Err(VulkanAssetMeshError::NonFinitePosition); } if model.node_stride != 38 || model.node_count == 0 || model.animation.is_none() { + if group != Group(0) { + return Ok(empty()); + } return project_msh_to_static_mesh_in_world_space_with_transform_and_root_pose( model, transform, scale, mount_pose, ); @@ -679,6 +716,9 @@ fn project_msh_to_static_mesh_in_world_space_with_node_poses( hierarchy }; let Some(hierarchy) = hierarchy else { + if group != Group(0) { + return Ok(empty()); + } return project_msh_to_static_mesh_in_world_space_with_transform_and_root_pose( model, transform, scale, mount_pose, ); @@ -690,7 +730,7 @@ fn project_msh_to_static_mesh_in_world_space_with_node_poses( for node_index in 0..model.node_count { let node = NodeId(u32::try_from(node_index).map_err(|_| VulkanAssetMeshError::IndexOutOfRange)?); - let Some(slot) = selected_slot(model, node, Lod(0), Group(0)) else { + let Some(slot) = selected_slot(model, node, Lod(0), group) else { continue; }; if hide_root_node && node_index == 0 { @@ -1492,6 +1532,64 @@ mod tests { } #[test] + fn cockpit_projection_does_not_include_the_exterior_slot() { + let mut source = model( + vec![[0.0; 3], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], + vec![0, 1, 2, 2, 1, 0], + vec![ + batch(0, 3, 0), + Batch { + material_index: 7, + ..batch(3, 3, 0) + }, + ], + ); + source.node_stride = 38; + source.node_count = 1; + source.nodes_raw = vec![0xff; 38]; + source.nodes_raw[0..8].fill(0); + source.nodes_raw[2..4].copy_from_slice(&u16::MAX.to_le_bytes()); + source.nodes_raw[8..10].copy_from_slice(&0_u16.to_le_bytes()); + source.nodes_raw[16..18].copy_from_slice(&1_u16.to_le_bytes()); + source.slots = [0, 1] + .map(|batch_start| Slot { + tri_start: batch_start, + tri_count: 1, + batch_start, + batch_count: 1, + aabb_min: [0.0; 3], + aabb_max: [1.0; 3], + sphere_center: [0.0; 3], + sphere_radius: 1.0, + opaque: [0; 5], + }) + .into(); + source.animation = Some(ModelAnimation { + keys: vec![AnimKey24 { + time: AnimationTime(0.0), + pose: Pose::default(), + }], + frame_map: Vec::new(), + frame_count: 0, + }); + let poses = node38_fallback_hierarchy(&source).expect("node pose"); + let transform = LegacyIron3dEulerTransform { + translation: [10.0, 20.0, 30.0], + orientation_radians: [0.0; 3], + }; + let exterior = project_msh_to_static_mesh_in_world_space_with_node_pose_buffer( + &source, transform, [1.0; 3], &poses, + ) + .expect("exterior"); + let cockpit = + project_cockpit_msh(&source, transform, [1.0; 3], &poses, None).expect("cockpit"); + assert_eq!(exterior.draw_ranges.len(), 1); + assert_eq!(exterior.draw_ranges[0].material_index, 0); + assert_eq!(cockpit.draw_ranges.len(), 1); + assert_eq!(cockpit.draw_ranges[0].material_index, 7); + } + + #[test] fn projects_xy_geometry_and_applies_base_vertex() { let mesh = project_msh_to_static_mesh(&model( vec![ diff --git a/adapters/fparkan-render-vulkan/src/ffi.rs b/adapters/fparkan-render-vulkan/src/ffi.rs index 04be2d35..92e4291e 100644 --- a/adapters/fparkan-render-vulkan/src/ffi.rs +++ b/adapters/fparkan-render-vulkan/src/ffi.rs @@ -22,9 +22,11 @@ mod swapchain; mod swapchain_resources; mod validation; +const FRAME_VIEWS_PER_SLOT: usize = 3; + pub use self::asset_mesh::{ node38_pose, node38_pose_from_hierarchy, node38_pose_relative_to_root, - node38_pose_relative_to_root_from_hierarchy, + node38_pose_relative_to_root_from_hierarchy, project_cockpit_msh, project_land_msh_lod_to_static_mesh_in_world_space, project_land_msh_to_static_mesh, project_land_msh_to_static_mesh_in_world_space, project_land_msh_to_static_mesh_in_xy_frame, project_msh_to_static_mesh, project_msh_to_static_mesh_in_world_space, diff --git a/adapters/fparkan-render-vulkan/src/ffi/resources.rs b/adapters/fparkan-render-vulkan/src/ffi/resources.rs index 449750fe..5916c87f 100644 --- a/adapters/fparkan-render-vulkan/src/ffi/resources.rs +++ b/adapters/fparkan-render-vulkan/src/ffi/resources.rs @@ -545,7 +545,7 @@ pub(super) fn create_frame_uniform_buffer( )? / alignment * alignment; let size = stride - .checked_mul(2 * super::VULKAN_FRAME_SLOT_COUNT as u64) + .checked_mul((super::FRAME_VIEWS_PER_SLOT * super::VULKAN_FRAME_SLOT_COUNT) as u64) .ok_or(VulkanSmokeRendererError::InvalidStaticMesh { context: "frame uniform buffer size overflow", })?; diff --git a/adapters/fparkan-render-vulkan/src/ffi/smoke.rs b/adapters/fparkan-render-vulkan/src/ffi/smoke.rs index 06d1d4de..121ffd25 100644 --- a/adapters/fparkan-render-vulkan/src/ffi/smoke.rs +++ b/adapters/fparkan-render-vulkan/src/ffi/smoke.rs @@ -432,6 +432,7 @@ impl VulkanSmokeRenderer { readback_enabled: false, camera: create_info.camera, frame_uniforms, + cockpit_camera: None, point_lights: Vec::new(), point_lights_explicit: false, frame_sync: Vec::new(), @@ -588,6 +589,28 @@ impl VulkanSmokeRenderer { Ok(()) } + /// Uses a separate projection for the active cockpit's model. The cockpit + /// is visible in the main view and excluded from planar reflections. + pub fn set_cockpit_camera( + &mut self, + camera: Option<(std::ops::Range<usize>, super::VulkanStaticCamera)>, + ) -> Result<(), VulkanSmokeRendererError> { + if let Some((ranges, camera)) = &camera { + if !camera.is_finite() { + return Err(VulkanSmokeRendererError::InvalidStaticCamera { + context: "non-finite cockpit clip_from_world matrix", + }); + } + if ranges.is_empty() || ranges.end > self.draw_ranges.len() { + return Err(VulkanSmokeRendererError::InvalidStaticMesh { + context: "invalid cockpit draw ranges", + }); + } + } + self.cockpit_camera = camera; + Ok(()) + } + /// Replaces lighting, fog, camera-position and matrix state for /// subsequent frames without recreating GPU resources. /// @@ -1611,11 +1634,12 @@ impl VulkanSmokeRenderer { context: "swapchain resources during frame upload", }, )?; + let main_slot = self.current_frame * super::FRAME_VIEWS_PER_SLOT; write_frame_uniform( device, &resources.frame_uniform_buffer, resources.frame_uniform_stride, - self.current_frame * 2, + main_slot, &frame_bytes, )?; if let Some(bytes) = &reflection_bytes { @@ -1623,10 +1647,21 @@ impl VulkanSmokeRenderer { device, &resources.frame_uniform_buffer, resources.frame_uniform_stride, - self.current_frame * 2 + 1, + main_slot + 1, bytes, )?; } + if let Some((_, camera)) = &self.cockpit_camera { + let mut uniforms = self.frame_uniforms; + uniforms.clip_from_world = camera.clip_from_world; + write_frame_uniform( + device, + &resources.frame_uniform_buffer, + resources.frame_uniform_stride, + main_slot + 2, + &uniforms.to_ne_bytes(), + )?; + } write_point_light_buffer( device, &resources.point_light_buffer, @@ -1952,6 +1987,13 @@ impl VulkanSmokeRenderer { reflected && self.draw_ranges[index].terrain_reflection })); for range_index in draw_order { + let cockpit = self + .cockpit_camera + .as_ref() + .is_some_and(|(ranges, _)| ranges.contains(&range_index)); + if reflected && cockpit { + continue; + } let range = self.draw_ranges.get(range_index).ok_or( VulkanSmokeRendererError::InvariantViolation { context: "stable draw order range handle", @@ -1981,10 +2023,13 @@ impl VulkanSmokeRenderer { vk::PipelineBindPoint::GRAPHICS, *pipeline, ); + let view_slot = if cockpit { 2 } else { usize::from(reflected) }; let frame_offset = u32::try_from( resources.frame_uniform_stride.saturating_mul( - u64::try_from(self.current_frame * 2 + usize::from(reflected)) - .unwrap_or(u64::MAX), + u64::try_from( + self.current_frame * super::FRAME_VIEWS_PER_SLOT + view_slot, + ) + .unwrap_or(u64::MAX), ), ) .unwrap_or(u32::MAX); diff --git a/adapters/fparkan-render-vulkan/src/ffi/smoke_types.rs b/adapters/fparkan-render-vulkan/src/ffi/smoke_types.rs index 643a5e3c..54b409d1 100644 --- a/adapters/fparkan-render-vulkan/src/ffi/smoke_types.rs +++ b/adapters/fparkan-render-vulkan/src/ffi/smoke_types.rs @@ -1623,6 +1623,7 @@ pub struct VulkanSmokeRenderer { pub(super) readback_enabled: bool, pub(super) camera: VulkanStaticCamera, pub(super) frame_uniforms: VulkanFrameUniforms, + pub(super) cockpit_camera: Option<(std::ops::Range<usize>, VulkanStaticCamera)>, /// Dynamically sized point-light records submitted by the client. pub(super) point_lights: Vec<VulkanPointLight>, /// Whether `set_point_lights` has replaced legacy `frame_uniforms.point_light`. |
