I'm working on a 3D renderer in MS-DOS in C, and I'm trying to fill 3D triangles onto the screen.
Here's a screenshot of it working.
It works fine, but when one vertex is behind the near clipping plane, it starts looking like this.
I understand when one vertex is behind the camera, the original triangle is split into two. When I tried to recreate this procedure, the program draws the triangles incorrectly. I'd like to show you my steps for rendering the triangle.
Step 1: For each three vertices of the triangle, the camera will move to (0, 0, 0) and will face 0 degrees for both the yaw and pitch. The vertex will be translated and rotated along with it.
Step 2: Check if the vertex is behind the near clipping plane (z = 0.01). If so, increment "behindZ". Go back to step 1 unless all 3 vertices are tested.
Step 3: Now that the program has tested all 3 of the vertices, if behindZ < 3, go to step 4. Otherwise, quit the whole rendering process.
Step 4: Check if behindZ equals 0, 1, or 2. Let's say it equals 1. Then the program sets the coordinates for the two visible vertices on the screen. For the vertex that's behind the near clipping plane, convert those to two vertices that intersect the z = 0.01 line.
Step 5: Render the two triangles: A triangle with the vertices 0, 1, & 2, and a triangle with 0, 2, & 3.
If those steps are too vague, I'd be happy to share my source code. I think my problem has to do with step 4. How do I fix it?