forked from rougier/loops-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise-7.py
58 lines (46 loc) · 1.3 KB
/
exercise-7.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
import numpy as np
from vispy import app
from vispy import gloo
from vispy.gloo import gl
app.use('glfw')
vertex = """
attribute float x;
attribute float y;
attribute float i;
varying float v_i;
void main (void)
{
v_i = i;
gl_Position = vec4(x, 1.0 - 1.0/10.0- i/5.0 +y, 0.0, 1.0);
}
"""
fragment = """
varying float v_i;
void main()
{
if (fract(v_i) > 0.0)
discard;
gl_FragColor = vec4(0.0,0.0,0.0,1.0);
}
"""
class Window(app.Canvas):
def __init__(self, n=50):
app.Canvas.__init__(self)
self.program = gloo.Program(vertex, fragment)
gl.glClearColor(1,1,1,1)
self.data = np.zeros((10,n), [('x', np.float32, 1),
('y', np.float32, 1),
('i', np.float32, 1)])
self.data['x'] = np.linspace(-1.0, +1.0, n)
self.data['y'] = np.random.uniform(-0.1, +0.1, (10,n))
self.data['i'] = np.repeat(np.arange(10),n).reshape(10,n)
self.vdata = gloo.VertexBuffer(self.data)
self.program.bind(self.vdata)
def on_resize(self, event):
gl.glViewport(0, 0, event.size[0], event.size[1])
def on_draw(self, event):
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
self.program.draw(gl.GL_LINE_STRIP)
window = Window(n=256)
window.show()
app.run()