io7m-smfj 0.5.1 Specification
Notational Conventions
Unicode
The specification makes reference to the Unicode character set which, at the time of writing, is at version 8.0.0. The specification often references specific Unicode characters, and does so using the standard notation U+NNNN, where N represents a hexadecimal digit. For example, U+03BB corresponds to the lowercase lambda symbol λ.
EBNF
The specification gives grammar definitions in ISO/IEC 14977:1996 Extended Backus-Naur form.
Haskell
Rather than rely on untyped and ambiguous mathematical notation, this documentation expresses all mathematics and type definitions in strict
Haskell 2010 with no extensions. All Haskell sources are included along with the documentation and can therefore be executed from the command line
GHCi tool in order to interactively check results and experiment with functions.
When used within prose, functions are referred to using fully qualified notation, such as (Vector3f.cross n t). This is the application of the cross function defined in the Vector3f module, to the arguments n and t.
JPRA
Definitions of binary structures are given as
jpra language definitions. All values are considered to be in big-endian byte order unless otherwise specified.
Model
Overview
The SMF model is a minimalist, portable storage model for
triangle mesh data. SMF files are intended to be consumed by 3D rendering engines directly and therefore do not contain any complex interchange features common to formats such as COLLADA . The
text and
binary encodings of the model are designed to be trivial to parse and to permit the easy construction of extremely fast event-based parsers that do not require loading the entire file into memory for processing.
Mesh
A
mesh in the SMF model is a set of
triangles.
Attributes
An attribute in the SMF model is a uniquely-named array of elements of a given type. All attributes within a particular SMF file have the same number of elements.
Implementations are required to support attributes of at least the following types:
Vertices
A
vertex is an abstract object consisting of exactly one element taken from each of the defined
attributes. A vertex can essentially be considered to be an array index; The vertex at index
n can be considered to be the aggregation of the
nth elements of all of the defined attributes. Vertices are numbered starting at
0.
Triangles
A triangle is a 3-tuple of
vertices. In the SMF model, a triangle references vertices by their numeric index.
Coordinate System
A coordinate system in the SMF model is a set of three axis names and a triangle winding order. The axis names specify which Cartesian coordinate system axes correspond to the right, up, and forward directions. The winding order specifies whether vertices for triangles are given in clockwise or counter-clockwise order. A coordinate system is only valid if the three chosen axes have different axis names. The axisName function relates an axis to an axis name. The axisValid function gives a formal definition of the validity of sets of axes.
Schema ID
A
schema identifier is an optional identifier that can be inserted into SMF files. Because the SMF model does not predefine any particular
attributes, tools that consume SMF files cannot know ahead of time if the file they have just loaded will actually contain the attributes that they are expecting. A
schema identifier effectively provides a concrete name for a set of attributes so that tools that process SMF files can perform validation of attributes based on the identifier. It is somewhat analogous to XML namespace declarations; The author of a particular document inserts an XML namespace identifier into their document, and validation tools use this identifier to locate schema definitions against which the document is then validated.
A schema identifier consists of four integer values:
The vendor_id uniquely identifiers the vendor, the schema_id uniquely identifies the file type, and the schema_version_major and schema_version_minor values identify the version of the schema.
Metadata
The SMF model supports the inclusion of arbitrary metadata. A
metadata value is an opaque data value with
vendor and
schema identifiers specified in the same manner as the
schema identifier. Applications may use the metadata facilities to associate small amounts of extra information with mesh data. Typical uses include checksums, copyright information, and generating software package version information.
SMFT - Text Encoding
Overview
The
SMFT format is a text encoding for the
SMF model. The format is separated into a
header and
data section.
The format is line-based, with one command per line. Lines (excluding the very first line in the file) containing only whitespace, or lines beginning with U+0023 # are ignored.
Header
The first line of an
SMFT file MUST consist of an
smf command. After the initial command, the header consists of any number of
header commands and is terminated by the
data command.
A header command is one of the following:
Header - smf
The smf command specifies the major and minor version of the specification to which the rest of the file is expected to conform. The first argument specifies the major version, and the second argument specifies the minor version. Implementations are required to immediately reject files that are not of supported versions, and are required to halt processing of the rest of the file in the case of syntax errors.
The command takes the following arguments:
Header - vertices
The
vertices command specifies the number of
vertices that will appear in the
data section for each attribute.
The command takes the following arguments:
The command is required to appear exactly once in the header.
Header - schema
The command takes the following arguments:
The command is allowed to appear at most once in the header. If the command is not specified, a default schema identifier consisting of all zeroes is implicitly defined.
Header - triangles
The
triangles command specifies the number of
triangles that will appear in the
data section of the file, and the size of the vertex indices in bits.
The command takes the following arguments:
The command is required to appear exactly once in the header.
Header - attribute
The
attribute command specifies an
attribute that will appear in the
data section of the file.
The command takes the following arguments:
The command is allowed to appear any number of times in the header, but all specified attribute names must be unique.
Header - coordinates
The
coordinates command specifies the
coordinate space of the mesh data that will follow in the
data section of the file.
The command takes the following arguments:
Header - meta
The command takes the following arguments:
The command is allowed to appear at most once in the header.
Data
The data section of the file specifies the data for attributes and triangles. The section begins with the
data command and consists of both data and
data commands.
A data command is one of the following:
Data - data
The data command indicates the start of the data section.
The command takes no arguments.
Data - attribute
The attribute command indicates the start of data for an attribute.
The command takes the following arguments:
The named attribute must have been declared in the header. For an attribute a that specifies c components of type t, in a file that is specified to contain v vertices, the next v non-empty, non-commented lines will contain exactly c whitespace-separated values of type t.
Data - triangles
The triangles command indicates the start of triangle data.
The command takes no arguments.
In a file that is specified to contain k triangles, the next k non-empty, non-commented lines will contain exactly 3 whitespace-separated unsigned integer values each representing the index of a vertex.
Data - metadata
The metadata command indicates the start of metadata.
The command takes no arguments.
Data - meta
The meta command indicates the start of the file metadata.
The command takes the following arguments:
For any
v,
s, and
n, the command
meta v s n will indicate that the next
n lines will contain
RFC4648 base64url encoded data of a type with vendor
v and schema
s.
In a file that has declared in the header that
m values will appear in the data section, the
meta command must be present exactly
m times, in any order, following a single
metadata command.
Example
The following is a complete example of an SMFT file:
SMFB - Binary Encoding
Header
The
SMFB format is a binary encoding for the
SMF model. The format is separated into a
header and
data section.
All SMFB files begin with a fixed header. The start of the header uses a fixed magic number and a major/minor version number pair indicating the major and minor versions of the specification that defines the file structure.
The magic number MUST always be 0x89534d460d0a1a0a; implementations are required to immediately reject any files that do not begin with this magic number. The derivation of this constant is taken almost verbatim from the PNG file format with the characters PNG changed to SMF .
The rest of the header is given by the SMFBV1Header type:
The
SMFBV1SchemaID type encodes the
schema ID:
The axis and winding order values are related to integers in the binary encoding via the following functions:
The header specifies the number of
vertices present in the file, the number of
triangles present in the file, the size in bits of the individual triangle indices, and the number of
attributes present in the file.
The header concludes by giving definitions for exactly n attributes, where n is the attribute count specified in the previous section of the header.
The header is constructed with no implicit padding and no particular alignment. However, the individual header structures are constructed such that their sizes are always a multiple of
8. This ensures that the
data that immediately follows the header also starts on an
8 octet boundary.
Data
After the
header, the data for each
attribute is given in full in the order in which the attributes were declared in the header. Specifically, in a file containing
v vertices and a list of
n attributes
a, the file will contain
v values of the type specified in
a !! 0, followed by
v values of the type specified by
a !! 1, and so on up to
a !! n - 1.
The start of the data for each attribute is aligned to the next 8 octet boundary regardless of type. The alignment is achieved by inserting padding octets at the end of the previous attribute. For example, the following diagram shows how the data is aligned for a file containing three vertices with two attributes A and B. Attribute A consists of three 16-bit floating-point components, and attribute B consists of three 32-bit floating-point components. As the file has three vertices, 3 * 3 = 9 values are placed into the file first, consuming 3 * 3 * 2 = 18 octets. Because the next octet, 18, is not divisible by 8, it's necessary to insert 6 octets of padding so that the data for B will start on the next 8 octet boundary.
All values are stored in big-endian form.
After all triangle and attribute data has been specified, the
metadata values are specified. A metadata value begins with the following structure:
The vendor and schema fields are for use by applications to identify the types of metadata. The size field records the size of the metadata in octets.
This structure is followed by the opaque metadata octets with any additional
padding to ensure that the next metadata value occurs on an 8 octet boundary. The padding is not included in the recorded size of the metadata.