In my path tracer I'm loading OBJ files line by line and I'm loading vertices and vert normals like this:
faceVerts = append(faceVerts, [3]Tuple{
vertices[v1-1], vertices[v2-1], vertices[v3-1],
})
faceNormals = append(faceNormals, [3]Tuple{
vertNormals[vn1-1], vertNormals[vn2-1], vertNormals[vn3-1],
})
And then I try to calculate normals of faces of each triangle like this:
for i := 0; i < len(faceVerts); i++ {
triangle := Triangle{
TrianglePosition{
faceVerts[i][0],
faceVerts[i][1],
faceVerts[i][2],
},
material,
Tuple{0, 0, 0, 0},
}
normal0 := faceNormals[i][0]
normal1 := faceNormals[i][1]
normal2 := faceNormals[i][2]
triangle.normal = (normal0.Add(normal1).Add(normal2)).Normalize()
*list = append(*list, triangle)
}
Most of the time this code seems to work fine, but the program renders some models (but it's visible only with reflective materials) with some black faces, like here:
I'm assuming this problem is related to wrong normals, because this doesn't happen when the model has a lambertian material, it only happens with materials where normal vector is used for calculating reflection or refraction (eg. reflected := r.direction.Reflection(rec.normal)
). This problem either doesn't happen with more diffuse lighting or it just looks better because instead of getting black spots, I'm getting spots of the same color as average lighting in the scene.