From adf11abd163d423d53cd1b10dc93b82cb452aef2 Mon Sep 17 00:00:00 2001 From: "Daniele Verducci (Slimpenguin)" Date: Fri, 6 Jan 2023 10:44:40 +0100 Subject: [PATCH] Rendering textures (for now, a single texture row)) --- v2/raycaster.py | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/v2/raycaster.py b/v2/raycaster.py index 52f918a..8bf3079 100755 --- a/v2/raycaster.py +++ b/v2/raycaster.py @@ -12,8 +12,8 @@ import time MAP_WIN_WIDTH = 640 MAP_WIN_HEIGHT = 640 -RAYCAST_WIN_WIDTH = 800 -RAYCAST_WIN_HEIGHT = 480 +RAYCAST_WIN_WIDTH = 400 +RAYCAST_WIN_HEIGHT = 255 DUNGEON_WIDTH = MAP_WIN_WIDTH DUNGEON_HEIGHT = MAP_WIN_HEIGHT PLAYER_SPEED = 10 @@ -43,6 +43,14 @@ MAP = [ MAP_SIZE = 16 DOF = 2*MAP_SIZE # Depth Of Field +TEXTURE = [ + 1, 0, 1, 0, + 0, 1, 0, 1, + 1, 0, 1, 0, + 0, 1, 0, 1, +] +TEXTURE_SIZE = 4 + class Main: def __init__(self): @@ -274,18 +282,31 @@ class Main: # Calculate line height based on distance lineHeight = MAP_SCALE * RAYCAST_WIN_HEIGHT / shortestDist - if lineHeight > RAYCAST_WIN_HEIGHT: - lineHeight = RAYCAST_WIN_HEIGHT # Center line vertically in window lineOffset = RAYCAST_WIN_HEIGHT / 2 - lineHeight / 2 - # Simulate lighting based on wall incidence - color = sdl2.ext.Color(255,255,255,255) - if vertDist > horizDist: - color = sdl2.ext.Color(200,200,200,255) + # Draw pixels vertically from top to bottom to obtain a line + for textureColumnPixel in range(0, TEXTURE_SIZE): + # Calc texture segment length on screen + textureSegmentLength = lineHeight / TEXTURE_SIZE + textureSegmentStart = lineOffset + textureColumnPixel * textureSegmentLength + textureSegmentEnd = textureSegmentStart + textureSegmentLength + # Obtain texture value in the pixel representing the current segment + texColumn = 1 + texel = TEXTURE[texColumn + textureColumnPixel * TEXTURE_SIZE] + # Calculate shading + shading = 255 + if vertDist > horizDist: + shading = 127 + # Calculate color resulting from texture pixel value + shading + color = sdl2.ext.Color(texel*shading,texel*shading,texel*shading,255) + # Clipping + lineEnd = textureSegmentEnd + if textureSegmentEnd > lineOffset + lineHeight: + textureSegmentEnd = lineOffset + lineHeight + # Draw segment + sdl2.ext.draw.line(self.raycastSurface, color, (i, int(textureSegmentStart), i, int(textureSegmentEnd))) - # Draw line - sdl2.ext.draw.line(self.raycastSurface, color, (i, int(lineOffset), i, int(lineOffset + lineHeight))) def dist(self, ax, ay, bx, by): return math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay))