/** * 3D * From Peter Collinridge's tutorial at * http://www.petercollingridge.appspot.com/3D-tutorial * * use up/down arrow keys to move in and out, mouse to look around * * Added: perspective projection, faces, move forward/back with up/down arrow keys, autorotate * */ var focalLength = 300; var focalLimiter = 0; var backgroundColour = color(255, 250, 255); var nodeColour = color(40, 168, 107); var edgeColour = color(34, 68, 204); var faceColour = color(69, 57, 237, 100); var nodeSize = 6; var showAxes = false; var autoRotateX = false; var autoRotateY = false; var autoRotateZ = false; var createCuboid = function(x, y, z, w, h, d) { var nodes = [[x, y, z ], [x, y, z+d], [x, y+h, z ], [x, y+h, z+d], [x+w, y, z ], [x+w, y, z+d], [x+w, y+h, z ], [x+w, y+h, z+d]]; var edges = [[0, 1], [1, 3], [2, 3], [2, 0], [4, 5], [5, 7], [6, 7], [6, 4], [0, 4], [1, 5], [2, 6], [3, 7]]; var faces = [[0, 2], [2, 6], [6, 4], [4, 0], [1, 5], [3, 7]]; return { 'nodes': nodes, 'edges': edges, 'faces':faces }; }; // -174, -120, 196 >> -116, -75, 254 // 60, 45, 60 var object1 = createCuboid(-55, -5, 320, 240, 40, 40); var object2 = createCuboid(-45, -35, 310, -30, 100, 60); var object3 = createCuboid( 175, -35, 310, 30, 100, 60); var objects = [object1, object2, object3]; var rotateZ3D = function(theta, nodes) { var sin_t = sin(theta); var cos_t = cos(theta); for (var n=0; n=330 && mouseX<=340 && mouseY>=385 && mouseY<=395) { fill(255, 0, 0); } rect(330, 385, 10, 10); fill(0, 0, 0); text("x", 333, 393); fill(140, 255, 140); if(mouseX>=350 && mouseX<=360 && mouseY>=385 && mouseY<=395) { fill(0, 255, 0); } rect(350, 385, 10, 10); fill(0, 0, 0); text("y", 352, 393); fill(135, 135, 255); if(mouseX>=370 && mouseX<=380 && mouseY>=385 && mouseY<=395) { fill(80, 80, 250); } rect(369, 385, 10, 10); fill(0, 0, 0); text("z", 372, 393); // for testing //text("first x: " + objects[0].nodes[0][0], 5, 353); //text("first y: " + objects[0].nodes[0][1], 5, 373); text("first z: " + objects[0].nodes[0][2], 5, 393); //text("fx: "+focalLength*objects[0].nodes[0][0]/objects[0].nodes[0][2], 170, 353); //text("fy: "+focalLength*objects[0].nodes[0][1]/objects[0].nodes[0][2], 170, 373); }; var mouseDragged = function() { var dx = mouseX - pmouseX; var dy = mouseY - pmouseY; // rotate each object for (var obj=0; obj=330 && mouseX<=340 && mouseY>=385 && mouseY<=395) { autoRotateX = !autoRotateX; } if(mouseX>=350 && mouseX<=360 && mouseY>=385 && mouseY<=395) { autoRotateY = !autoRotateY; } if(mouseX>=370 && mouseX<=380 && mouseY>=385 && mouseY<=395) { autoRotateZ = !autoRotateZ; } };