forked from glumpy/glumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippet-cubes.py
executable file
·154 lines (134 loc) · 3.95 KB
/
snippet-cubes.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
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import numpy as np
from glumpy.geometry import primitives
from glumpy import app, gl, glm, gloo, data
from glumpy.graphics.collections import BaseCollection
vertex = """
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
attribute float index;
attribute vec3 position;
attribute vec2 texcoord;
varying float v_index;
varying vec2 v_texcoord;
void main()
{
v_texcoord = texcoord;
v_index = index;
vec4 pos = projection * view * model * vec4(position,1.0);
gl_Position = <grid>;
}
"""
fragment = """
uniform sampler2D texture;
varying vec2 v_texcoord;
varying float v_index;
void main()
{
<clip>;
float r = texture2D(texture, v_texcoord).r;
gl_FragColor = vec4(vec3(r),1.0);
}
"""
Grid = gloo.Snippet("""
uniform float rows, cols;
vec4 cell(vec4 position, float index)
{
float col = mod(index,cols) + 0.5;
float row = floor(index/cols) + 0.5;
float x = -1.0 + col * (2.0/cols);
float y = -1.0 + row * (2.0/rows);
float width = 0.95 / (1.0*cols);
float height = 0.95 / (1.0*rows);
vec4 P = position / position.w;
P = vec4(x + width*P.x, y + height*P.y, P.z, P.w);
return P*position.w;
}
""")
Clip = gloo.Snippet("""
uniform vec2 iResolution;
uniform float rows, cols;
void clip(float index)
{
vec2 P = gl_FragCoord.xy;
// mod doesn't play well with 0
float i = index+.00001;
float col = mod(i,cols);
float row = floor(i/cols);
float width = iResolution.x / cols;
float height = iResolution.y / rows;
float x = col * width;
float y = row * height;
float gap = 1.5;
if( P.x < (x+gap)) discard;
if( P.x > (x+width-gap)) discard;
if( P.y < (y+gap)) discard;
if( P.y > (y+height-gap)) discard;
}
""")
rows,cols = 3,3
window = app.Window(width=1024, height=1024, color=(0.30, 0.30, 0.35, 1.00))
# Build collection
dtype = [("position", np.float32, 3),
("normal", np.float32, 3),
("texcoord", np.float32, 2),
("color", np.float32, 4),
("index", np.float32, 1)]
cubes = BaseCollection(vtype=dtype, itype=np.uint32)
V,I = primitives.cube()
C = np.zeros(len(V),dtype=dtype)
for key in V.dtype.names: C[key] = V[key]
for i in range(rows*cols):
C["index"] = i
cubes.append(vertices=C, indices=I)
cubes._update()
V = cubes._vertices_buffer
I = cubes._indices_buffer
@window.event
def on_draw(dt):
global phi, theta
window.clear()
program.draw(gl.GL_TRIANGLES, I)
theta += 0.5
phi += 0.5
model = np.eye(4, dtype=np.float32)
glm.rotate(model, theta, 0, 0, 1)
glm.rotate(model, phi, 0, 1, 0)
program['model'] = model
@window.event
def on_resize(width, height):
program['projection'] = glm.perspective(fovy, width / float(height), 1.0, 100.0)
program['clip']['iResolution'] = width, height
@window.event
def on_mouse_scroll(x, y, dx, dy):
global fovy
fovy = np.minimum(np.maximum(fovy*(1+dy/100), 10.0), 179.0)
program['projection'] = glm.perspective(fovy,
window.width/float(window.height),
1.0, 100.0)
@window.event
def on_init():
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glDisable(gl.GL_BLEND)
program = gloo.Program(vertex, fragment)
program.bind(V)
view = np.eye(4, dtype=np.float32)
model = np.eye(4, dtype=np.float32)
projection = np.eye(4, dtype=np.float32)
glm.translate(view, 0, 0, -3)
program['texture'] = data.get("checkerboard")
program['model'] = model
program['view'] = view
program['grid'] = Grid("pos", "index")
program['grid']["rows"] = rows
program['grid']["cols"] = cols
program['clip'] = Clip("v_index")
program['clip']["rows"] = rows
program['clip']["cols"] = cols
fovy = 30
phi, theta = 30, 20
app.run()