From 7092dfee462d14061875ba175a7e81cbc6849d02 Mon Sep 17 00:00:00 2001 From: "Daniele Verducci (Slimpenguin)" Date: Fri, 6 Jan 2023 11:26:47 +0100 Subject: [PATCH] Multiple textures --- v2/raycaster.py | 58 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/v2/raycaster.py b/v2/raycaster.py index dc9eb0d..a0255cd 100755 --- a/v2/raycaster.py +++ b/v2/raycaster.py @@ -26,14 +26,14 @@ MAP = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, - 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, - 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, - 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, - 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, - 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, - 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, - 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 2, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 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 +190,7 @@ class Main: posX = i % MAP_SIZE * MAP_SCALE posY = math.floor(i / MAP_SIZE) * MAP_SCALE color = 0 - if MAP[i] == 1: + if MAP[i] > 0: color = 255 sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(color,color,color,255), (posX, posY, MAP_SCALE - 1, MAP_SCALE - 1)) @@ -194,6 +206,10 @@ class Main: if rayAngle > math.pi * 2: rayAngle = rayAngle - math.pi * 2 + # Which map wall tiles have been hit by rayX and rayY + mapBlockHitX = 0 + mapBlockHitY = 0 + # Check horizontal lines dof = 0 # Depth of field if rayAngle == 0 or rayAngle == math.pi: @@ -223,6 +239,7 @@ class Main: mapArrayPosition = mapY * MAP_SIZE + mapX if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE and MAP[mapArrayPosition] != 0: dof = DOF # Hit the wall: we are done, no need to do other checks + mapBlockHitY = MAP[mapArrayPosition] # Save which map wall tile we reached else: # Didn't hit the wall: check successive horizontal line rayX = rayX + xOffset @@ -264,6 +281,7 @@ class Main: mapArrayPosition = mapY * MAP_SIZE + mapX if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE-1 and MAP[mapArrayPosition] != 0: dof = DOF # Hit the wall: we are done, no need to do other checks + mapBlockHitX = MAP[mapArrayPosition] # Save which map wall tile we reached else: # Didn't hit the wall: check successive horizontal line rayX = rayX + xOffset @@ -297,13 +315,15 @@ class Main: textureSegmentEnd = textureSegmentStart + textureSegmentLength # Obtain texture value in the pixel representing the current segment and calculate shading if vertDist > horizDist: + texIndex = mapBlockHitY - 1 # The texture covering the selected map tile (0 is no texture, 1 is texture at TEXTURES[0] etc) texColumn = int(rayX / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE) shading = 127 else: + texIndex = mapBlockHitX - 1 # The texture covering the selected map tile texColumn = int(rayY / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE) shading = 255 - texel = TEXTURE[texColumn + textureColumnPixel * TEXTURE_SIZE] + texel = TEXTURES[texIndex][texColumn + textureColumnPixel * TEXTURE_SIZE] # Calculate color resulting from texture pixel value + shading color = sdl2.ext.Color(texel*shading,texel*shading,texel*shading,255) # Clipping