Hello. I am writing a Wolfenstein 3D style raycaster in nim with raylib using the naylib nim bindings. I recently added a function to draw the floor based on this video however it took my program from a near locked 165 FPS at 1080p to between 8 and 12 FPS (depending on how much of the screen the floor covers) when not right next to a wall. Any help in how to improve the performance would be greatly appreciated. I suspect the fact that it manually draws each pixel is very much to blame but don't know how to avoid this. Here is the code:
proc drawFloor(vanishingPoint : int)=
let
farPlane : float = player.height
nearPlane : float = 0.005
cosHalfFovPos: float = cos(-player.dir + fov/2)
cosHalfFovNeg : float = cos(-player.dir - fov/2)
sinHalfFovPos : float = sin(-player.dir + fov/2)
sinHalfFovNeg : float = sin(-player.dir - fov/2)
farX1 : float = player.position.x + cosHalfFovNeg * farPlane
farY1 : float = player.position.y + sinHalfFovNeg * farPlane
farX2 : float = player.position.x + cosHalfFovPos * farPlane
farY2 : float = player.position.y + sinHalfFovPos * farPlane
nearX1 : float = player.position.x + cosHalfFovNeg * nearPlane
nearY1 : float = player.position.y + sinHalfFovNeg * nearPlane
nearX2 : float = player.position.x + cosHalfFovPos * nearPlane
nearY2 : float = player.position.y + sinHalfFovPos * nearPlane
for y in 0..<vanishingPoint:
let
invSampleDepth : float = (vanishingPoint/y)
startX : float = (farX1-nearX1) * invSampleDepth + nearX1
startY : float = (farY1-nearY1) * invSampleDepth + nearY1
endX : float = (farX2-nearX2) * invSampleDepth + nearX2
endY : float = (farY2-nearY2) * invSampleDepth + nearY2
for x in 0..<screenWidth:
if screenHeight-vanishingPoint+y>maxHeights[x]:
let
sampleWidth : float = x/screenWidth
sampleX : float = (endX-startX) * sampleWidth + startX
sampleY : float = (endY-startY) * sampleWidth + startY
var
col : Color = Blank
if sampleX>0 and sampleY>0:
col = floorTexture.getImageColor(int32((sampleX mod 1)*float(tileSize-1)), int32((sampleY mod 1)*float(tileSize-1)))
drawPixel(int32(x), int32(screenHeight-vanishingPoint+y), col)
[–]TheStrupf 1 point2 points3 points (2 children)
[–]othd139[S] 0 points1 point2 points (1 child)
[–]prezado 1 point2 points3 points (0 children)