Smooth Triangles in OBJ Files

Your OBJ parser is already quite close to supporting smooth triangles. All it needs now is to support the vertex normal (vn) command and to update the way it parses the f (“face”) command.

Test #19: OBJ File with Vertex Normal Data

Vertex normal data should be correctly imported from an OBJ file.

This test sets up an OBJ file that contains four vertex normal statements (“vn”), and then shows that each of them is imported as a vector. Note that the normals collection is 1-based, just as the vertices collection was.

 Scenario​: Vertex normal records
 Given​ file ← a file containing:
 """
  vn 0 0 1
  vn 0.707 0 -0.707
  vn 1 2 3
  """
 When​ parser ← parse_obj_file(file)
 Then​ parser.normals[1] = vector(0, 0, 1)
 And​ parser.normals[2] = vector(0.707, 0, -0.707)
 And​ parser.normals[3] = vector(1, 2, 3)

The normals are imported as is, with no normalization or other processing done. Once those are imported, it’s just a matter of associating each of those vertex normals with a vertex, which you’ll do next.

Test #20: Faces with Normal Vectors

Vertex normal data should be correctly associated with face data from an OBJ file.

The f command that you implemented earlier is only half done, really. The following test demonstrates a more complete version of the syntax, permitting the vertices of a face to be associated with normal vectors.

 Scenario​: Faces with normals
 Given​ file ← a file containing:
 """
  v 0 1 0
  v -1 0 0
  v 1 0 0
 
  vn -1 0 0
  vn 1 0 0
  vn 0 1 0
 
  f 1//3 2//1 3//2
  f 1/0/3 2/102/1 3/14/2
  """
 When​ parser ← parse_obj_file(file)
 And​ g ← parser.default_group
 And​ t1 ← first child of g
 And​ t2 ← second child of g
 Then​ t1.p1 = parser.vertices[1]
 And​ t1.p2 = parser.vertices[2]
 And​ t1.p3 = parser.vertices[3]
 And​ t1.n1 = parser.normals[3]
 And​ t1.n2 = parser.normals[1]
 And​ t1.n3 = parser.normals[2]
 And​ t2 = t1

It turns out that the f command supports the following variations, the first of which you’ve already implemented:

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

The forward slash is used to delimit up to three different kinds of information per vertex. The first number in each triple is the vertex index itself. The second is an optional texture vertex (which you won’t implement for this feature and can be ignored). The third is an optional index into the list of vertex normals, corresponding to the vn command you just implemented.

To make this test pass, your f command needs to check to see if vertex normals are present for the vertices, and if they are, the command should call smooth_triangle instead of triangle.

Make that test pass. Once everything is good, you’re ready to sign off on this chapter!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.171.121