2

I have been reading Developing Games In Java by David Brackeen. I have understood everything in the book until now. In a Wavefront Object file, I understand what the v command does but I don't understand the f command. For example:

# OBJ - Wavefront object file
# The javagamebook loader only understands these commands:
#   mtllib <filename>    - Load materials from an external .mtl
#                          file.
#   v <x> <y> <z>        - Define a vertex with floating-point
#                          coords (x,y,z).
#   f <v1> <v2> <v3> ... - Define a new face. a face is a flat,
#                          convex polygon with vertices in
#                          counter-clockwise order. Positive
#                          numbers indicate the index of the
#                          vertex that is defined in the file.
#                          Negative numbers indicate the vertex
#                          defined relative to last vertex read.
#                          For example, 1 indicates the first
#                          vertex in the file, -1 means the last
#                          vertex read, and -2 is the vertex
#                          before that.
#   g <name>             - Define a new group by name. The faces
#                          following are added to this group.
#   usemtl <name>        - Use the named material (loaded from a
#                          .mtl file) for the faces in this group.

# load materials
mtllib textures.mtl

# define vertices
v 16 32 16
v 16 32 -16
v 16 0 16
v 16 0 -16
v -16 32 16
v -16 32 -16
v -16 0 16
v -16 0 -16

g myCube
usemtl wall1
f 1 3 4 2
f 6 8 7 5
f 2 6 5 1
f 3 7 8 4
f 1 5 7 3
f 4 8 6 2

What does the f command do here?

wchargin
  • 15,589
  • 12
  • 71
  • 110
DCSoft
  • 217
  • 1
  • 4
  • 17

2 Answers2

5

A cube has eight corners (denoted by vertices or points) which are defined by v (v for vertex) A face (f) is the surface defined by the coordinates (v) of the corners, for an illustration see Polygon_mesh.

    v1+-----------+  v2
     /            /
    /     f1     /
   /            /
v4+------------+v3
  |            |
  |            |
  |     f2     |
  |            |
  |            |
v6+------------+v5

That means the face f1 ist defined by v1,v2,v3 and v4; f2 by v4,v3,v5,v6.

stacker
  • 68,052
  • 28
  • 140
  • 210
4

The f indicates a face of the model (e.g., a triangle or a quad). The numbers are indices into the vertex list that indicate the way you should join it to form the face.

In the posted obj file, the first f you find indicates that the face is formed by a quad (because there are 4 vertices) with the vertices #1, #3, #4 and #2.

Important: you should join the vertices in the specified order to avoid problems later in normals calculation and/or problems with non-polygonal (self-intersecting) shapes.

wchargin
  • 15,589
  • 12
  • 71
  • 110
higuaro
  • 15,730
  • 4
  • 36
  • 43