Skip to content

Instantly share code, notes, and snippets.

@nonoesp
Created November 17, 2015 19:44
Show Gist options
  • Save nonoesp/50aaff45615aa6f52732 to your computer and use it in GitHub Desktop.
Save nonoesp/50aaff45615aa6f52732 to your computer and use it in GitHub Desktop.
Processing sketch to project corners of a 3D box to 2D screen space.
// Nono Martínez Alonso (@nonoesp) (www.nono.ma)
// 151027
boolean shouldSaveFrames = false; // Set to true to export footage frames
PShape s;
void setup(){
size(800, 600, P3D);
pixelDensity(displayDensity());
}
void draw() {
background(255);
strokeWeight(1.0);
float f = 60 * frameCount * 0.0005;
f = 0.5 * PI * frameCount / 48;
if(sin(f) == 1.0) {
println(frameCount);
}
if(shouldSaveFrames && frameCount >= 48 && frameCount <= 240) {
saveFrame("/Users/nono/Desktop/frames/frame-######");
}
pushMatrix();
translate(width/2, height/2);
rotateX(f); // Rotate continuously
//Other rotations
//rotateX(2 * PI*0.25 * mouseX/100);
//rotateX(HALF_PI*0.5 + sin(f) / 2);
noFill();
box(100, 100, 100);
float boxY = 50;
float p1[] = new float[]{screenX(-50, boxY, 50), screenY(-50, boxY, 50)};
float p2[] = new float[]{screenX(-50, boxY,-50), screenY(-50, boxY,-50)};
float p3[] = new float[]{screenX( 50, boxY,-50), screenY( 50, boxY,-50)};
float p4[] = new float[]{screenX( 50, boxY, 50), screenY( 50, boxY, 50)};
popMatrix();
stroke(220);
line(0,0, p1[0], p1[1]);
line(0,height, p2[0], p2[1]);
line(width,height, p3[0], p3[1]);
line(width,0, p4[0], p4[1]);
stroke(0);
strokeWeight(3.0);
s = createShape();
s.beginShape();
s.fill(255,100, 80);
s.strokeWeight(2.0);
s.vertex(p1[0], p1[1]);
s.vertex(p2[0], p2[1]);
s.vertex(p3[0], p3[1]);
s.vertex(p4[0], p4[1]);
s.endShape(CLOSE);
shape(s, 0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment