1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/raycastlib.git synced 2024-11-23 20:49:57 +01:00

Precompute more stuff

This commit is contained in:
Miloslav Číž 2018-09-08 07:23:21 +02:00
parent 5c9c7cb299
commit 61e747c8d6

View file

@ -40,6 +40,12 @@
#define VERTICAL_FOV (UNITS_PER_SQUARE / 2) #define VERTICAL_FOV (UNITS_PER_SQUARE / 2)
#endif #endif
#ifndef HORIZONTAL_FOV
#define HORIZONTAL_FOV (UNITS_PER_SQUARE / 4)
#endif
#define HORIZONTAL_FOV_HALF (HORIZONTAL_FOV / 2)
#define logVector2D(v)\ #define logVector2D(v)\
printf("[%d,%d]\n",v.x,v.y); printf("[%d,%d]\n",v.x,v.y);
@ -103,7 +109,6 @@ typedef struct
Vector2D resolution; Vector2D resolution;
int16_t shear; /* Shear offset in pixels (0 => no shear), can simulate int16_t shear; /* Shear offset in pixels (0 => no shear), can simulate
looking up/down. */ looking up/down. */
Unit fovAngle;
Unit height; Unit height;
Unit collisionRadius; Unit collisionRadius;
@ -585,10 +590,8 @@ void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc,
ArrayFunction typeFunction, ColumnFunction columnFunc, ArrayFunction typeFunction, ColumnFunction columnFunc,
RayConstraints constraints) RayConstraints constraints)
{ {
uint16_t fovHalf = cam.fovAngle / 2; Vector2D dir1 = angleToDirection(cam.direction - HORIZONTAL_FOV_HALF);
Vector2D dir2 = angleToDirection(cam.direction + HORIZONTAL_FOV_HALF);
Vector2D dir1 = angleToDirection(cam.direction - fovHalf);
Vector2D dir2 = angleToDirection(cam.direction + fovHalf);
Unit dX = dir2.x - dir1.x; Unit dX = dir2.x - dir1.x;
Unit dY = dir2.y - dir1.y; Unit dY = dir2.y - dir1.y;
@ -621,6 +624,8 @@ Unit _middleRow = 0;
ArrayFunction _floorFunction = 0; ArrayFunction _floorFunction = 0;
ArrayFunction _ceilFunction = 0; ArrayFunction _ceilFunction = 0;
uint8_t _computeTextureCoords = 0; uint8_t _computeTextureCoords = 0;
Unit _fogStartYBottom = 0;
Unit _fogStartYTop = 0;
/** /**
Helper function that determines intersection with both ceiling and floor. Helper function that determines intersection with both ceiling and floor.
@ -646,9 +651,6 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
PixelInfo p; PixelInfo p;
p.position.x = x; p.position.x = x;
Unit fogStartYBottom;
Unit fogStartYTop;
#define VERTICAL_DEPTH_MULTIPLY 2 #define VERTICAL_DEPTH_MULTIPLY 2
// we'll be simulatenously drawing the floor and the ceiling now // we'll be simulatenously drawing the floor and the ceiling now
@ -661,8 +663,6 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
distance at all, but rather compute the distance of the collision distance at all, but rather compute the distance of the collision
point from the projection plane (line). */ point from the projection plane (line). */
Unit halfResY = _camera.resolution.y / 2;
Unit dist = // adjusted distance Unit dist = // adjusted distance
(hit.distance * vectorsAngleCos(angleToDirection(_camera.direction), (hit.distance * vectorsAngleCos(angleToDirection(_camera.direction),
ray.direction)) / UNITS_PER_SQUARE; ray.direction)) / UNITS_PER_SQUARE;
@ -732,13 +732,10 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
Unit floorCameraDiff = absVal(worldZPrev) * VERTICAL_DEPTH_MULTIPLY; Unit floorCameraDiff = absVal(worldZPrev) * VERTICAL_DEPTH_MULTIPLY;
fogStartYBottom = _middleRow + halfResY;
fogStartYTop = _middleRow - halfResY;
for (int_maybe32_t i = y; i > z1Screen; --i) for (int_maybe32_t i = y; i > z1Screen; --i)
{ {
p.position.y = i; p.position.y = i;
p.depth = (fogStartYBottom - i) * _floorDepthStep + floorCameraDiff; p.depth = (_fogStartYBottom - i) * _floorDepthStep + floorCameraDiff;
_pixelFunction(p); _pixelFunction(p);
} }
@ -756,7 +753,7 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
for (int_maybe32_t i = y2; i < z1ScreenCeil; ++i) for (int_maybe32_t i = y2; i < z1ScreenCeil; ++i)
{ {
p.position.y = i; p.position.y = i;
p.depth = (i - fogStartYTop) * _floorDepthStep + ceilCameraDiff; p.depth = (i - _fogStartYTop) * _floorDepthStep + ceilCameraDiff;
_pixelFunction(p); _pixelFunction(p);
} }
} }
@ -827,7 +824,7 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
for (int_maybe32_t i = y; i >= horizon + (horizon > y2 ? 0 : 1); --i) for (int_maybe32_t i = y; i >= horizon + (horizon > y2 ? 0 : 1); --i)
{ {
p.position.y = i; p.position.y = i;
p.depth = (fogStartYBottom - i) * _floorDepthStep + floorCameraDiff; p.depth = (_fogStartYBottom - i) * _floorDepthStep + floorCameraDiff;
_pixelFunction(p); _pixelFunction(p);
} }
@ -843,7 +840,7 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
for (int_maybe32_t i = y2; i < horizon; ++i) for (int_maybe32_t i = y2; i < horizon; ++i)
{ {
p.position.y = i; p.position.y = i;
p.depth = (i - fogStartYTop) * _floorDepthStep + ceilCameraDiff; p.depth = (i - _fogStartYTop) * _floorDepthStep + ceilCameraDiff;
_pixelFunction(p); _pixelFunction(p);
} }
} }
@ -860,9 +857,13 @@ void render(Camera cam, ArrayFunction floorHeightFunc,
_ceilFunction = ceilingHeightFunc; _ceilFunction = ceilingHeightFunc;
_camera = cam; _camera = cam;
_camResYLimit = cam.resolution.y - 1; _camResYLimit = cam.resolution.y - 1;
_middleRow = cam.resolution.y / 2;
_middleRow = _middleRow + cam.shear; int16_t halfResY = cam.resolution.y / 2;
_middleRow = halfResY + cam.shear;
_fogStartYBottom = _middleRow + halfResY;
_fogStartYTop = _middleRow - halfResY;
_computeTextureCoords = constraints.computeTextureCoords; _computeTextureCoords = constraints.computeTextureCoords;
@ -944,12 +945,10 @@ PixelInfo mapToScreen(Vector2D worldPosition, Unit height, Camera camera)
if (!pointIsLeftOfRay(worldPosition,r)) if (!pointIsLeftOfRay(worldPosition,r))
a *= -1; a *= -1;
Unit alpha = camera.fovAngle / 2; Unit cos = cosInt(HORIZONTAL_FOV_HALF);
Unit cos = cosInt(alpha);
Unit b = Unit b =
(result.depth * sinInt(alpha)) / (cos == 0 ? 1 : cos); // sin/cos = tan (result.depth * sinInt(HORIZONTAL_FOV_HALF)) / (cos == 0 ? 1 : cos); // sin/cos = tan
result.position.x = (a * middleColumn) / b; result.position.x = (a * middleColumn) / b;
result.position.x = middleColumn - result.position.x; result.position.x = middleColumn - result.position.x;