Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
void main() {
gl_FragColor = v_color;
// Discard the fragments between the signals (emulate glMultiDrawArrays).
if ((fract(v_index.x) > 0.) || (fract(v_index.y) > 0.))
discard;
// Clipping test.
vec2 test = abs((v_position.xy-v_ab.zw)/v_ab.xy);
if ((test.x > 1) || (test.y > 1))
discard;
}
"""
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, title='Use your wheel to zoom!',
keys='interactive')
self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)
self.program['a_position'] = y.reshape(-1, 1)
self.program['a_color'] = color
self.program['a_index'] = index
self.program['u_scale'] = (1., 1.)
self.program['u_size'] = (nrows, ncols)
self.program['u_n'] = n
gloo.set_viewport(0, 0, *self.physical_size)
self._timer = app.Timer('auto', connect=self.on_timer, start=True)
gloo.set_state(clear_color='black', blend=True,
# Author: Tomas Hodan (hodantom@cmp.felk.cvut.cz)
# Center for Machine Perception, Czech Technical University in Prague
import numpy as np
from vispy import app, gloo
import OpenGL.GL as gl
# WARNING: doesn't work with Qt4 (update() does not call on_draw()??)
app.use_app('PyGlet') # Set backend
# Color vertex shader
#-------------------------------------------------------------------------------
_color_vertex_code = """
uniform mat4 u_mv;
uniform mat4 u_mvp;
uniform vec3 u_light_eye_pos;
attribute vec3 a_position;
attribute vec4 a_color;
varying vec4 v_color;
varying vec3 v_eye_pos;
varying vec3 v_L;
void main() {
self.line1 = Line(None, pos, (3, 9, 0))
self.line2 = DashedLine(None, pos, color)
self.line2.transform = STTransform(scale=(0.5, 0.5),
translate=(0.4, 0.4))
def on_draw(self, ev):
gloo.clear((0, 0, 0, 1), True)
gloo.set_viewport(0, 0, *self.size)
self.line1.draw()
self.line2.draw()
c = Canvas()
c.show()
timer = app.Timer()
timer.start(0.016)
th = 0.0
@timer.connect
def on_timer(event):
global th
th += 0.01
pos = (np.cos(th) * 0.2 + 0.4, np.sin(th) * 0.2 + 0.4)
c.line2.transform.translate = pos
c.update()
app.run()
x0 * (1./scale_x - 1./scale_x_new),
pan_y +
y0 * (1./scale_y - 1./scale_y_new))
self.update()
def on_mouse_wheel(self, event):
dx = np.sign(event.delta[1])*.05
scale_x, scale_y = self.program['u_scale']
scale_x_new, scale_y_new = (scale_x * math.exp(2.5*dx),
scale_y * math.exp(2.5*dx))
self.program['u_scale'] = (scale_x_new, scale_y_new)
self.update()
if __name__ == '__main__':
c = Canvas()
app.run()
set_clear_color('black')
self.show()
def on_resize(self, event):
width, height = event.physical_size
set_viewport(0, 0, *event.physical_size)
def on_draw(self, event):
clear(color=True, depth=True)
self.image.draw('triangle_strip')
if __name__ == '__main__':
canvas = Canvas()
app.run()
1. Not showing the canvas (show=False).
2. Rendering to an FBO.
3. Manually triggering a rendering pass with self.update().
4. Retrieving the scene with _screenshot().
5. Closing the app after the first rendering pass (if that's the intended
scenario).
"""
from vispy import gloo
from vispy import app
from vispy.util.ptime import time
from vispy.gloo.util import _screenshot
# WARNING: doesn't work with Qt4 (update() does not call on_draw()??)
app.use_app('glfw')
vertex = """
attribute vec2 position;
void main()
{
gl_Position = vec4(position, 0, 1.0);
}
"""
fragment = """
uniform vec2 resolution;
uniform vec2 center;
uniform float scale;
uniform int iter;
if __name__ == '__main__':
from vispy import app
# vertex positions of data to draw
N = 200
pos = np.zeros((N, 3), dtype=np.float32)
pos[:, 0] = np.linspace(-0.9, 0.9, N)
pos[:, 1] = np.random.normal(size=N, scale=0.2).astype(np.float32)
# color array
color = np.ones((N, 3), dtype=np.float32)
color[:, 0] = np.linspace(0, 1, N)
color[:, 1] = color[::-1, 0]
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, keys='interactive')
self.line1 = Line(pos, (3, 9, 0))
self.line2 = DashedLine(pos, color)
self.line2.transform = STTransform(scale=(0.5, 0.5),
translate=(0.4, 0.4))
def on_draw(self, ev):
gloo.clear((0, 0, 0, 1), True)
gloo.set_viewport(0, 0, *self.size)
self.line1.draw()
self.line2.draw()
c = Canvas()
c.show()
def __init__(self):
app.Canvas.__init__(self, keys='interactive', size=((512), (512)))
self.program = gloo.Program(VERT_SHADER, FRAG_SHADER % 'Nearest')
self.texture = gloo.Texture2D(I, interpolation='nearest')
# using packed data as discussed in pr #1069
self.kernel = gloo.Texture2D(kernel, interpolation='nearest')
self.program['u_texture'] = self.texture
self.names = names
self.filter = 16
self.title = 'Spatial Filtering using %s Filter' % \
self.names[self.filter]
self.program.bind(gloo.VertexBuffer(data))
self.context.set_clear_color('white')
FRAG_CODE = """
uniform sampler2D s_texture;
uniform vec4 u_color;
varying float v_lifetime;
void main()
{
vec4 texColor;
texColor = texture2D(s_texture, gl_PointCoord);
gl_FragColor = vec4(u_color) * texColor;
gl_FragColor.a *= v_lifetime;
}
"""
class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self)
self.size = 400, 400
self._starttime = time.time()
self._new_explosion()
def on_initialize(self, event):
gl.glClearColor(0,0,0,1);
# Enable blending
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE)
# Create shader program
self._prog_handle = gl.glCreateProgram()