From 67c41106ff29322757760819a862e1a171b5cac7 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Sun, 30 Apr 2023 22:47:45 +0100 Subject: [PATCH] Fix fisheye effect with cosine --- firstperson.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/firstperson.cpp b/firstperson.cpp index 9373aeb..a5791cd 100644 --- a/firstperson.cpp +++ b/firstperson.cpp @@ -27,8 +27,12 @@ void firstperson_update(sf::RenderTarget* renderTarget, Camera* camera) renderTexture.clear(); for (unsigned int i = 0; i < camera->resolution; i++) { - float distance = camera->rays[i].distance; - if (distance > 0.f) columnHeight = 0.5f * (float)height/camera->rays[i].distance; + // Get distance of ray intersection in the direction of the camera, + // instead of the actual ray distance to avoid fish eye effect. + float angleDiff = camera->rays[i].direction - camera->direction; + float distance = camera->rays[i].distance * cos(angleDiff); + + if (distance > 0.f) columnHeight = 0.5f * (float)height/distance; float centeredHeight = (float)height/2.f - columnHeight/2.f; float distanceScale = distance/10.f;