Skip to content

Commit

Permalink
Added simplify_sloppy, and updated FFI bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
gwihlidal committed Mar 30, 2019
1 parent dd2af91 commit 6897172
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
38 changes: 37 additions & 1 deletion gen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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."]
Expand All @@ -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"]
Expand Down Expand Up @@ -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),
>,
);
}
2 changes: 1 addition & 1 deletion src/clusterize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn compute_meshlet_bounds<T: DecodePosition>(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::<f32>() * 3,
Expand Down
37 changes: 35 additions & 2 deletions src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -34,3 +35,35 @@ pub fn simplify<T: DecodePosition>(
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<T: DecodePosition>(
indices: &[u32],
vertices: &[T],
target_count: usize,
) -> Vec<u32> {
let positions = vertices
.iter()
.map(|vertex| vertex.decode_position())
.collect::<Vec<[f32; 3]>>();
let mut result: Vec<u32> = 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::<f32>() * 3,
target_count,
)
};
result.resize(index_count, 0u32);
result
}

0 comments on commit 6897172

Please sign in to comment.