!---------------------------------------------------------------
!
! TUTORIAL24 - RayDance tutorial script #24.
!
! This script demonstrates replicas of imported objects.
!
! Concepts include :
!
!  o Replicas
!
!---------------------------------------------------------------

? "TUTORIAL24 - This script creates an asteroid field using\n",
  "an imported asteroid and lots of replicas.  Asteroids\n",
  "are located on a 3d grid, however they are 'jittered'\n",
  "so they don't all line up perfectly.\n\n";

! Variables
real  X,Y,Z,
      STEP = 500,
      QSTEP = STEP*0.5;

! Load the maps
asteroid_tm : texturemap( "asteroid.ilbm" );
asteroid_bm : bumpmap( "asteroid.ilbm" );

! Create surfaces for our stuff.
MATTE : surface(PHONG, 0.4,.8, 0, 0, 0,0,0, 0, 0 );

boundary

! Import an asteroid object.  Place it in a named boundary for
! future reference
vector pos1 = [0,0,0];
asteroid1 : boundary( pos1 )
  ast1 : IMPORT( "ast4_b.obj", pos1, [1,1,1]*0.2, [0,0,0],
           [1,1,1], 0 );
  txt1 : texture( SPHERE, asteroid_tm, pos1,
	   [0,0,1], [1,0,0], 1,1, SMOOTH3 );
  bmp1 : bump( SPHERE, asteroid_bm, MATTE, 5.0, pos1,
	   [0,0,1], [1,0,0], 1,1, NEGATIVE );
  ast1.color[0] = txt1;
  ast1.surface[0] = bmp1;
endboundary

randomize(100);     ! Set random number seed

? "Building asteroid matrix.  Relax this takes time...\n";

! Create a 9 x 9 x 9 grid of asteroids (729 of 'em)
X = -1000;
while (X <= 1000) {
  Y = -1000;
  while (Y <= 1000) {
    Z = -1000;
    while (Z <= 1000) {

! Make another replica placing it at a jittered grid position
! and rotating it by a random amount.  Make sure we don't
! overwrite our parent object!
      if (X <> 0 or Y <> 0 or Z <> 0) {
        replica( asteroid1,
          [X+RAND*QSTEP-50,Y+RAND*QSTEP-50,Z+RAND*QSTEP-50],
          [RAND*360,RAND*360,RAND*360] );
      }
      Z = Z + STEP;
    }
    Y = Y + STEP;
  }
  ? ".";
  X = X + STEP;
}
? "\nAll asteroids installed\n\n";
endboundary optimize


! Specify the lighting.
AMBIENT( [0,0,0], [0.6,0.6,0.6], [0,0,1], 0, 0 );
STAR( [40000,-50000,60000], [1,.9,1], 300 );

! A plain black background
BACKGROUND( PLAIN, [0,0,0] );

! The camera looks up the positive y axis
CAMERA'POS = [100,-2000,300];
CAMERA'TARGET = [-100,1000,-200];

RENDER;

END
