forked from brianhouse/housepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.py
executable file
·245 lines (207 loc) · 9.96 KB
/
animation.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
import pyglet, time
from . import dispatcher
try:
import numpy as np
except ImportError:
pass
"""
Check http://cwru-hackers.googlecode.com/svn-history/r233/splatterboard/trunk/draw.py
"""
MAX_OBJECTS = 20
show_fps = True
class Context(dispatcher.Dispatcher):
def __init__(self, width=800, height=600, background=(1.0, 1.0, 1.0, 1.0), fullscreen=False, title="animation", chrome=True, screen=0, smooth=True, _3d=False):
self._width = width
self._height = height
self._background = background
self._fullscreen = fullscreen
self._title = title
self._screen = screen
self._chrome = chrome
self.window = None
self._3d = _3d
self.last_frame = 0
self.smooth = smooth
self.objects = []
self.textures = []
dispatcher.Dispatcher.__init__(self)
@property
def width(self):
return self._width
@property
def height(self):
return self._height
@property
def fps(self):
return pyglet.clock.get_fps()
def start(self, draw_func, update_func=None):
config = pyglet.gl.Config(sample_buffers=1, samples=4, depth_size=24, double_buffer=True)
style = pyglet.window.Window.WINDOW_STYLE_DEFAULT if self._chrome else pyglet.window.Window.WINDOW_STYLE_BORDERLESS
screens = pyglet.window.get_platform().get_default_display().get_screens()
screen_index = min(self._screen, len(screens) - 1)
screen = screens[screen_index]
if not (self._fullscreen and screen_index !=0):
self.window = pyglet.window.Window(config=config, width=self.width, height=self.height, resizable=False, fullscreen=self._fullscreen, caption=self._title, style=style, screen=screen)
else: # hack because pyglet fullscreen doesnt work on secondary screen (bug)
self._width = screen.width
self._height = screen.height
self.window = pyglet.window.Window(config=config, width=self.width, height=self.height, resizable=False, fullscreen=False, caption=self._title, style=style, screen=screen)
self.window.set_location(screen.x, screen.y)
self.window.on_mouse_press = self.on_mouse_press
self.window.on_mouse_release = self.on_mouse_release
self.window.on_mouse_drag = self.on_mouse_drag
self.window.on_key_press = self.on_key_press
self.window.on_key_release = self.on_key_release
self.draw_func = draw_func
self.update_func = update_func if update_func is not None else lambda x: x
pyglet.gl.glClearColor(*self._background)
if self._3d:
pyglet.gl.glViewport(0, 0, self.width, self.height)
pyglet.gl.glMatrixMode(pyglet.gl.GL_PROJECTION)
pyglet.gl.glLoadIdentity()
# pyglet.gl.gluPerspective(60.0, self.width / self.height, 0.1, 1000.0) # on-axis projection
# usr Frustum to do off-axis projection (if the coords arent symmetrical). Equiv to gluPerspective above is left, right, bottom, top = -0.082479, 0.082479, -0.057735, 0.057735
left, right, bottom, top = -0.082479, 0.082479, -0.057735, 0.057735
# left, right, bottom, top = -0.001, 0.001, -0.001, 0.001
pyglet.gl.glFrustum(left, right, bottom, top, 0.1, 1000.0)
pyglet.gl.glMatrixMode(pyglet.gl.GL_MODELVIEW)
pyglet.gl.glEnable(pyglet.gl.GL_DEPTH_TEST)
# for 3d shapes / shading
# pyglet.gl.glEnable(pyglet.gl.GL_CULL_FACE)
# pyglet.gl.glPolygonMode(pyglet.gl.GL_FRONT_AND_BACK, pyglet.gl.GL_LINE) # Uncomment this line for a wireframe view
# pyglet.gl.glEnable(pyglet.gl.GL_LIGHTING)
# pyglet.gl.glEnable(pyglet.gl.GL_LIGHT0)
# pyglet.gl.glEnable(pyglet.gl.GL_LIGHT1)
if self.smooth:
pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
pyglet.gl.glEnable(pyglet.gl.GL_LINE_SMOOTH)
pyglet.gl.glHint(pyglet.gl.GL_LINE_SMOOTH_HINT, pyglet.gl.GL_NICEST)
self.window.on_draw = self.draw_loop
if self._3d:
self.window.on_resize = lambda w,h: True # stop the default 2d projection
pyglet.clock.schedule(self.update_func)
pyglet.app.run()
def translate(self, *matrix):
pyglet.gl.glTranslatef(*matrix)
def rotate(self, *matrix):
pyglet.gl.glRotatef(*matrix)
def draw_loop(self):
self.window.clear()
pyglet.gl.glLoadIdentity()
pyglet.gl.glColor4f(1., 1., 1., 1.)
pyglet.gl.glTexParameteri(pyglet.gl.GL_TEXTURE_2D, pyglet.gl.GL_TEXTURE_MAG_FILTER, pyglet.gl.GL_NEAREST)
for t in self.textures:
t.blit(t.x, t.y) # draw
self.draw_func()
for o in self.objects:
o.draw()
fps = pyglet.clock.get_fps()
if fps < 30 and show_fps:
print("%f fps" % fps)
def pixels(self, pixels):
""" This expects a 2D numpy array """
pixels = np.flipud(np.rot90(pixels))
w = pixels.shape[0]
h = pixels.shape[1]
pyglet.gl.glDrawPixels(w, h, pyglet.gl.GL_RGBA, pyglet.gl.GL_UNSIGNED_BYTE, pixels.transpose().tobytes())
def line(self, x1, y1, x2, y2, color=(0., 0., 0., 1.), thickness=1.0):
pyglet.gl.glColor4f(*color)
pyglet.gl.glLineWidth(thickness)
if not self._3d:
pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
('v2f', (x1 * self.width, y1 * self.height, x2 * self.width, y2 * self.height))
)
else:
pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
('v2f', (x1, y1, x2, y2))
)
def line3D(self, x1, y1, z1, x2, y2, z2, color=(0., 0., 0., 1.), thickness=1.0):
self.depth = self.height
pyglet.gl.glColor4f(*color)
pyglet.gl.glLineWidth(thickness)
pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
('v3f', (x1, y1, z1, x2, y2, z2)) # dont multiply by canvas -- but by what?
)
def lines(self, points, color=(0., 0., 0., 1.), thickness=1.0):
pyglet.gl.glColor4f(*color)
pyglet.gl.glLineWidth(thickness)
if not self._3d:
points = [(item * self.width) if (i % 2 == 0) else (item * self.height) for sublist in points for (i, item) in enumerate(sublist)] # flatten
else:
points = [item for sublist in points for (i, item) in enumerate(sublist)]
pyglet.graphics.draw(len(points) // 2, pyglet.gl.GL_LINE_STRIP, ('v2f', points))
def lines3D(self, points, color=(0., 0., 0., 1.), thickness=1.0):
pyglet.gl.glColor4f(*color)
pyglet.gl.glLineWidth(thickness)
points = [item for sublist in points for (i, item) in enumerate(sublist)] # flatten
pyglet.graphics.draw(len(points) // 3, pyglet.gl.GL_LINE_STRIP, ('v3f', points))
def plot(self, signal, color=(0., 0., 0., 1.), thickness=1.0):
points = [(float(s) / self.width, sample) for (s, sample) in enumerate(signal)]
self.lines(points, color=color, thickness=thickness)
def rect(self, x, y, width, height, color=(0., 0., 0., 1.), thickness=1.0):
pyglet.gl.glColor4f(*color)
pyglet.gl.glLineWidth(thickness)
if not self._3d:
x *= self.width
y *= self.height
width *= self.width
height *= self.height
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS,
('v2f', (x, y, x, y + height, x + width, y + height, x + width, y)),
# ('c3b', (color, color, color, color))
)
def arc():
pass
def curve():
pass
def label(self, x, y, text="", font="Helvetica", size=36, width=400, color=(0., 0., 0., 1.), center=False):
# why is the antialiasing so awful
color = [int(c * 255) for c in color] # why?
l = pyglet.text.Label(text, x=x * self.width, y=y * self.height, width=width, multiline=True, align=("center" if center else "left"))
l.font_name = font
l.font_size = size
l.color = color
if center:
l.anchor_x = 'center'
self.objects.append(l)
l.draw()
return l
def load_image(self, filename, x, y, width, height):
image = pyglet.resource.image(filename)
texture = image.get_texture()
texture.x = x
texture.y = y
texture.width = width
texture.height = height
self.textures.append(texture)
def on_mouse_press(self, x, y, button, modifiers):
if not self._3d:
x /= self.width
y /= self.height
self.fire('mouse_press', (x, y, button, modifiers))
def on_mouse_release(self, x, y, button, modifiers):
if not self._3d:
x /= self.width
y /= self.height
self.fire('mouse_release', (x, y, button, modifiers))
def on_key_press(self, key, modifiers):
self.fire('key_press', (chr(key), modifiers))
def on_key_release(self, key, modifiers):
self.fire('key_release', (chr(key), modifiers))
def on_mouse_drag(self, x, y, dx, dy, button, modifiers):
if not self._3d:
x /= self.width
y /= self.height
dx /= self.width
dy /= self.height
self.fire('mouse_drag', (x, y, dx, dy, button, modifiers))
def rgb_to_html(rgb_tuple):
return '#%02x%02x%02x' % rgb_tuple[:3]
if __name__ == "__main__":
from random import random
ctx = Context(1200, 600, background=(0.9, 0.9, 0.9, 1.), fullscreen=False)
def draw():
ctx.line(random(), random(), random(), random(), thickness=2.0)#, color=(1., 1., 1., 1.))
ctx.start(draw)