forked from glumpy/glumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotein.py
executable file
·98 lines (80 loc) · 2.64 KB
/
protein.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
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
from glumpy import app, gloo, gl, data
from glumpy.transforms import Position, Trackball
from glumpy.graphics.filter import Filter
vertex = """
uniform vec3 light_position;
attribute vec3 position;
attribute vec3 color;
attribute float radius;
varying float v_size;
varying vec3 v_color;
varying float v_radius;
varying vec4 v_eye_position;
varying vec3 v_light_direction;
void main (void)
{
v_color = color;
v_radius = radius;
v_eye_position = <transform.trackball_view> *
<transform.trackball_model> *
vec4(position,1.0);
v_light_direction = normalize(light_position);
gl_Position = <transform(position)>;
// stackoverflow.com/questions/8608844/...
// ... resizing-point-sprites-based-on-distance-from-the-camera
vec4 p = <transform.trackball_projection> *
vec4(radius, radius, v_eye_position.z, v_eye_position.w);
v_size = 512.0 * p.x / p.w;
gl_PointSize = v_size + 5.0;
}
"""
fragment = """
#include "antialias/outline.glsl"
varying float v_size;
varying vec3 v_color;
varying float v_radius;
varying vec4 v_eye_position;
varying vec3 v_light_direction;
void main()
{
vec2 P = gl_PointCoord.xy - vec2(0.5,0.5);
float point_size = v_size + 5.0;
float distance = length(P*point_size) - v_size/2;
vec2 texcoord = gl_PointCoord* 2.0 - vec2(1.0);
float x = texcoord.x;
float y = texcoord.y;
float d = 1.0 - x*x - y*y;
if (d <= 0.0) discard;
float z = sqrt(d);
vec4 pos = v_eye_position;
pos.z += v_radius*z;
vec3 pos2 = pos.xyz;
pos = <transform.trackball_projection> * pos;
gl_FragDepth = 0.5*(pos.z / pos.w)+0.5;
vec3 normal = vec3(x,y,z);
float diffuse = clamp(dot(normal, v_light_direction), 0.0, 1.0);
vec4 color = vec4((0.5 + 0.5*diffuse)*v_color, 1.0);
gl_FragColor = outline(distance, 1.0, 1.0, vec4(0,0,0,1), color);
// gl_FragColor = color;
}
"""
window = app.Window(width=800, height=800, color=(1,1,1,1))
protein = gloo.Program(vertex, fragment)
protein['light_position'] = 0., 0., 2.
protein["transform"] = Trackball(Position())
protein.bind(data.get("protein.npy").view(gloo.VertexBuffer))
protein['color'] *= .25
protein['color'] += .75
@window.event
def on_draw(dt):
window.clear()
protein.draw(gl.GL_POINTS)
@window.event
def on_init():
gl.glEnable(gl.GL_DEPTH_TEST)
window.attach(protein["transform"])
app.run()