From bc1055792ac505a55394cbaa6942e99f3bf4df95 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Thu, 16 Aug 2012 17:52:27 +0000 Subject: [PATCH] Fixed Framingham and Frames examples in Video, added ImageMask in OpenGL/Shaders --- .../OpenGL/Shaders/ImageMask/ImageMask.pde | 25 ++++++++++++++++ .../OpenGL/Shaders/ImageMask/data/mask.glsl | 12 ++++++++ .../Capture/Framingham/Framingham.pde | 8 ++--- .../video/examples/Movie/Frames/Frames.pde | 29 +++++++++---------- 4 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 java/examples/OpenGL/Shaders/ImageMask/ImageMask.pde create mode 100644 java/examples/OpenGL/Shaders/ImageMask/data/mask.glsl diff --git a/java/examples/OpenGL/Shaders/ImageMask/ImageMask.pde b/java/examples/OpenGL/Shaders/ImageMask/ImageMask.pde new file mode 100644 index 000000000..6c3020437 --- /dev/null +++ b/java/examples/OpenGL/Shaders/ImageMask/ImageMask.pde @@ -0,0 +1,25 @@ +PImage srcImage; +PGraphics maskImage; +PShader maskShader; + +void setup() { + size(200, 200, P2D); + srcImage = loadImage("milan_rubbish.jpg"); + maskImage = createGraphics(srcImage.width, srcImage.height, P2D); + maskImage.noSmooth(); + maskShader = loadShader("mask.glsl"); + maskShader.set("maskSampler", maskImage); +} + +void draw() { + maskImage.beginDraw(); + maskImage.background(0); + maskImage.noStroke(); + maskImage.fill(255, 0, 0); + maskImage.ellipse(mouseX, mouseY, 30, 30); + maskImage.endDraw(); + + shader(maskShader); + image(srcImage, 0, 0, width, height); + resetShader(); +} \ No newline at end of file diff --git a/java/examples/OpenGL/Shaders/ImageMask/data/mask.glsl b/java/examples/OpenGL/Shaders/ImageMask/data/mask.glsl new file mode 100644 index 000000000..e4d0dbb26 --- /dev/null +++ b/java/examples/OpenGL/Shaders/ImageMask/data/mask.glsl @@ -0,0 +1,12 @@ +uniform sampler2D textureSampler; +uniform sampler2D maskSampler; + +uniform vec2 texcoordOffset; +varying vec4 vertColor; +varying vec4 vertTexcoord; + +void main() { + vec4 texColor = texture2D(textureSampler, vertTexcoord.st).rgba; + vec4 maskColor = texture2D(maskSampler, vec2(vertTexcoord.s, 1.0 - vertTexcoord.t)).rgba; + gl_FragColor = mix(texColor, vec4(0, 0, 0, 0), 1.0 - maskColor.r); +} \ No newline at end of file diff --git a/java/libraries/video/examples/Capture/Framingham/Framingham.pde b/java/libraries/video/examples/Capture/Framingham/Framingham.pde index 51062711d..a53b94321 100644 --- a/java/libraries/video/examples/Capture/Framingham/Framingham.pde +++ b/java/libraries/video/examples/Capture/Framingham/Framingham.pde @@ -1,5 +1,3 @@ -// Currently broken with the P2D renderer. - /** * Framingham * by Ben Fry. @@ -42,14 +40,14 @@ void draw() { if (video.available()) { video.read(); video.loadPixels(); - set(video.width*column, video.height*lastRow, video); + image(video, video.width*column, video.height*lastRow); column++; if (column == columnCount) { loadPixels(); // Scoot everybody up one row - arraycopy(pixels, video.height*width, scoot, 0, scoot.length); - arraycopy(scoot, 0, pixels, 0, scoot.length); + arrayCopy(pixels, video.height*width, scoot, 0, scoot.length); + arrayCopy(scoot, 0, pixels, 0, scoot.length); // Set the moved row to black for (int i = scoot.length; i < width*height; i++) { diff --git a/java/libraries/video/examples/Movie/Frames/Frames.pde b/java/libraries/video/examples/Movie/Frames/Frames.pde index 3f087217d..e91673190 100644 --- a/java/libraries/video/examples/Movie/Frames/Frames.pde +++ b/java/libraries/video/examples/Movie/Frames/Frames.pde @@ -3,8 +3,11 @@ * by Andres Colubri * * Moves through the video one frame at the time by using the - * arrow keys. It estimates the frame counts using the framerate - * of the movie file, so it might not be exact in some cases. + * arrow keys. Because it estimates the frame counts using the + * framerate of the movie file, and also, many movie codecs don't + * actually support jumping to arbitrary frames, don't expect + * accurate results in most cases. + * */ import processing.video.*; @@ -16,28 +19,22 @@ PFont font; void setup() { size(320, 240, P2D); background(0); - // Load and set the video to play. Setting the video - // in play mode is needed so at least one frame is read - // and we can get duration, size and other information from - // the video stream. + movie = new Movie(this, "station.mov"); - movie.play(); + newFrame = 0; + setFrame(newFrame); + + noLoop(); font = loadFont("DejaVuSans-24.vlw"); textFont(font, 24); } void movieEvent(Movie movie) { - movie.read(); + movie.read(); } void draw() { - if (frameCount == 5) { - // Trick to force start at frame 0... - newFrame = 0; - setFrame(newFrame); - } - image(movie, 0, 0, width, height); fill(240, 20, 30); @@ -79,7 +76,9 @@ void setFrame(int n) { movie.jump(where); - movie.pause(); + movie.pause(); + + redraw(); } int getLength() {