[Merz Akademie workshop] Here is the code I wrote to create a 3D mesh by rotating a set of 2D points:

// 3dmesh.pde
// Marius Watz - http://workshop.evolutionzone.com

import processing.opengl.*;

int numrot;
float px[],py[],mesh[][][];

void setup() {
  size(700,700, OPENGL);

  // To create a mesh, first create two arrays to hold the
  // X and Y coordinates of the path you will use to create it
  px=new float[20];
  py=new float[20];
  float t=0;
  for(int i=0; i< px.length; i++) {
    px[i]=bezierPoint(0,130, 130, 0, t);
    py[i]=bezierPoint(450,350,150,50, t);
    t+=(1.0/(float)(px.length-1));
  }

  // use createMesh to create the mesh. createMesh takes three parameters:
  // num == number of rotations
  // startDeg == start degree of rotation
  // endDeg == end degree of rotation

  mesh=createMesh(px,py,20, -60,60);
}

void draw() {

translate(width/2,height/2);
  pushMatrix();

  background(0);
  lights();

  rotateY(radians(frameCount));
  rotateX(radians(frameCount)/2);

  stroke(255);
  noStroke();
  fill(255,120,0);

  translate(0,0);
  for(int i=0; i< 6; i++) {
    rotateZ(radians(60));
    pushMatrix();
    rotateX(radians(-60));

    // to draw the mesh, use drawMesh and pass your mesh variable to it
    drawMesh(mesh);
    popMatrix();
  }

  popMatrix();
}

float [][][] createMesh(float px[],float py[],int numrot,
  float startDeg,float endDeg) {

  float deg,x,z;
  double cosval,sinval,tmp1,tmp2;

  float [][][] mesh=new float[numrot][px.length][3];
  endDeg-=startDeg; 

  int meshindex=0;
  for(int i=0; i< numrot; i++) {
    deg=radians(startDeg+(endDeg/(float)(numrot-1))*(float)i);
    for(int j=0; j< px.length; j++) {
      x=px[j];
      z=0;
      cosval=Math.cos(deg);
      sinval=Math.sin(deg);
      tmp1=x*cosval - z*sinval;
      tmp2=x*sinval + z*cosval;
      mesh[i][j][0]=(float)tmp1;
      mesh[i][j][1]=py[j];
      mesh[i][j][2]=(float)tmp2;
    }
  }

  return mesh;
}

void drawMesh(float mesh[][][]) {
  println(mesh.length+" "+mesh[0].length+" "+mesh[0][0].length);
  for(int i=0; i< mesh.length-1; i++) {
    beginShape(QUAD_STRIP);
    for(int j=0; j< mesh[0].length; j++) {
      vertex(mesh[i][j][0],mesh[i][j][1],mesh[i][j][2]);
      vertex(mesh[i+1][j][0],mesh[i+1][j][1],mesh[i+1][j][2]);
    }
    endShape();
  }
}

There is one comment to "Code: 3dmesh.pde". You may leave your own comment.
1. rider, March 11th, 2008 at 06:01

when copying the source via button
the “<” would be change into “< ”

happend at xubuntu and firefox 2.0.0.11

greets Michael

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>