Detailed description of OBJ file format in 3D

Source: Internet
Author: User

Reprinted from: http://www.cppblog.com/lovedday/archive/2008/06/13/53153.html

 

OBJ file is a standard 3D model file format developed by alias | wavefront for its workstation-based 3D modeling and animation software "Advanced visualizer, it is suitable for intercommunication between 3D software models, and can be read and written through Maya. For example, if you create a model in 3dsmax or Lightwave and want to adjust it to rendering or animation in Maya, exporting the OBJ file is a good choice. Currently, almost all well-known 3D software supports reading and writing OBJ files, but many of them need to be implemented through plug-ins.

An OBJ file is a text file that can be opened directly on the WordPad for viewing, editing, and modification. In addition, there is a binary file format (*. mod) related to this, which is not disclosed as a patent, so it is not discussed here.

1. Features of OBJ files

The obj3.0 file format supports line, polygon, surface, and free-form curve ). Straight Lines and polygon are described by their points. curves and surfaces are defined based on their control points and additional information that is attached to the curve type. These information supports regular and irregular curves, these include curves based on the besserl curve, B-spline, Cardinal/Catmull-Rom, and Taylor equations. Other features are as follows:

(1) The OBJ file is a 3D model file. It does not contain animation, material properties, texture paths, dynamics, particles, and other information.

(2) The OBJ file mainly supports polygons models. Although curves, surfaces, and point group materials are also supported, the OBJ file exported by Maya does not include this information.

(3) The OBJ file supports three or more vertices, which is useful. Many other model file formats only support three vertices. Therefore, models imported into Maya are often triangular, which is inconvenient for us to re-process the models.

(4) OBJ files support normal and texture coordinates. After the Paster is adjusted in other software, the Paster coordinate information can be saved to the OBJ file. After the file is imported to Maya, you only need to specify the path of the Paster file. You do not need to adjust the Paster coordinates.
 

2. Basic Structure of the OBJ file

The OBJ file does not need any file header, although the comments of several lines of file information are often used as the beginning of the file. An OBJ file consists of one line of text. The comment line starts with the symbol "#". Spaces and blank lines can be added to the file at will to increase the readability of the file. A row with words starts with one or two marked letters, that is, the key word. A keyword can indicate the data of a row. Multiple rows can be logically connected together to represent a row by adding a connector (\) at the end of each row (\). Note that there cannot be spaces or tabs after the connector (\); otherwise, an error will occur in the file.

The following keywords can be used in the OBJ file. In this list, keywords are arranged according to the data type. Each keyword has a brief description.

Vertex data ):
V Ric vertices)
VT texture coordinate point (texture vertices)
Vn vertex normal (vertex normals)
VP parameter space vertex (parameter space vertices)

Free-form curve/surface attributes ):
DEG (degree)
BMAT base matrix)
Step Size)
Cstype curve or surface type (curve or surface type)

Element (elements ):
P point (point)
L line)
F (FACE)
Curv curve (curve)
Curv2 2D curve (2d curve)
Surf Surface)

Free-form curve/surface body statements ):
Parm parameter value (parameter values)
Trim external trim Loop)
Inner trimming Loop)
Scrv special curve (special curve)
SP Special Point)
End statement)

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

Grouping ):
G group name)
S smooth group)
MG merge Group)
O Object Name)

Display/render attributes ):
Bevel Interpolation)
C_interp color interpolation (color interpolation)
D_interp dissolve interpolation (dissolve interpolation)
Level of detail)
Usemtl material name (material name)
Mtllib material library (material library)
Shadow_obj shadow (shadow casting)
Trace_obj Ray Tracing)
Ctech Curve Approximation Technology (Curve Approximation Technique)
Surface approximation technique)

 

3. OBJ file instance

It is really difficult to understand. The following is an example. Let's create an OBJ file with a quadrilateral content. However, this time we don't need 3D software, but we use a WordPad to create it. Open the WordPad and write the following five lines of code. Add a comment as appropriate. Save the file as a text file named "myobj. OBJ ".

V-0.58, 0.84, 0
V 2.68 1.17 0
V 2.84-2.03 0
V-1.92-2.89 0
F 1 2 3 4

Note: At the end of the code, you must press enter to switch the cursor to the next line, that is, add a line break (\ n ). Otherwise, the following error message is displayed:
// Error: Line 1: OBJ file line 5: Index out of range .//
// Error: Line 1: Error reading file .//

Import the "myobj. OBJ" file in Maya. You can see it and import a quadrilateral. The shape of this quadrilateral is completely determined by the preceding five lines of code.

 

Let's analyze the code. V-0.58, 0.84, 0

Four vertices are required to draw a quadrilateral. This is the first vertex. "v" indicates vertex and "-0.58" indicates the X axis coordinate value of the vertex, "0.84" is the Y axis coordinate value, and "0" is the Z axis coordinate value. Its index number is 1. The index number is used in the screen.
V 2.68 1.17 0
V 2.84-2.03 0
V-1.92-2.89 0
These are the second, third, and fourth vertices. Their index numbers are 2, 3, and 4, respectively.

F 1 2 3 4
Now on the screen, "F" indicates the index number of the first four vertices (FACE), 1, 2, and 3. Pay attention to the order of drawing the surface connection points, starting from the first point and connecting the second, third, and fourth points in sequence.

If the order of the connection is different, the planes are generated completely differently. For example, "F 1 2 4 3" produces an overlapping plane, for example.

The connection points of a plane are arranged clockwise or counterclockwise, which determines the direction of the plane's normal (the plane's anyway ). For example, the normal direction of the "F 1 2 3 4" plane is inside the normal direction of the "F 4 3 2 1" plane. An error in the connection point sequence of the above is an important cause for the broken surface of the import model.
A plane cannot contain more than two identical vertices, which is also the key to checking for errors in the OBJ file. For example, "F 1 2 3 4 3" has two identical vertices, and the index number is 3. Two identical vertices appear on one side, which may cause memory allocation errors.

Next we will study the OBJ file exported by Maya.

Create a polygon cube in Maya, select the cube, and select "file-> export selection... "export format: OBJ, file name:" cube. OBJ ". If this format is not available, load" objexport. MLL ". Open "cube. OBJ" on the WordPad and you can see the following code:

# The units used in this file are centimeters.
G default
V-0.500000-0.500000 0.500000
V 0.500000-0.500000 0.500000
V-0.500000 0.500000 0.500000
V 0.500000 0.500000 0.500000
V-0.500000 0.500000-0.500000
V 0.500000 0.500000-0.500000
V-0.500000-0.500000-0.500000
V 0.500000-0.500000-0.500000
VT 0.000000 0.000000
VT 1.000000 0.000000
VT 0.000000 1.000000
VT 1.000000 1.000000
VT 0.000000 2.000000
VT 1.000000 2.000000
VT 0.000000 3.000000
VT 1.000000 3.000000
VT 0.000000 4.000000
VT 1.000000 4.000000
VT 2.000000 0.000000
VT 2.000000 1.000000
VT-1.000000 0.000000
VT-1.000000 1.000000
Vn 0.000000 0.000000 1.000000
Vn 0.000000 0.000000 1.000000
Vn 0.000000 0.000000 1.000000
Vn 0.000000 0.000000 1.000000
Vn 0.000000 1.000000 0.000000
Vn 0.000000 1.000000 0.000000
Vn 0.000000 1.000000 0.000000
Vn 0.000000 1.000000 0.000000
Vn 0.000000 0.000000-1.000000
Vn 0.000000 0.000000-1.000000
Vn 0.000000 0.000000-1.000000
Vn 0.000000 0.000000-1.000000
Vn 0.000000-1.000000 0.000000
Vn 0.000000-1.000000 0.000000
Vn 0.000000-1.000000 0.000000
Vn 0.000000-1.000000 0.000000
Vn 1.000000 0.000000 0.000000
Vn 1.000000 0.000000 0.000000
Vn 1.000000 0.000000 0.000000
Vn 1.000000 0.000000 0.000000
VPC-1.000000 0.000000 0.000000
VPC-1.000000 0.000000 0.000000
VPC-1.000000 0.000000 0.000000
VPC-1.000000 0.000000 0.000000
S off
G pcube1
Usemtl initialshadinggroup
F 1/1, 1 2/2, 2 4/4, 3 3/3, 4
F 3/3/5 4/4/6 6/6/7 5/5/8
F 5/5/9 6/6/10 8/8/11 7/7/12
F 7/7/13 8/8/14 2/10/15 1/9/16
F 2/2/17 8/11/18 6/12/19 4/4/20
F 7/13/21 1/1/22 3/3/23 5/14/24
This file looks a little complicated and uses many keywords. You can view the meaning of each keyword in the previous list. Let me explain:
"VT 1.000000 0.000000" indicates the texture coordinate of the point.
"VN 0.000000 0.000000-1.000000" indicates the normal of the vertex.
"S off" indicates that the smooth group is disabled.
"Usemtl initialshadinggroup" indicates the material used.
"F 7/13/21" in this case, the texture coordinate UV points and the normal index numbers are added to the data, and the index numbers are separated by the Left slash.
Format: "f vertex index/UV point index/normal index ".

