DKL9 GitList
Repositories
DKL9 home
voxels
Code
Commits
Branches
Tags
Search
Tree:
466ea72
Branches
Tags
master
voxels
main.py
Make basic 3D "game" with two shapes
dkl9
commited
466ea72
at 2025-170 10:42:16
main.py
Blame
History
Raw
from OpenGL.GL import shaders from libs import * from player import Player from entity import Entity VS = """ #version 120 attribute vec3 position; uniform mat4 MVP; void main() { gl_Position = MVP * vec4(position, 1.0); } """ FS = """ #version 120 uniform vec4 matCol; void main() { gl_FragColor = matCol; } """ def weaken(self, button): if button == 1: self.col *= 0.9 def toggle_spin(self, button): if button == 3: self.spin = not self.spin def build_scene(): ot = Entity( [[-0.5, -0.5, 0.0], [0.5, -0.5, 0.0], [0.0, 0.5, 0.0]], [1.0, 0.5, 0.2, 1.0], [0.0, 0.0, 3.0] ) ot.spin = True ot.onclick = toggle_spin cr = Entity( [[-0.8, -0.4, 0.0], [-0.8, 0.4, 0.0], [0.8, -0.4, 0.0], [0.8, 0.4, 0.0]], [0.2, 0.5, 1.0, 1.0], [3.0, -0.5, 0.0] ) cr.rotate(np.radians(90), [0.0, 1.0, 0.0]) cr.onclick = weaken return [ot, cr] 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") loc_matcol = glGetUniformLocation(sp, "matCol") glClearColor(0.1, 0.1, 0.1, 1.0) scene = build_scene() clock = pygame.time.Clock() cam = Player() cam.set_focus(True) while True: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) for ent in scene: cam.draw(ent, loc_mvp, loc_matcol) for ev in pygame.event.get(): if ev.type == pygame.QUIT or ev.type == pygame.KEYUP and ev.unicode == "q": pygame.quit() return cam.handle_event(ev, scene) cam.update_pos() if scene[0].spin: scene[0].rotate(0.03 / np.linalg.norm(scene[0].pos - cam.pos) ** 2, [0.0, 1.0, 0.0]) pygame.display.flip() clock.tick(60) main()