本文整理汇总了Python中vispy.gloo.program.Program类的典型用法代码示例。如果您正苦于以下问题:Python Program类的具体用法?Python Program怎么用?Python Program使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Program类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_detach
def test_detach(self):
vert = VertexShader("A")
frag = FragmentShader("B")
program = Program(vert, frag)
program.detach(frag)
assert len(program.shaders) == 1
assert program.shaders[0].code == "A"
示例2: test_attach
def test_attach(self):
vert = VertexShader("A")
frag = FragmentShader("B")
program = Program(vert)
program.attach(frag)
assert len(program.shaders) == 2
assert program.shaders[0].code == "A"
assert program.shaders[1].code == "B"
示例3: test_setting_shaders
def test_setting_shaders(self):
program = Program("A", "B")
assert program.shaders[0] == "A"
assert program.shaders[1] == "B"
program.set_shaders('C', 'D')
assert program.shaders[0] == "C"
assert program.shaders[1] == "D"
示例4: test_failed_build
def test_failed_build(self):
vert = VertexShader("A")
frag = FragmentShader("B")
program = Program(vert=vert)
program._need_create = False # fool program that it already exists
self.assertRaises(ValueError, program.activate)
program = Program(frag=frag)
program._need_create = False # fool program that it already exists
self.assertRaises(ValueError, program.activate)
示例5: test_setting_shaders
def test_setting_shaders(self):
from vispy.gloo.program import VertexShader, FragmentShader
program = Program("A", "B")
assert isinstance(program.shaders[0], VertexShader)
assert program.shaders[0].code == 'A'
assert isinstance(program.shaders[1], FragmentShader)
assert program.shaders[1].code == 'B'
program.set_shaders('C', 'D')
assert program.shaders[0].code == "C"
assert program.shaders[1].code == "D"
示例6: test_attributes
def test_attributes(self):
program = Program("attribute float A; attribute vec4 B;", "foo")
assert ('attribute', 'float', 'A') in program.variables
assert ('attribute', 'vec4', 'B') in program.variables
assert len(program.variables) == 2
from vispy.gloo import VertexBuffer
vbo = VertexBuffer()
# Set existing uniforms
program['A'] = vbo
assert program['A'] == vbo
assert 'A' in program._user_variables
assert program._user_variables['A'] is vbo
# Set data - update existing vbp
program['A'] = np.zeros((10,), np.float32)
assert program._user_variables['A'] is vbo
# Set data - create new vbo
program['B'] = np.zeros((10, 4), np.float32)
assert isinstance(program._user_variables['B'], VertexBuffer)
# Set non-exisint uniforms
program['C'] = vbo
assert program['C'] == vbo
assert 'C' not in program._user_variables
assert 'C' in program._pending_variables
# C should be taken up when code comes along that mentions it
program.set_shaders("attribute float A; attribute vec2 C;", "foo")
assert program['C'] == vbo
assert 'C' in program._user_variables
assert 'C' not in program._pending_variables
# Set wrong values
self.assertRaises(ValueError, program.__setitem__, 'A', 'asddas')
# Set wrong values beforehand
program['D'] = ""
self.assertRaises(ValueError, program.set_shaders,
'attribute vec3 D;', '')
# Set to one value per vertex
program.set_shaders("attribute float A; attribute vec2 C;", "foo")
program['A'] = 1.0
assert program['A'] == 1.0
program['C'] = 1.0, 2.0
assert all(program['C'] == np.array((1.0, 2.0), np.float32))
#
self.assertRaises(ValueError, program.__setitem__, 'A', (1.0, 2.0))
self.assertRaises(ValueError, program.__setitem__, 'C', 1.0)
self.assertRaises(ValueError, program.bind, 'notavertexbuffer')
示例7: test_failed_build
def test_failed_build(self):
vert = VertexShader("A")
frag = FragmentShader("B")
program = Program(vert = vert)
with self.assertRaises(RuntimeError):
program.activate()
program = Program(frag = frag)
with self.assertRaises(RuntimeError):
program.activate()
示例8: test_draw
def test_draw(self):
# Init
program = Program("attribute float A;", "uniform float foo")
program['A'] = np.zeros((10,), np.float32)
# We need to disable flushing to run this test
flush = program._context.glir.flush
program._context.glir.flush = lambda x=None: None
try:
# Draw arrays
program.draw('triangles')
glir_cmd = program._context.glir.clear()[-1]
assert glir_cmd[0] == 'DRAW'
assert len(glir_cmd[-1]) == 2
# Draw elements
indices = gloo.IndexBuffer(np.zeros(10, dtype=np.uint8))
program.draw('triangles', indices)
glir_cmd = program._context.glir.clear()[-1]
assert glir_cmd[0] == 'DRAW'
assert len(glir_cmd[-1]) == 3
# Invalid mode
self.assertRaises(ValueError, program.draw, 'nogeometricshape')
# Invalid index
self.assertRaises(TypeError, program.draw, 'triangles', 'notindex')
# No atributes
program = Program("attribute float A;", "uniform float foo")
self.assertRaises(RuntimeError, program.draw, 'triangles')
# Atributes with different sizes
program = Program("attribute float A; attribute float B;", "foo")
program['A'] = np.zeros((10,), np.float32)
program['B'] = np.zeros((11,), np.float32)
self.assertRaises(RuntimeError, program.draw, 'triangles')
finally:
program._context.glir.flush = flush
示例9: test_draw
def test_draw(self):
# Init
program = Program("attribute float A;", "uniform float foo")
program['A'] = np.zeros((10,), np.float32)
dummy_canvas = DummyCanvas()
glir = dummy_canvas.context.glir
set_current_canvas(dummy_canvas)
try:
# Draw arrays
program.draw('triangles')
glir_cmd = glir.clear()[-1]
assert glir_cmd[0] == 'DRAW'
assert len(glir_cmd[-1]) == 2
# Draw elements
indices = gloo.IndexBuffer(np.zeros(10, dtype=np.uint8))
program.draw('triangles', indices)
glir_cmd = glir.clear()[-1]
assert glir_cmd[0] == 'DRAW'
assert len(glir_cmd[-1]) == 3
# Invalid mode
self.assertRaises(ValueError, program.draw, 'nogeometricshape')
# Invalid index
self.assertRaises(TypeError, program.draw, 'triangles', 'notindex')
# No atributes
program = Program("attribute float A;", "uniform float foo")
self.assertRaises(RuntimeError, program.draw, 'triangles')
# Atributes with different sizes
program = Program("attribute float A; attribute float B;", "foo")
program['A'] = np.zeros((10,), np.float32)
program['B'] = np.zeros((11,), np.float32)
self.assertRaises(RuntimeError, program.draw, 'triangles')
finally:
forget_canvas(dummy_canvas)
示例10: test_application
def test_application():
"""Test application running"""
app = use_app()
print(app) # __repr__ without app
app.create()
wrong = 'glut' if app.backend_name.lower() != 'glut' else 'pyglet'
assert_raises(RuntimeError, use_app, wrong)
app.process_events()
print(app) # test __repr__
assert_raises(ValueError, Canvas, keys='foo')
assert_raises(TypeError, Canvas, keys=dict(escape=1))
assert_raises(ValueError, Canvas, keys=dict(escape='foo')) # not an attr
pos = [0, 0] if app.backend_module.capability['position'] else None
size = (100, 100)
# Use "with" statement so failures don't leave open window
# (and test context manager behavior)
title = 'default'
with Canvas(title=title, size=size, app=app, show=True,
position=pos) as canvas:
assert_true(canvas.create_native() is None) # should be done already
assert_is(canvas.app, app)
assert_true(canvas.native)
assert_equal('swap_buffers', canvas.events.draw.callback_refs[-1])
canvas.measure_fps(0.001)
sleep(0.002)
canvas.update()
app.process_events()
assert_true(canvas.fps > 0)
# Other methods
print(canvas) # __repr__
assert_equal(canvas.title, title)
canvas.title = 'you'
with use_log_level('warning', record=True, print_msg=False) as l:
if app.backend_module.capability['position']:
# todo: disable more tests based on capability
canvas.position = pos
canvas.size = size
if 'ipynb_vnc' in canvas.app.backend_name.lower():
assert_true(len(l) >= 1)
else:
assert_true(len(l) == 0)
canvas.connect(on_mouse_move)
assert_raises(ValueError, canvas.connect, _on_mouse_move)
if sys.platform != 'darwin': # XXX knownfail, prob. needs warmup
canvas.show(False)
canvas.show()
app.process_events()
assert_raises(ValueError, canvas.connect, on_nonexist)
# deprecation of "paint"
with use_log_level('info', record=True, print_msg=False) as log:
olderr = sys.stderr
try:
with open(os.devnull, 'w') as fid:
sys.stderr = fid
@canvas.events.paint.connect
def fake(event):
pass
finally:
sys.stderr = olderr
assert_equal(len(log), 1)
assert_in('deprecated', log[0])
# screenshots
gl.glViewport(0, 0, *size)
ss = _screenshot()
assert_array_equal(ss.shape, size + (4,))
assert_equal(len(canvas._backend._vispy_get_geometry()), 4)
if (app.backend_name.lower() != 'glut' and # XXX knownfail for Almar
sys.platform != 'win32'): # XXX knownfail for windows
assert_array_equal(canvas.size, size)
assert_equal(len(canvas.position), 2) # XXX knawnfail, doesn't "take"
# GLOO: should have an OpenGL context already, so these should work
vert = VertexShader("void main (void) {gl_Position = pos;}")
frag = FragmentShader("void main (void) {gl_FragColor = pos;}")
program = Program(vert, frag)
assert_raises(RuntimeError, program.activate)
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)
#uniform = program.uniforms[0]
program['pos'] = [1, 2, 3, 4]
program.activate() # should print
#uniform.upload(program)
program.detach(vert)
program.detach(frag)
assert_raises(RuntimeError, program.detach, vert)
assert_raises(RuntimeError, program.detach, frag)
vert = VertexShader("attribute vec4 pos;"
"void main (void) {gl_Position = pos;}")
frag = FragmentShader("void main (void) {}")
#.........这里部分代码省略.........
示例11: _test_application
def _test_application(backend):
"""Test application running"""
app = Application()
assert_raises(ValueError, app.use, "foo")
app.use(backend)
wrong = "Glut" if app.backend_name != "Glut" else "Pyglet"
assert_raises(RuntimeError, app.use, wrong)
app.process_events()
if backend is not None:
# "in" b/c "qt" in "PySide (qt)"
assert_in(backend, app.backend_name)
print(app) # test __repr__
# Canvas
pos = [0, 0]
size = (100, 100)
# Use "with" statement so failures don't leave open window
# (and test context manager behavior)
title = "default" if backend is None else backend
with Canvas(title=title, size=size, app=app, show=True, position=pos) as canvas:
assert_is(canvas.app, app)
assert_true(canvas.native)
assert_equal("swap_buffers", canvas.events.paint.callback_refs[-1])
print(canvas) # __repr__
assert_array_equal(canvas.size, size)
assert_equal(canvas.title, title)
canvas.title = "you"
canvas.position = pos
canvas.size = size
canvas.connect(on_mouse_move)
assert_raises(ValueError, canvas.connect, _on_mouse_move)
if sys.platform != "darwin": # XXX knownfail, prob. needs warmup
canvas.show(False)
canvas.show()
app.process_events()
assert_raises(ValueError, canvas.connect, on_nonexist)
# screenshots
gl.glViewport(0, 0, *size)
ss = _screenshot()
assert_array_equal(ss.shape, size + (3,))
assert_equal(len(canvas._backend._vispy_get_geometry()), 4)
assert_array_equal(canvas.size, size)
assert_equal(len(canvas.position), 2) # XXX knawnfail, doesn't "take"
# GLOO: should have an OpenGL context already, so these should work
vert = VertexShader("void main (void) {gl_Position = pos;}")
frag = FragmentShader("void main (void) {gl_FragColor = pos;}")
program = Program(vert, frag)
assert_raises(RuntimeError, program.activate)
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)
# uniform = program.uniforms[0]
program["pos"] = [1, 2, 3, 4]
program.activate() # should print
# uniform.upload(program)
program.detach(vert)
program.detach(frag)
assert_raises(RuntimeError, program.detach, vert)
assert_raises(RuntimeError, program.detach, frag)
vert = VertexShader("attribute vec4 pos;" "void main (void) {gl_Position = pos;}")
frag = FragmentShader("void main (void) {}")
program = Program(vert, frag)
# attribute = program.attributes[0]
program["pos"] = [1, 2, 3, 4]
program.activate()
# attribute.upload(program)
# cannot get element count
# assert_raises(RuntimeError, program.draw, 'POINTS')
# use a real program
vert = (
"uniform mat4 u_model;"
"attribute vec2 a_position; attribute vec4 a_color;"
"varying vec4 v_color;"
"void main (void) {v_color = a_color;"
"gl_Position = u_model * vec4(a_position, 0.0, 1.0);"
"v_color = a_color;}"
)
frag = "void main() {gl_FragColor = vec4(0, 0, 0, 1);}"
n, p = 250, 50
T = np.random.uniform(0, 2 * np.pi, n)
position = np.zeros((n, 2), dtype=np.float32)
position[:, 0] = np.cos(T)
position[:, 1] = np.sin(T)
color = np.ones((n, 4), dtype=np.float32) * (1, 1, 1, 1)
data = np.zeros(n * p, [("a_position", np.float32, 2), ("a_color", np.float32, 4)])
data["a_position"] = np.repeat(position, p, axis=0)
data["a_color"] = np.repeat(color, p, axis=0)
program = Program(vert, frag)
program.bind(VertexBuffer(data))
program["u_model"] = np.eye(4, dtype=np.float32)
# different codepath if no call to activate()
program.draw(gl.GL_POINTS)
subset = IndexBuffer(np.arange(10, dtype=np.uint32))
program.draw(gl.GL_POINTS, subset)
#.........这里部分代码省略.........
示例12: test_delete_no_context
def test_delete_no_context(self):
program = Program()
program.delete()
示例13: _test_application
def _test_application(backend):
"""Test application running"""
app = Application()
assert_raises(ValueError, app.use, 'foo')
app.use(backend)
wrong = 'Glut' if app.backend_name != 'Glut' else 'Pyglet'
assert_raises(RuntimeError, app.use, wrong)
app.process_events()
if backend is not None:
# "in" b/c "qt" in "PySide (qt)"
assert_true(backend in app.backend_name)
print(app) # test __repr__
# Canvas
pos = [0, 0, 1, 1]
# Use "with" statement so failures don't leave open window
# (and test context manager behavior)
with Canvas(title='me', app=app, show=True, position=pos) as canvas:
assert_true(canvas.app is app)
assert_true(canvas.native)
print(canvas.size >= (1, 1))
canvas.resize(90, 90)
canvas.move(1, 1)
assert_equal(canvas.title, 'me')
canvas.title = 'you'
canvas.position = (0, 0)
canvas.size = (100, 100)
canvas.connect(on_mouse_move)
assert_raises(ValueError, canvas.connect, _on_mouse_move)
canvas.show()
assert_raises(ValueError, canvas.connect, on_nonexist)
# screenshots
ss = _screenshot()
assert_array_equal(ss.shape[2], 3) # XXX other dimensions not correct?
# XXX it would be good to do real checks, but sometimes the
# repositionings don't "take" (i.e., lead to random errors)
assert_equal(len(canvas._backend._vispy_get_geometry()), 4)
assert_equal(len(canvas.size), 2)
assert_equal(len(canvas.position), 2)
# GLOO: should have an OpenGL context already, so these should work
vert = VertexShader("void main (void) {gl_Position = pos;}")
frag = FragmentShader("void main (void) {gl_FragColor = pos;}")
program = Program(vert, frag)
assert_raises(ShaderError, program.activate)
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)
uniform = program.uniforms[0]
uniform.set_data([1, 2, 3, 4])
program.activate() # should print
uniform.upload(program)
program.detach(vert, frag)
assert_raises(ShaderError, program.detach, vert)
assert_raises(ShaderError, program.detach, frag)
vert = VertexShader("attribute vec4 pos;"
"void main (void) {gl_Position = pos;}")
frag = FragmentShader("void main (void) {}")
program = Program(vert, frag)
attribute = program.attributes[0]
attribute.set_data([1, 2, 3, 4])
program.activate()
attribute.upload(program)
# cannot get element count
assert_raises(ProgramError, program.draw, 'POINTS')
# use a real program
vert = ("uniform mat4 u_model;"
"attribute vec2 a_position; attribute vec4 a_color;"
"varying vec4 v_color;"
"void main (void) {v_color = a_color;"
"gl_Position = u_model * vec4(a_position, 0.0, 1.0);"
"v_color = a_color;}")
frag = "void main() {gl_FragColor = vec4(0, 0, 0, 1);}"
n, p = 250, 50
T = np.random.uniform(0, 2 * np.pi, n)
position = np.zeros((n, 2), dtype=np.float32)
position[:, 0] = np.cos(T)
position[:, 1] = np.sin(T)
color = np.ones((n, 4), dtype=np.float32) * (1, 1, 1, 1)
data = np.zeros(n * p, [('a_position', np.float32, 2),
('a_color', np.float32, 4)])
data['a_position'] = np.repeat(position, p, axis=0)
data['a_color'] = np.repeat(color, p, axis=0)
program = Program(vert, frag)
program.set_vars(VertexBuffer(data))
program['u_model'] = np.eye(4, dtype=np.float32)
program.draw('POINTS') # different codepath if no call to activate()
subset = ElementBuffer(np.arange(10, dtype=np.uint32))
program.draw('POINTS', subset=subset)
# bad programs
frag_bad = ("varying vec4 v_colors") # no semicolon
program = Program(vert, frag_bad)
#.........这里部分代码省略.........
示例14: test_uniform
def test_uniform(self):
# Text array unoforms
program = Program("uniform float A[10];", "foo")
assert ('uniform', 'float', 'A[0]') in program.variables
assert len(program.variables) == 10
# Init program
program = Program("uniform float A;",
"uniform float A; uniform vec4 B;")
assert ('uniform', 'float', 'A') in program.variables
assert ('uniform', 'vec4', 'B') in program.variables
assert len(program.variables) == 2
# Set existing uniforms
program['A'] = 3.0
assert isinstance(program['A'], np.ndarray)
assert program['A'] == 3.0
assert 'A' in program._user_variables
#
program['B'] = 1.0, 2.0, 3.0, 4.0
assert isinstance(program['B'], np.ndarray)
assert all(program['B'] == np.array((1.0, 2.0, 3.0, 4.0), np.float32))
assert 'B' in program._user_variables
# Set non-exisint uniforms
program['C'] = 1.0, 2.0
assert program['C'] == (1.0, 2.0)
assert 'C' not in program._user_variables
assert 'C' in program._pending_variables
# Set samplers
program.set_shaders("uniform sampler2D T2; uniform sampler3D T3;", "f")
program['T2'] = np.zeros((10, 10), np.float32)
program['T3'] = np.zeros((10, 10, 10), np.float32)
assert isinstance(program['T2'], gloo.Texture2D)
assert isinstance(program['T3'], gloo.Texture3D)
# Set samplers with textures
tex = gloo.Texture2D((10, 10))
program['T2'] = tex
assert program['T2'] is tex
program['T2'] = np.zeros((10, 10), np.float32) # Update texture
assert program['T2'] is tex
# C should be taken up when code comes along that mentions it
program.set_shaders("uniform float A; uniform vec2 C;",
"uniform float A; uniform vec4 B;")
assert isinstance(program['C'], np.ndarray)
assert all(program['C'] == np.array((1.0, 2.0), np.float32))
assert 'C' in program._user_variables
assert 'C' not in program._pending_variables
# Set wrong values
self.assertRaises(ValueError, program.__setitem__, 'A', (1.0, 2.0))
self.assertRaises(ValueError, program.__setitem__, 'B', (1.0, 2.0))
self.assertRaises(ValueError, program.__setitem__, 'C', 1.0)
# Set wrong values beforehand
program['D'] = 1.0, 2.0
self.assertRaises(ValueError, program.set_shaders,
'', 'uniform vec3 D;')