1

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:

render with black spots

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.

hamster121
  • 378
  • 1
  • 4
  • 13
  • 1
    Open the file in a modeling software to visualize normals, e.g., Blender, and ensure they are all facing the same direction. If not, then these software offer ways to recalculate the normals correctly. Where did you take the OBJ from? – Gilles-Philippe Paillé Feb 06 '20 at 13:39
  • 1
    Also note that the "Stanford Models" were scanned and reconstructed from point clouds. They often contains degenerated faces, duplicate vertices, self-intersection, which can produce artifacts when using path tracing. Some have tried to clean them up, but there is always a chance that they contain undesirable features. These 'undesirable features' are good for research since they act as stress tests, but a clean model used in production rarely contains this much artifacts. – Gilles-Philippe Paillé Feb 06 '20 at 13:51
  • I took the model from here: https://casual-effects.com/data/ but I'll check the normals in Blender, thanks for the tip – hamster121 Feb 06 '20 at 17:41
  • Yep, some normals are facing the wrong way, I'll recalculate them and render the image again – hamster121 Feb 06 '20 at 18:03
  • Okay, so the issue was not with wrong normals, but with too low accuracy (too high epsilon value) of calculations. – hamster121 Feb 07 '20 at 23:14
  • 1
    Great! Glad that you were able to solve your problem. – Gilles-Philippe Paillé Feb 10 '20 at 13:34

0 Answers0