1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/raycastlib.git synced 2024-11-24 20:59:58 +01:00

Add type function

This commit is contained in:
Miloslav Číž 2018-09-05 16:26:13 +02:00
parent 657b5a2891
commit a793e9f720

View file

@ -36,7 +36,9 @@
typedef uint16_t uint_maybe32_t; typedef uint16_t uint_maybe32_t;
#endif #endif
#ifndef VERTICAL_FOV
#define VERTICAL_FOV (UNITS_PER_SQUARE / 2) #define VERTICAL_FOV (UNITS_PER_SQUARE / 2)
#endif
#define logVector2D(v)\ #define logVector2D(v)\
printf("[%d,%d]\n",v.x,v.y); printf("[%d,%d]\n",v.x,v.y);
@ -87,7 +89,8 @@ typedef struct
Vector2D position; ///< Exact collision position in Units. Vector2D position; ///< Exact collision position in Units.
Unit distance; /**< Euclidean distance to the hit position, or -1 if Unit distance; /**< Euclidean distance to the hit position, or -1 if
no collision happened. */ no collision happened. */
Unit textureCoord; /// Normalized (0 to UNITS_PER_SQUARE - 1) tex coord. Unit textureCoord; ///< Normalized (0 to UNITS_PER_SQUARE - 1) tex coord.
Unit type; ///< Integer identifying type of square.
uint8_t direction; ///< Direction of hit. uint8_t direction; ///< Direction of hit.
} HitResult; } HitResult;
@ -145,8 +148,8 @@ PixelInfo mapToScreen(Vector2D worldPosition, Unit height, Camera camera);
/** /**
Casts a single ray and returns a list of collisions. Casts a single ray and returns a list of collisions.
*/ */
void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, HitResult *hitResults, void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, ArrayFunction typeFunc,
uint16_t *hitResultsLen, RayConstraints constraints); HitResult *hitResults, uint16_t *hitResultsLen, RayConstraints constraints);
Vector2D angleToDirection(Unit angle); Vector2D angleToDirection(Unit angle);
Unit cosInt(Unit input); Unit cosInt(Unit input);
@ -176,7 +179,8 @@ Unit perspectiveScale(Unit originalSize, Unit distance);
function. function.
*/ */
void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc, void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc,
ColumnFunction columnFunc, RayConstraints constraints); ArrayFunction typeFunction, ColumnFunction columnFunc,
RayConstraints constraints);
/** /**
Renders a complete camera view. Renders a complete camera view.
@ -185,11 +189,14 @@ void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc,
@param floorHeightFunc function that returns floor height (in Units) @param floorHeightFunc function that returns floor height (in Units)
@param ceilingHeightFunc same as floorHeightFunc but for ceiling, can also be @param ceilingHeightFunc same as floorHeightFunc but for ceiling, can also be
0 (no ceiling will be rendered) 0 (no ceiling will be rendered)
@param typeFunction function that says a type of square (e.g. its texture
index), can be 0 (no type in hit result)
@param pixelFunc callback function to draw a single pixel on screen @param pixelFunc callback function to draw a single pixel on screen
@param constraints constraints for each cast ray @param constraints constraints for each cast ray
*/ */
void render(Camera cam, ArrayFunction floorHeightFunc, ArrayFunction void render(Camera cam, ArrayFunction floorHeightFunc,
ceilingHeightFunc, PixelFunction pixelFunc, RayConstraints constraints); ArrayFunction ceilingHeightFunc, ArrayFunction typeFunction,
PixelFunction pixelFunc, RayConstraints constraints);
//============================================================================= //=============================================================================
// privates // privates
@ -449,8 +456,8 @@ void castRaySquare(Ray localRay, Vector2D *nextCellOff, Vector2D *collOff)
collOff->y += nextCellOff->y; collOff->y += nextCellOff->y;
} }
void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, HitResult *hitResults, void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, ArrayFunction typeFunc,
uint16_t *hitResultsLen, RayConstraints constraints) HitResult *hitResults, uint16_t *hitResultsLen, RayConstraints constraints)
{ {
profileCall(castRayMultiHit); profileCall(castRayMultiHit);
@ -487,6 +494,9 @@ void castRayMultiHit(Ray ray, ArrayFunction arrayFunc, HitResult *hitResults,
h.square = currentSquare; h.square = currentSquare;
h.distance = dist(initialPos,currentPos); h.distance = dist(initialPos,currentPos);
if (typeFunc != 0)
h.type = typeFunc(currentSquare.x,currentSquare.y);
if (no.y > 0) if (no.y > 0)
{ {
h.direction = 0; h.direction = 0;
@ -543,7 +553,7 @@ HitResult castRay(Ray ray, ArrayFunction arrayFunc)
c.maxSteps = 1000; c.maxSteps = 1000;
c.maxHits = 1; c.maxHits = 1;
castRayMultiHit(ray,arrayFunc,&result,&len,c); castRayMultiHit(ray,arrayFunc,0,&result,&len,c);
if (len == 0) if (len == 0)
result.distance = -1; result.distance = -1;
@ -552,7 +562,8 @@ HitResult castRay(Ray ray, ArrayFunction arrayFunc)
} }
void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc, void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc,
ColumnFunction columnFunc, RayConstraints constraints) ArrayFunction typeFunction, ColumnFunction columnFunc,
RayConstraints constraints)
{ {
uint16_t fovHalf = cam.fovAngle / 2; uint16_t fovHalf = cam.fovAngle / 2;
@ -573,7 +584,7 @@ void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc,
r.direction.x = dir1.x + (dX * i) / cam.resolution.x; r.direction.x = dir1.x + (dX * i) / cam.resolution.x;
r.direction.y = dir1.y + (dY * i) / cam.resolution.x; r.direction.y = dir1.y + (dY * i) / cam.resolution.x;
castRayMultiHit(r,arrayFunc,hits,&hitCount,constraints); castRayMultiHit(r,arrayFunc,typeFunction,hits,&hitCount,constraints);
columnFunc(hits,hitCount,i,r); columnFunc(hits,hitCount,i,r);
} }
@ -787,8 +798,9 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
#undef VERTICAL_DEPTH_MULTIPLY #undef VERTICAL_DEPTH_MULTIPLY
} }
void render(Camera cam, ArrayFunction floorHeightFunc, ArrayFunction void render(Camera cam, ArrayFunction floorHeightFunc,
ceilingHeightFunc, PixelFunction pixelFunc, RayConstraints constraints) ArrayFunction ceilingHeightFunc, ArrayFunction typeFunction,
PixelFunction pixelFunc, RayConstraints constraints)
{ {
_pixelFunction = pixelFunc; _pixelFunction = pixelFunc;
_floorFunction = floorHeightFunc; _floorFunction = floorHeightFunc;
@ -812,7 +824,8 @@ void render(Camera cam, ArrayFunction floorHeightFunc, ArrayFunction
// TODO // TODO
_floorDepthStep = (12 * UNITS_PER_SQUARE) / cam.resolution.y; _floorDepthStep = (12 * UNITS_PER_SQUARE) / cam.resolution.y;
castRaysMultiHit(cam,_floorCeilFunction,_columnFunction,constraints); castRaysMultiHit(cam,_floorCeilFunction,typeFunction,
_columnFunction,constraints);
} }
Vector2D normalize(Vector2D v) Vector2D normalize(Vector2D v)