Detailed explanation of OBJ file format in 3D

There are two common *.obj files: the first is an OBJ file (also called an object file) based on the COFF (Common Object File Format) format, which is used to compile applications; the second is Alias|Wavefront The company's OBJ model file. This paper analyzes the second obj model file.

3D file format, there are several common "*.3ds", "*.max", "*.lw", "*.mb", "*.dxf", "*.obj". However, few people can give a more satisfactory description of the specific characteristics of OBJ files. Many people know that OBJ files start from using Poser. Poser is a human body modeling software. To export the human body generated by Poser to other 3D software for reprocessing, OBJ files are used. OBJ file is a standard 3D model file format, which is very suitable for mutual guidance between 3D software models . For example, if you build a model in 3dsMax or LightWave, and want to transfer it to Maya for rendering or animation, exporting OBJ files is a good choice. At present, almost all well-known 3D software supports reading and writing of OBJ files, but many software need plug-ins to do this. 
In addition, as an excellent file format, many game engines also support the reading of OBJ files. The mutual conduction between 3D software models is a very common thing. Unfortunately, the current 3D software model export function is not so perfect, there are often missing surfaces and lines, and sometimes the exported models are encountered. Can't open at all.

OBJ file is a text file format. Compared with 3DS, which is mainly binary file, and even the purpose of each block has to be tried and tested, OBJ with text file is more friendly to us. Different from the tree-like [block structure] of 3DS files, OBJ files are just a very simple dictionary-like structure. There is no block ID to represent the name, but it is simply represented by easy-to-understand ideographic characters. In short, it looks pleasing to the eye, and the pain is only known when you actually write the import code--. OBJ files optimize storage but degrade reading and writing.

If Maya's own model is wrong, it can also be converted to OBJ format first, and then imported back to Maya after modification.

OBJ file -- concept

OBJ files are a file format developed by Wavefront for its "Advanced Visualizer" workstation-based 3D modeling and animation software, which is also readable and writable via Maya.

An OBJ file is a text file that can be opened directly with WordPad for viewing, editing and modification.

Also, there is a related binary file format (using the ".MOD" suffix), which is not patented, so we won't discuss it here.

The most recent documented version of OBJ is v3.0, replacing the previous v2.11 version.

The OBJ3.0 format supports Polygon, Lines, Surfaces, and Free-form Curves.

Lines and polygons are described by their points, curves and surfaces are defined by their control points and additional information attached to the curve type. This information supports both regular and irregular curves, including those based on Bezier curves, B-splines, cardinal (Cardinal/Catmull-Rom splines), and Taylor equations .

1. OBJ file -- features

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

(2) OBJ files mainly support polygon (Polygons) models.

Although OBJ files also support Curves, Surfaces, and Point Group Materials, the OBJ files exported by Maya do not include this information.

(3) OBJ files support faces with more than three points, which is useful.

Many other model file formats only support three-point faces, so the models we import into Maya are often triangulated, which is not good for us to reprocess the model.

(4) OBJ files support normal and texture coordinates.

After adjusting the texture in other software, the texture coordinate information can be stored in the OBJ file, so that after importing the file into Maya, you only need to specify the texture file path, and there is no need to adjust the texture coordinates.

 

2. OBJ file -- basic structure

OBJ files do not require any kind of file header (File Header), although a comment of several lines of file information is often used as the beginning of the file.

The OBJ file consists of lines of text, and the comment line starts with a "pound" sign (#). Spaces and blank lines can be added to the file at will to increase the readability of the file. Lines with words start with one or two marked letters, that is, a keyword (Keyword), which can indicate what kind of data this line is. Multiple lines can be logically joined together to represent a single line by adding a hyphen (\) at the end of each line.

Note that no spaces or tabs can appear after the connector (\), otherwise it will cause a file error.

The following keywords can be used in OBJ files.

In this list, the keywords are arranged by data type, and each keyword has a short description.

Vertex data:

v Geometric vertices

vt Texture vertices

vn Vertex normals

vp Parameter space vertices

Free-form curve/surface attributes:

deg 度 (Degree)

bmat Basis matrix

step Step size

cstype Curve or surface type

Elements:

p 点 (Point)

l Line

f face (Face)

curve (Curve)

curv2 2D curve (2D curve)

surf surface (Surface)

Free-form curve/surface body statements:

parm parameter values ​​(Parameter values)

trim Outer trimming loop

hole Inner trimming loop

scrv Special curve

sp Special point

end End statement

Connectivity between free-form surfaces:

connect (Connect)

Grouping:

g Group name

s Smoothing group

mg Merging group

o Object name

Display/render attributes:

  bevel Bevel interpolation 
  c_interp Color interpolation 
  d_interp Dissolve interpolation 
  lod Level of detail 
  usemtl Material name 
  mtllib Material library 
  shadow_obj Shadow casting ) 
  trace_obj Ray tracing 
  ctech Curve approximation technique 
  stech Surface approximation technique

3. OBJ file - example: 
  The following example will be explained in detail. 
  The OBJ file records the code for a quad: 
  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

Let's create an OBJ file, but this time we won't use 3D software, but WordPad. 
  Open WordPad, write the above 5 lines of code, you can add a little comment appropriately. 
  Save the file in text format with the file name "myObj.obj".

Note: At the end of the code, you must press Enter to switch the cursor to the next line, that is, add a newline character (\n). Otherwise you will see the following error message: 
  // Error: line 1: OBJ file line 5: index out of range. // 
  // Error: line 1: Error reading file. //

  Import the "myObj.obj" file in Maya, and you see, a quad is imported. The shape of this quad is completely determined by the previous 5 lines of code.

            

 Let's analyze one of these codes. 

  v -0.58 0.84 0 
  Drawing a quadrilateral requires four vertices, this is the first vertex, "v" represents the vertex, "-0.58" is the X-axis coordinate value of this vertex, and "-0.84" is the Y-axis coordinate value, "0" is the Z-axis coordinate value. This is the first vertex, and its index number is 1. The index number is used when the screen is displayed.
  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, and their index numbers are 2, 3, and 4, respectively.

 Now start the picture, "f" represents the face (face), 1, 2, 3, 4 are the index numbers of the previous four vertices. Please pay attention to the order of drawing the connection points of this surface, starting from the first point, connecting the second, third, and fourth points in turn. If the order of connections is different, the generated faces will be very different. For example, "f 1 2 4 3" will produce an overlapping face, as shown in the figure. The connection points of the face are arranged clockwise or counterclockwise, which will determine the normal direction of the face (the inverse of the face). For example: "f 1 2 3 4" face normals outward, "f 4 3 2 1" face normals inward. The wrong order of the connection points of the surface is an important reason for the fragmented surface of the imported model.

A face cannot have more than two identical vertices, which is also a point to check for errors in OBJ files.

For example: "f 1 2 3 4 3", there are two identical vertices, the index number is 3. Two identical vertices appear on a face, which may cause memory allocation errors in the program.

  下面来研究一下Maya导出的OBJ文件。 
  在Maya中创建一个多边形立方体,选中这个立方体,选择菜单"File -> Export Selection..."导出格式为OBJ,文件名为"cube.obj",如果没有此格式,请在Plug-in Manager中载入"objExport.mll"。 用写字板打开"cube.obj",可以看到如下代码: 
  # 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 
  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 
  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 
  这个文件看起来稍复杂一些,用到了许多关键词,你可以对照前面的列表查看一下每个关键词的意思。

 

