from OpenGL.GL import shaders from libs import * from entity import Entity from player import Player from region import WorldRegion VS = """ #version 120 attribute vec3 position; attribute vec4 colIn; varying vec4 colOut; uniform mat4 MVP; void main() { gl_Position = MVP * vec4(position, 1.0); colOut = colIn; } """ FS = """ #version 120 varying vec4 colOut; void main() { gl_FragColor = colOut; } """ FPS = 240 def debug_mode(): pass def click_mesh(self, button, ind): a, p = self.faces[ind] match button: case 1: self.del_voxel(ind) case 2: print(f"face {a} of {p}") case 3: self.add_voxel(p + np.sign(a) * np.roll([0, 0, 1], abs(a))) if button % 2 == 1: self.remesh() self.update_attr(0) self.update_attr(1) def build_scene(): box = WorldRegion() print(f"map generated with {box.gen_perlin(np.random.rand(2, 2, 2, 3))} voxels") box.remesh() box.onclick = click_mesh return [box] def main(): pygame.init() pygame.display.set_mode((640, 480), pygame.DOUBLEBUF | pygame.OPENGL | pygame.RESIZABLE) pygame.display.set_caption("OpenGL") sp = shaders.compileProgram( shaders.compileShader(VS, GL_VERTEX_SHADER), shaders.compileShader(FS, GL_FRAGMENT_SHADER) ) glUseProgram(sp) glEnable(GL_DEPTH_TEST) loc_mvp = glGetUniformLocation(sp, "MVP") glClearColor(0.1, 0.1, 0.1, 1.0) scene = build_scene() clock = pygame.time.Clock() cam = Player() cam.pos = [8, 16, 8] cam.set_focus(True) while True: 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() cam.handle_event(ev, scene) cam.update_pos() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) perf = clock.get_fps() / FPS cam.hud["fps"].col = GREEN if 0.9 < perf < 1.1 else YELLOW if perf > 0.5 else RED cam.hud["fps"].update_attr(1) for ent in itertools.chain(scene, cam.hud.values()): cam.draw(ent, loc_mvp) pygame.display.flip() clock.tick(240) main()