Created
December 14, 2018 04:09
-
-
Save EricThomson/de3ee283952393b2cd5240240749d00c to your computer and use it in GitHub Desktop.
vispy drifting sinusoid example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gpu drifting sinusoid. | |
# based on gpuimage example in docs: | |
# https://github.com/vispy/vispy/blob/master/examples/basics/gloo/gpuimage.py | |
from vispy import app, gloo | |
vertex = """ | |
attribute vec2 xy_position; | |
varying vec2 v_position; | |
void main() | |
{ | |
gl_Position = vec4(xy_position, 0.0, 1.0); | |
v_position = xy_position; | |
} | |
""" | |
fragment = """ | |
uniform float u_time; | |
varying vec2 v_position; | |
const float z_offset = 1.; | |
const float z_max = 2.; | |
const float x_scale = 5.; // x is between -x_scale and +x_scale | |
const float y_scale = 5.; // y is between -y_scale and +y_scale | |
const float t_scale = 5.; // scale for the time | |
float f(float x, float y, float t) { | |
float phase_shift = 2; | |
float spatial_freq = 3.0; | |
return cos(x*spatial_freq - phase_shift*t); | |
} | |
//greyscale color scheme (needs work) | |
vec4 greyscale(float x) { | |
return vec4(x, x, x, 1.0); | |
} | |
void main() { | |
vec2 pos = v_position; | |
float z = f(x_scale * pos.x, y_scale * pos.y, t_scale * u_time); | |
gl_FragColor = greyscale((z + z_offset) / (z_max)); | |
} | |
""" | |
#check out canvas.fps and other things | |
#%% | |
class Canvas(app.Canvas): | |
def __init__(self): | |
app.Canvas.__init__(self, position=(300, 100), | |
size=(800, 800), keys='interactive') | |
self.program = gloo.Program(vertex, fragment) | |
self.program['xy_position'] = [(-1., -1.), (-1., +1.), | |
(+1., -1.), (+1., +1.)] | |
self.program['u_time'] = 0.0 | |
self.timer = app.Timer('auto', connect=self.on_timer, start=True) | |
self.show() | |
def on_timer(self, event): | |
self.program['u_time'] = event.elapsed | |
self.update() | |
def on_resize(self, event): | |
width, height = event.physical_size | |
gloo.set_viewport(0, 0, width, height) | |
def on_draw(self, event): | |
self.program.draw('triangle_strip') | |
#%% | |
if __name__ == '__main__': | |
canvas = Canvas() | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment