1/**
2 * Mirror
3 * by Daniel Shiffman.
4 *
5 * Each pixel from the video source is drawn as a rectangle with rotation based on brightness.
6 */
7
8
9import processing.video.*;
10
11// Size of each cell in the grid
12int cellSize = 20;
13// Number of columns and rows in our system
14int cols, rows;
15// Variable for capture device
16Capture video;
17
18
19void setup() {
20  size(640, 480, P2D);
21  frameRate(30);
22  cols = width / cellSize;
23  rows = height / cellSize;
24  colorMode(RGB, 255, 255, 255, 100);
25
26  // Uses the default video input, see the reference if this causes an error
27  video = new Capture(this, width, height, 12);
28
29  background(0);
30}
31
32
33void draw() {
34  if (video.available()) {
35    video.read();
36    video.loadPixels();
37
38    // Not bothering to clear background
39    // background(0);
40
41    // Begin loop for columns
42    for (int i = 0; i < cols; i++) {
43      // Begin loop for rows
44      for (int j = 0; j < rows; j++) {
45
46        // Where are we, pixel-wise?
47        int x = i*cellSize;
48        int y = j*cellSize;
49        int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image
50
51        float r = red(video.pixels[loc]);
52        float g = green(video.pixels[loc]);
53        float b = blue(video.pixels[loc]);
54        // Make a new color with an alpha component
55        color c = color(r, g, b, 75);
56
57        // Code for drawing a single rect
58        // Using translate in order for rotation to work properly
59        pushMatrix();
60        translate(x+cellSize/2, y+cellSize/2);
61        // Rotation formula based on brightness
62        rotate((2 * PI * brightness(c) / 255.0));
63        rectMode(CENTER);
64        fill(c);
65        noStroke();
66        // Rects are larger than the cell for some overlap
67        rect(0, 0, cellSize+6, cellSize+6);
68        popMatrix();
69      }
70    }
71  }
72}
73