Rendering textures (for now, a single texture row))

This commit is contained in:
Daniele Verducci (Slimpenguin) 2023-01-06 10:44:40 +01:00
parent 37d4573778
commit adf11abd16

View File

@ -12,8 +12,8 @@ import time
MAP_WIN_WIDTH = 640 MAP_WIN_WIDTH = 640
MAP_WIN_HEIGHT = 640 MAP_WIN_HEIGHT = 640
RAYCAST_WIN_WIDTH = 800 RAYCAST_WIN_WIDTH = 400
RAYCAST_WIN_HEIGHT = 480 RAYCAST_WIN_HEIGHT = 255
DUNGEON_WIDTH = MAP_WIN_WIDTH DUNGEON_WIDTH = MAP_WIN_WIDTH
DUNGEON_HEIGHT = MAP_WIN_HEIGHT DUNGEON_HEIGHT = MAP_WIN_HEIGHT
PLAYER_SPEED = 10 PLAYER_SPEED = 10
@ -43,6 +43,14 @@ MAP = [
MAP_SIZE = 16 MAP_SIZE = 16
DOF = 2*MAP_SIZE # Depth Of Field 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: class Main:
def __init__(self): def __init__(self):
@ -274,18 +282,31 @@ class Main:
# Calculate line height based on distance # Calculate line height based on distance
lineHeight = MAP_SCALE * RAYCAST_WIN_HEIGHT / shortestDist lineHeight = MAP_SCALE * RAYCAST_WIN_HEIGHT / shortestDist
if lineHeight > RAYCAST_WIN_HEIGHT:
lineHeight = RAYCAST_WIN_HEIGHT
# Center line vertically in window # Center line vertically in window
lineOffset = RAYCAST_WIN_HEIGHT / 2 - lineHeight / 2 lineOffset = RAYCAST_WIN_HEIGHT / 2 - lineHeight / 2
# Simulate lighting based on wall incidence # Draw pixels vertically from top to bottom to obtain a line
color = sdl2.ext.Color(255,255,255,255) 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: if vertDist > horizDist:
color = sdl2.ext.Color(200,200,200,255) 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): def dist(self, ax, ay, bx, by):
return math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay)) return math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay))