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

Compute fog

This commit is contained in:
Miloslav Číž 2018-09-01 12:04:29 +02:00
parent c4b664714b
commit 471b989fcb

View file

@ -494,15 +494,23 @@ void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc,
PixelFunction _pixelFunction = 0; PixelFunction _pixelFunction = 0;
ArrayFunction _arrayFunction = 0; ArrayFunction _arrayFunction = 0;
Camera _camera; Camera _camera;
Unit _floorDepthStep = 0;
Unit _startHeight = 0;
void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray) void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
{ {
int32_t y = _camera.resolution.y - 1; // on screen y, will only go upwards int32_t y = _camera.resolution.y - 1; // on screen y, will only go upwards
Unit worldZPrev = -1 * _camera.height; Unit worldZPrev = _startHeight;
Unit previousDepth = 1;
uint16_t middleRow = _camera.resolution.y / 2; uint16_t middleRow = _camera.resolution.y / 2;
PixelInfo p;
p.position.x = x;
for (uint32_t j = 0; j < hitCount; ++j) for (uint32_t j = 0; j < hitCount; ++j)
{ {
HitResult hit = hits[j]; HitResult hit = hits[j];
@ -522,36 +530,40 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
Unit worldZ2 = -1 * _camera.height + wallHeight; Unit worldZ2 = -1 * _camera.height + wallHeight;
Unit z1Screen = middleRow - int16_t z1Screen = middleRow -
perspectiveScale( perspectiveScale(
(worldZPrev * _camera.resolution.y) / UNITS_PER_SQUARE, (worldZPrev * _camera.resolution.y) / UNITS_PER_SQUARE,
dist, dist,1);
1);
Unit z2Screen = middleRow - z1Screen = clamp(z1Screen,0,_camera.resolution.y - 1);
int16_t z2Screen = middleRow -
perspectiveScale( perspectiveScale(
(worldZ2 * _camera.resolution.y) / UNITS_PER_SQUARE, (worldZ2 * _camera.resolution.y) / UNITS_PER_SQUARE,
dist, dist,1);
1);
PixelInfo p; z2Screen = clamp(z2Screen,0,_camera.resolution.y - 1);
p.position.x = x;
Unit zTop = z1Screen < z2Screen ? z1Screen : z2Screen;
// draw floor until the wall // draw floor until the wall
p.isWall = 0; p.isWall = 0;
Unit depthDiff = dist - previousDepth;
Unit zTop = z1Screen < z2Screen ? z1Screen : z2Screen; Unit floorCameraDiff = _camera.height - worldZPrev;
for (int32_t i = y; i > zTop; --i) for (int32_t i = y; i > zTop; --i)
{ {
p.position.y = i; p.position.y = i;
p.depth = (_camera.resolution.y - i) * _floorDepthStep + floorCameraDiff;
_pixelFunction(p); _pixelFunction(p);
} }
// draw the wall // draw the wall
p.isWall = 1; p.isWall = 1;
p.depth = dist;
for (int32_t i = z1Screen < y ? z1Screen : y; i > z2Screen; --i) for (int32_t i = z1Screen < y ? z1Screen : y; i > z2Screen; --i)
{ {
@ -562,6 +574,20 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray)
y = y > zTop ? zTop : y; y = y > zTop ? zTop : y;
worldZPrev = worldZ2; worldZPrev = worldZ2;
previousDepth = dist;
}
// draw floor until horizon
p.isWall = 0;
Unit floorCameraDiff = _camera.height - worldZPrev;
for (int32_t i = y; i >= middleRow; --i)
{
p.position.y = i;
_pixelFunction(p);
p.depth = (_camera.resolution.y - i) * _floorDepthStep + floorCameraDiff;
} }
} }
@ -571,6 +597,14 @@ void render(Camera cam, ArrayFunction arrayFunc, PixelFunction pixelFunc,
_pixelFunction = pixelFunc; _pixelFunction = pixelFunc;
_arrayFunction = arrayFunc; _arrayFunction = arrayFunc;
_camera = cam; _camera = cam;
_startHeight = arrayFunc(
cam.position.x / UNITS_PER_SQUARE,
cam.position.y / UNITS_PER_SQUARE) -1 * cam.height;
// TODO
_floorDepthStep = (10 * UNITS_PER_SQUARE) / cam.resolution.y;
castRaysMultiHit(cam,arrayFunc,_columnFunction,constraints); castRaysMultiHit(cam,arrayFunc,_columnFunction,constraints);
} }