/*   
 Written in Processing
 
 RGB_video
 By Stijn Toussaint http://saintonline.nl stijn@saintonline.nl
 Version 1
 Licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
 https://creativecommons.org/licenses/by-nc-sa/4.0/
 */

  import processing.video.*;

  Movie img;

  float res = 10;
  float alpha = 200;
  float raster = 1.5;
  float t;
  float t_r = random(100);
  float t_g = random(100);
  float t_b = random(100);
  int time = millis();
  float speed = 0.02;
  float d = 15;

  void setup() {
    fullScreen();
    noCursor();
    blendMode(ADD);
    background(0);

    img = new Movie(this, "cheeta.mp4");
    img.loop();
  }

  void draw() {
    img.jump(t);
    img.read();
    background(0);
    noStroke();
    pushMatrix();
    translate(map(noise(t_r), 0, 1, -d, d), map(noise(t_r+100), 0, 1, -d, d));
    for (int x = 0; x < img.width; x += res*raster) {
      for (int y = 0; y < img.height; y += res*raster) {
        int loc = x+y*img.width;
        float r = red(img.pixels[loc]);

        pushMatrix();
        fill(r, 0, 0, 150);
        ellipse(x, y, res, res);
        popMatrix();
      }
    }
    popMatrix();
    pushMatrix();
    translate(map(noise(t_g), 0, 1, -d, d), map(noise(t_g+100), 0, 1, -d, d));
    for (int x = 0; x < img.width; x += res*raster) {
      for (int y = 0; y < img.height; y += res*raster) {
        int loc = x+y*img.width;
        float g = green(img.pixels[loc]);

        pushMatrix();
        fill(0, g, 0, 150);
        ellipse(x, y, res, res);
        popMatrix();
      }
    } 
    popMatrix();
    pushMatrix();
    translate(map(noise(t_b), 0, 1, -d, d), map(noise(t_b+100), 0, 1, -d, d));
    for (int x = 0; x < img.width; x += res*raster) {
      for (int y = 0; y < img.height; y += res*raster) {
        int loc = x+y*img.width;
        float b = blue(img.pixels[loc]);

        pushMatrix();
        fill(0, 0, b, 150);
        ellipse(x, y, res, res);
        popMatrix();
      }
    }
    popMatrix();
    t += 0.1;
    t_r += speed;
    t_g += speed;
    t_b += speed;

    saveFrame("output/RGB_mix_####.png");
  }