Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Three Dee Tree

27. Oktober 2011

Den rekursiven Baum aus Lektion 5 habe ich umgeschrieben für den dreidimensionalen Raum. Ich musste eine Ast-Klase erstellen, damit die Koordinaten gespeichert werden können. Sonst würde der Baum in jedem Frame wieder neu zufällig dargestellt. Die Äste wachsen nach oben in X, Y und Z-Richtung. Mit den Up- und Down-Tasten kann die Tiefe einstellen, wie viele Äste erstellt werden. Mit A erstellt man einen neuen Baum, und mit S speicher man einen Screenshot ab.
<pre>import peasy.test.*;
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
import processing.opengl.*;

Limb mainLimb;
PeasyCam camera;
int maxDepth = 5;

void setup()
{
  size(600,700, OPENGL);      // def. fenstergroesse
  hint(DISABLE_DEPTH_TEST);

  randomSeed(millis());  // seed random

  strokeWeight(1);    // linienbreite
  stroke(255,150);

  createLimb();

  camera = new PeasyCam(this, width/2, height/2, 300, 1000);

  PFont font;
  font = loadFont("DIN-Light-16.vlw");
  textFont(font, 16);
  textMode(SCREEN);
}

void draw()
{
  background(0);
  lights();

  pushMatrix();
  translate(0,height,0);
  rotateX(radians(90));
  pushStyle();
  strokeWeight(0);
  fill(255,255,255,50);
  ellipse(300,300,800,800);
  popStyle();
  popMatrix();

  pushMatrix();
  translate(width/2,height,width/2);
  mainLimb.display();
  popMatrix();

  text("Mouse - Move Camera\nA - Generate new tree\nS - Save Screenshot\nUp / Down - Change Depth", 20, height-80);
  text("Depth: " + (maxDepth+1), 500,height-30);
}

void createLimb() {
   mainLimb = new Limb(50,10,5,0,0,-150,0);
}

class Limb {
  int xPos, yPos, zPos, depth, limbCount, nextLimbs, flowerSize, i=0;
  ArrayList myLimbs = new ArrayList();
  float radius, xPosNew, yPosNew, zPosNew;

  Limb(float r, int f, int l, int d, int x, int y, int z){
     depth = d;
     radius = r;
     xPosNew = x;
     yPosNew = y;
     zPosNew = z;
     limbCount = l;
     flowerSize = f;
     for(int i=0; i<limbCount; i++){
       if(depth < maxDepth){
         nextLimbs = int(random(4));
       }else{
         nextLimbs = 0;
       }
       myLimbs.add(new Limb(random(360),int(random(10,25)), nextLimbs, depth+1, int(random(-150,150)), int(random(-50,-300)), int(random(-200,200))));
     }
   }

  void display() {
     xPosNew += sin(radius)*0;
     yPosNew += cos(radius)*0;
     strokeWeight(maxDepth+2-depth);
     line(0,0,0,xPosNew,yPosNew,zPosNew);
     strokeWeight(1);
     pushMatrix();
     pushStyle();
     fill(xPosNew*2,255,255,200);
     noStroke();
     translate(xPosNew, yPosNew, zPosNew);
     rotate(radians(radius));
     beginShape();
     vertex(-flowerSize/2,0);
     vertex(flowerSize/2,0);
     vertex(0,-flowerSize);
     vertex(-flowerSize/2,0);
     endShape();
     pushMatrix();
     rotateY(radians(90));
     beginShape();
     vertex(-flowerSize/2,0);
     vertex(flowerSize/2,0);
     vertex(0,-flowerSize);
     vertex(-flowerSize/2,0);
     endShape();
     popMatrix();
     popStyle();
     popMatrix();
     for(int i=0; i < myLimbs.size(); i++){
       Limb limb = (Limb) myLimbs.get(i);
       pushMatrix();
       translate(xPosNew, yPosNew, zPosNew);
       limb.display();
       popMatrix();
     }
   }

}

void keyPressed()
{
  switch(key)
  {
  case 's':
    save("screenshot" + millis() + ".png");
    break;
  case 'a':
    createLimb();
    break;
  }
  if(key == CODED){
     if(keyCode == UP && maxDepth<15){
       maxDepth++;
       createLimb();
     }else if(keyCode == DOWN && maxDepth>0){
       maxDepth--;
       createLimb();
     }
   }
}