80 likes | 96 Views
The .obj model format is a text-based format developed by Alias Wavefront. This format allows for easy reading of vertices and faces from a file and displaying them. Each line in the file specifies the data type and is followed by the accompanying data. This example demonstrates the structure of a cube model.
E N D
The .obj Model Format Jason Goffeney 3/14/2006
Model Format • The .obj model format was developed by Alias Wavefront. • It is in text and about a simple as you can get. • For assignment 3 you will need to read vertices and faces from the file and display them.
Model Format • Each line in the file begins with a character string to specify its type and is followed by the data accompanying it. # - the line is a comment v - a vertex followed by x, y and z float coordinate info f - a face followed by integer indices of vertices starting with 1
Example • Here is a small example of a cube model. • The first line starts with a comment symbol # so it is discarded • The lines beginning with vdefine the x, y and z position of each vertex • The lines beginning with f are followed by the vertices it is composed of. These each represent the side of a cube so they have 4 vertices. • # Example • v 1.0 1.0 1.0 • v 1.0 1.0 -1.0 • v 1.0 -1.0 1.0 • v 1.0 -1.0 -1.0 • v -1.0 1.0 1.0 • v -1.0 1.0 -1.0 • v -1.0 -1.0 1.0 • v -1.0 -1.0 -1.0 • f 1 3 4 2 • f 5 7 8 6 • f 1 5 6 2 • f 3 7 8 4 • f 1 5 7 3 • f 2 6 8 4
Example 2 6 • # Example • v 1.0 1.0 1.0 • v 1.0 1.0 -1.0 • v 1.0 -1.0 1.0 • v 1.0 -1.0 -1.0 • v -1.0 1.0 1.0 • v -1.0 1.0 -1.0 • v -1.0 -1.0 1.0 • v -1.0 -1.0 -1.0 • f 1 3 4 2 • f 5 7 8 6 • f 1 5 6 2 • f 3 7 8 4 • f 1 5 7 3 • f 2 6 8 4 5 1 X 4 8 3 7 Z Y
Other Data Types • There are two other data types that may be held in a .obj file. • vnfloatfloatfloat defines the normal of the vertex at the equivalent index (the first vn matches the first v) • vt float float defines a texture coordinate for the vertex at the equivalent index (the first vt matches the first v)
Managing the Data • A model class should mirror the structure of the file • A vertex is composed of 3 floats • A face is composed of a list of integers • A model is a list of vertices and a list of faces
The Model Class • Some ideas for implementing a model class • A constructor that takes the file path as a parameter • An interior method to open the file, read each line and fill the appropriate data structures • A method to render the model by drawing each polygon using OpenGL commands (see GL_POLYGON)