Skip to content

Instantly share code, notes, and snippets.

@tsulej
Created May 5, 2015 09:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tsulej/f4b637a843efa53dc925 to your computer and use it in GitHub Desktop.
Save tsulej/f4b637a843efa53dc925 to your computer and use it in GitHub Desktop.
GLSL Shader and Processing - 2D example
// shader example for Processing
// uniform - variables set in Processing sketch using shader.set()
// varying - variables set by Processing itself
// image to process
uniform sampler2D texture;
// mouse position normalized
uniform vec2 inp;
// vertex color
varying vec4 vertColor;
// vertex position
varying vec4 vertTexCoord;
#define TWO_PI (6.28318530718)
void main() {
vec4 col = texture2D(texture,vertTexCoord.xy); // take color from texture
float lumaang = TWO_PI*(0.2126 * col.r + 0.7152 * col.g + 0.0722 * col.b); // calculate luma
vec2 off = vec2(2.0*inp.x*sin(lumaang),2.0*inp.y*cos(lumaang)); // calculate offset vector from luma angle
gl_FragColor = vec4(texture2D(texture, fract(vertTexCoord.st+off)).rgb,1.0); // set color from texture from current position + offest. Wrapped.
}
// generateme.tumblr.com
// using OpenGL shader example
// imagelens
// shader declaration
PShader sh;
String uid;
void setup() {
uid = hex((int)random(0x10000),4);
PImage img = loadImage("test.jpg");
size(img.width,img.height,P2D);
background(0);
// load and compile shader
sh = loadShader("lens.glsl");
// upload texture to graphics card
sh.set("texture",img);
}
void draw() {
// normalize mouse position
float inpx = mouseX/(float)width;
float inpy = mouseY/(float)height;
// set shader variable
sh.set("inp",inpx,inpy);
// run shader
shader(sh);
// fill whole window
rect(0,0,width,height);
}
void keyPressed() {
saveFrame("res"+uid+"######.jpg");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment