2023-03-17

how could i speed up texture rendering on my pygame raycaster?

the raycaster I built worked fine by itself, but it got problematic when I added textures. I only get ~1-2fps running the caster with textures on, while >120fps otherwise. I'm pretty new to programming. so I would like some help with optimizations.

"list" is all of the distance values gotten from the raycast. "clist" is the mod of the x & y position where the ray touched the wall. look is the up/down angle the player is looking at.

I removed shading and shadows as it slowed it down even more

def disp(list , clist):
    rayspace = ceil(WIDTH / len(list))
    for i in range(len(list)):   
        top = round(HEIGHT / 2 + 12000/list[i] + (look))
        bottom = round(HEIGHT / 2 - 12000/list[i] + (look))
        if top > bottom:
            tallness = top - bottom
        else:
            tallness = bottom - top
        y = 0
        j = bottom
        while not(j == top):
            y += HEIGHT/tallness
            k = 0
            while not(k == rayspace):
                color = wallpaper.get_at((floor(clist[i]/rayspace + k * 2), floor(y)))
                color = (color[0] , color[1]  ,color[2] )    
                screen.set_at((round(i * rayspace) + k , j) , color)
                k+=1
            j+=1

I've replaced my for loops with while loops, removed all global variables, and used "from" on imports, and that helped a bit, but it still is extremely slow

if there is anything that needs clarification about the code I'll be happy to help



No comments:

Post a Comment