/** SplineExtrude.pde http://www.davebollinger.com The technique intended to be illustrated here is how to use Processing's PMatrix and spherical coordinates to build a mesh around a curve. A Bezier spline is used here, but this demo really isn't about Beziers, it's about using a PMatrix to transform points - so take technique and apply elsewhere. (note that this demo is overly simple, and creates curves that are entirely random that may contain sharp "cusps" that won't generate "nice" meshes -- the mesh may self-intersect, or "twist" wildly along its length, etc -- you'd want to avoid such curves in actual use, or use a smaller radius, or more steps, attempting to better capture such detail) drag to rotate right-click to generate next shape */ // P3D used instead for web version //import processing.opengl.*; // simple storage class for a 3d point class P3 { float x, y, z; P3() { this(0,0,0); } P3(float _x, float _y, float _z) { x=_x; y=_y; z=_z; } void draw() { vertex(x,y,z); } } // storage/convenience class for a random bezier spline // (as used in this demo) class Bezier { float x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4; Bezier() { make(); } void make() { x1=random(-200,200); y1=random(-200,200); z1=random(-200,200); x2=random(-200,200); y2=random(-200,200); z2=random(-200,200); x3=random(-200,200); y3=random(-200,200); z3=random(-200,200); x4=random(-200,200); y4=random(-200,200); z4=random(-200,200); } float getX(float t) { return bezierPoint(x1,x2,x3,x4,t); } float getY(float t) { return bezierPoint(y1,y2,y3,y4,t); } float getZ(float t) { return bezierPoint(z1,z2,z3,z4,t); } void draw() { bezier(x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4); } } // how many points "around" the mesh int usteps = 8; // how many points "along" the mesh int vsteps = 50; // the u-shape points to be lathed along a curve // ("+1" for convenience to dup first point and close the curve) P3 [] shape = new P3[usteps+1]; // the curve to extrude along: Bezier bez; // the resulting mesh by extruding "shape" along the curve P3 [][] mesh = new P3[vsteps][usteps+1]; // camera rotation float rotx, roty; void setup() { size(500,500,P3D); //size(500,500,OPENGL); build(); } void build() { // create a new curve: bez = new Bezier(); // create a matrix to perform the transforms PMatrix mat = new PMatrix(); // a place to store transformed points float [] vout = new float[3]; // build the mesh: for (int v=0; v