obj-rs icon indicating copy to clipboard operation
obj-rs copied to clipboard

Better support for custom vertex formats

Open wolfiestyle opened this issue 3 years ago • 1 comments

As of current version, the loading interface looks like this:

pub fn load_obj<V: FromRawVertex<I>, T: BufRead, I>(input: T) -> ObjResult<Obj<V, I>>

so i'm like "yeah, i can plug my own vertex format too!", but when i look at the FromRawVertex trait, i see this:

pub trait FromRawVertex<I>: Sized {
    /// Build vertex and index buffer from raw object data.
    fn process(
        vertices: Vec<(f32, f32, f32, f32)>,
        normals: Vec<(f32, f32, f32)>,
        tex_coords: Vec<(f32, f32, f32)>,
        polygons: Vec<Polygon>,
    ) -> ObjResult<(Vec<Self>, Vec<I>)>;
}

and the vertex from raw conversion in done in an opaque way inside the library, so if I wanted to implement my own vertex format, I would have to re-write all this conversion code myself or copy-paste it from the source.

A better way support user-defined custom vertex formats would be proving a trait like:

pub trait FromRawVertex {
    type Output;

    fn convert_vertex(position: [f32; 4], normal: [f32; 3], tex_coord: [f32; 3]) -> Self::Output;
}

that can be used convert each vertex individually, then have a standalone conversion function like:

fn process_vertices<T: FromRawVertex, I>(raw: RawObj) -> ObjResult<(Vec<T::Output>, Vec<I>)>

Maybe i'm missing some stuff, but that's what I understood so far.

wolfiestyle avatar Feb 21 '23 15:02 wolfiestyle

I got the point but it was to avoid performance overhead caused by the interface. Maybe exposing both interfaces can be convenient.

simnalamburt avatar Mar 30 '23 19:03 simnalamburt