Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Created December 2, 2015 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atduskgreg/fce473cd10f06b914ef5 to your computer and use it in GitHub Desktop.
Save atduskgreg/fce473cd10f06b914ef5 to your computer and use it in GitHub Desktop.
uniform sampler2D srcImg;
varying vec4 vertTexCoord;
void main() {
gl_FragColor = texture2D(srcImg, vertTexCoord.st).rgba;
}
PShader shader;
PGraphics output;
void setup() {
size(500, 500, P2D);
output = createGraphics(500, 500, P2D);
updateCanvas();
initShader();
}
void initShader() {
shader = loadShader("drawGraphic.glsl");
shader.set("srcImg", output);
println("shader loaded");
}
void updateCanvas(){
output.beginDraw();
output.background(random(255), random(255), random(255));
output.fill(255, 0, 0);
output.ellipse(250, 250, 20, 20);
output.endDraw();
}
void draw() {
shader(shader);
drawOutput(0,0, width, height);
}
void mouseMoved(){
updateCanvas();
shader.set("srcImg", output);
}
// borrowed from Andres Colubri's CustomBlend example
void drawOutput(float x, float y, float w, float h) {
pushMatrix();
translate(x, y);
noStroke();
beginShape(QUAD);
// Although we are not associating a texture to
// this shape, the uv coordinates will be stored
// anyways so they can be used in the fragment
// shader to access the destination and source
// images.
vertex(0, 0, 0, 0);
vertex(w, 0, 1, 0);
vertex(w, h, 1, 1);
vertex(0, h, 0, 1);
endShape();
popMatrix();
}
void keyPressed() {
if (key == 'r') {
initShader();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment