Skip to content Skip to sidebar Skip to footer

Depth Issue With 3d Graphics

I copied a code off YouTube recently, about 3d graphics in Python without the use of 3D modules, like OpenGL. The video worked with only cubes, so I tried adding a tetrahedron to t

Solution 1:

What you actually try to do is to sort the faces by the square of the Euclidean distance to the vertices of the face.

But sum(vert_list[j][i] for j in face)**2 does not compute the square of the euclidean distance. It computes the square of the sum of the components of a coordinate. The square of the euclidean distance is

vert_list[j][0]**2 + vert_list[j][1]**2 + vert_list[j][2]**2

That is not the same as

(vert_list[j][0] + vert_list[j][1] + vert_list[j][2])**2

Furthermore you do not take into account the number of vertices of a face, because the squared distances are summed. Note, the faces of the cube are quads and have 4 vertices, but the faces of the tetrahedron are triangles and have 3 vertices.

Correct the computation of the squared distance and divide by the number of vertices, to solve the issue:

depth += [sum(sum(vert_list[j][i] for j in face)**2 for i in range(3))]

depth += [sum(sum(vert_list[j][i]**2for i inrange(3)) for j in face) / len(face)]

But note, if faces are close to another there still may occur an issue (e.g. covering faces in the same plane). That is not an exact algorithm. It is not designed for "touching" objects.

Post a Comment for "Depth Issue With 3d Graphics"