From 942115567105eefe6e8415f0aed35f15d7beaf14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Mon, 17 Sep 2018 15:05:38 +0200 Subject: [PATCH] Add inverse scale func --- raycastlib.h | 13 +++++++++++++ test.c | 30 +++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index a838772..da067cf 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -275,6 +275,8 @@ Unit degreesToUnitsAngle(int16_t degrees); ///< Computes the change in size of an object due to perspective. Unit perspectiveScale(Unit originalSize, Unit distance); +Unit perspectiveScaleInverse(Unit originalSize, Unit scaledSize); + /** Casts rays for given camera view and for each hit calls a user provided function. @@ -1228,6 +1230,9 @@ Unit coordStep = 1; PIXEL_FUNCTION(&p); ++y; p.depth -= _horizontalDepthStep; + + if (p.depth < 0) // just in case + p.depth = 0; } } @@ -1379,6 +1384,14 @@ Unit perspectiveScale(Unit originalSize, Unit distance) : 0; } +Unit perspectiveScaleInverse(Unit originalSize, Unit scaledSize) +{ + return scaledSize != 0 ? + (originalSize * UNITS_PER_SQUARE) / + ((VERTICAL_FOV * 2 * scaledSize) / UNITS_PER_SQUARE) + : 0; +} + void moveCameraWithCollision(Camera *camera, Vector2D planeOffset, Unit heightOffset, ArrayFunction floorHeightFunc, ArrayFunction ceilingHeightFunc, int8_t computeHeight, int8_t force) diff --git a/test.c b/test.c index b0c1461..6c65f85 100644 --- a/test.c +++ b/test.c @@ -6,6 +6,9 @@ #define RAYCASTLIB_PROFILE +#define HORIZONTAL_FOV UNITS_PER_SQUARE / 2 +#define PIXEL_FUNCTION pixelFunc + #include #include "raycastlib.h" #include @@ -63,7 +66,7 @@ int testSingleRay(Unit startX, Unit startY, Unit dirX, Unit dirY, } int testSingleMapping(Unit posX, Unit posY, Unit posZ, uint32_t resX, - uint32_t resY, Unit camX, Unit camY, Unit camZ, Unit camDir, Unit fov, + uint32_t resY, Unit camX, Unit camY, Unit camZ, Unit camDir, Unit expectX, Unit expectY, Unit expectZ) { int result; @@ -76,7 +79,6 @@ int testSingleMapping(Unit posX, Unit posY, Unit posZ, uint32_t resX, c.position.y = camY; c.direction = camDir; c.height = camZ; - c.fovAngle = fov; Vector2D pos; Unit height; @@ -151,7 +153,6 @@ void benchmarkMapping() c.position.y = UNITS_PER_SQUARE * 2; c.direction = UNITS_PER_SQUARE / 8; c.height = 0; - c.fovAngle = UNITS_PER_SQUARE / 2; PixelInfo p; @@ -172,7 +173,7 @@ void benchmarkMapping() } } -void pixelFunc(PixelInfo p) +void pixelFunc(PixelInfo *p) { } @@ -186,7 +187,6 @@ void benchmarkRender() c.position.y = 12; c.direction = 100; c.height = 200; - c.fovAngle = UNITS_PER_SQUARE / 3; RayConstraints constraints; @@ -237,6 +237,23 @@ int main() 16)) return 1; + printf("testing perspective scale...\n"); + + for (Unit i = 1; i < 100; ++i) + { + Unit size = i * 3; + Unit distance = i * 6 + 200; + + Unit scaled = perspectiveScale(size,distance); + Unit distance2 = perspectiveScaleInverse(size,scaled); + + if (absVal(distance - distance2 > 2)) + printf("ERROR: distance: %d, distance inverse: %d\n",distance,distance2); + } + + printf("OK\n"); + +/* if (!testSingleMapping( -UNITS_PER_SQUARE, 0, @@ -247,13 +264,12 @@ int main() 0, 0, UNITS_PER_SQUARE / 2, - UNITS_PER_SQUARE / 4, 640, 0, 1024 )) return -1; - +*/ printf("benchmark:\n"); long t;