-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenglapp.py
179 lines (134 loc) · 5.61 KB
/
openglapp.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""This simulates the events and functionality of `cmu_112_graphics`.
It tries to stick as closely as possible to the design of the original,
and their usage is mostly the same.
"""
import glfw
import inspect
import time
from dataclasses import make_dataclass
from OpenGL.GL import * # type:ignore
from canvas import Canvas
class App:
_callersGlobals = dict()
width: int
height: int
mouseX: int = 0
mouseY: int = 0
canvas: Canvas
def __init__(self, width=300, height=300):
if not glfw.init():
print("Init failed")
# This trick borrowed from `cmu_112_graphics`
self._callersGlobals = inspect.stack()[1][0].f_globals
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
self.window = glfw.create_window(width, height, "Hello, world", None, None)
glfw.make_context_current(self.window)
self.width, self.height = width, height
self.timerDelay = 200
self.lastTimer = time.time()
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
glFrontFace(GL_CW)
glEnable(GL_BLEND)
self.canvas = Canvas(self.width, self.height)
self.run()
# From cmu_112_graphics
def _callFn(self, fn, *args):
if (fn in self._callersGlobals): self._callersGlobals[fn](*args)
def run(self):
self._callFn("appStarted", self)
def sizeChanged(window, width, height):
self.width = width
self.height = height
print(f'new size: {width} {height}')
self.canvas.resize(width, height)
self._callFn('sizeChanged', self)
def charEvent(*args): self.dispatchCharEvent(*args)
def keyEvent(*args): self.dispatchKeyEvent(*args)
def mouseMoved(*args): self.dispatchMouseMoved(*args)
def mouseChanged(*args): self.dispatchMouseChanged(*args)
glfw.set_framebuffer_size_callback(self.window, sizeChanged)
glfw.set_key_callback(self.window, keyEvent)
glfw.set_char_callback(self.window, charEvent)
glfw.set_cursor_pos_callback(self.window, mouseMoved)
glfw.set_mouse_button_callback(self.window, mouseChanged)
i = 0
times = [0.0] * 20
while not glfw.window_should_close(self.window):
times.pop(0)
glClearColor(0.2, 0.3, 0.3, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #type:ignore
self._callFn('redrawAll', self, self.window, self.canvas)
self.canvas.redraw()
if time.time() - self.lastTimer > (self.timerDelay / 1000.0):
self.lastTimer = time.time()
self._callFn('timerFired', self)
glfw.swap_buffers(self.window)
glfw.poll_events()
times.append(time.time())
i += 1
if i % 50 == 0:
timeDiff = (times[-1] - times[0]) / (len(times) - 1)
print(timeDiff)
self._callFn('appStopped', self)
glfw.terminate()
def dispatchKeyEvent(self, window, key: int, scancode: int, action: int, mods: int):
if ((ord('A') <= key and key <= ord('Z'))
or (ord('0') <= key and key <= ord('9'))):
keyName = chr(key)
elif key == glfw.KEY_MINUS:
if mods & glfw.MOD_SHIFT:
keyName = '_'
else:
keyName = '-'
elif key == glfw.KEY_LEFT_SHIFT:
keyName = 'Shift'
elif key == glfw.KEY_SLASH:
keyName = '/'
elif key == glfw.KEY_ESCAPE:
keyName = 'Escape'
elif key == glfw.KEY_ENTER:
keyName = 'Enter'
elif key == glfw.KEY_SPACE:
keyName = ' '
elif key == glfw.KEY_PERIOD:
keyName = '.'
elif key == glfw.KEY_BACKSPACE:
keyName = 'Backspace'
else:
keyName = None
KeyEvent = make_dataclass('KeyEvent', ['window', 'key'])
event = KeyEvent(window, keyName)
if action != glfw.REPEAT:
if keyName is None:
print(f"Unknown key {key}, scancode {scancode}")
elif action == glfw.PRESS:
self._callFn('keyPressed', self, event)
else:
self._callFn('keyReleased', self, event)
def dispatchCharEvent(self, window, codepoint: int):
CharEvent = make_dataclass('KeyEvent', ['window', 'char'])
event = CharEvent(window, chr(codepoint))
self._callFn('charPressed', self, event)
def dispatchMouseMoved(self, window, mouseX, mouseY):
self.mouseX = mouseX
self.mouseY = mouseY
MoveEvent = make_dataclass('MoveEvent', ['x', 'y'])
self._callFn('mouseMoved', self, MoveEvent(mouseX, mouseY))
def dispatchMouseChanged(self, window, button: int, action: int, mods: int):
ChangeEvent = make_dataclass('ChangeEvent', ['x', 'y'])
event = ChangeEvent(self.mouseX, self.mouseY)
if button == glfw.MOUSE_BUTTON_LEFT:
if action == glfw.PRESS:
self._callFn('mousePressed', self, event)
else:
self._callFn('mouseReleased', self, event)
elif button == glfw.MOUSE_BUTTON_RIGHT:
if action == glfw.PRESS:
self._callFn('rightMousePressed', self, event)
else:
self._callFn('rightMouseReleased', self, event)
runApp = App