我来解释一下。 
  "vt 1.000000 0.000000"这句"vt"代表点的贴图坐标。 
  "vn 0.000000 0.000000 -1.000000"这句"vn"代表点的法线。

  "s off"表示关闭光滑组。 
  "usemtl initialShadingGroup"表示使用的材质。 
  "f 7/13/21"这时在面的数据中多了贴图坐标uv点和法线的索引号,索引号分别用左斜线(/)隔开。 
  格式:"f 顶点索引/uv点索引/法线索引"。 
  "g pCube1"表示组,这里的成组与Maya中的成组不一样,这里的成组是指把"g pCube1"后出现的面都结合到一起,组成一个整的多边形几何体。

  把"cube.obj"文件修改一下就知道成组的意思了。把"s off"这句后面的代码替换成以下代码: 
  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 
  导入Maya后可以看到,立方体的每个面是分离的,每个面的名称分别是"pCube_Face(1~6)",可见组的名称其实就是单独几何体的名称。

  可不可以用中文命名几何体(组)呢?试试就知道了,把前面的代码改成: 
  usemtl initialShadingGroup 
  g 立方体面1 
  f 1/1/1 2/2/2 4/4/3 3/3/4 
  g 立方体面2 
  f 3/3/5 4/4/6 6/6/7 5/5/8 
  g 立方体面3 
  f 5/5/9 6/6/10 8/8/11 7/7/12 
  g 立方体面4 
  f 7/7/13 8/8/14 2/10/15 1/9/16 
  g 立方体面5 
  f 2/2/17 8/11/18 6/12/19 4/4/20 
  g 立方体面6 
  f 7/13/21 1/1/22 3/3/23 5/14/24

  试一下,会发现模型顺利的导入了。虽然物体的名称都变乱码了,可这并不是很严重的事。 
  不过使用中文名并不总是这么顺利,把"g 立方体面1"这行改为"g 选择"再试试看,这回导入时模型根本无法出现,只会出现如下的错误信息: 
  // 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. // 
  由此可见,物体命名的不规范也是导致OBJ文件出错的原因之一。 
  关于Maya的物体命名,英文名是很保险的,标点符号中只有下划线(_)可用,数字不能用放到名称的开头,尽量不要用中、日、韩等双字节文字。

 

  OBJ文件不支持有孔的多边形面。 
  举个例子说明一下: 
  选择Maya的创建多边形工具(Polygons -> Create Polyon Tool),在视图中画一个四边形,不要按回车,按Ctrl在四边形中间点一下,可以继续在四边形中挖一个洞。把这个有孔的多边形存成OBJ格式,在导入Maya时,会发现多边形少了一块。如果你把这也看成错误,现在至少你已经知道错误的原因了,就是OBJ文件不支持有孔的多边形面。

OBJ文件 -- 实际问题: 
  现在来讨论一点比较实际的问题吧,就是一旦你遇到了一个出错的OBJ文件,倒底该怎么办? 
  当你打开OBJ文件后,往往会看到有几万行的代码,你恐怕还没本事情一眼看出错误所在行,除非程序的错误信息中已经告诉你错误行。如果你不知道错误在哪里,可以用排除法,弄清楚肯定正确的代码范围,通过缩减错误代码范围定位错误。例如,你先新建一个空的OBJ文件,把有错的OBJ文件代码粘贴一半过来,然后把这个只有一半代码的新OBJ文件导入Maya。如果这时没有错误信息,说明错误行是在另一半代码中,可以从另一半代码中再粘贴一部分代码试试看;如果这时出现错误,说明错误行就在粘贴的代码中,可以把粘贴过来的代码删去一部分再试试看。就这样,逐步缩减范围直到找到错误行为止。 
  这种方法虽然很麻烦,不过颇为有效。如果你不会编程,又遇到非常紧急的情况,这种方法还是值得一试的。

  OBJ文件 -- 细节: 
  掌握了这么多差不多也够用了,不过由于网上详细讲解OBJ文件的中文文档很少,我还是再讲一些例子,给大家提供多一点的信息吧。

  简单的OBJ格式写法。 
  # 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 

  面可以使用负值索引,有时用负值索引描述面更为简便。 
  "f -4 -3 -2 -1"这句索引值"-3"表示从"f"这行往上数第3个顶点,就是"v -0.500000 0.000000 -0.800000",其它的索引值以此类推。 因此与这一行等效的正值索引写法为:"f 1 2 3 4" 
  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 

  OBJ文件不包含面的颜色定义信息,不过可以引用材质库,材质库信息储存在一个后缀是".mtl"的独立文件中。关键字"mtllib"即材质库的意思。 
  材质库中包含材质的漫射(diffuse),环境(ambient),光泽(specular)的RGB(红绿蓝)的定义值,以及反射(specularity),折射(refraction),透明度(transparency)等其它特征。 
  "usemtl"指定了材质之后,以后的面都是使用这一材质,直到遇到下一个"usemtl"来指定新的材质。

 

下面的例子说明了指定材质的方法。 
  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 

  贝塞尔片面(Bezier Patch): 
  Maya不能导出OBJ格式的贝塞尔片面,却能够导入它。导入的贝塞尔片面自动转换为Nurbs表面。 
  # 3.0 Bezier 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 bezier 
  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好像不支持OBJ格式的曲线,导入时不会出现错误信息,却也不会出现曲线。 
  # 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 3.000000 end 
  # 1 element 

  贴图映射(Texture-Mapped): 
  # A 2 x 2 square mapped with a 1 x 1 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 
  vt 0.000000 1.000000 0.000000 
  vt 0.000000 0.000000 0.000000 
  vt 1.000000 0.000000 0.000000 
  vt 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324594194&siteId=291194637