!---------------------------------------------------------------
!
! TUTORIAL16 - RayDance tutorial script #16.
!
! This script demonstrates polygon mesh creation.
!
! Concepts include :
!
!  o Using the MESH statement to create polygon meshes.
!
!  o Using the PERTURB statement to change the position of the
!    mesh vertices.
!
!---------------------------------------------------------------

! Use a print statement to display descriptive text on the
! message window.


? "TUTORIAL16 - This script creates a polygon mesh using the\n",
  "MESH statement.  It then tweaks the vertices with a\n",
  "cosine function whose amplitude decreases as with the\n",
  "distance from its center\n\n",
  "Relax, this script can take a while to execute...\n\n";


! More steps makes a better picture but takes much longer and
! uses more memory.

integer   STEPS = 30;


! Create colors for our objects

WHITE  : color(RGB, [1,1,1] );

! Create surfaces 
!                ka kd ks  n km kr ir kb flgs
MATTE     :
  surface(PHONG, .3,.8, 0, 0, 0, 0, 0, 0, 0 );


vector   CENTER,
         POS;

real     DIST,
         AMPLITUDE,
         WAVELEN = 50;

integer  X,
         Y;


! Create two polygon meshes.  Each mesh will be 200 units of
! length on each side and contain 400 vertices (20x20)

CENTER = [0,0,0];

OUR_MESH : mesh(
  [-100,100,0]+CENTER, [100,100,0]+CENTER,    ! corners 1 & 2
  [100,-100,0]+CENTER, [-100,-100,0]+CENTER,  ! corners 3 & 4
  STEPS,STEPS,                  ! number of rows and columns
  WHITE, MATTE, PHONG );            ! color, surface, flags

! Now we're going to adjust the height of each point in the
! meshes making a sine wave ripple out from corner 2 of
! mesh 1.

Y = 0;
while (Y < OUR_MESH'ROWS) {

  X = 0;
  while (X < OUR_MESH'COLS) {

! Get coordinates of this point
    
    POS = OUR_MESH.POINT[X,Y]'POS;

! Length of vector from our point to center

    DIST = | POS - CENTER |;

! Amplitude is inversely proportional to distance plus a
! constant

    AMPLITUDE = 100 / (2 + DIST * .1 );

! and the phase of the cosine wave

    AMPLITUDE = AMPLITUDE * COS( DIST * 360 / WAVELEN);

    OUR_MESH.POINT[X,Y]'POS = POS + [0,0,AMPLITUDE];

    X = X + 1;
  }
  Y = Y + 1;
}


! Specify the ambient light.

!       always 0's  lamp color   dir.    k1  k2

ambient( [0,0,0], [0.6,0.6,0.6], [0,0,1], 0, 0 );


! Specify the STAR light.

!         position        lamp color rad  flags

star( [3000,-30000,40000], [1,.9,1], 300 );


! Set the background color to a dark purple

BACKGROUND( PLAIN, [0,0.05,0.15] );


! The camera will be positioned along the negative y axis aiming
! toward the central objects

CAMERA'POS = [0,-300,400];
CAMERA'TARGET = [0,0,0];


! The scene has now been constructed, render it!

RENDER;


! All scripts must terminate with an END

END
