本文整理汇总了Python中vispy.gloo.Program类的典型用法代码示例。如果您正苦于以下问题:Python Program类的具体用法?Python Program怎么用?Python Program使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Program类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Canvas
class Canvas(app.Canvas):
def __init__(self):
self.image = Program(img_vertex, img_fragment, 4)
self.image["position"] = (-1, -1), (-1, +1), (+1, -1), (+1, +1)
self.image["texcoord"] = (0, 0), (0, +1), (+1, 0), (+1, +1)
self.image["vmin"] = +0.1
self.image["vmax"] = +0.9
self.image["cmap"] = 0 # Colormap index to use
self.image["colormaps"] = colormaps
self.image["colormaps"].interpolation = "linear"
self.image["colormaps_shape"] = colormaps.shape[1], colormaps.shape[0]
self.image["image"] = I
self.image["image"].interpolation = "linear"
self.image["image_shape"] = I.shape[1], I.shape[0]
app.Canvas.__init__(self, show=True, size=(512, 512), keys="interactive")
def on_initialize(self, event):
set_clear_color("black")
def on_resize(self, event):
width, height = event.size
set_viewport(0, 0, *event.size)
def on_draw(self, event):
clear(color=True, depth=True)
self.image.draw("triangle_strip")
示例2: test_context_sharing
def test_context_sharing():
"""Test context sharing"""
with Canvas() as c1:
vert = "attribute vec4 pos;\nvoid main (void) {gl_Position = pos;}"
frag = "void main (void) {gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
program = Program(vert, frag)
program['pos'] = [(1, 2, 3, 1), (4, 5, 6, 1)]
program.draw('points')
def check():
# Do something to program and see if it worked
program['pos'] = [(1, 2, 3, 1), (4, 5, 6, 1)] # Do command
program.draw('points')
check_error()
# Check while c1 is active
check()
# Check while c2 is active (with different context)
with Canvas() as c2:
# pyglet always shares
if 'pyglet' not in c2.app.backend_name.lower():
assert_raises(Exception, check)
# Check while c2 is active (with *same* context)
with Canvas(shared=c1.context) as c2:
assert c1.context.shared is c2.context.shared # same object
check()
示例3: Canvas
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self)
# Create program
self._program = Program(VERT_SHADER, FRAG_SHADER)
# Set uniforms and samplers
self._program['a_position'] = VertexBuffer(positions)
self._program['a_texcoord'] = VertexBuffer(texcoords)
#
self._program['u_texture1'] = Texture2D(im1)
self._program['u_texture2'] = Texture2D(im2)
self._program['u_texture3'] = Texture2D(im3)
def on_initialize(self, event):
gl.glClearColor(1,1,1,1)
def on_resize(self, event):
width, height = event.size
gl.glViewport(0, 0, width, height)
def on_paint(self, event):
# Clear
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
# Draw shape with texture, nested context
self._program.draw(gl.GL_TRIANGLE_STRIP)
示例4: Canvas
class Canvas(app.Canvas):
def __init__(self):
self.image = Program(img_vertex, img_fragment, 4)
self.image['position'] = (-1, -1), (-1, +1), (+1, -1), (+1, +1)
self.image['texcoord'] = (0, 0), (0, +1), (+1, 0), (+1, +1)
self.image['vmin'] = +0.1
self.image['vmax'] = +0.9
self.image['cmap'] = 0 # Colormap index to use
self.image['colormaps'] = colormaps
self.image['colormaps'].interpolation = 'linear'
self.image['colormaps_shape'] = colormaps.shape[1], colormaps.shape[0]
self.image['image'] = I.astype('float32')
self.image['image'].interpolation = 'linear'
app.Canvas.__init__(self, show=True, size=(512, 512),
keys='interactive')
def on_initialize(self, event):
set_clear_color('black')
def on_resize(self, event):
width, height = event.size
set_viewport(0, 0, *event.size)
def on_draw(self, event):
clear(color=True, depth=True)
self.image.draw('triangle_strip')
示例5: on_initialize
def on_initialize(self, event):
self.rho = 0.0
# Build cube data
# --------------------------------------
self.checker = Program(cube_vertex, cube_fragment)
self.checker['texture'] = checkerboard()
self.checker['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.checker['texcoord'] = [(0, 0), (0, 1), (1, 0), (1, 1)]
# sheet, indices = make_sheet((960, 1080))
# sheet_buffer = VertexBuffer(sheet)
left_eye = Texture2D((960, 1080, 3), interpolation='linear')
self.left_eye_buffer = FrameBuffer(left_eye, RenderBuffer((960, 1080)))
# Build program
# --------------------------------------
self.view = np.eye(4, dtype=np.float32)
self.program = Program(vertex, fragment)
distortion_buffer = VertexBuffer(make_distortion())
self.program.bind(distortion_buffer)
self.program['rotation'] = self.view
self.program['texture'] = left_eye
# OpenGL and Timer initalization
# --------------------------------------
set_state(clear_color=(.3, .3, .35, 1), depth_test=True)
self.timer = app.Timer('auto', connect=self.on_timer, start=True)
self._set_projection(self.size)
示例6: test_context_sharing
def test_context_sharing():
"""Test context sharing"""
with Canvas() as c1:
vert = VertexShader("uniform vec4 pos;"
"void main (void) {gl_Position = pos;}")
frag = FragmentShader("uniform vec4 pos;"
"void main (void) {gl_FragColor = pos;}")
program = Program(vert, frag)
program['pos'] = [1, 2, 3, 4]
program.activate() # should print
def check():
program.activate()
check_error()
with Canvas() as c2:
# pyglet always shares
if 'pyglet' not in c2.app.backend_name.lower():
assert_raises(RuntimeError, check)
if c1.app.backend_name.lower() in ('glut',):
assert_raises(RuntimeError, Canvas, context=c1.context)
else:
with Canvas(context=c1.context) as c2:
assert c1.context is c2.context # Same context object
check()
示例7: __init__
def __init__ (self, channels=10,timepoints=10000,srate=1017.25):
super(TSV_TEST_GLOO, self).__init__()
self.n_channels = channels
self.n_timepoints = timepoints
self.srate = srate
self.magrin = 10
self.ticksize = 10
self.height = 0.0
self.width = 0.0
self._is_init = False
self.is_on_draw = False
self._init_data()
# Build program & data
# ----------------------------------------
self.data_pgr = Program(vertex, fragment, count=timepoints)
#self.data_pgr'color'] = [ (1,0,0,1), (0,1,0,1), (0,0,1,1), (1,1,0,1) ]
#self.data_pgr['position'] = [ (-1,-1), (-1,+1), (+1,-1), (+1,+1) ]
#self.program['scale'] = 1.0
self.xgrid_pgr = Program(grid_vertex, grid_fragment)
self.xgrid_pgr['position'] = self.init_xgrid()
self.xgrid_pgr['color'] = np.array([0.0,0.0,0.0,1.0],dtype=np.float32)
self.ygrid_pgr = Program(grid_vertex, grid_fragment)
self.ygrid_pgr['position'] = self.init_ygrid()
self.ygrid_pgr['color'] = np.array([0.50,0.50,0.50,1.0],dtype=np.float32)
示例8: Canvas
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, size=(512, 512), title='Rotating quad',
keys='interactive')
self.timer = app.Timer('auto', self.on_timer)
# Build program & data
self.program = Program(vertex, fragment, count=4)
self.program['color'] = [(1, 0, 0, 1), (0, 1, 0, 1),
(0, 0, 1, 1), (1, 1, 0, 1)]
self.program['position'] = [(-1, -1), (-1, +1),
(+1, -1), (+1, +1)]
self.program['theta'] = 0.0
gloo.set_viewport(0, 0, *self.physical_size)
self.clock = 0
self.timer.start()
self.show()
def on_draw(self, event):
gloo.set_clear_color('white')
gloo.clear(color=True)
self.program.draw('triangle_strip')
def on_resize(self, event):
gloo.set_viewport(0, 0, *event.physical_size)
def on_timer(self, event):
self.clock += 0.001 * 1000.0 / 60.
self.program['theta'] = self.clock
self.update()
示例9: __init__
def __init__(self):
app.Canvas.__init__(self, size=(512, 512),
keys='interactive')
self.image = Program(image_vertex, image_fragment, 4)
self.image['position'] = (-1, -1), (-1, +1), (+1, -1), (+1, +1)
self.image['texcoord'] = (0, 0), (0, +1), (+1, 0), (+1, +1)
self.image['vmin'] = +0.0
self.image['vmax'] = +1.0
self.image['cmap'] = 0 # Colormap index to use
self.image['colormaps'] = colormaps
self.image['n_colormaps'] = colormaps.shape[0]
self.image['image'] = I.astype('float32')
self.image['image'].interpolation = 'linear'
set_viewport(0, 0, *self.physical_size)
self.lines = Program(lines_vertex, lines_fragment)
self.lines["position"] = np.zeros((4+4+514+514, 2), np.float32)
color = np.zeros((4+4+514+514, 4), np.float32)
color[1:1+2, 3] = 0.25
color[5:5+2, 3] = 0.25
color[9:9+512, 3] = 0.5
color[523:523+512, 3] = 0.5
self.lines["color"] = color
set_state(clear_color='white', blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
self.show()
示例10: __init__
def __init__(self):
app.Canvas.__init__(self, title='Grayscott Reaction-Diffusion',
size=(512, 512), keys='interactive')
self.scale = 4
self.comp_size = (256, 256)
comp_w, comp_h = self.comp_size
dt = 1.0
dd = 1.5
species = {
# name : [r_u, r_v, f, k]
'Bacteria 1': [0.16, 0.08, 0.035, 0.065],
'Bacteria 2': [0.14, 0.06, 0.035, 0.065],
'Coral': [0.16, 0.08, 0.060, 0.062],
'Fingerprint': [0.19, 0.05, 0.060, 0.062],
'Spirals': [0.10, 0.10, 0.018, 0.050],
'Spirals Dense': [0.12, 0.08, 0.020, 0.050],
'Spirals Fast': [0.10, 0.16, 0.020, 0.050],
'Unstable': [0.16, 0.08, 0.020, 0.055],
'Worms 1': [0.16, 0.08, 0.050, 0.065],
'Worms 2': [0.16, 0.08, 0.054, 0.063],
'Zebrafish': [0.16, 0.08, 0.035, 0.060]
}
P = np.zeros((comp_h, comp_w, 4), dtype=np.float32)
P[:, :] = species['Unstable']
UV = np.zeros((comp_h, comp_w, 4), dtype=np.float32)
UV[:, :, 0] = 1.0
r = 32
UV[comp_h / 2 - r:comp_h / 2 + r,
comp_w / 2 - r:comp_w / 2 + r, 0] = 0.50
UV[comp_h / 2 - r:comp_h / 2 + r,
comp_w / 2 - r:comp_w / 2 + r, 1] = 0.25
UV += np.random.uniform(0.0, 0.01, (comp_h, comp_w, 4))
UV[:, :, 2] = UV[:, :, 0]
UV[:, :, 3] = UV[:, :, 1]
self.pingpong = 1
self.compute = Program(compute_vertex, compute_fragment, 4)
self.compute["params"] = P
self.compute["texture"] = UV
self.compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)]
self.compute['dt'] = dt
self.compute['dx'] = 1.0 / comp_w
self.compute['dy'] = 1.0 / comp_h
self.compute['dd'] = dd
self.compute['pingpong'] = self.pingpong
self.render = Program(render_vertex, render_fragment, 4)
self.render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)]
self.render["texture"] = self.compute["texture"]
self.render['pingpong'] = self.pingpong
self.fbo = FrameBuffer(self.compute["texture"],
RenderBuffer(self.comp_size))
set_state(depth_test=False, clear_color='black')
self._timer = app.Timer('auto', connect=self.update, start=True)
示例11: __init__
def __init__(self):
app.Canvas.__init__(self, title="Grayscott Reaction-Diffusion", size=(512, 512), keys="interactive")
self.scale = 4
self.comp_size = self.size
comp_w, comp_h = self.comp_size
dt = 1.0
dd = 1.5
species = {
# name : [r_u, r_v, f, k]
"Bacteria 1": [0.16, 0.08, 0.035, 0.065],
"Bacteria 2": [0.14, 0.06, 0.035, 0.065],
"Coral": [0.16, 0.08, 0.060, 0.062],
"Fingerprint": [0.19, 0.05, 0.060, 0.062],
"Spirals": [0.10, 0.10, 0.018, 0.050],
"Spirals Dense": [0.12, 0.08, 0.020, 0.050],
"Spirals Fast": [0.10, 0.16, 0.020, 0.050],
"Unstable": [0.16, 0.08, 0.020, 0.055],
"Worms 1": [0.16, 0.08, 0.050, 0.065],
"Worms 2": [0.16, 0.08, 0.054, 0.063],
"Zebrafish": [0.16, 0.08, 0.035, 0.060],
}
P = np.zeros((comp_h, comp_w, 4), dtype=np.float32)
P[:, :] = species["Unstable"]
UV = np.zeros((comp_h, comp_w, 4), dtype=np.float32)
UV[:, :, 0] = 1.0
r = 32
UV[comp_h / 2 - r : comp_h / 2 + r, comp_w / 2 - r : comp_w / 2 + r, 0] = 0.50
UV[comp_h / 2 - r : comp_h / 2 + r, comp_w / 2 - r : comp_w / 2 + r, 1] = 0.25
UV += np.random.uniform(0.0, 0.01, (comp_h, comp_w, 4))
UV[:, :, 2] = UV[:, :, 0]
UV[:, :, 3] = UV[:, :, 1]
self.pingpong = 1
self.compute = Program(compute_vertex, compute_fragment, 4)
self.compute["params"] = P
self.compute["texture"] = UV
self.compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)]
self.compute["dt"] = dt
self.compute["dx"] = 1.0 / comp_w
self.compute["dy"] = 1.0 / comp_h
self.compute["dd"] = dd
self.compute["pingpong"] = self.pingpong
self.render = Program(render_vertex, render_fragment, 4)
self.render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)]
self.render["texture"] = self.compute["texture"]
self.render["pingpong"] = self.pingpong
self.fbo = FrameBuffer(self.compute["texture"], RenderBuffer(self.comp_size))
set_state(depth_test=False, clear_color="black")
self._timer = app.Timer("auto", connect=self.update, start=True)
self.show()
示例12: on_initialize
def on_initialize(self, event):
self.scale = 4
self.comp_size = (256, 256)
comp_w, comp_h = self.comp_size
dt = 1.0
dd = 1.5
species = {
# name : [r_u, r_v, f, k]
'Bacteria 1': [0.16, 0.08, 0.035, 0.065],
'Bacteria 2': [0.14, 0.06, 0.035, 0.065],
'Coral': [0.16, 0.08, 0.060, 0.062],
'Fingerprint': [0.19, 0.05, 0.060, 0.062],
'Spirals': [0.10, 0.10, 0.018, 0.050],
'Spirals Dense': [0.12, 0.08, 0.020, 0.050],
'Spirals Fast': [0.10, 0.16, 0.020, 0.050],
'Unstable': [0.16, 0.08, 0.020, 0.055],
'Worms 1': [0.16, 0.08, 0.050, 0.065],
'Worms 2': [0.16, 0.08, 0.054, 0.063],
'Zebrafish': [0.16, 0.08, 0.035, 0.060]
}
P = np.zeros((comp_h, comp_w, 4), dtype=np.float32)
P[:, :] = species['Unstable']
UV = np.zeros((comp_h, comp_w, 4), dtype=np.float32)
UV[:, :, 0] = 1.0
r = 32
UV[comp_h / 2 - r:comp_h / 2 + r,
comp_w / 2 - r:comp_w / 2 + r, 0] = 0.50
UV[comp_h / 2 - r:comp_h / 2 + r,
comp_w / 2 - r:comp_w / 2 + r, 1] = 0.25
UV += np.random.uniform(0.0, 0.01, (comp_h, comp_w, 4))
UV[:, :, 2] = UV[:, :, 0]
UV[:, :, 3] = UV[:, :, 1]
self.pingpong = 1
self.compute = Program(compute_vertex, compute_fragment, 4)
self.compute["params"] = P
self.compute["texture"] = UV
self.compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)]
self.compute['dt'] = dt
self.compute['dx'] = 1.0 / comp_w
self.compute['dy'] = 1.0 / comp_h
self.compute['dd'] = dd
self.compute['pingpong'] = self.pingpong
self.render = Program(render_vertex, render_fragment, 4)
self.render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)]
self.render["texture"] = self.compute["texture"]
self.render['pingpong'] = self.pingpong
self.fbo = FrameBuffer(self.compute["texture"],
DepthBuffer(self.comp_size))
set_state(depth_test=False, clear_color='black')
示例13: Canvas
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, title='Rain [Move mouse]',
size=(512, 512), keys='interactive')
# Build data
# --------------------------------------
n = 500
self.data = np.zeros(n, [('a_position', np.float32, 2),
('a_fg_color', np.float32, 4),
('a_size', np.float32, 1)])
self.index = 0
self.program = Program(vertex, fragment)
self.vdata = VertexBuffer(self.data)
self.program.bind(self.vdata)
self.program['u_antialias'] = 1.00
self.program['u_linewidth'] = 1.00
self.program['u_model'] = np.eye(4, dtype=np.float32)
self.program['u_view'] = np.eye(4, dtype=np.float32)
self.activate_zoom()
gloo.set_clear_color('white')
gloo.set_state(blend=True,
blend_func=('src_alpha', 'one_minus_src_alpha'))
self.timer = app.Timer('auto', self.on_timer, start=True)
self.show()
def on_draw(self, event):
gloo.clear()
self.program.draw('points')
def on_resize(self, event):
self.activate_zoom()
def activate_zoom(self):
gloo.set_viewport(0, 0, *self.physical_size)
projection = ortho(0, self.size[0], 0,
self.size[1], -1, +1)
self.program['u_projection'] = projection
def on_timer(self, event):
self.data['a_fg_color'][..., 3] -= 0.01
self.data['a_size'] += 1.0
self.vdata.set_data(self.data)
self.update()
def on_mouse_move(self, event):
x, y = event.pos
h = self.size[1]
self.data['a_position'][self.index] = x, h - y
self.data['a_size'][self.index] = 5
self.data['a_fg_color'][self.index] = 0, 0, 0, 1
self.index = (self.index + 1) % 500
示例14: test_use_texture3D
def test_use_texture3D():
"""Test using a 3D texture"""
vals = [0, 200, 100, 0, 255, 0, 100]
d, h, w = len(vals), 3, 5
data = np.zeros((d, h, w), np.float32)
VERT_SHADER = """
attribute vec2 a_pos;
varying vec2 v_pos;
void main (void)
{
v_pos = a_pos;
gl_Position = vec4(a_pos, 0., 1.);
}
"""
FRAG_SHADER = """
uniform sampler3D u_texture;
varying vec2 v_pos;
uniform float i;
void main()
{
gl_FragColor = texture3D(u_texture,
vec3((v_pos.y+1.)/2., (v_pos.x+1.)/2., i));
gl_FragColor.a = 1.;
}
"""
# populate the depth "slices" with different gray colors in the bottom left
for ii, val in enumerate(vals):
data[ii, :2, :3] = val / 255.
with Canvas(size=(100, 100)) as c:
if not has_pyopengl():
t = Texture3D(data)
assert_raises(ImportError, t.glir.flush, c.context.shared.parser)
return
program = Program(VERT_SHADER, FRAG_SHADER)
program['a_pos'] = [[-1., -1.], [1., -1.], [-1., 1.], [1., 1.]]
tex = Texture3D(data, interpolation='nearest')
assert_equal(tex.width, w)
assert_equal(tex.height, h)
assert_equal(tex.depth, d)
program['u_texture'] = tex
for ii, val in enumerate(vals):
set_viewport(0, 0, w, h)
clear(color='black')
iii = (ii + 0.5) / float(d)
print(ii, iii)
program['i'] = iii
program.draw('triangle_strip')
out = _screenshot()[:, :, 0].astype(int)[::-1]
expected = np.zeros_like(out)
expected[:2, :3] = val
assert_allclose(out, expected, atol=1./255.)
示例15: __init__
def __init__(self):
app.Canvas.__init__(self, title="Conway game of life",
size=(512, 512), keys='interactive')
# Build programs
# --------------
self.comp_size = self.size
size = self.comp_size + (4,)
Z = np.zeros(size, dtype=np.float32)
Z[...] = np.random.randint(0, 2, size)
Z[:256, :256, :] = 0
gun = """
........................O...........
......................O.O...........
............OO......OO............OO
...........O...O....OO............OO
OO........O.....O...OO..............
OO........O...O.OO....O.O...........
..........O.....O.......O...........
...........O...O....................
............OO......................"""
x, y = 0, 0
for i in range(len(gun)):
if gun[i] == '\n':
y += 1
x = 0
elif gun[i] == 'O':
Z[y, x] = 1
x += 1
self.pingpong = 1
self.compute = Program(compute_vertex, compute_fragment, 4)
self.compute["texture"] = Z
self.compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)]
self.compute['dx'] = 1.0 / size[1]
self.compute['dy'] = 1.0 / size[0]
self.compute['pingpong'] = self.pingpong
self.render = Program(render_vertex, render_fragment, 4)
self.render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
self.render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)]
self.render["texture"] = self.compute["texture"]
self.render['pingpong'] = self.pingpong
self.fbo = FrameBuffer(self.compute["texture"],
RenderBuffer(self.comp_size))
set_state(depth_test=False, clear_color='black')
self._timer = app.Timer('auto', connect=self.update, start=True)
self.show()