Skip to content

Instantly share code, notes, and snippets.

@dte dte/Triangle2.pde
Created May 2, 2011

Embed
What would you like to do?
Faceless Void [Processing] ... updated to kinda work...from code here: http://www.creativeapplications.net/processing/faceless-void-processing/
import megamu.mesh.*;
import processing.opengl.*;
import javax.vecmath.*;
import org.processing.wiki.triangulate.*;
import processing.video.*;
Triangle2 ttt;
Capture cam; //cam image
PImage lastFrame; //last frame image
PImage diffFrame; //difference and dark image
int mode = 0; //lines and gradients?
Vector ps; //points (from diffFrame)
ArrayList ts; //Triangle2 (custom written class)
ArrayList triangles;
ArrayList points = new ArrayList();
void setup()
{
size(640, 420,P3D);
cam = new Capture(this, 320, 240);
lastFrame = new PImage(cam.width,cam.height);
diffFrame = new PImage(cam.width,cam.height);
noCursor();
}
void draw()
{
if(frameCount%200==0)
{
keyPressed();
}
if(cam.available() == true)
{
background(255);
cam.read();
//init with corners
points.add(new PVector(0,0,0));
points.add(new PVector(cam.width-1,0,0));
points.add(new PVector(0,cam.height-1,0));
points.add(new PVector(cam.width-1,cam.height-1,0));
//find points (ignore very near points (10))
for(int i=0;i<diffFrame.pixels.length;i++)
{
if(diffFrame.pixels[i] == color(255))
{
boolean near = false;
for(int j=0;j<points.size();j++)
{
PVector p = (PVector)points.get(j);
if(dist(p.x,p.y,i%cam.width,i/cam.width)<5)
{
near = true;
break;
}
}
if(!near)
{
points.add(new PVector(i%cam.width,i/cam.width,0));
}
}
}
ts = new ArrayList();
if(points.size()>0)
{
triangles = Triangulate.triangulate(points); //library helps finding the points (love Delaunay!)
if(triangles!= null)
{
for(int i=0;i<triangles.size();i++)
{
Triangle t = (Triangle)triangles.get(i);
PVector p1 = t.p1;
PVector p2 = t.p2;
PVector p3 = t.p3;
ts.add(new Triangle2(p1,p2,p3,
lastFrame.pixels[floor(p1.x)+floor(p1.y)*lastFrame.width],
lastFrame.pixels[floor(p2.x)+floor(p2.y)*lastFrame.width],
lastFrame.pixels[floor(p3.x)+floor(p3.y)*lastFrame.width]));
}
}
}
int pos=0;
int count = 0;
for(int i=0;i<lastFrame.pixels.length;i++)
{
//looking for moved pixel and dark pixel
if(dist(red(cam.pixels[pos]),green(cam.pixels[pos]),blue(cam.pixels[pos]),
red(lastFrame.pixels[pos]),green(lastFrame.pixels[pos]),blue(lastFrame.pixels[pos]))>130 ||
(brightness(lastFrame.pixels[pos])<5 &&brightness(lastFrame.pixels[pos])>=0))
{
count++;
diffFrame.pixels[pos] = color(255);
if(count>300)
break;
}
else diffFrame.pixels[pos] = color(0);
//trying to approach a even space point traversal
pos+=25603;
if(pos>lastFrame.pixels.length) pos-=lastFrame.pixels.length;
}
diffFrame.updatePixels();
//copy current cam to lastFrame, does it seems the fastest (and quite easy) way?
lastFrame.pixels = cam.pixels.clone();
lastFrame.updatePixels();
//draw the triangles
//draw the triangles
pushMatrix();
scale(width/320.0,height/240.0);
for(int i=0;i<ts.size();i++)
{
Triangle2 t = (Triangle2)ts.get(i);
t.draw();
}
popMatrix();
}
}
class Triangle2
{
PVector p1,p2,p3;
color c1,c2,c3;
Triangle2() {
}
Triangle2(PVector _p1, PVector _p2, PVector _p3, color _c1, color _c2, color _c3)
{
p1 = _p1;
p2 = _p2;
p3 = _p3;
c1 = _c1;
c2 = _c2;
c3 = _c3;
reArrange();
}
//look for leftMost(o0) point and can brighten it, also darken the rests
//or do slightly color adjustments
void reArrange()
{
PVector o0,o1,o2;
color m0,m1,m2;
if(p1.x <= p2.x && p1.x <=p3.x)
{
o0 = p1;
o1 = p2;
o2 = p3;
m0 = c1;
m1 = c2;
m2 = c3;
}
else if(p2.x < p3.x)
{
o0 = p2;
o1 = p1;
o2 = p3;
m0 = c2;
m1 = c1;
m2 = c3;
}
else
{
o0 = p3;
o1 = p1;
o2 = p2;
m0 = c3;
m1 = c1;
m2 = c2;
}
p1 = o0;
p2 = o1;
p3 = o2;
c1 = m0;
c2 = m1;
c3 = m2;
colorMode(HSB);
float amplify = 1;
float d2 = p2.x-p1.x;
float d3 = p3.x-p1.x;
c1 = color(hue(c1),saturation(c1)*1,constrain(brightness(c1),20,255));//+(d2+d3)*amplify);
c2 = color(hue(c2),saturation(c2)*1,constrain(brightness(c2),20,255));//-d2*amplify);
c3 = color(hue(c3),saturation(c3)*1,constrain(brightness(c3),20,255));//-d3*amplify);
colorMode(RGB);
}
void draw()
{
if(mode>1)
noStroke();
else stroke(0);
beginShape();
fill(c1);
vertex(p1.x,p1.y);
if(mode %2==1)fill(c2);
vertex(p2.x,p2.y);
if(mode %2== 1)fill(c3);
vertex(p3.x,p3.y);
endShape(CLOSE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.