-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathcircle-sdf-distances.py
45 lines (39 loc) · 1.27 KB
/
circle-sdf-distances.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
# -----------------------------------------------------------------------------
# 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.
# -----------------------------------------------------------------------------
from glumpy import app, gloo, gl
vertex = """
attribute vec2 position;
varying vec2 v_position;
void main(){
v_position = position;
gl_Position = vec4(position, 0.0, 1.0);
} """
fragment = """
float distance(vec2 P, vec2 center, float radius)
{
return length(P-center) - radius;
}
varying vec2 v_position;
void main()
{
const float epsilon = 0.005;
float d = distance(v_position, vec2(0.0), 0.5);
if (d < -epsilon)
gl_FragColor = vec4(1.0-abs(d), 0.0, 0.0, 1.0);
else if (d > epsilon)
gl_FragColor = vec4(0.0, 0.0, 1.0-abs(d), 1.0);
else
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
} """
window = app.Window(512, 512)
quad = gloo.Program(vertex, fragment, count=4)
quad['position'] = (-1,+1), (+1,+1), (-1,-1), (+1,-1)
@window.event
def on_draw(dt):
window.clear()
quad.draw(gl.GL_TRIANGLE_STRIP)
app.run()