-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.py
106 lines (68 loc) · 2.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import pygame as pg
import moderngl as mgl
import sys
from model import *
from camera import Camera
from light import Light
from mesh import Mesh
from scene import Scene
from scene_renderer import SceneRenderer
class GraphicsEngine:
def __init__(self, win_size=(1600, 900)):
# init pygame modules
pg.init()
# window size
self.WIN_SIZE = win_size
# set opengl attr
pg.display.gl_set_attribute(pg.GL_CONTEXT_MAJOR_VERSION, 3)
pg.display.gl_set_attribute(pg.GL_CONTEXT_MINOR_VERSION, 3)
pg.display.gl_set_attribute(pg.GL_CONTEXT_PROFILE_MASK, pg.GL_CONTEXT_PROFILE_CORE)
# create opengl context
pg.display.set_mode(self.WIN_SIZE, flags=pg.OPENGL | pg.DOUBLEBUF)
# mouse settings
pg.event.set_grab(True)
pg.mouse.set_visible(False)
# detect and use existing opengl context
self.ctx = mgl.create_context()
# self.ctx.front_face = 'cw'
self.ctx.enable(flags=mgl.DEPTH_TEST | mgl.CULL_FACE)
# create an object to help track time
self.clock = pg.time.Clock()
self.time = 0
self.delta_time = 0
# light
self.light = Light()
# camera
self.camera = Camera(self)
# mesh
self.mesh = Mesh(self)
# scene
self.scene = Scene(self)
# renderer
self.scene_renderer = SceneRenderer(self)
def check_events(self):
for event in pg.event.get():
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
self.mesh.destroy()
self.scene_renderer.destroy()
pg.quit()
sys.exit()
def render(self):
# clear framebuffer
self.ctx.clear(color=(0.08, 0.16, 0.18))
# render scene
self.scene_renderer.render()
# swap buffers
pg.display.flip()
def get_time(self):
self.time = pg.time.get_ticks() * 0.001
def run(self):
while True:
self.get_time()
self.check_events()
self.camera.update()
self.render()
self.delta_time = self.clock.tick(60)
if __name__ == '__main__':
app = GraphicsEngine()
app.run()