From 3404194dfb4581659f7e0be7bb9ef0a30a8efb61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 1 Sep 2018 08:17:01 +0200 Subject: [PATCH] Render floor --- raycastlib.h | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index 9535f37..cbaac83 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -495,6 +495,8 @@ Camera _camera; void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray) { + int32_t y = _camera.resolution.y - 1; + for (uint32_t j = 0; j < hitCount; ++j) { HitResult hit = hits[j]; @@ -516,17 +518,36 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray) Unit offset = perspectiveScale(_camera.height - z,dist,1); - uint32_t start = _camera.resolution.y / 2 - height / 2 + offset; + int32_t start = _camera.resolution.y / 2 + height / 2 + offset; - for (uint32_t i = start; i < start + height; ++i) + PixelInfo p; + p.position.x = x; + + p.isWall = 0; + + // draw floor until the wall + for (int32_t i = y; i > start; --i) { - PixelInfo p; - - p.position.x = x; p.position.y = i; - p.isWall = 1; - p.hit = hit; + _pixelFunction(p); + } + int32_t yPrev = y; + + y = start - height; + + if (y > yPrev) // don't overwrite the previous - y can only get smaller + y = yPrev; + + start = start > yPrev ? yPrev : start; + + p.isWall = 1; + + // draw the wall + for (int32_t i = start; i > (y < 0 ? 0 : y); --i) + { + p.position.y = i; + p.hit = hit; _pixelFunction(p); } }