Skip to content

Instantly share code, notes, and snippets.

@fiskurgit
Created May 30, 2018 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiskurgit/e1f80d3c9c51452253b141d524f754db to your computer and use it in GitHub Desktop.
Save fiskurgit/e1f80d3c9c51452253b141d524f754db to your computer and use it in GitHub Desktop.
Draw 3d object in 2d space...
PShape shape;
float xmag, ymag = 0;
float newXmag, newYmag = 0;
void setup(){
size(500, 500, P3D);
noFill();
stroke(255, 0, 0);
shape = makeCube();
}
ArrayList<Float> xCoords = new ArrayList();
ArrayList<Float> yCoords = new ArrayList();
void draw(){
background(255);
stroke(255, 0, 0);
pushMatrix();
stroke(255, 0, 0);
translate(width/2, height/2, 0);
float xRotation = (mouseY/360.0)*-(TWO_PI) + PI;
float yRotation = (mouseX/420.0)*(TWO_PI) - PI;
float zRotation = PI/36;
rotateX(xRotation);
rotateY(yRotation);
rotateZ(zRotation);
scale(90);
//shape(shape);
xCoords.clear();
yCoords.clear();
for (int i = 0; i < shape.getVertexCount(); i++) {
PVector vertex = shape.getVertex(i);
float xx = screenX(vertex.x, vertex.y, vertex.z);
float yy = screenY(vertex.x, vertex.y, vertex.z);
xCoords.add(xx);
yCoords.add(yy);
}
popMatrix();
float lastX = xCoords.get(0);
float lastY = yCoords.get(0);
for(int v = 1 ; v < shape.getVertexCount() ; v++){
float x = xCoords.get(v);
float y = yCoords.get(v);
ellipse(x, y, 10, 10);
stroke(0, 0, 255);
line(lastX, lastY, x, y);
lastX = x;
lastY = y;
}
}
void keyPressed(){
if(key == 'p'){
println("...");
}
}
PShape makeCube(){
PShape shape = createShape();
shape.beginShape(QUADS);
shape.vertex(-1, 1, 1);
shape.vertex( 1, 1, 1);
shape.vertex( 1, -1, 1);
shape.vertex(-1, -1, 1);
shape.vertex( 1, 1, 1);
shape.vertex( 1, 1, -1);
shape.vertex( 1, -1, -1);
shape.vertex( 1, -1, 1);
shape.vertex( 1, 1, -1);
shape.vertex(-1, 1, -1);
shape.vertex(-1, -1, -1);
shape.vertex( 1, -1, -1);
shape.vertex(-1, 1, -1);
shape.vertex(-1, 1, 1);
shape.vertex(-1, -1, 1);
shape.vertex(-1, -1, -1);
shape.vertex(-1, 1, -1);
shape.vertex( 1, 1, -1);
shape.vertex( 1, 1, 1);
shape.vertex(-1, 1, 1);
shape.vertex(-1, -1, -1);
shape.vertex( 1, -1, -1);
shape.vertex( 1, -1, 1);
shape.vertex(-1, -1, 1);
shape.endShape();
return shape;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment