DKL9 GitList
Repositories
DKL9 home
voxels
Code
Commits
Branches
Tags
Search
Tree:
be2e5cd
Branches
Tags
master
voxels
main.py
Gravity, sun-disc, voxel picking, hand indicator
dkl9
commited
be2e5cd
at 2025-177 00:03:49
main.py
Blame
History
Raw
from OpenGL.GL import shaders from libs import * from entity import Entity from mapgen import MapGen from mesh import SquareMesh from player import Player from region import WorldRegion import os VS = f""" #version 120 {"\n".join([f"attribute {a};" for a in VERTEX_ATTRS])} {"\n".join([f"varying {a}F;" for a in VERTEX_ATTRS])} {"\n".join([f"uniform {v[1]} {k};" for (k, v) in UNIFORMS.items() if v[0] == "vert"])} void main() {{ gl_Position = MVP * vec4(position, 1.0); {"\n".join([f" {a.split()[1]}F = {a.split()[1]};" for a in VERTEX_ATTRS])} }} """ FS = f""" #version 120 {"\n".join([f"varying {a}F;" for a in VERTEX_ATTRS])} {"\n".join([f"uniform {v[1]} {k};" for (k, v) in UNIFORMS.items() if v[0] == "frag"])} void main() {{ float ambient = reflsF.x; vec3 lightDir = normalize(lightPos.xyz - positionF); float lambert = dot(normalF, lightDir); float diffuse = reflsF.y * lightPos.w * lambert / pow(distance(lightPos.xyz, positionF), 2); float specular = reflsF.z * lightPos.w * pow(max(dot(normalize(camPos - positionF), reflect(-lightDir, normalF)), 0.0), reflsF.w); gl_FragColor = vec4((ambient + diffuse + specular) * colF.xyz, colF.w); }} """ def debug_mode(mg, cam): pass def build_scene(): mg = MapGen() mg.fetch((0, 0, 0)) return mg def in_view(wr, cam): rwrc = wr.pos + wr.r / 2 - cam.pos close = np.linalg.norm(rwrc) < VIEW_DIST ld = cam.looking() cv = rwrc + 8 * ld frust = np.dot(cv, ld) / np.linalg.norm(cv) > 0.5 return close and frust def main(): pygame.init() pygame.display.set_mode((640, 480), pygame.DOUBLEBUF | pygame.OPENGL | (pygame.FULLSCREEN | pygame.SCALED if os.environ.get("FULLSCREEN") else pygame.RESIZABLE), vsync=1) pygame.event.set_allowed([pygame.KEYDOWN, pygame.KEYUP, pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP, pygame.MOUSEMOTION, pygame.QUIT, pygame.WINDOWRESIZED, pygame.WINDOWSIZECHANGED]) pygame.display.set_caption("Voxels") sp = shaders.compileProgram( shaders.compileShader(VS, GL_VERTEX_SHADER), shaders.compileShader(FS, GL_FRAGMENT_SHADER) ) for (k, v) in UNIFORMS.items(): v[2] = glGetUniformLocation(sp, k) glUseProgram(sp) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glClearColor(0.1, 0.1, 0.1, 1.0) glUniform4fv(UNIFORMS["lightPos"][2], 1, vec([*LIGHT_POS, 100])) mg = build_scene() clock = pygame.time.Clock() cam = Player(speed=6/FPS) cam.pos = [4, 8, 4] cam.set_focus(True) while True: scene = list(filter(lambda wr: in_view(wr, cam), mg.regions.values())) for wr in scene: if wr.dirty: wr.remesh() for ev in pygame.event.get(): if ev.type == pygame.QUIT or ev.type == pygame.KEYUP and ev.unicode == "q": pygame.quit() return elif ev.type == pygame.KEYUP and ev.unicode == "d": debug_mode(mg, cam) cam.handle_event(ev, mg, scene) cam.update_motion(pygame.key.get_pressed(), mg) cam.update_pos() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) for ent in scene: cam.draw(ent) perf = clock.get_fps() / FPS cam.draw_hud(scene, perf) pygame.display.flip() clock.tick(FPS) main()