本文整理汇总了Python中vispy.gloo.Program.draw方法的典型用法代码示例。如果您正苦于以下问题:Python Program.draw方法的具体用法?Python Program.draw怎么用?Python Program.draw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类vispy.gloo.Program
的用法示例。
在下文中一共展示了Program.draw方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, size=(512, 512), title='Rotating cube',
keys='interactive')
self.timer = app.Timer('auto', self.on_timer)
def on_initialize(self, event):
# Build cube data
V, I, O = create_cube()
vertices = VertexBuffer(V)
self.faces = IndexBuffer(I)
self.outline = IndexBuffer(O)
# Build program
# --------------------------------------
self.program = Program(vertex, fragment)
self.program.bind(vertices)
# Build view, model, projection & normal
# --------------------------------------
view = translate((0, 0, -5))
model = np.eye(4, dtype=np.float32)
self.program['u_model'] = model
self.program['u_view'] = view
self.phi, self.theta = 0, 0
# OpenGL initalization
# --------------------------------------
gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True,
polygon_offset=(1, 1), line_width=0.75,
blend_func=('src_alpha', 'one_minus_src_alpha'))
self.timer.start()
def on_draw(self, event):
gloo.clear(color=True, depth=True)
# Filled cube
gloo.set_state(blend=False, depth_test=True, polygon_offset_fill=True)
self.program['u_color'] = 1, 1, 1, 1
self.program.draw('triangles', self.faces)
# Outlined cube
gloo.set_state(blend=True, depth_mask=False, polygon_offset_fill=False)
self.program['u_color'] = 0, 0, 0, 1
self.program.draw('lines', self.outline)
gloo.set_state(depth_mask=True)
def on_resize(self, event):
gloo.set_viewport(0, 0, *event.size)
projection = perspective(45.0, event.size[0] / float(event.size[1]),
2.0, 10.0)
self.program['u_projection'] = projection
def on_timer(self, event):
self.theta += .02
self.phi += .02
model = rotate(self.phi, (0, 1, 0)) * rotate(self.theta, (0, 0, 1))
self.program['u_model'] = model
self.update()
示例2: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
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)
示例3: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
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")
示例4: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
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()
示例5: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
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')
示例6: test_context_sharing
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
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()
示例7: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
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
示例8: test_use_texture3D
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
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.)
示例9: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, size=(512, 512), title='Textured cube',
keys='interactive')
self.timer = app.Timer('auto', self.on_timer)
# Build cube data
V, I, _ = create_cube()
vertices = VertexBuffer(V)
self.indices = IndexBuffer(I)
# Build program
self.program = Program(vertex, fragment)
self.program.bind(vertices)
# Build view, model, projection & normal
view = translate((0, 0, -5))
model = np.eye(4, dtype=np.float32)
self.program['model'] = model
self.program['view'] = view
self.program['texture'] = checkerboard()
self.activate_zoom()
self.phi, self.theta = 0, 0
# OpenGL initalization
gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True)
self.timer.start()
self.show()
def on_draw(self, event):
gloo.clear(color=True, depth=True)
self.program.draw('triangles', self.indices)
def on_resize(self, event):
self.activate_zoom()
def activate_zoom(self):
gloo.set_viewport(0, 0, *self.physical_size)
projection = perspective(45.0, self.size[0] / float(self.size[1]),
2.0, 10.0)
self.program['projection'] = projection
def on_timer(self, event):
self.theta += .5
self.phi += .5
self.program['model'] = np.dot(rotate(self.theta, (0, 0, 1)),
rotate(self.phi, (0, 1, 0)))
self.update()
示例10: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, size=(512, 512), title='Textured cube',
close_keys='escape')
self.timer = app.Timer(1./60., self.on_timer)
def on_initialize(self, event):
# Build cube data
V, I, _ = cube()
vertices = VertexBuffer(V)
self.indices = IndexBuffer(I)
# Build program
self.program = Program(vertex, fragment)
self.program.bind(vertices)
# Build view, model, projection & normal
view = np.eye(4, dtype=np.float32)
model = np.eye(4, dtype=np.float32)
translate(view, 0, 0, -5)
self.program['model'] = model
self.program['view'] = view
self.program['texture'] = checkerboard()
self.phi, self.theta = 0, 0
# OpenGL initalization
gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True)
self.timer.start()
def on_draw(self, event):
gloo.clear(color=True, depth=True)
self.program.draw('triangles', self.indices)
def on_resize(self, event):
gloo.set_viewport(0, 0, *event.size)
projection = perspective(45.0, event.size[0] / float(event.size[1]),
2.0, 10.0)
self.program['projection'] = projection
def on_timer(self, event):
self.theta += .5
self.phi += .5
model = np.eye(4, dtype=np.float32)
rotate(model, self.theta, 0, 0, 1)
rotate(model, self.phi, 0, 1, 0)
self.program['model'] = model
self.update()
示例11: Example
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
class Example(Drawable):
'''Draw a cube'''
cube_vertex = """
#version 120
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
attribute vec3 position;
attribute vec2 texcoord;
varying vec2 v_texcoord;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0);
v_texcoord = texcoord;
}
"""
cube_fragment = """
#version 120
uniform sampler2D texture;
varying vec2 v_texcoord;
void main()
{
float r = texture2D(texture, v_texcoord).r;
gl_FragColor = vec4(0, r, r, 1);
}
"""
def __init__(self):
vertices, indices, _ = create_cube()
vertices = VertexBuffer(vertices)
self.indices = IndexBuffer(indices)
self.program = Program(self.cube_vertex, self.cube_fragment)
self.model = np.eye(4)
self.view = np.eye(4)
self.program.bind(vertices)
self.program['texture'] = utils.checkerboard()
self.program['texture'].interpolation = 'linear'
self.program['model'] = self.model
self.program['view'] = self.view
def draw(self):
self.program.draw('triangles', self.indices)
示例12: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, size=(512, 512), title='Colored quad',
keys='interactive')
# 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)]
def on_draw(self, event):
gloo.clear(color='white')
self.program.draw('triangle_strip')
def on_resize(self, event):
gloo.set_viewport(0, 0, *event.size)
示例13: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, size=(512, 512), title="Colored quad", keys="interactive")
# 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)]
gloo.set_viewport(0, 0, *self.physical_size)
self.show()
def on_draw(self, event):
gloo.clear(color="white")
self.program.draw("triangle_strip")
def on_resize(self, event):
gloo.set_viewport(0, 0, *event.physical_size)
示例14: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, size=(512, 512), title='Textured quad',
close_keys='escape')
def on_initialize(self, event):
# Build program & data
self.program = Program(vertex, fragment, count=4)
self.program['position'] = [(-1, -1), (-1, +1),
(+1, -1), (+1, +1)]
self.program['texcoord'] = [(0, 0), (1, 0), (0, 1), (1, 1)]
self.program['texture'] = checkerboard()
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.size)
示例15: Canvas
# 需要导入模块: from vispy.gloo import Program [as 别名]
# 或者: from vispy.gloo.Program import draw [as 别名]
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self)
self.position = 50, 50
self.size = 512, 512
self.program = Program(VS, FS)
self.tex = Texture2D(data1)
self.program['a_position'] = VertexBuffer(positions)
self.program['a_texcoord'] = VertexBuffer(texcoords)
self.program['u_tex'] = self.tex
self._timer = app.Timer(.01)
self._timer.connect(self.on_timer)
self._timer.start()
self.measure_fps(.05, callback=self.fps_callback)
def fps_callback(self, e):
print "{0:.1f} FPS, {1:.1f} MB/s\r".format(e, e*RES*RES*4*3/(1024.**2)),
def on_resize(self, event):
width, height = event.size
gl.glViewport(0, 0, width, height)
def on_paint(self, event):
gl.glClearColor(0.2, 0.4, 0.6, 1.0)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
self.program.draw(gl.GL_TRIANGLE_STRIP)
def on_timer(self, event):
if event.iteration % 2 == 0:
data = data1
else:
data = data2
self.tex.set_subdata((0,0), data)
self.update()