Semi-working png textures (broken on horizontal walls)
This commit is contained in:
parent
b4f1cf6353
commit
770d5c02b2
BIN
assets/texture_wall1.png
Normal file
BIN
assets/texture_wall1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
BIN
assets/texture_wall2.png
Normal file
BIN
assets/texture_wall2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
BIN
assets/texture_wall3.png
Normal file
BIN
assets/texture_wall3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
@ -3,13 +3,14 @@
|
|||||||
# RAYCASTER
|
# RAYCASTER
|
||||||
# Inspired by https://www.youtube.com/watch?v=gYRrGTC7GtA
|
# Inspired by https://www.youtube.com/watch?v=gYRrGTC7GtA
|
||||||
#
|
#
|
||||||
# pip install pysdl2 pysdl2-dll
|
# pip install pysdl2 pysdl2-dll pypng
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import sdl2.ext
|
import sdl2.ext
|
||||||
import math
|
import math
|
||||||
import time
|
import time
|
||||||
import ctypes
|
import ctypes
|
||||||
|
import png
|
||||||
|
|
||||||
# Map cfg
|
# Map cfg
|
||||||
MAP_HIDDEN = True
|
MAP_HIDDEN = True
|
||||||
@ -19,7 +20,12 @@ MAP_WIN_WIDTH = MAP_SIZE * MAP_SCALE
|
|||||||
MAP_WIN_HEIGHT = MAP_SIZE * MAP_SCALE
|
MAP_WIN_HEIGHT = MAP_SIZE * MAP_SCALE
|
||||||
|
|
||||||
# Textures cfg
|
# Textures cfg
|
||||||
TEXTURE_SIZE = 8
|
TEXTURES = [
|
||||||
|
"../assets/texture_wall1.png",
|
||||||
|
"../assets/texture_wall2.png",
|
||||||
|
"../assets/texture_wall3.png",
|
||||||
|
]
|
||||||
|
TEXTURE_SIZE = 64
|
||||||
|
|
||||||
# Raycast cfg
|
# Raycast cfg
|
||||||
RAYCAST_WIN_WIDTH = 1000
|
RAYCAST_WIN_WIDTH = 1000
|
||||||
@ -57,38 +63,6 @@ MAP = [
|
|||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1, 1,
|
|
||||||
1, 1, 1, 1, 1, 1, 1, 1,
|
|
||||||
]
|
|
||||||
]
|
|
||||||
|
|
||||||
class Main:
|
class Main:
|
||||||
|
|
||||||
@ -97,6 +71,11 @@ class Main:
|
|||||||
if len(MAP) != MAP_SIZE * MAP_SIZE:
|
if len(MAP) != MAP_SIZE * MAP_SIZE:
|
||||||
raise ValueError("Map size is {}, but should be a power of {}".format(len(MAP), MAP_SIZE))
|
raise ValueError("Map size is {}, but should be a power of {}".format(len(MAP), MAP_SIZE))
|
||||||
|
|
||||||
|
# Load textures
|
||||||
|
self.textures = []
|
||||||
|
for texFile in TEXTURES:
|
||||||
|
self.textures.append(self.loadTexture(texFile))
|
||||||
|
|
||||||
# Graphics
|
# Graphics
|
||||||
sdl2.ext.init()
|
sdl2.ext.init()
|
||||||
if not MAP_HIDDEN:
|
if not MAP_HIDDEN:
|
||||||
@ -350,7 +329,7 @@ class Main:
|
|||||||
textureSegmentEnd = textureSegmentStart + textureSegmentLength
|
textureSegmentEnd = textureSegmentStart + textureSegmentLength
|
||||||
# Obtain texture value in the pixel representing the current segment and calculate shading
|
# Obtain texture value in the pixel representing the current segment and calculate shading
|
||||||
if vertDist > horizDist:
|
if vertDist > horizDist:
|
||||||
texIndex = mapBlockHitY - 1 # The texture covering the selected map tile (0 is no texture, 1 is texture at TEXTURES[0] etc)
|
texIndex = mapBlockHitY - 1 # The texture covering the selected map tile (0 is no texture, 1 is texture at self.textures[0] etc)
|
||||||
texColumn = int(rayX / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE)
|
texColumn = int(rayX / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE)
|
||||||
shading = True
|
shading = True
|
||||||
else:
|
else:
|
||||||
@ -358,9 +337,9 @@ class Main:
|
|||||||
texColumn = int(rayY / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE)
|
texColumn = int(rayY / (MAP_SCALE / TEXTURE_SIZE) % TEXTURE_SIZE)
|
||||||
shading = False
|
shading = False
|
||||||
|
|
||||||
texel = TEXTURES[texIndex][texColumn + textureColumnPixel * TEXTURE_SIZE]
|
# Obtain texture pixel color
|
||||||
|
color = self.textures[texIndex][texColumn + textureColumnPixel * TEXTURE_SIZE]
|
||||||
# Calculate color resulting from texture pixel value + shading
|
# Calculate color resulting from texture pixel value + shading
|
||||||
color = (texel * 255) # TODO: Remove "* 255" when the texel will contain the full color integer (3 channels shifted by 8 bit each)
|
|
||||||
if shading:
|
if shading:
|
||||||
color = color * (-SHADING_COLOR)
|
color = color * (-SHADING_COLOR)
|
||||||
|
|
||||||
@ -392,10 +371,23 @@ class Main:
|
|||||||
for idx in range(startIdx, endY * RAYCAST_WIN_WIDTH + x, RAYCAST_WIN_WIDTH):
|
for idx in range(startIdx, endY * RAYCAST_WIN_WIDTH + x, RAYCAST_WIN_WIDTH):
|
||||||
self.raycast_u32_pixels[idx] = color
|
self.raycast_u32_pixels[idx] = color
|
||||||
|
|
||||||
|
|
||||||
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))
|
||||||
|
|
||||||
|
def loadTexture(self, pngFilePath):
|
||||||
|
# Loads a texture from png file and converts to sdl2-friendly format
|
||||||
|
reader = png.Reader(filename=pngFilePath)
|
||||||
|
w, h, pixels, metadata = reader.read_flat()
|
||||||
|
if metadata['alpha']:
|
||||||
|
raise ValueError("Textures with alpha channel are not supported")
|
||||||
|
if w != TEXTURE_SIZE or h != TEXTURE_SIZE:
|
||||||
|
raise ValueError("Texture {} is not {}x{}, but {}x{}".format(pngFilePath, TEXTURE_SIZE, TEXTURE_SIZE, w, h))
|
||||||
|
# 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))
|
||||||
|
return converted
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user