mirror of
https://git.coom.tech/drummyfish/raycastlib.git
synced 2024-12-24 01:36:19 +01:00
Add render function
This commit is contained in:
parent
1e5d223be2
commit
30f8e6f205
1 changed files with 42 additions and 0 deletions
42
raycastlib.h
42
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);
|
||||
|
|
Loading…
Reference in a new issue