Fixed shading

This commit is contained in:
Daniele Verducci (Slimpenguin) 2023-01-11 08:59:10 +01:00
parent 770d5c02b2
commit cc7892cb69

View File

@ -21,9 +21,12 @@ MAP_WIN_HEIGHT = MAP_SIZE * MAP_SCALE
# Textures cfg
TEXTURES = [
"../assets/texture_wall1.png",
"../assets/texture_wall2.png",
"../assets/texture_wall3.png",
"../assets/texture_wall3.png",
"../assets/texture_wall3.png",
# "../assets/texture.png",
# "../assets/texture.png",
# "../assets/texture.png",
]
TEXTURE_SIZE = 64
@ -34,7 +37,6 @@ RAYCAST_RESOLUTION_SCALING = 4
RAYCAST_RENDER_WIDTH = int(RAYCAST_WIN_WIDTH / RAYCAST_RESOLUTION_SCALING)
RAYCAST_RENDER_HEIGHT = int(RAYCAST_WIN_HEIGHT / RAYCAST_RESOLUTION_SCALING)
DOF = 2*MAP_SIZE # Depth Of Field
SHADING_COLOR = 128 + (128 << 8) + (128 << 16) # Color to subtract to obtain shading (128 in every channel)
CEILING_COLOR = sdl2.ext.Color(0,128,255,255)
FLOOR_COLOR = sdl2.ext.Color(0,128,0,255)
@ -341,7 +343,7 @@ class Main:
color = self.textures[texIndex][texColumn + textureColumnPixel * TEXTURE_SIZE]
# Calculate color resulting from texture pixel value + shading
if shading:
color = color * (-SHADING_COLOR)
color = self.shade(color)
# Clipping
lineEnd = textureSegmentEnd
@ -371,6 +373,18 @@ class Main:
for idx in range(startIdx, endY * RAYCAST_WIN_WIDTH + x, RAYCAST_WIN_WIDTH):
self.raycast_u32_pixels[idx] = color
def shade(self, color):
# Obtain channels
b = color & 0b000000000000000011111111
g = color >> 8 & 0b000000000000000011111111
r = color >> 16 & 0b000000000000000011111111
# Dim channels (and limit to 255, because python doesn't have a fixed byte length)
b = (b >> 1)
g = (g >> 1)
r = (r >> 1)
# Compose color
return b + (g << 8) + (r << 16)
def dist(self, ax, ay, bx, by):
return math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay))
@ -385,7 +399,8 @@ class Main:
# Convert to sdl2-friendly format
converted = []
for i in range(0, len(pixels), 3):
converted.append(pixels[i] + (pixels[i+1] << 8) + (pixels[i+2] << 16))
# PNG is RGB, SDL surface is BGR
converted.append(pixels[i+2] + (pixels[i+1] << 8) + (pixels[i] << 16)) # BGR
return converted