-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathspiral.py
72 lines (63 loc) · 1.92 KB
/
spiral.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
# -----------------------------------------------------------------------------
# Python & OpenGL for Scientific Visualization
# www.labri.fr/perso/nrougier/python+opengl
# Copyright (c) 2017, Nicolas P. Rougier
# Distributed under the 2-Clause BSD License.
# -----------------------------------------------------------------------------
import numpy as np
from glumpy import app, gloo, gl
vertex = """
attribute vec2 position;
attribute float size;
varying vec2 center;
varying float radius;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
gl_PointSize = 2.0+ceil(size);
center = 512.0 + position*512.0;
radius = size/2.0;
} """
fragment = """
float coverage(float d, float antialias)
{
d = d + antialias;
float alpha = d/antialias;
if( d < 0.0 ) return 1.0;
return exp(-alpha*alpha);
}
float circle(vec2 p, vec2 center, float radius)
{
return length(p - center) - radius;
}
varying vec2 center;
varying float radius;
void main()
{
vec2 p = gl_FragCoord.xy;
float antialias = 1.0;
if (radius < 1.0) {
float d = circle(p, center, 1.0);
gl_FragColor = vec4(vec3(0.0), radius*coverage(d, 1.0));
} else {
float d = circle(p, center, radius);
gl_FragColor = vec4(vec3(0.0), coverage(d, 1.0));
}
} """
window = app.Window(1024, 1024, color=(1,1,1,1))
V = np.zeros(600, [("position", np.float32, 2),
("size", np.float32, 1)])
R = np.linspace(0.05, 0.95, len(V))
T = np.linspace(0, 10.125*2*np.pi, len(V))
S = np.linspace(0.0, 42.0, len(V))
V["position"][:,0] = R*np.cos(T)
V["position"][:,1] = R*np.sin(T)
V["size"] = S
points = gloo.Program(vertex, fragment)
V = V.view(gloo.VertexBuffer)
points['position'] = V['position']
points['size'] = V['size']
@window.event
def on_draw(dt):
window.clear()
points.draw(gl.GL_POINTS)
app.run()