Here's a complete conceptual plan for how the mechanics I will program ought to work.Vertex Visibility
First, each vertex is checked against all faces except the face it belong to.
An imaginary horizontal line with Y value equal to that of the vertex being tested extends from the smallest X-value of any vertex in that face, until it reaches the X-value of the tested vertex. All edges in the face being tested are checked to see if they overlap with this line. If an overlap is detected, a counter called edgecounter
is incremented. edgecounter
begins life with a value of 1, and is incremented by 1 every time an edge is encountered in this manner.
When all of the face's edges have been tested to see if they meet the imaginary horizontal line, we can then say whether this vertex appears inside the face, or outside it, in the following way:
if math.floor(edgenumber/2) == math.ceil(edgenumber/2) then
-- The vertex is inside the face.
-- The vertex is outside the face.
If a vertex is detected as being inside a face, the distance of that vertex from the camera is then compared to the distance from the plane at the line-of-sight point of overlap. If the plane is found to be closer to the camera than the vertex, the vertex is marked as "hidden". Hidden vertices will not be drawn, and edges will not be drawn to or from hidden vertices.
If a vertex is found to not be blocked by any faces in the way described above, the vertex is marked as visible. It will be drawn, and edges will be drawn to/from the opposing vertex or the first-in-line pseudo vertex.Edge Visibility
This diagram shows two intersecting planes.
A plane that enters another plane from above, and goes beneath it (thus being obscured), requires pseudo-vertices to be created at each edge which goes below the surface of the plane.Top Two Pseudo-Vertices - Edge to Face Check
During edge checking, each 3D edge must be checked against each 3D face to determine if it passes through.
If it does, a pseudo-vertex must be created at the point where the line meets the plane.
The pseudo-vertex is assigned to the edge's list of pseudo-vertices.Bottom Two Pseudo-Vertices - Edge to Edge Check
During edge checking, each 2D edge must be checked to see if it passes through another 2D edge.
If they meet, a pseudo-vertex must be created at the 2D position of their meeting.
The pseudo-vertex will be assigned to whichever edge is furthest from the camera at the point of overlap.Drawing the edges
Once the above checks are complete, edges can be drawn. For each edge, a list is created. The first item in the list is the ID of the origin vertex of the edge, if visible. The next items are the ID's of the pseudo-vertices belonging to that edge, if there are any. The final item in the list is the ID of the desitnation vertex of the edge, if visible. The vertices are added to the list in that specific order
The edge is then drawn according to the following rule:
Between the first and second vertices, draw.
Between the second and third vertices, don't draw.
Between the third and fourth vertices, draw.
Between the fourth and fifth vertices, don't draw.
If both vertices are hidden, and there are no pseudo-vertices, the edge is not drawn.