//
import render.*;
import java.awt.*;

public class Teapot extends RenderApplet
{
   Geometry teapot;
   Material opaque, transparent;

   public void initialize() {

     setBgColor(.5,.5,1);
     setFL(15);
     setFOV(.65);

     Material baseColor = new Material();
     baseColor.setColor(0,0,0, .8,0,0,10).setAmbient(.5,0,0);

     push();
	translate(0,.4,0);
	rotateX(.2);
	transform(world);
     pop();

     Geometry base = world.add().globe(50,20).superquadric(4,0).setMaterial(baseColor);
     push();
	translate(.1,-1.5,0);
	rotateX(Math.PI/2);
	scale(4,4,.5);
	transform(base);
     pop();

     opaque = new Material();
     opaque.setColor(.6,.4,.2, .8,.8,.8,5).setAmbient(.2,0,0).setDoubleSided(true);
     //opaque.setColor(.6,.4,.2, .8,.8,.8,5).setAmbient(.2,0,0);

     transparent = new Material();
     transparent.setColor(.3,.3,.3, 2,2,2,20).setAmbient(.4,0,0).setTransparency(.4);

     teapot = world.add();

     push();
        translate(0,-1,0);
        rotateX(-Math.PI/2);
        scale(.03,.03,.03);
        transform(teapot);
     pop();
     Geometry s;
     for (int i = 0 ; i < teapot_data.length ; i++) {
/*
         if (i == 2) {
	    s = teapot.add().setMaterial(transparent);
	    Geometry p = new Geometry().patch(4,4, Geometry.Bezier, teapot_data[i]);
	    double data[] = new double[4*4*3];
	    for (int I = 0 ; I < 4 ; I++)
	    for (int J = 0 ; J < 4 ; J++)
	       int m = 7, n = 7;
	       double v[][] = p.vertices;
	       for (int k = 0 ; k < 3 ; k++) {
	          data[  k] = v[(I+1)*m + (J+1)][k];
	          data[3+k] = v[(I+2)*m + (J+1)][k];
               }
	       s.add().patch(4,4, Geometry.Hermite, data);
	    }
	 }
	 else */ {
	    //int m = 16, n = i < 12 ? 32 : 16;
	    int m = 8, n = i < 12 ? 16 : 8;
	    s = teapot.add().patch(m,n, Geometry.Bezier, teapot_data[i]).setMaterial(opaque);
         }
	 if (i >= 24)
	    s.setMaterial(transparent);
     }

     addLight( 1, 0,-1, .25,.25,.5);
     addLight(-1, 0,-1, .25,.25,.5);
     addLight( 2,-.5,0, .3,.4,.3);
     addLight(-1, 0, 0, .3,.4,.3);
     addLight( 0,.5, 1, .3,.4,.3);
     addLight( 0,-1, 0, .2, 0, 0);
   }

   boolean isMorphing = false;
   double time = 0;
   public void animate(double time) {
      if (this.time == 0)
	 this.time = time;
      if (isMorphing) {
         teapot.noiseOrigin[0] += 100;
         teapot.displaceByImprovedNoise(.02, 3. * (time - this.time));
      }
      this.time = time;
   }

   boolean isTransparent[] = {false,false,false,false,false,false,true,true};
   int noiseLevel = 0;

   public boolean keyUp(Event e, int key) {
      if (key >= '1' && key <= '8') {
	 int k = key - '1';
	 for (int i = 4*k ; i < 4*k+1 ; i++)
	    teapot.child[i].setMaterial(isTransparent[k] ? opaque : transparent);
	 isTransparent[k] = !isTransparent[k];
	 damage();
	 return true;
      }
      switch (key) {
      case 'm':
	 isMorphing = ! isMorphing;
	 damage();
	 return true;
      case 'n':
	 switch (noiseLevel) {
	 case 0: teapot.displaceByImprovedNoise(.015, 20); break;
	 case 1: teapot.displaceByImprovedNoise(.03 , 10); break;
	 case 2: teapot.displaceByImprovedNoise(.06 ,  5); break;
	 }
	 noiseLevel++;
	 damage();
	 return true;
      case 'o':
	 renderer.seeMesh = ! renderer.seeMesh;
	 damage();
	 return true;
      }

      return super.keyUp(e, key);
   }

double teapot_data[][] = {
{ // 0
 -80.00,   0.00,  30.00,  -80.00, -44.80,  30.00,  -44.80, -80.00,  30.00,    0.00, -80.00,  30.00,
 -80.00,   0.00,  12.00,  -80.00, -44.80,  12.00,  -44.80, -80.00,  12.00,    0.00, -80.00,  12.00,
 -60.00,   0.00,   3.00,  -60.00, -33.60,   3.00,  -33.60, -60.00,   3.00,    0.00, -60.00,   3.00,
 -60.00,   0.00,   0.00,  -60.00, -33.60,   0.00,  -33.60, -60.00,   0.00,    0.00, -60.00,   0.00,
},{
  0.00, -80.00,  30.00,   44.80, -80.00,  30.00,   80.00, -44.80,  30.00,   80.00,   0.00,  30.00,
  0.00, -80.00,  12.00,   44.80, -80.00,  12.00,   80.00, -44.80,  12.00,   80.00,   0.00,  12.00,
  0.00, -60.00,   3.00,   33.60, -60.00,   3.00,   60.00, -33.60,   3.00,   60.00,   0.00,   3.00,
  0.00, -60.00,   0.00,   33.60, -60.00,   0.00,   60.00, -33.60,   0.00,   60.00,   0.00,   0.00,
},{
 -60.00,   0.00,  90.00,  -60.00, -33.60,  90.00,  -33.60, -60.00,  90.00,    0.00, -60.00,  90.00,
 -70.00,   0.00,  69.00,  -70.00, -39.20,  69.00,  -39.20, -70.00,  69.00,    0.00, -70.00,  69.00,
 -80.00,   0.00,  48.00,  -80.00, -44.80,  48.00,  -44.80, -80.00,  48.00,    0.00, -80.00,  48.00,
 -80.00,   0.00,  30.00,  -80.00, -44.80,  30.00,  -44.80, -80.00,  30.00,    0.00, -80.00,  30.00,
},{
  0.00, -60.00,  90.00,   33.60, -60.00,  90.00,   60.00, -33.60,  90.00,   60.00,   0.00,  90.00,
  0.00, -70.00,  69.00,   39.20, -70.00,  69.00,   70.00, -39.20,  69.00,   70.00,   0.00,  69.00,
  0.00, -80.00,  48.00,   44.80, -80.00,  48.00,   80.00, -44.80,  48.00,   80.00,   0.00,  48.00,
  0.00, -80.00,  30.00,   44.80, -80.00,  30.00,   80.00, -44.80,  30.00,   80.00,   0.00,  30.00,
},{
 -56.00,   0.00,  90.00,  -56.00, -31.36,  90.00,  -31.36, -56.00,  90.00,    0.00, -56.00,  90.00,
 -53.50,   0.00,  95.25,  -53.50, -29.96,  95.25,  -29.96, -53.50,  95.25,    0.00, -53.50,  95.25,
 -57.50,   0.00,  95.25,  -57.50, -32.20,  95.25,  -32.20, -57.50,  95.25,    0.00, -57.50,  95.25,
 -60.00,   0.00,  90.00,  -60.00, -33.60,  90.00,  -33.60, -60.00,  90.00,    0.00, -60.00,  90.00,
},{ // 5
  0.00, -56.00,  90.00,   31.36, -56.00,  90.00,   56.00, -31.36,  90.00,   56.00,   0.00,  90.00,
  0.00, -53.50,  95.25,   29.96, -53.50,  95.25,   53.50, -29.96,  95.25,   53.50,   0.00,  95.25,
  0.00, -57.50,  95.25,   32.20, -57.50,  95.25,   57.50, -32.20,  95.25,   57.50,   0.00,  95.25,
  0.00, -60.00,  90.00,   33.60, -60.00,  90.00,   60.00, -33.60,  90.00,   60.00,   0.00,  90.00,
},{
 80.00,   0.00,  30.00,   80.00,  44.80,  30.00,   44.80,  80.00,  30.00,    0.00,  80.00,  30.00,
 80.00,   0.00,  12.00,   80.00,  44.80,  12.00,   44.80,  80.00,  12.00,    0.00,  80.00,  12.00,
 60.00,   0.00,   3.00,   60.00,  33.60,   3.00,   33.60,  60.00,   3.00,    0.00,  60.00,   3.00,
 60.00,   0.00,   0.00,   60.00,  33.60,   0.00,   33.60,  60.00,   0.00,    0.00,  60.00,   0.00,
},{
  0.00,  80.00,  30.00,  -44.80,  80.00,  30.00,  -80.00,  44.80,  30.00,  -80.00,   0.00,  30.00,
  0.00,  80.00,  12.00,  -44.80,  80.00,  12.00,  -80.00,  44.80,  12.00,  -80.00,   0.00,  12.00,
  0.00,  60.00,   3.00,  -33.60,  60.00,   3.00,  -60.00,  33.60,   3.00,  -60.00,   0.00,   3.00,
  0.00,  60.00,   0.00,  -33.60,  60.00,   0.00,  -60.00,  33.60,   0.00,  -60.00,   0.00,   0.00,
},{
 60.00,   0.00,  90.00,   60.00,  33.60,  90.00,   33.60,  60.00,  90.00,    0.00,  60.00,  90.00,
 70.00,   0.00,  69.00,   70.00,  39.20,  69.00,   39.20,  70.00,  69.00,    0.00,  70.00,  69.00,
 80.00,   0.00,  48.00,   80.00,  44.80,  48.00,   44.80,  80.00,  48.00,    0.00,  80.00,  48.00,
 80.00,   0.00,  30.00,   80.00,  44.80,  30.00,   44.80,  80.00,  30.00,    0.00,  80.00,  30.00,
},{
  0.00,  60.00,  90.00,  -33.60,  60.00,  90.00,  -60.00,  33.60,  90.00,  -60.00,   0.00,  90.00,
  0.00,  70.00,  69.00,  -39.20,  70.00,  69.00,  -70.00,  39.20,  69.00,  -70.00,   0.00,  69.00,
  0.00,  80.00,  48.00,  -44.80,  80.00,  48.00,  -80.00,  44.80,  48.00,  -80.00,   0.00,  48.00,
  0.00,  80.00,  30.00,  -44.80,  80.00,  30.00,  -80.00,  44.80,  30.00,  -80.00,   0.00,  30.00,
},{ // 10
 56.00,   0.00,  90.00,   56.00,  31.36,  90.00,   31.36,  56.00,  90.00,    0.00,  56.00,  90.00,
 53.50,   0.00,  95.25,   53.50,  29.96,  95.25,   29.96,  53.50,  95.25,    0.00,  53.50,  95.25,
 57.50,   0.00,  95.25,   57.50,  32.20,  95.25,   32.20,  57.50,  95.25,    0.00,  57.50,  95.25,
 60.00,   0.00,  90.00,   60.00,  33.60,  90.00,   33.60,  60.00,  90.00,    0.00,  60.00,  90.00,
},{
  0.00,  56.00,  90.00,  -31.36,  56.00,  90.00,  -56.00,  31.36,  90.00,  -56.00,   0.00,  90.00,
  0.00,  53.50,  95.25,  -29.96,  53.50,  95.25,  -53.50,  29.96,  95.25,  -53.50,   0.00,  95.25,
  0.00,  57.50,  95.25,  -32.20,  57.50,  95.25,  -57.50,  32.20,  95.25,  -57.50,   0.00,  95.25,
  0.00,  60.00,  90.00,  -33.60,  60.00,  90.00,  -60.00,  33.60,  90.00,  -60.00,   0.00,  90.00,
},{
 -64.00,   0.00,  75.00,  -64.00,  12.00,  75.00,  -60.00,  12.00,  84.00,  -60.00,   0.00,  84.00,
 -92.00,   0.00,  75.00,  -92.00,  12.00,  75.00, -100.00,  12.00,  84.00, -100.00,   0.00,  84.00,
   -108.00,   0.00,  75.00, -108.00,  12.00,  75.00, -120.00,  12.00,  84.00, -120.00,   0.00,  84.00,
   -108.00,   0.00,  66.00, -108.00,  12.00,  66.00, -120.00,  12.00,  66.00, -120.00,   0.00,  66.00,
},{
 -60.00,   0.00,  84.00,  -60.00, -12.00,  84.00,  -64.00, -12.00,  75.00,  -64.00,   0.00,  75.00,
   -100.00,   0.00,  84.00, -100.00, -12.00,  84.00,  -92.00, -12.00,  75.00,  -92.00,   0.00,  75.00,
   -120.00,   0.00,  84.00, -120.00, -12.00,  84.00, -108.00, -12.00,  75.00, -108.00,   0.00,  75.00,
   -120.00,   0.00,  66.00, -120.00, -12.00,  66.00, -108.00, -12.00,  66.00, -108.00,   0.00,  66.00,
},{
   -108.00,   0.00,  66.00, -108.00,  12.00,  66.00, -120.00,  12.00,  66.00, -120.00,   0.00,  66.00,
   -108.00,   0.00,  57.00, -108.00,  12.00,  57.00, -120.00,  12.00,  48.00, -120.00,   0.00,  48.00,
   -100.00,   0.00,  39.00, -100.00,  12.00,  39.00, -106.00,  12.00,  31.50, -106.00,   0.00,  31.50,
 -80.00,   0.00,  30.00,  -80.00,  12.00,  30.00,  -76.00,  12.00,  18.00,  -76.00,   0.00,  18.00,
},{ // 15
   -120.00,   0.00,  66.00, -120.00, -12.00,  66.00, -108.00, -12.00,  66.00, -108.00,   0.00,  66.00,
   -120.00,   0.00,  48.00, -120.00, -12.00,  48.00, -108.00, -12.00,  57.00, -108.00,   0.00,  57.00,
   -106.00,   0.00,  31.50, -106.00, -12.00,  31.50, -100.00, -12.00,  39.00, -100.00,   0.00,  39.00,
 -76.00,   0.00,  18.00,  -76.00, -12.00,  18.00,  -80.00, -12.00,  30.00,  -80.00,   0.00,  30.00,
},{
 68.00,   0.00,  51.00,   68.00,  26.40,  51.00,   68.00,  26.40,  18.00,   68.00,   0.00,  18.00,
 104.00,   0.00,  51.00,  104.00,  26.40,  51.00,  124.00,  26.40,  27.00,  124.00,   0.00,  27.00,
 92.00,   0.00,  78.00,   92.00,  10.00,  78.00,   96.00,  10.00,  75.00,   96.00,   0.00,  75.00,
 108.00,   0.00,  90.00,  108.00,  10.00,  90.00,  132.00,  10.00,  90.00,  132.00,   0.00,  90.00,
},{
 68.00,   0.00,  18.00,   68.00, -26.40,  18.00,   68.00, -26.40,  51.00,   68.00,   0.00,  51.00,
 124.00,   0.00,  27.00,  124.00, -26.40,  27.00,  104.00, -26.40,  51.00,  104.00,   0.00,  51.00,
 96.00,   0.00,  75.00,   96.00, -10.00,  75.00,   92.00, -10.00,  78.00,   92.00,   0.00,  78.00,
 132.00,   0.00,  90.00,  132.00, -10.00,  90.00,  108.00, -10.00,  90.00,  108.00,   0.00,  90.00,
},{
 108.00,   0.00,  90.00,  108.00,  10.00,  90.00,  132.00,  10.00,  90.00,  132.00,   0.00,  90.00,
 112.00,   0.00,  93.00,  112.00,  10.00,  93.00,  141.00,  10.00,  93.75,  141.00,   0.00,  93.75,
 116.00,   0.00,  93.00,  116.00,   6.00,  93.00,  138.00,   6.00,  94.50,  138.00,   0.00,  94.50,
 112.00,   0.00,  90.00,  112.00,   6.00,  90.00,  128.00,   6.00,  90.00,  128.00,   0.00,  90.00,
},{
 132.00,   0.00,  90.00,  132.00, -10.00,  90.00,  108.00, -10.00,  90.00,  108.00,   0.00,  90.00,
 141.00,   0.00,  93.75,  141.00, -10.00,  93.75,  112.00, -10.00,  93.00,  112.00,   0.00,  93.00,
 138.00,   0.00,  94.50,  138.00,  -6.00,  94.50,  116.00,  -6.00,  93.00,  116.00,   0.00,  93.00,
 128.00,   0.00,  90.00,  128.00,  -6.00,  90.00,  112.00,  -6.00,  90.00,  112.00,   0.00,  90.00,
},{ // 20
 50.00,   0.00,  90.00,   50.00,  28.00,  90.00,   28.00,  50.00,  90.00,    0.00,  50.00,  90.00,
 52.00,   0.00,  90.00,   52.00,  29.12,  90.00,   29.12,  52.00,  90.00,    0.00,  52.00,  90.00,
 54.00,   0.00,  90.00,   54.00,  30.24,  90.00,   30.24,  54.00,  90.00,    0.00,  54.00,  90.00,
 56.00,   0.00,  90.00,   56.00,  31.36,  90.00,   31.36,  56.00,  90.00,    0.00,  56.00,  90.00,
},{
  0.00,  50.00,  90.00,  -28.00,  50.00,  90.00,  -50.00,  28.00,  90.00,  -50.00,   0.00,  90.00,
  0.00,  52.00,  90.00,  -29.12,  52.00,  90.00,  -52.00,  29.12,  90.00,  -52.00,   0.00,  90.00,
  0.00,  54.00,  90.00,  -30.24,  54.00,  90.00,  -54.00,  30.24,  90.00,  -54.00,   0.00,  90.00,
  0.00,  56.00,  90.00,  -31.36,  56.00,  90.00,  -56.00,  31.36,  90.00,  -56.00,   0.00,  90.00,
},{
 -50.00,   0.00,  90.00,  -50.00, -28.00,  90.00,  -28.00, -50.00,  90.00,    0.00, -50.00,  90.00,
 -52.00,   0.00,  90.00,  -52.00, -29.12,  90.00,  -29.12, -52.00,  90.00,    0.00, -52.00,  90.00,
 -54.00,   0.00,  90.00,  -54.00, -30.24,  90.00,  -30.24, -54.00,  90.00,    0.00, -54.00,  90.00,
 -56.00,   0.00,  90.00,  -56.00, -31.36,  90.00,  -31.36, -56.00,  90.00,    0.00, -56.00,  90.00,
},{
  0.00, -50.00,  90.00,   28.00, -50.00,  90.00,   50.00, -28.00,  90.00,   50.00,   0.00,  90.00,
  0.00, -52.00,  90.00,   29.12, -52.00,  90.00,   52.00, -29.12,  90.00,   52.00,   0.00,  90.00,
  0.00, -54.00,  90.00,   30.24, -54.00,  90.00,   54.00, -30.24,  90.00,   54.00,   0.00,  90.00,
  0.00, -56.00,  90.00,   31.36, -56.00,  90.00,   56.00, -31.36,  90.00,   56.00,   0.00,  90.00,
},{
  8.00,   0.00, 102.00,    8.00,   4.48, 102.00,    4.48,   8.00, 102.00,    0.00,   8.00, 102.00,
 16.00,   0.00,  96.00,   16.00,   8.96,  96.00,    8.96,  16.00,  96.00,    0.00,  16.00,  96.00,
 52.00,   0.00,  96.00,   52.00,  29.12,  96.00,   29.12,  52.00,  96.00,    0.00,  52.00,  96.00,
 52.00,   0.00,  90.00,   52.00,  29.12,  90.00,   29.12,  52.00,  90.00,    0.00,  52.00,  90.00,
},{ // 25
  0.00,   8.00, 102.00,   -4.48,   8.00, 102.00,   -8.00,   4.48, 102.00,   -8.00,   0.00, 102.00,
  0.00,  16.00,  96.00,   -8.96,  16.00,  96.00,  -16.00,   8.96,  96.00,  -16.00,   0.00,  96.00,
  0.00,  52.00,  96.00,  -29.12,  52.00,  96.00,  -52.00,  29.12,  96.00,  -52.00,   0.00,  96.00,
  0.00,  52.00,  90.00,  -29.12,  52.00,  90.00,  -52.00,  29.12,  90.00,  -52.00,   0.00,  90.00,
},{
 -8.00,   0.00, 102.00,   -8.00,  -4.48, 102.00,   -4.48,  -8.00, 102.00,    0.00,  -8.00, 102.00,
 -16.00,   0.00,  96.00,  -16.00,  -8.96,  96.00,   -8.96, -16.00,  96.00,    0.00, -16.00,  96.00,
 -52.00,   0.00,  96.00,  -52.00, -29.12,  96.00,  -29.12, -52.00,  96.00,    0.00, -52.00,  96.00,
 -52.00,   0.00,  90.00,  -52.00, -29.12,  90.00,  -29.12, -52.00,  90.00,    0.00, -52.00,  90.00,
},{
  0.00,  -8.00, 102.00,    4.48,  -8.00, 102.00,    8.00,  -4.48, 102.00,    8.00,   0.00, 102.00,
  0.00, -16.00,  96.00,    8.96, -16.00,  96.00,   16.00,  -8.96,  96.00,   16.00,   0.00,  96.00,
  0.00, -52.00,  96.00,   29.12, -52.00,  96.00,   52.00, -29.12,  96.00,   52.00,   0.00,  96.00,
  0.00, -52.00,  90.00,   29.12, -52.00,  90.00,   52.00, -29.12,  90.00,   52.00,   0.00,  90.00,
},{
  0.00,   0.00, 120.00,    0.00,   0.00, 120.00,    0.00,   0.00, 120.00,    0.00,   0.00, 120.00,
 32.00,   0.00, 120.00,   32.00,  18.00, 120.00,   18.00,  32.00, 120.00,    0.00,  32.00, 120.00,
  0.00,   0.00, 108.00,    0.00,   0.00, 108.00,    0.00,   0.00, 108.00,    0.00,   0.00, 108.00,
  8.00,   0.00, 102.00,    8.00,   4.48, 102.00,    4.48,   8.00, 102.00,    0.00,   8.00, 102.00,
},{
  0.00,   0.00, 120.00,    0.00,   0.00, 120.00,    0.00,   0.00, 120.00,    0.00,   0.00, 120.00,
  0.00,  32.00, 120.00,  -18.00,  32.00, 120.00,  -32.00,  18.00, 120.00,  -32.00,   0.00, 120.00,
  0.00,   0.00, 108.00,    0.00,   0.00, 108.00,    0.00,   0.00, 108.00,    0.00,   0.00, 108.00,
  0.00,   8.00, 102.00,   -4.48,   8.00, 102.00,   -8.00,   4.48, 102.00,   -8.00,   0.00, 102.00,
},{ // 30
  0.00,   0.00, 120.00,    0.00,   0.00, 120.00,    0.00,   0.00, 120.00,    0.00,   0.00, 120.00,
 -32.00,   0.00, 120.00,  -32.00, -18.00, 120.00,  -18.00, -32.00, 120.00,    0.00, -32.00, 120.00,
  0.00,   0.00, 108.00,    0.00,   0.00, 108.00,    0.00,   0.00, 108.00,    0.00,   0.00, 108.00,
 -8.00,   0.00, 102.00,   -8.00,  -4.48, 102.00,   -4.48,  -8.00, 102.00,    0.00,  -8.00, 102.00,
},{
  0.00,   0.00, 120.00,    0.00,   0.00, 120.00,    0.00,   0.00, 120.00,    0.00,   0.00, 120.00,
  0.00, -32.00, 120.00,   18.00, -32.00, 120.00,   32.00, -18.00, 120.00,   32.00,   0.00, 120.00,
  0.00,   0.00, 108.00,    0.00,   0.00, 108.00,    0.00,   0.00, 108.00,    0.00,   0.00, 108.00,
  0.00,  -8.00, 102.00,    4.48,  -8.00, 102.00,    8.00,  -4.48, 102.00,    8.00,   0.00, 102.00,
}
};

}

//