"G pcube1" indicates a group. The group here is different from the group in Maya. The Group here refers to combining all the faces after "G pcube1, form an integral polygon ry.

Modify the "cube. OBJ" file to understand the meaning of grouping. Replace the code after "s off" with the following code:
Usemtl initialshadinggroup
G pcube_face1
F 1/1, 1 2/2, 2 4/4, 3 3/3, 4
G pcube_face2
F 3/3/5 4/4/6 6/6/7 5/5/8
G pcube_face3
F 5/5/9 6/6/10 8/8/11 7/7/12
G pcube_face4
F 7/7/13 8/8/14 2/10/15 1/9/16
G pcube_face5
F 2/2/17 8/11/18 6/12/19 4/4/20
G pcube_face6
F 7/13/21 1/1/22 3/3/23 5/14/24
After importing the image to Maya, we can see that each plane of the cube is separated, and each plane is named "pcube_face (1 ~ 6) ", the visible group name is actually the name of a separate ry.

Can I name the ry (Group) in Chinese? Try and change the previous code:
Usemtl initialshadinggroup
G cube surface 1
F 1/1, 1 2/2, 2 4/4, 3 3/3, 4
G cube surface 2
F 3/3/5 4/4/6 6/6/7 5/5/8
G cube surface 3
F 5/5/9 6/6/10 8/8/11 7/7/12
G cube surface 4
F 7/7/13 8/8/14 2/10/15 1/9/16
G cube surface 5
F 2/2/17 8/11/18 6/12/19 4/4/20
G cube surface 6
F 7/13/21 1/1/22 3/3/23 5/14/24

Try it and you will find that the model is successfully imported. Although the object names are all garbled, this is not a serious issue.
However, the use of the Chinese name is not always so smooth. Change the line "G cube surface 1" to "G select" and try again. The model cannot appear at the time of import, but only the following error message will appear:
// Error: Line 1: your obj file contains a line which is too long to be parsed. Please edit your obj file .//
// Error: Line 1: Error reading file .//
It can be seen that the nonstandard object naming is also one of the causes of errors in the OBJ file.
The name of an object in Maya is safe. Only the underscore (_) is available in punctuation marks, and numbers cannot start with a name, do not use double-byte text such as Chinese, Japanese, and Korean.
The OBJ file does not support polygon with holes.
For example:
Select the polygons creation tool (polygons-> Create polyon tool) of Maya and draw a quadrilateral in the view. Do not press enter and press Ctrl to click in the center of the Quadrilateral, you can continue to dig a hole in the Quadrilateral. Save the polygon with holes into the OBJ format. When importing the polygon to Maya, you will find that there is one less polygon. If you think of this as an error, At least you already know the cause of the error, that is, the OBJ file does not support polygon with holes.

 

4. Actual Problems with the OBJ file:

Now let's discuss a more practical problem, that is, what should you do if you encounter an incorrect OBJ file?

When you open the OBJ file, you will often see tens of thousands of lines of code, you may not be able to give a look at the error line, unless the program's error message has told you the error line. If you do not know where the error is, you can use exclusion to find out the correct code range and locate the error by reducing the error code range. For example, you first create an empty OBJ file, paste the wrong OBJ file code in half, and then import the new OBJ file with only half of the Code into Maya. If there is no error message at this time, it indicates that the error line is in the other half of the code. You can try another part of the code from the other half. If an error occurs at this time, the error message is displayed in the pasted code. You can delete a part of the pasted code and try again. In this way, the range is gradually reduced until the error line is found.

Although this method is troublesome, it is quite effective. If you don't know how to program and encounter a very urgent situation, this method is worth a try.

5. More details about the OBJ file:

Simple OBJ format.
# Simple wavefront File
V 0.0 0.0 0.0
V 0.0 1.0 0.0
V 1.0 0.0 0.0
F 1 2 3

You can use a negative index. Sometimes it is easier to use a negative index to describe the surface.

V-0.500000 0.000000 0.400000
V-0.500000 0.000000-0.800000
V-0.500000 1.000000-0.800000
V-0.500000 1.000000 0.400000
F-4-3-2-1

"F-4-3-2-1" This index value "-3" indicates to count 3rd vertices from the "f" line, it is "V-0.500000 0.000000-0.800000", and other index values are similar. Therefore, the equivalent positive value index of this row is written as: "F 1 2 3 4"

The OBJ file does not contain the color definition information, but can reference the material library. The material library information is stored in an independent file with the suffix ". MTL. The keyword "mtllib" indicates the meaning of the material library. The material library contains the definition values of diffuse, ambient, specular RGB (red, green, and blue), specularity, and refraction of materials ), transparency and other features. After "usemtl" specifies the material, this material will be used in the future until the next "usemtl" is used to specify the new material.

Specify the material method:
Cube with materials:
# This cube has a different material
# Applied to each of its faces.
Mtllib master. MTL
V 0.000000 2.000000 2.000000
V 0.000000 0.000000 2.000000
V 2.000000 0.000000 2.000000
V 2.000000 2.000000 2.000000
V 0.000000 2.000000 0.000000
V 0.000000 0.000000 0.000000
V 2.000000 0.000000 0.000000
V 2.000000 2.000000 0.000000
#8 vertices
G front
Usemtl red
F 1 2 3 4
G back
Usemtl blue
F 8 7 6 5
G right
Usemtl green
F 4 3 7 8
G top
Usemtl gold
F 5 1 4 8
G left
Usemtl orange
F 5 6 2 1
G bottom
Usemtl purple
F 2 6 7 3
#6 Elements

Besserl one-sided (bezerpatch ):
Maya cannot export the one-sided besell of OBJ format, but can import it. The imported Bessert is automatically converted to a green surface.
#3.0 besuppliers patch
V-5.000000-5.000000 0.000000
V-5.000000-1.666667 0.000000
V-5.000000 1.666667 0.000000
V-5.000000 5.000000 0.000000
V-1.666667-5.000000 0.000000
V-1.666667-1.666667 0.000000
V-1.666667 1.666667 0.000000
V-1.666667 5.000000 0.000000
V 1.666667-5.000000 0.000000
V 1.666667-1.666667 0.000000
V 1.666667 1.666667 0.000000
V 1.666667 5.000000 0.000000
V 5.000000-5.000000 0.000000
V 5.000000-1.666667 0.000000
V 5.000000 1.666667 0.000000
V 5.000000 5.000000 0.000000
#16 vertices
Cstype betiller
DEG 3 3
# Example of line continuation
Surf 0.000000 1.000000 0.000000 1.000000 13 14 \
15 16 9 10 11 12 5 6 7 8 1 2 3 4
Parm U 0.000000 1.000000
Parm v 0.000000 1.000000
End
#1 Element

Cardinal curve ):
Maya does not seem to support curves in the OBJ format. There is no error message or curve during import.
#3.0 Cardinal Curve
V 0.940000 1.340000 0.000000
V-0.670000 0.820000 0.000000
V-0.770000-0.940000 0.000000
V 1.030000-1.350000 0.000000
V 3.070000-1.310000 0.000000
#6 vertices
Cstype Cardinal
DEG 3
Curv 0.000000 3.000000 1 2 3 4 5 6
Parm U 0.000000 1.000000 2.000000 end
#1 Element

Texture-mapped ):
# A 2x2 square mapped with a 1x1 square
# Texture stretched to fit the Square exactly.
Mtllib master. MTL
V 0.000000 2.000000 0.000000
V 0.000000 0.000000 0.000000
V 2.000000 0.000000 0.000000
V 2.000000 2.000000 0.000000
VVT 0.000000 1.000000 0.000000
VVT 0.000000 0.000000 0.000000
VVT 1.000000 0.000000 0.000000
VVT 1.000000 1.000000 0.000000
#4 vertices
Usemtl wood
# The first number is the point,
# Then the slash,
# And the second is the texture point
F 1/1 2/2 3/3 4/4
#1 Element

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.