From 740f2e2a0a0fcde8675e84f25e2e0a7b2ab4c0d0 Mon Sep 17 00:00:00 2001 From: James Ball Date: Sun, 10 Sep 2023 12:39:34 +0100 Subject: [PATCH] Slightly improve line culling --- Source/obj/Camera.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Source/obj/Camera.cpp b/Source/obj/Camera.cpp index 71c8d06..e60f824 100644 --- a/Source/obj/Camera.cpp +++ b/Source/obj/Camera.cpp @@ -10,9 +10,22 @@ std::vector> Camera::draw(WorldObject& object) { for (auto edge : object.edges) { edge.rotate(object.rotateX, object.rotateY, object.rotateZ); // very crude frustum culling - if (edge.z1 < z || edge.z2 < z) { + double minZ = z + 0.1; + if (edge.z1 < minZ && edge.z2 < minZ) { continue; } + if (edge.z1 < minZ) { + double ratio = (minZ - edge.z1) / (edge.z2 - edge.z1); + edge.x1 = edge.x1 + (edge.x2 - edge.x1) * ratio; + edge.y1 = edge.y1 + (edge.y2 - edge.y1) * ratio; + edge.z1 = minZ; + } + if (edge.z2 < minZ) { + double ratio = (minZ - edge.z2) / (edge.z1 - edge.z2); + edge.x2 = edge.x2 + (edge.x1 - edge.x2) * ratio; + edge.y2 = edge.y2 + (edge.y1 - edge.y2) * ratio; + edge.z2 = minZ; + } Vector2 start = project(edge.x1, edge.y1, edge.z1); Vector2 end = project(edge.x2, edge.y2, edge.z2);