3Д БУМ

3Д принтеры и всё что с ними связано

Understanding the OBJ Format

As mentioned, the OBJ format is in plain text. If you open it in Notepad or Wordpad, you’ll see lists of vertices and other geometry. Having an ASCII (plain text)-based format rather than a binary one has advantages

Table 4.1 Parameters for Building a Format String

Parameter

%s

%c

%d

%f

Use

A %s as a format string means that scanf will look for a string. It will read until it finds a null, space, or newline character.

%c reads a single character. It will read the first charac­ter it sees, regardless of what type it is, even a space or a newline.

Reading in integers uses %d. With integers scanf will read until it finds a space, letter, or symbol that is not part of a number. Integers read with %d can be positive or negative.

%f is used for reading real numbers, particularly floating point values. It will cause scanf to read a number the same way as %d, but it will include decimals.

and disadvantages. Because they are stored in plain text, it is easier for a person to edit the file by hand. This can be good or bad. It is a good thing if you need to tweak small parts of the model, but these innocent tweaks can end in disaster if you accidentally change the way the file is laid out. ASCII formats also tend to take up more disk space than binary — based formats because they require extra space for large values due to their length. One “part” of the geometry is on each line.

There are four types of lines you’re looking for. They are the lines that give you details about a vertex, a texture coordinate, a vertex normal, or a face. There may be other lines for things like curved surfaces and comments as well, but you will not be needing them here.

Each line of the model file starts with one or two letters that tell the program what that line is for. There are four prefixes, one for each of the important types, as described here:

■ v: A letter v followed by a space is a plain vanilla vertex. If this is the case, following a single space will be three floating-point values, each separated by a single space as well.

■ vt: The string vt signifies that the line contains texture coordi­nates. Each texture coordinate is two floats, again separated by one space.

■ vn: vn signifies a vertex normal. Other than the prefix, the line mirrors the vertex lines: three floats.

■ f: An f signifies a face. A face is a set of indexes into the arrays of vertices, texcoords, and normals. However, only the vertex indexes must be present; the other two are optional. Every vertex index should be positive. A negative value in the face structure probably means the file is corrupt because you cannot have a negative array index. It wouldn’t be right to say “retrieve the negative second object,” so you obviously could not retrieve the “negative second” vertex either. The best approach if you find a negative value within a face structure is to simply ignore the face altogether.

■ Anything else: Any other line of prefixes such as g (group), # (comments), p (point), l (line), surf (surface), and curv (curve) should be ignored for now. Although they are part of the for­mat, they are not covered here.

A typical line in the OBJ file that represents a face or triangle and contains only vertices would look something like this:

f 1 2 3

This code says that the faces use the first, second, and third vertex indexes to draw the triangles. The absence of any other sets of indexes indicates that texture coordinates and vertex normals are not needed.

If the faces are textured, but contain no vertex normals, the syntax would be similar to this:

f 1/4 2/5 3/6

This line says that the triangles use vertices 1, 2, and 3 and texture indexes 4, 5, and 6.

Yet another variation of this line could use all three types of vertex data, the vertex, texture coordinate, and normal indexes. A line that does that would look like:

f 1/4/7 2/5/8 3/6/9

The first number is for the vertex itself, the second is for its texture coordinate, and the third is for the vertex normal.

There is only one more variation: vertex and vertex normals, but no texture coordinates. It looks like this:

f 1//4 2//5 3//6

Two slashes separate the numbers instead of one; this indicates that the second number is a vertex normal, not a texture coordinate.

The fact that the faces don’t need to contain all of the information can be very useful. There is no reason an object that needs no lighting or texturing should contain that information. By simply leaving the unneeded information out, the file size is reduced considerably.

Let’s now move on to some code that shows you how to load the file into something you can work with.

Для любых предложений по сайту: [email protected]