topical media & game development

talk show tell print

graphic-processing-algorithm-Ch09-p226-MyFace.pde / pde



  class MyFace{
    int npoints = 0;
    MyPoint [] points;
    color c = color(255,255,255);
  
    MyFace (){
      points = new MyPoint[0];
    }
  
    MyFace(MyPoint[] inPoints){
      points = new MyPoint[inPoints.length];
      npoints = inPoints.length;
      for(int i=0; i<inPoints.length; i++)
        points[i] = new MyPoint(inPoints[i].x, inPoints[i].y, inPoints[i].z);
    }
  
    void addPoint(float addX, float addY, float addZ){
      npoints++;
      points = (MyPoint[])append(points, new MyPoint(addX,addY,addZ));
    }
  
    void setColor(color cin){
      c = cin;
      setShades(c);
      c = shadeTable[getShade()];
    }
    void draw(){
      fill(c);
      beginShape(POLYGON);
      for(int i = 0; i < npoints; i++){
        vertex(points[i].x,points[i].y, points[i].z);
      }
      endShape(CLOSE);
    }
  
    void scale(float xs, float ys, float zs, MyPoint ref){
      for(int i=0; i<npoints; i++)
        points[i].scale(xs, ys, zs, ref);
    }
  
    void move(float xoff, float yoff, float zoff){
      for(int i=0; i<npoints; i++)
        points[i].move(xoff, yoff, zoff);
    }
    void rotatex (float angle, MyPoint ref) {
      for(int i=0; i<npoints; i++)
        points[i].rotatex(angle, ref);
    }
  
    void rotatey (float angle, MyPoint ref) {
      for(int i=0; i<npoints; i++)
        points[i].rotatey(angle, ref);
    }
  
    void rotatez (float angle, MyPoint ref) {
      for(int i=0; i<npoints; i++)
        points[i].rotatez(angle, ref);
    }
  
    boolean isVisible()  {
      float x1, y1, x2, y2, norm=0;
      int ahead1, ahead2;
  
      for (int i=0; i<npoints; i++) {
        ahead1 = i+1;
        ahead2 = i+2;
        if(i == (npoints-2)) ahead2 = 0;
        if(i == (npoints-1)) { 
          ahead2=1; 
          ahead1=0; 
        }
        x1 = points[ahead1].xscreen() - points[i].xscreen();  //make vector 1
        y1 = points[ahead1].yscreen() - points[i].yscreen();  //make vector2
        x2 = points[ahead1].xscreen() - points[ahead2].xscreen();
        y2 = points[ahead1].yscreen() - points[ahead2].yscreen();
        norm += (x1*y2 - y1*x2);  //cross product
      }   
      if(norm > 0.0) return false;  //if clockwise
      else  return true;  //else ccw
    }
  
    color[] shadeTable;   //define a table array
    void setShades(color c) {
      float r, g, b;
      r = red(c);  //extract the color
      g = green(c);   
      b = blue(c);  
      r /= 255.;  //get a unit
      g /= 255.; 
      b /= 255.;
      shadeTable = new color[256];  //allocate memory
      for( int i = 0; i < 255; i++ )
        shadeTable[i] = color((int)(r*i),(int)(g*i),(int)(b*i));  //draw the shade
    }
  
    MyVector vlight = new MyVector(0.5, -1., 2.);
    int getShade(){
      int shade;
      MyVector v1 = new MyVector(); //
      MyVector v2 = new MyVector();
      vlight.norm();
      v1.buildVector( points[0], points[1] ).norm();
      v2.buildVector( points[1], points[2] ).norm();
      v1.cross(v2);
      if (v1.w < 0) {
        shade = 0 ;
      } 
      else {
        //shade = (int)( 100+(155*v1.dot(vlight)));
        shade = (int)( 255*v1.dot(vlight));
        println(shade);
        if (shade <= 0) shade = 0;
      }
      return shade;
    }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.