[Ityran original]iphone OpenGL ES shows one of 3DS max models: obj file format Analysis
Yuezang-ityran
In iOS 3D development, it is often necessary to import a model generated from 3D design software such as 3DS Max. Because OpenGL ES cannot read these files directly, it is often necessary for developers to add interfaces to import them. It is common practice to import and control in OpenGL ES after creating a 3D model in the modeling software.
The typical save format for 3DS Max is *.max (the format of the version that is now generated), *.3DS (the format of the low version of 3DS Max generated), *. ASC (ASC File Explorer), *. ASE (ASCII Scene Explorer), *. File formats such as obj. Other software formats are not the same, the same, the software and format in the preservation of data are also different. For general purpose, we choose to compare the common obj format as an example, if other formats are needed, you can study the format of data preservation, and the way to read it is very similar.
1. Export the obj file from 3DS max
First of all, let's open 3DS max and create a random model, like this one (simple is a little bit primitive ...). )
After the drawing, export the file to. obj format, note that this is the export, if it is selected to save it is not found in. obj format. The steps to export are:
1) Open the "File" menu and select the "Export" option inside;
2) In the Export dialog box, select Wavefront Object (*. OBJ) ", enter a filename and choose" Save ", here such as" test.obj ";
3) After clicking "Save" will pop up a named "Object Exporter" dialog box, generally as follows to choose to be able to, in which to note is the "Polygon" option to select the "triangles" option, so that the choice of triangles as the basic entity;
4) Click "OK" to open the "Export MTL" dialog box, this dialog is used to set up and obj file with the. mtl file, MTL file is mainly used to save material information, generally as follows the look of the check on it can be;
5) Click "OK" to generate two files for "Test.obj" and "TEST.MTL" respectively.
2. obj file format analysis
Open the Test.obj file directly with Notepad or another text editing tool, and you'll see some of these strings at the beginning:
# max2obj Version 4.0 Mar 10th, 2001
#
Mtllib./TEST.MTL
G
# object (NULL) to come ...
#
It is important to note that the "#" number in the. obj file is used as an annotation (similar to a script like bash). The Mtlib statement declares the relative path to the referenced MTL file. The character "G" in the next line represents the group in the obj file.
Look down and you can see something like that.
V 7.268521 0.000000-2.808495
V 43.775921 3.692307-11.804825
V 40.087490 4.896347-20.872227
......
VT 0.500000 0.000000 0.000000
VT 0.750000 0.000000 1.000000
VT 0.791667 0.000000 1.000000
......
VN 0.061305-0.985877-0.155850
VN 0.176545-2.839121-0.448815
VN 0.176545-2.839120-0.448815
......
g (NULL)
S 1
F 1/1/1 3/3/3 2/2/2
F 1/1/1 4/4/4 3/3/3
F 1/1/1 5/5/5 4/4/4
......
As you can see from the above, the format of the data in the general obj file is this:
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 data type is typically:
- 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.
Some other unimportant information can be seen in the appendix. The parameters V, VT, vn are better understood.
Before explaining the format of the line prefixed with F, we have to mention a new concept, which is the vertex index (Vertex Indices). We know that for every triangle, we need 3 vertices to represent them. For example, in the above cube model, there are altogether 6x2x3=36 vertices. If you think about it, you will know that in these 36 vertices, a significant number of vertices are coincident. If these coincident vertices are represented, it is a waste of storage space. So, we put forward the idea of vertex index to solve the space occupancy problem. The idea of vertex indexing is to create two arrays, one to store all the vertex coordinate values in the model, and the other to store the index of the three vertices corresponding to each surface in the first array. Shows this all-in-a-row relationship.
And so on, we can set up a corresponding index for all normals and texture coordinates in the model to save more space. In fact, this is what obj files do. You can then look at the type F data. Type F data typically has the following formats:
- F 1 2 3
- F 1/3 2/5 3/4
- F 1/3/4 2/5/6 3/4/2
The first type represents a triangle created with vertex-n-A as index;
The second type represents a triangle with vertex-n-A-Z as an index, and Vertex 1 has a texture coordinate of 3, the second vertex has a texture coordinate of 5, and the third vertex has a texture coordinate of 4;
The third type represents a triangle with vertex-X-y as an index, and Vertex 1 has a texture coordinate of 3, a jurisprudential coordinate of 4, a second vertex with a texture coordinate of 5, a jurisprudential coordinate of 6, a third vertex with a texture coordinate of 4, and a jurisprudential coordinate of 2.
3. MTL file format Analysis
The MTL file is very similar to the obj file. The prefix used to identify the row is different. The meanings of these prefixes are shown in the following table:
Prefixes description
- NEWMTL represents a new material. The prefix is followed by a string that represents the name of the material. The information after this line is set for this material.
- KA Specifies the ambient light composition of the last established material. This row contains 3 single-precision floating-point parameters.
- KD Specifies the diffuse light component of the last established material. This row contains 3 single-precision floating-point parameters.
- KS Specifies the specular component of the material that was finally created. This row contains 3 single-precision floating-point parameters.
- MAP_KD specifies the reflection map of the last created material. If the first argument of this line is the string "-S", the row will include 5 parameters. Where the second, third, and fourth parameters are scaled values for this texture map in the U, V, W direction, and the fifth parameter is the file name of the texture picture. If the first string is not "-S", then the first parameter is the file name of the texture picture.
- MAP_KS Specifies the mirror map of the last created material. If the first argument of this line is the string "-S", the row will include 5 parameters. Where the second, third, and fourth parameters are scaled values for this texture map in the U, V, W direction, and the fifth parameter is the file name of the texture picture. If the first string is not "-S", then the first parameter is the file name of the texture picture.
Based on this information, we can implement the read and display of the obj and MTL files in OpenGL es. As for the implementation and analysis of the code, the next article will be carefully said.
Appendix 1 All data types in the obj file
Vertex (Vertex data):
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)
Reference articles
1. An explanation of the obj file format in 3D
2. Using 3D Models
[Ityran original]iphone OpenGL ES shows one of the 3DS max models: obj format analysis