diff --git a/gen/bindings.rs b/gen/bindings.rs index 20cbe87..40a22a5 100644 --- a/gen/bindings.rs +++ b/gen/bindings.rs @@ -245,6 +245,8 @@ extern "C" { extern "C" { #[doc = " Experimental: Mesh simplifier"] #[doc = " Reduces the number of triangles in the mesh, attempting to preserve mesh appearance as much as possible"] + #[doc = " The algorithm tries to preserve mesh topology and can stop short of the target goal based on topology constraints or target error."] + #[doc = " If not all attributes from the input mesh are required, it\'s recommended to reindex the mesh using meshopt_generateShadowIndexBuffer prior to simplification."] #[doc = " Returns the number of indices after simplification, with destination containing new index data"] #[doc = " The resulting index buffer references vertices from the original vertex buffer."] #[doc = " If the original vertex data isn\'t required, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended."] @@ -262,6 +264,26 @@ extern "C" { target_error: f32, ) -> usize; } +extern "C" { + #[doc = " Experimental: Mesh simplifier (sloppy)"] + #[doc = " Reduces the number of triangles in the mesh, sacrificing mesh apperance for simplification performance"] + #[doc = " The algorithm doesn\'t preserve mesh topology but is always able to reach target triangle count."] + #[doc = " Returns the number of indices after simplification, with destination containing new index data"] + #[doc = " The resulting index buffer references vertices from the original vertex buffer."] + #[doc = " If the original vertex data isn\'t required, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended."] + #[doc = ""] + #[doc = " destination must contain enough space for the target index buffer"] + #[doc = " vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer"] + pub fn meshopt_simplifySloppy( + destination: *mut ::std::os::raw::c_uint, + indices: *const ::std::os::raw::c_uint, + index_count: usize, + vertex_positions: *const f32, + vertex_count: usize, + vertex_positions_stride: usize, + target_index_count: usize, + ) -> usize; +} extern "C" { #[doc = " Mesh stripifier"] #[doc = " Converts a previously vertex cache optimized triangle list to triangle strip, stitching strips using restart index"] @@ -431,9 +453,23 @@ extern "C" { } extern "C" { pub fn meshopt_computeMeshletBounds( - meshlet: meshopt_Meshlet, + meshlet: *const meshopt_Meshlet, vertex_positions: *const f32, vertex_count: usize, vertex_positions_stride: usize, ) -> meshopt_Bounds; } +extern "C" { + #[doc = " Experimental: Set allocation callbacks"] + #[doc = " These callbacks will be used instead of the default operator new/operator delete for all temporary allocations in the library."] + #[doc = " Note that all algorithms only allocate memory for temporary use."] + #[doc = " allocate/deallocate are always called in a stack-like order - last pointer to be allocated is deallocated first."] + pub fn meshopt_setAllocator( + allocate: ::std::option::Option< + unsafe extern "C" fn(allocate: usize) -> *mut ::std::os::raw::c_void, + >, + deallocate: ::std::option::Option< + unsafe extern "C" fn(allocate: *mut ::std::os::raw::c_void), + >, + ); +} diff --git a/src/clusterize.rs b/src/clusterize.rs index d44be9d..7b960b6 100644 --- a/src/clusterize.rs +++ b/src/clusterize.rs @@ -80,7 +80,7 @@ pub fn compute_meshlet_bounds(meshlet: &Meshlet, vertices: &[ let positions = vertices.as_ptr() as *const f32; unsafe { ffi::meshopt_computeMeshletBounds( - *meshlet, + meshlet, positions, vertices.len() * 3, ::std::mem::size_of::() * 3, diff --git a/src/simplify.rs b/src/simplify.rs index 57a2b30..49a8eac 100644 --- a/src/simplify.rs +++ b/src/simplify.rs @@ -3,8 +3,9 @@ use crate::DecodePosition; use std::mem; /// Reduces the number of triangles in the mesh, attempting to preserve mesh -/// appearance as much as possible. The resulting index buffer references vertices -/// from the original vertex buffer. +/// appearance as much as possible. +/// +/// The resulting index buffer references vertices from the original vertex buffer. /// /// If the original vertex data isn't required, creating a compact vertex buffer /// using `optimize_vertex_fetch` is recommended. @@ -34,3 +35,35 @@ pub fn simplify( result.resize(index_count, 0u32); result } + +/// Reduces the number of triangles in the mesh, sacrificing mesh appearance for simplification performance. +/// The algorithm doesn't preserve mesh topology but is always able to reach target triangle count. +/// +/// The resulting index buffer references vertices from the original vertex buffer. +/// +/// If the original vertex data isn't required, creating a compact vertex buffer using `optimize_vertex_fetch` +/// is recommended. +pub fn simplify_sloppy( + indices: &[u32], + vertices: &[T], + target_count: usize, +) -> Vec { + let positions = vertices + .iter() + .map(|vertex| vertex.decode_position()) + .collect::>(); + let mut result: Vec = vec![0; indices.len()]; + let index_count = unsafe { + ffi::meshopt_simplifySloppy( + result.as_mut_ptr() as *mut ::std::os::raw::c_uint, + indices.as_ptr() as *const ::std::os::raw::c_uint, + indices.len(), + positions.as_ptr() as *const f32, + positions.len(), + mem::size_of::() * 3, + target_count, + ) + }; + result.resize(index_count, 0u32); + result +}