OBJ format Model Read

Source: Internet
Author: User

Original address: http://blog.csdn.net/caca001/article/details/5207597

Obj file features

(1) obj is a 3D model file and therefore does not contain information such as animations, material properties, map paths, dynamics, particles, and so on.

(2) The obj file mainly supports the polygon (polygons) model.

Although the obj file also supports curves (Curves), surface (surfaces), point group materials, the obj file exported by Maya does not include this information.

(3) The obj file supports polygons with more than three points, which is useful.

(4) The obj file supports normal and mapping coordinates. After adjusting the map in other software, the mapping coordinates information can be stored in the obj file, so that the file import Maya only need to specify the map file path, no need to adjust the mapping coordinates.

(5) The obj file does not support polygon polygons with holes.

(6) OBJ files do not contain color definition information for polygons, but can reference material libraries, and material library information is stored in a separate file with a suffix of ". MTL". The keyword "Mtllib" is the meaning of the material library.

The prefix of the obj file

The obj file stores vertex, normal, and texture coordinates and material usage information for the model in plain text form.

In the obj file, each line is formatted as follows: Prefix parameter 1 parameter 2 parameter 3 ...

(The prefix identifies the type of information stored in this row.) The parameter is the specific data. )

The prefix for the obj file can be:

V indicates that the bank specifies a vertex. The prefix is followed by 3 single-precision floating-point numbers, representing the X, Y, and z coordinate values of the fixed point.

VT Indicates that the bank specifies a texture coordinate. The prefix is followed by two single-precision floating-point numbers. Represents the U, v value of this texture coordinate, respectively

VN indicates that the bank specifies a normal vector. This prefix is followed by 3 single-precision floating-point numbers, representing the X, Y, and z coordinate values of the normal vector, respectively.

F indicates that the bank specifies a surface (face). A surface is actually a triangular entity. The parameter format for this prefix line is described in more detail later.

USEMTL follows only one parameter after the prefix. This parameter specifies the name of the material used for all surfaces from this line to the next line starting with USEMTL. The material can be found in the MTL file attached to this obj file for specific information.

Mtllib follows only one parameter after the prefix. This parameter specifies the file path of the material library file (*.MTL) used by this obj file

The prefix for the comment that begins with "#", and the prefix for the group that begins with G. These prefixes do not affect the appearance of the model and can be ignored.

Creating a vertex index

Suppose Indices:array of integer is an array of vertex indices, Vertices:array of Tvertex is a vertex array, and all triangles corresponding to the entire vertex index can be drawn using the following code snippet:

Procedure Drawindex (Indices:array of Integer; Vertices:array of Tvertex);

var I:integer;

Begin

Glbegin (Gl_triangles);

For I: = 0 to (high (Vertices) +1) div 3-1 do

Begin

GLVERTEX3FV (@Vertices [indices[i*3]]);

GLVERTEX3FV (@Vertices [indices[i*3+1]]);

GLVERTEX3FV (@Vertices [indices[i*3+2]]);

End

Glend;

End

The obj file creates a corresponding index for all vertices, normals, and texture coordinates in the model to save more space.

Polygons can use negative indexes, and sometimes it is easier to describe polygons with negative indexes.

The index value "-3" of "f-4-3-2-1" means the 3rd vertex from the line "F",

Structure of the Obj file

In an obj file, first some lines that begin with a V, VT, or vn prefix specify all vertices, texture coordinates, and normals coordinates. The index of the vertices, texture coordinates, and normals corresponding to each triangle is then specified by some lines that begin with F. Between the vertices, the texture coordinates, and the index of the normals, using the symbol "/" separated. An F-line can appear in the following formats:

F 1 2 3

Represents a triangle consisting of vertices 1th, 2, 3rd.

F 1/3 2/5 3/4

Represents a triangle with vertices 1th, 2, 3rd, where the texture coordinates of the first vertex have an index value of 3, the second vertex has an index value of 5 for the texture coordinates, and a third vertex has an index value of 4 for the texture coordinates.

F 1/3/4 2/5/6 3/4/2

Represents a triangle with vertices 1th, 2, 3rd, where the texture coordinates of the first vertex have an index value of 3, the index value of its normals is 4, the index value of the second vertex is 5, the index value of its normals is 6, the index value of the third vertex's texture coordinates is 6, and the index value of its normals is 2.

F 1//4 2//6 3//2

Represents a triangle with vertices 1th, 2, 3rd, and the texture coordinates are ignored. The index value of the normals of the first vertex is 4, the index value of the normals of the second vertex is 6, and the third vertex has a normal index value of 2.

Note: The index value in the file starts with 1, and you should pay attention to subtracting 1 of the coordinate values that are read from the file when rendering.

The basic structure of the obj file

Point data (Vertex):

V Geometry vertex (geometric vertices)

VT Map coordinate point (Texture vertices)

VN vertex normals (Vertex normals)

VP parameter Space Vertex (Parameter space vertices)

--------------------------------------------------------

Free-form curve (free-form curve)/surface properties (surface attributes):

DEG degrees (degree)

Bmat Foundation Matrix (Basis matrix)

Step Dimension (step size)

Cstype curves or surface types (Curve or surfaces type)

--------------------------------------------------------

Element (Elements):

P Points (point)

L Lines (line)

F Surface (Face)

CURV Curve (Curve)

Curv2 2D curve (2D curve)

Surf surface (surface)

--------------------------------------------------------

Free form curve (free-form curve)/Surface body Statement (surface body statements):

Parm parameter value (Parameter values)

Trim external trim cycle (Outer trimming loop)

Hole internal refurbishment cycle (Inner trimming loop)

SCRV Special curve (special curve)

SP Special points (special point)

End statement (End statement)

--------------------------------------------------------

The connection between free-form surfaces (Connectivity between free-form surfaces):

Con connection (Connect)

--------------------------------------------------------

Group (Grouping):

Group G name (team name)

s smooth set (smoothing group)

MG Consolidation Group (merging)

O Object Name

--------------------------------------------------------

Display/render properties (render attributes):

Bevel angular interpolation (bevel interpolation)

C_interp colour interpolation (color interpolation)

D_interp dissolution interpolation (dissolve interpolation)

Lod levels of detail (level of detail)

USEMTL Material name (Material name)

Mtllib Material Gallery (Material library)

Shadow_obj cast Shadow (shadow casting)

Trace_obj ray Tracing (ray tracing)

CTech curve approximation technique (Curve approximation technique)

Stech Surface approximation technology (surface approximation technique)

OBJ format Model Read

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.