From 30f8e6f205b9c53a327fd4faa9bc3bb419d76395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Fri, 31 Aug 2018 18:26:51 +0200 Subject: [PATCH] Add render function --- raycastlib.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/raycastlib.h b/raycastlib.h index ce78e3f..bb37b17 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -141,6 +141,9 @@ Unit perspectiveScale(Unit originalSize, Unit distance, Unit fov); void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc, ColumnFunction columnFunc, RayConstraints constraints); +void render(Camera cam, ArrayFunction arrayFunc, PixelFunction pixelFunc, + RayConstraints constraints); + //============================================================================= // privates @@ -481,6 +484,45 @@ void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc, } } +PixelFunction _pixelFunction = 0; +Camera _camera; + +void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray) +{ + if (hitCount == 0) + return; + + HitResult hit = hits[0]; + + Unit dist = // adjusted distance + (hit.distance * vectorsAngleCos(angleToDirection(_camera.direction), + ray.direction)) / UNITS_PER_SQUARE; + + dist = dist == 0 ? 1 : dist; // prevent division by zero + + int32_t height = (UNITS_PER_SQUARE * 50) / dist; + uint32_t start = _camera.resolution.y / 2 - height / 2; + + for (uint32_t i = start; i < start + height; ++i) + { + PixelInfo p; + + p.position.x = x; + p.position.y = i; + p.isWall = 1; + + _pixelFunction(p); + } +} + +void render(Camera cam, ArrayFunction arrayFunc, PixelFunction pixelFunc, + RayConstraints constraints) +{ + _pixelFunction = pixelFunc; + _camera = cam; + castRaysMultiHit(cam,arrayFunc,_columnFunction,constraints); +} + Vector2D normalize(Vector2D v) { profileCall(normalize);