OpenGL | obj file & mtl file

1. OBJ file

1. OBJ file overview

  • OBJ is a 3D model file, so it does not contain information such as animation, material properties, texture paths, dynamics, particles, etc.

  • OBJ files mainly support polygonal (Polygons) models. Although OBJ files also support Curves, Surfaces, and Point Group Materials, the OBJ files exported by Maya do not include these information.

  • OBJ files support surfaces with more than three points.

  • OBJ files support normals and texture coordinates.

2. OBJ file analysis

OBJ files do not require any kind of file header (File Header), although a few lines of file information are often used as the beginning of the file. The OBJ file is composed of lines of text, and the comment line starts with a "#" 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, which are keywords (Keyword), which can indicate what kind of data this line is. Multiple lines can be logically joined together to represent one line by adding a hyphen (\) at the end of each line. Note that spaces or tabs cannot appear after the connector (\), otherwise it will cause file errors.

(1) Example

Below is the cube.obj data exported after creating the cube in maya:

# This file uses centimeters as units for non-parametric coordinates.

mtllib Cube.mtl
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.375000 0.000000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.625000 1.000000
vt 0.875000 0.000000
vt 0.875000 0.250000
vt 0.125000 0.000000
vt 0.125000 0.250000
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

(2) Keyword Description

  • # : comment marker

  • mtllib : describes the path where the material file (mtl) used by the model is located

  • v : vertex

  • vt : texture coordinates of the vertex

  • vn : the normal of the vertex

  • o : object name

  • g : group name

  • s : smoothing group (Smoothing group), which processes the lighting information between faces to achieve a smooth effect

  • usemtl : the currently used material

  • f : face

Among them, the surface f needs to be taken out, for example, f 7/13/21 1/1/22 3/3/23 5/14/24 means to draw a surface with four points, and the four sets of data separated by spaces represent the plane Four vertices, in each set of data, each number represents the index of the vertex / texture coordinate / normal , so f 7/13/21 1/1/22 3/3/23 5/14/24 The data for the first point (7/13/21) in is:

vertex

texture coordinates

normal

(0.500000,-0.500000,-0.500000)

(0.125000,0.250000)

(-1.000000,0.000000,0.000000)

Two, mtl file

1.mtl file overview

We know that .OBJ files are used to store model information. Observe the first line of code of cube.obj above, and you can see that the material library file is mtllib Cube.mtl

The .mtl file (Material Library File) is a material library file, which describes the material information of the object, stored in ASCII, and can be opened and edited by any text editor. A .mtl file can contain one or more material definitions, for each material there is a description of its color, texture and reflection map, applied to the surface and vertices of the object.

2.mtl file analysis

(1) Example

The cube.mtl file data is as follows:

newmtl initialShadingGroup
illum 4
Kd 0.50 0.50 0.50
Ka 0.00 0.00 0.00
Tf 1.00 1.00 1.00
Ni 1.00

(2) Keyword Description

  • newmtl : represents the material, the following are the attribute parameters of the material

  • Ns : Specular reflection coefficient, the higher the value, the denser the highlight

  • NI : Specify the optical density of the material surface, that is, the refraction value

  • d : Indicates the number of objects blended into the background, the value range is 0.0~1.0, the value of 1.0 means completely opaque, and the value of 0.0 means completely transparent

  • Tr : defines the alpha transparency of the material

  • Tf : material transmission filter (transmission filter), the corresponding data is r, g, b value

  • illum : illumination (illumination), which can be followed by digital parameters in the range of 0~10

  • Ka: ambient light (ambient color)

  • Kd: scattered light (diffuse color)

  • Ks: specular light (specular color)

  • Ke : emissive color

  • map_Ka : The texture map path sampled by the ambient light, in the root directory of the .obj model file

  • map_Kd : texture map path sampled by diffuse light

Guess you like

Origin blog.csdn.net/weixin_39766005/article/details/129561729