Struct tobj::Mesh

source · []
pub struct Mesh {
    pub positions: Vec<f32>,
    pub vertex_color: Vec<f32>,
    pub normals: Vec<f32>,
    pub texcoords: Vec<f32>,
    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 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<f32>

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

vertex_color: Vec<f32>

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<f32>

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<f32>

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Create a new, empty mesh.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.