-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
86 lines (73 loc) · 2.66 KB
/
app.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
77
78
79
80
81
82
83
84
85
from OpenGL.GL import *
from OpenGL.GLUT import glutSwapBuffers, glutPostRedisplay
from OpenGL.GLU import gluPerspective
import sys
import scene
from env import Environment
class App(object):
def __init__(self):
self.drawDelegate = None # expect has onDraw method
self.idleDelegate = None # expect has onIdle method
self.keyDelegate = None # expect callable or has onKey method
self.mouseDelegate = None # expect callable or has onMouse method
self.resizeDelegate = None # expect callable or has onResize method
self.scene = scene.DummyScene()
def onDraw(self):
if self.drawDelegate:
self.drawDelegate.onDraw()
return
self._defaultOnDraw()
def onIdle(self):
if self.idleDelegate:
self.idleDelegate.onIdle()
return
self.scene.step(Environment.dt)
glutPostRedisplay()
def onKey(self, *args):
if not self.keyDelegate:
print 'key pressed:', args
key = args[0]
if key=='q':
sys.exit(0)
return
if callable(self.keyDelegate):
self.keyDelegate(*args)
else:
self.keyDelegate.onKey(*args)
def onMouse(self, *args):
if not self.mouseDelegate:
return
if callable(self.mouseDelegate):
self.mouseDelegate(*args)
else:
self.mouseDelegate.onMouse(*args)
def onResize(self, *args):
if not self.resizeDelegate:
self._defaultOnResize(*args)
return
if callable(self.resizeDelegate):
self.resizeDelegate(*args)
else:
self.resizeDelegate.onResize(*args)
def _defaultOnResize(self, width, height):
if height == 0:
height = 1
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(60.0, float(width)/float(height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
def _defaultOnDraw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
# We have smooth color mode on, this will blend across the vertices.
# Draw a triangle rotated on the Y axis.
glBegin(GL_POLYGON) # Start drawing a polygon
glColor3f(1.0, 0.0, 0.0) # Red
glVertex3f(0.0, 1.0, 0.0) # Top
glColor3f(0.0, 1.0, 0.0) # Green
glVertex3f(1.0, -1.0, 0.0) # Bottom Right
glColor3f(0.0, 0.0, 1.0) # Blue
glVertex3f(-1.0, -1.0, 0.0) # Bottom Left
glEnd()
glutSwapBuffers()