mirror of
https://git.coom.tech/drummyfish/raycastlib.git
synced 2024-11-23 20:49:57 +01:00
Rework castRays function
This commit is contained in:
parent
40d80db615
commit
1e5d223be2
1 changed files with 11 additions and 13 deletions
24
raycastlib.h
24
raycastlib.h
|
@ -75,7 +75,7 @@ typedef struct
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
Vector2D position; ///< On-screen position.
|
Vector2D position; ///< On-screen position.
|
||||||
int8_t type; ///< Type of pixel: 0 - wall, 1 - floor, 2 - ceiling.
|
int8_t isWall; ///< Whether the pixel is a wall or a floor(/ceiling).
|
||||||
Unit depth; ///< Corrected depth.
|
Unit depth; ///< Corrected depth.
|
||||||
HitResult hit; ///< Corresponding ray hit.
|
HitResult hit; ///< Corresponding ray hit.
|
||||||
} PixelInfo;
|
} PixelInfo;
|
||||||
|
@ -87,8 +87,9 @@ typedef struct
|
||||||
} RayConstraints;
|
} RayConstraints;
|
||||||
|
|
||||||
typedef int16_t (*ArrayFunction)(int16_t x, int16_t y);
|
typedef int16_t (*ArrayFunction)(int16_t x, int16_t y);
|
||||||
typedef void (*HitFunction)(uint16_t pos, HitResult h, uint16_t hitNo, Ray r);
|
typedef void (*ColumnFunction)(HitResult *hits, uint16_t hitCount, uint16_t x,
|
||||||
typedef void (*PixelFunc)(PixelInfo info);
|
Ray ray);
|
||||||
|
typedef void (*PixelFunction)(PixelInfo info);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Casts a single ray and returns the first collision result.
|
Casts a single ray and returns the first collision result.
|
||||||
|
@ -137,11 +138,8 @@ Unit perspectiveScale(Unit originalSize, Unit distance, Unit fov);
|
||||||
Casts rays for given camera view and for each hit calls a user provided
|
Casts rays for given camera view and for each hit calls a user provided
|
||||||
function.
|
function.
|
||||||
*/
|
*/
|
||||||
void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc, HitFunction hitFunc,
|
void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc,
|
||||||
RayConstraints constraints);
|
ColumnFunction columnFunc, RayConstraints constraints);
|
||||||
|
|
||||||
void render(Camera cam, ArrayFunction arrayFunc, PixelFunc pixelFunc,
|
|
||||||
RayConstraints constraints);
|
|
||||||
|
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
// privates
|
// privates
|
||||||
|
@ -454,8 +452,8 @@ HitResult castRay(Ray ray, ArrayFunction arrayFunc)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc, HitFunction hitFunc,
|
void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc,
|
||||||
RayConstraints constraints)
|
ColumnFunction columnFunc, RayConstraints constraints)
|
||||||
{
|
{
|
||||||
uint16_t fovHalf = cam.fovAngle / 2;
|
uint16_t fovHalf = cam.fovAngle / 2;
|
||||||
|
|
||||||
|
@ -466,20 +464,20 @@ void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc, HitFunction hitFunc,
|
||||||
Unit dY = dir2.y - dir1.y;
|
Unit dY = dir2.y - dir1.y;
|
||||||
|
|
||||||
HitResult hits[constraints.maxHits];
|
HitResult hits[constraints.maxHits];
|
||||||
|
Ray rays[constraints.maxHits];
|
||||||
uint16_t hitCount;
|
uint16_t hitCount;
|
||||||
|
|
||||||
Ray r;
|
Ray r;
|
||||||
r.start = cam.position;
|
r.start = cam.position;
|
||||||
|
|
||||||
for (uint8_t i = 0; i < cam.resolution.x; ++i)
|
for (uint16_t i = 0; i < cam.resolution.x; ++i)
|
||||||
{
|
{
|
||||||
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,hits,&hitCount,constraints);
|
||||||
|
|
||||||
for (uint8_t j = 0; j < hitCount; ++j)
|
columnFunc(hits,hitCount,i,r);
|
||||||
hitFunc(i,hits[j],j,r);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue