Crate nobject_rs[][src]

Expand description

Overview

nobject-rs is a library for parsing wavefront .obj and .mtl content. To this end, the crate exposes two methos:

  • load_obj
  • load_mtl

Both methods take the content of the respective files (.obj and .mtl), parse and then return a result with either some kind of parse error, or a struct containing the data.

Note that this crate leaves the responsibility of file I/O to the consuming application. For example, it’s possible to specify file names as attributes in the material, or file names as material libraries in the obj file. This library will NOT attempt to open and parse those files. It is left to the consuming application/library to take the file information from the results of the parse methods, find and open the appropriate files, and then pass on the contents to be parsed.

Reference

Parsing is done based on the specification for Obj’s and Mtl’s found at:

Examples

Obj parsing

fn main() {
    let input =
    "
    o 1
    v -0.5 -0.5 0.5
    v -0.5 -0.5 -0.5
    v -0.5 0.5 -0.5
    v -0.5 0.5 0.5
    v 0.5 -0.5 0.5
    v 0.5 -0.5 -0.5
    v 0.5 0.5 -0.5
    v 0.5 0.5 0.5
     
    usemtl Default
    f 4 3 2 1
    f 2 6 5 1
    f 3 7 6 2
    f 8 7 3 4
    f 5 8 4 1
    f 6 7 8 5
    ";

    let res = nobject_rs::load_obj(&input).unwrap();
    let group = &res.groups["default"];
    let face_group = &res.faces["default"];
    assert_eq!(res.vertices.len(), 8);
    assert_eq!(group.material_name, "Default".to_string());
    assert_eq!(res.normals.len(), 0);
    assert_eq!(res.faces.len(), 1);
    assert_eq!(face_group.len(), 6);;
}

Mtl parsing

fn main() {
    let input =
    "newmtl frost_wind
    Ka 0.2 0.2 0.2
    Kd 0.6 0.6 0.6
    Ks 0.1 0.1 0.1
    d 1
    Ns 200
    illum 2
    map_d -mm 0.200 0.800 window.mps";

    let res = nobject_rs::load_mtl(&input).unwrap();
    assert_eq!(res.len(), 1);
}

Structs

BumpMap

Contains information specific to bump maps.

ColorCorrectedMap

Common settings for texture maps which can be color corrected.

Face

The primary purpose is to store the collection of elements (vertices/normals/texture coordinates) that compose a face. This also contains a smoothing group identifier, as specified by the obj file.

FaceElement

Holds the vertex/texture/normal indicies for a part of a face.

Group

Defines the settings that get applied to a group of faces.

Line

Contains the set of elements which compose a line.

LineElement

Contains the indicies for a line element.

Material

Defines a single material.

Model

This holds the end result of parsing an obj file. The default group for all models is “default”. That is to say, if no group is defined in a file, a “default” group will be used.

NonColorCorrectedMap

Common settings for texture maps which can not be color corrected.

Normal

Representation of normal data.

Point

Contains a set of id’s for the verticies which compose the point collection.

ReflectionMap

Reflection specific information.

Texture

Representation of texture data. v/w are optional.

Vertex

Representation of vertex data. The w component is optional.

Enums

ColorType

An enum for possible ways of specifying a material color

DisolveType

Enum for the possible ways to specify the disolve

MaterialError

A wrapper for an underlying error which occurred while parsing the token stream.

ModelError

A wrapper for an underlying error which occurred while parsing the token stream.

ObjError

The set of errors which might be generated.

Functions

load_mtl

Takes the content of an mtl file and parses it.

load_obj

Takes the content of an obj file and parses it.