Created
November 25, 2014 22:37
-
-
Save rossant/dbd8237978bdbd750344 to your computer and use it in GitHub Desktop.
Vispy heatmap
This file contains hidden or 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
import numpy as np | |
from vispy import app, gloo | |
vertex = """ | |
uniform vec4 viewport; | |
attribute vec2 position; | |
attribute vec2 texcoord; | |
varying vec2 v_texcoord; | |
varying vec2 v_pixcoord; | |
varying vec2 v_quadsize; | |
void main() | |
{ | |
gl_Position = vec4(position,0.0,1.0); | |
v_texcoord = texcoord; | |
v_quadsize = viewport.zw; | |
v_pixcoord = texcoord * v_quadsize; | |
} | |
""" | |
fragment = """ | |
uniform sampler2D texture; | |
varying vec2 v_texcoord; | |
varying vec2 v_quadsize; | |
varying vec2 v_pixcoord; | |
vec4 filled(float distance, float linewidth, float antialias, vec4 fill) | |
{ | |
vec4 frag_color; | |
float t = linewidth/2.0 - antialias; | |
float signed_distance = distance; | |
float border_distance = abs(signed_distance) - t; | |
float alpha = border_distance/antialias; | |
alpha = exp(-alpha*alpha); | |
if( border_distance < 0.0 ) | |
frag_color = fill; | |
else if( signed_distance < 0.0 ) | |
frag_color = fill; | |
else | |
frag_color = vec4(fill.rgb, alpha * fill.a); | |
return frag_color; | |
} | |
float marker(vec2 P, float size) | |
{ | |
return max(abs(P.x), abs(P.y)) - size/2.0; | |
} | |
void main() | |
{ | |
const float rows = 50.0; | |
const float cols = 50.0; | |
float v = texture2D(texture, v_texcoord).r; | |
vec2 size = v_quadsize / vec2(cols,rows); | |
vec2 center = (floor(v_pixcoord/size) + vec2(0.5,0.5)) * size; | |
float d = marker(v_pixcoord - center, 0.9*size.x); | |
gl_FragColor = filled(d, 1.0, 1.0, vec4(v,v,v,1)); | |
} | |
""" | |
c = app.Canvas(keys='interactive', size=(600, 600)) | |
program = gloo.Program(vertex, fragment) | |
program['position'] = np.array([(-1,-1), (-1,1), (1,-1), (1,1)], | |
dtype=np.float32) | |
program['texcoord'] = np.array([( 0, 1), ( 0, 0), ( 1, 1), ( 1, 0)], | |
dtype=np.float32) | |
program['texture'] = np.random.uniform(0,1,(50,50)).astype(np.float32) | |
@c.connect | |
def on_draw(ev): | |
gloo.clear('white') | |
program.draw("triangle_strip") | |
@c.connect | |
def on_resize(ev): | |
width, height = ev.size | |
program['viewport'] = 0, 0, width, height | |
c.show() | |
app.run() |
Took me a while, but I figured it out...
Replace
width, height = ev.size
program['viewport'] = 0, 0, width, height
with ' gloo.set_viewport` and some fixed size, like
gloo.set_viewport(0, 0, 1000, 1000)
This last line of code can also be placed under def on_draw(ev):
Phew!
Vispy is a remarkable piece of software, but, even assuming proficiency in GLSL, it's non-obvious how vispy interfaces with gloo/OpenGL.... @rossant Do you know of any Vispy tutorials other than your (pretty good) IPython Cookbook one? There's a ton of material on GLSL and OpenGL ES 2.0, but I'm desperately looking for more material on the vispy API other than the official documentation (which is not the most verbose, if you don't mind me saying) and the source itself.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you know how I can add color this, and also set a 1:1 aspect ratio?