Created
October 24, 2012 03:17
-
-
Save lizkhoo/3943470 to your computer and use it in GitHub Desktop.
Diamonds02 with video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//crystal raindrops with video | |
import processing.video.*; | |
Capture video; | |
int totalDiamonds = 10; | |
Diamond[] ourDiamonds = new Diamond[totalDiamonds]; | |
void setup() { | |
size(640, 480, P3D); | |
smooth(); | |
for (int i=0; i < ourDiamonds.length; i++) { | |
ourDiamonds[i] = new Diamond(random(width), random(height)); | |
} | |
video= new Capture(this, width, height); | |
video.start(); | |
} | |
void draw() { | |
background(0); | |
if (video.available()) { | |
video.read(); | |
video.loadPixels(); | |
} | |
for (int i=0; i<ourDiamonds.length; i++) { | |
ourDiamonds[i].moveDiamond(); | |
} | |
} | |
class Diamond { | |
float xpos, ypos, speed, r, pointplace; | |
float[] pointsX = new float[8]; | |
float[] pointsY = new float[8]; | |
float theta; | |
Diamond(float x, float y) { | |
xpos = x; | |
ypos = y; | |
pointplace = 50; | |
speed = random(1, 5); | |
r = (HALF_PI); | |
//create random coordinates around origin | |
for (int i=0;i < pointsX.length; i++) { | |
pointsX[i]=random(-pointplace, pointplace); | |
pointsY[i]=random(-pointplace, pointplace); | |
} | |
} | |
//create random points and connect lines between | |
void drawDiamond() { | |
stroke(255); | |
textureMode(NORMAL); //maps image 0-1 instead of actual size | |
point(0, 0); //initiate center points at origin | |
//fill(255, 50); | |
beginShape(TRIANGLE_STRIP); | |
texture(video); //color with video | |
for (int i = 0; i < pointsX.length; i++) { //loop through point array to create vertices | |
float u = map(pointsX[i], -50, 50, 0, 1); //map points to 0-1 so video can map 0-1 | |
float v = map(pointsY[i], -50, 50, 0, 1); | |
vertex(pointsX[i], pointsY[i], u, v); | |
} | |
endShape(); | |
} | |
void moveDiamond() { | |
ypos+=speed; | |
pushMatrix(); | |
translate(xpos, ypos); //now move all diamonds from 0 to random positions | |
rotate(theta); //rotate uses radians | |
drawDiamond(); //put this here so that diamonds are only drawn after random pos is recognized as origin via translate | |
popMatrix(); | |
if (ypos > height+100) { | |
ypos = 0; | |
} | |
theta += (radians (3)); //converts radians to degrees | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment