48. OBJ file Introduction

Source: Internet
Author: User

The model file is in OBJ format. This format is very popular and can be exported using a large number of 3D modeling software. OBJ is a simple format that can be opened and saved in any text editor. Accurately speaking, OBJ files are not designed to process animation-related information, but OBJ may be useful if a static object needs to be quickly loaded into the scenario. The exported OBJ model consists of vertices, normal, texture coordinates, and triangle indexes (appearance. Therefore, in the OBJ format, no model is used for any object except a simple static grid.

Note]

Be careful when using OBJ files exported from different types of software. For example, the OBJ file created with lightwave7.5 is different from that created by 3D Studio.
The OBJ file exported by Max. Try to write for a slightly different type of fileCodeOpening the OBJ file created with lightwave7.5 produces an error. To avoid errors, only the OBJ file containing wavefront at the beginning of the file is loaded.

 

12.3.1 OBJ file format

As you know, the OBJ format is text-based. The words at the beginning of each line in the OBJ file determine the information represented by this line. If this row starts with the letter V, three different floating point values following it represent the vertex position. If this row starts with the letter VT, two different floating point values following it represent texture coordinates. The three floating point values following VN represent the legal line. Generally, three pairs of index values are followed by F. Each set of indexes represents a triangle, which consists of three index indexes, one representing the vertex, one representing the texture coordinate of the vertex, And the last representing the normal used by the vertex. For example, 1/1/1/2/3/2/1/2. The first group means that the first vertex of the triangle uses the first vertex in the vertex array, the first texture coordinate in the texture coordinate array, and the first normal in the normal array. The second set means that the second vertex of the triangle uses the second vertex in the vertex array, the third texture coordinate in the texture coordinate array, and the second normal in the normal array. Therefore, each of these three pairs uses indexes to describe vertices, texture coordinates, and normal arrays. Because there are three groups of values, each vertex uses one, and each vertex has its own position, texture coordinate, and normal. Some points may share location, texture coordinates, and/or normal, so that data can be reused.


In addition to the mentioned characters, there are also some other characters, such as mtllib, which refer to the file name for saving material information for the model,.ProgramListing 12.12 provides an example of the OBJ model file exported by milkshape3d.

Program list 12.2 OBJ format textureCubeBody example

# Wavefront OBJ exported by milkshape 3D

Mtllib test. MTL

V-21.000000 20.750000 20.750000

V-21.000000-21.250000 20.750000

V-21.000000 20.750000 20.750000

V-21.000000-21.250000 20.750000

V-21.000000 20.750000-19.000000

V-21.000000-21.250000-19.000000

V-21.000000 20.750000-19.000000

V-21.000000-21.250000-19.000000

#8 vertices

VT 0.000000 1.000000

VT 0.000000 0.000000

VT 1.000000 1.000000

VT 1.000000 0.000000

#4 texture coordinates

Vn 0.000000 0.000000 1.000000

Vn 1.000000 0.000000 0.000000

Vn 0.000000 0.000000-1.000000

VPC-1.000000 0.000000 0.000000

Vn 0.000000 1.000000 0.000000

Vn 0.000000-1.000000 1.000000

#6 Normals

G box01

Usemtl material01

S 1

F 1/1/1 2/2/1 3/3/1

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

S 2

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

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

S1

F 5/1/3 6/2/3 7/3/3

F 6/2/3 8/4/3 7/3/3

S2

F 7/1/4 8/2/4 1/3/4

F 8/2/4 2/4/4 1/3/4

S3

F 7/1/5 1/2/5 5/3/5

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

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

F 8/2/6 6/4/6 4/3/6

#12 triangles in group

#12 triangles total

In program list 12.12, once all vertices, texture coordinates, and normal are defined, each object is defined. First, the object name, the file name obtained from the object used by the object, and the polygon that constitute the object. Once the last appearance is read, it is either at the end of the file or ready to start reading another object. If the read content is neither a comment nor an end identifier of the file, you can assume that there are still objects to be read. Read until all objects are read. When defining an object, the file uses indexes in the array of vertices, texture coordinates, and normal. Objects with common edges, texture coordinates, and normal can make files smaller than normal. However, when creating an object in direct3d, you have to search for all the data instead of the indexes that can be used directly like this.

Together with the OBJ file itself, the material file is also required. A material file usually contains the. MTL extension and all the information required to add a texture to an object. Like the OBJ file, each line of the. MTL file contains characters that follow the subsequent data. This includes Ka representing the environment attribute, KD representing the diffuse reflection, KS representing the mirror reflection, and NS representing the reflection energy. If you see the word newmtl in the material file, it means you need to define a new material. Map_kd indicates that the material contains the texture image associated with it, and this line is followed by the file name of the texture image. An example of the. MTL Material file exported by milkshape3d is shown in program list 12.13.

Program list 12.13 Material file example used by the OBJ Model

#

# Test. MTL

#

Newmtl material01

Ka 0.200000 0.200000 0.200000

Ka 0.800000 0.800000 0.800000

KS 0.000000 0.000000 0.000000

Service 0.000000

Map_kd. \ decal. TGA

Models exported to OBJ are text-based and easy to analyze. A large number of different modeling software can be exported as wavefront
OBJ file format. This is useful when dealing with static objects, or if you need to convert the model to an intermediate format before converting it to a custom type. Be careful when using different types of software to export OBJ files. Recall that Lightwave
7.5 wavefront cannot be exported
OBJ file. If it is not used as a precaution, a large number of errors will occur. When writing code for the demo program that loads the OBJ file, you must add a check statement to check whether the wavefront word appears at a certain position on the top of the file. If this word appears, it can be assumed that the OBJ file imported by the software is in the wavefront format.

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.