Struct tobj::Mesh

source ·
pub struct Mesh {
    pub positions: Vec<f64>,
    pub vertex_color: Vec<f64>,
    pub normals: Vec<f64>,
    pub texcoords: Vec<f64>,
    pub indices: Vec<u32>,
    pub face_arities: Vec<u32>,
    pub vertex_color_indices: Vec<u32>,
    pub texcoord_indices: Vec<u32>,
    pub normal_indices: Vec<u32>,
    pub material_id: Option<usize>,
}
Expand description

A mesh made up of triangles loaded from some OBJ file.

It is assumed that all meshes will at least have positions, but normals and texture coordinates are optional. If no normals or texture coordinates where found then the corresponding Vecs in the Mesh will be empty. Values are stored packed as f32s (or f64s with the use_f64 feature) in flat Vecs.

For examples the positions member of a loaded mesh will contain [x, y, z, x, y, z, ...] which you can then use however you like. Indices are also loaded and may re-use vertices already existing in the mesh. This data is stored in the indices member.

§Example:

Load the Cornell box and get the attributes of the first vertex. It’s assumed all meshes will have positions (required), but normals and texture coordinates are optional, in which case the corresponding Vec will be empty.

let cornell_box = tobj::load_obj("obj/cornell_box.obj", &tobj::GPU_LOAD_OPTIONS);
assert!(cornell_box.is_ok());

let (models, materials) = cornell_box.unwrap();

let mesh = &models[0].mesh;
let i = mesh.indices[0] as usize;

// pos = [x, y, z]
let pos = [
    mesh.positions[i * 3],
    mesh.positions[i * 3 + 1],
    mesh.positions[i * 3 + 2],
];

if !mesh.normals.is_empty() {
    // normal = [x, y, z]
    let normal = [
        mesh.normals[i * 3],
        mesh.normals[i * 3 + 1],
        mesh.normals[i * 3 + 2],
    ];
}

if !mesh.texcoords.is_empty() {
    // texcoord = [u, v];
    let texcoord = [mesh.texcoords[i * 2], mesh.texcoords[i * 2 + 1]];
}

Fields§

§positions: Vec<f64>

Flattened 3 component floating point vectors, storing positions of vertices in the mesh.

§vertex_color: Vec<f64>

Flattened 3 component floating point vectors, storing the color associated with the vertices in the mesh.

Most meshes do not have vertex colors. If no vertex colors are specified this will be empty.

§normals: Vec<f64>

Flattened 3 component floating point vectors, storing normals of vertices in the mesh.

Not all meshes have normals. If no normals are specified this will be empty.

§texcoords: Vec<f64>

Flattened 2 component floating point vectors, storing texture coordinates of vertices in the mesh.

Not all meshes have texture coordinates. If no texture coordinates are specified this will be empty.

§indices: Vec<u32>

Indices for vertices of each face. If loaded with triangulate set to true each face in the mesh is a triangle.

Otherwise face_arities indicates how many indices are used by each face.

When single_index is set to true, these indices are for all of the data in the mesh. Positions, normals and texture coordinaes. Otherwise normals and texture coordinates have their own indices, each.

§face_arities: Vec<u32>

The number of vertices (arity) of each face. Empty if loaded with triangulate set to true or if the mesh constists only of triangles.

The offset for the starting index of a face can be found by iterating through the face_arities until reaching the desired face, accumulating the number of vertices used so far.

§vertex_color_indices: Vec<u32>

The indices for vertex colors. Only present when the merging feature is enabled, and empty unless the corresponding load option is set to true.

§texcoord_indices: Vec<u32>

The indices for texture coordinates. Can be omitted by setting single_index to true.

§normal_indices: Vec<u32>

The indices for normals. Can be omitted by setting single_index to true.

§material_id: Option<usize>

Optional material id associated with this mesh. The material id indexes into the Vec of Materials loaded from the associated MTL file

Trait Implementations§

source§

impl Clone for Mesh

source§

fn clone(&self) -> Mesh

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Mesh

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Mesh

source§

fn default() -> Mesh

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Mesh

§

impl RefUnwindSafe for Mesh

§

impl Send for Mesh

§

impl Sync for Mesh

§

impl Unpin for Mesh

§

impl UnwindSafe for Mesh

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.