// ### ground.C ###
// Raphael Clancy
// rafemonkey@yahoo.com
// This program makes a plane
// which is perturbed (bumpy) on the
// Z axis.
// ### ground.C ###

#include <fstream.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
  if(argc != 3)
  {
    cout << "Usage: ground <size> <roughness>" << endl;
    return(0);
  }

  //extract the size and roughness from argv[]
  int size = atoi(argv[1]);
  int rough = atoi(argv[2]);

  //seed the random number generator
  //this time I'm going to try it like
  //it works on unix, and see what happens
  srand(time(NULL));

  //now the file stuff
  ofstream fout;
   fout.open("ground.an8");
   if(fout.fail())
   {
      //file error
      cerr << "Unable to create ground.an8" << endl;
      return(0);
   }
   //write the header
   fout << "header {" << endl;
   fout << "\tversion { \"0.62\" }" << endl;
   fout << "\tbuild { \"2000.11.19\" }" << endl;
   fout << "}" << endl;
   fout << "object { \"object01\"" << endl;
   fout << "\tmesh {" << endl;
   fout << "\t\tname { \"mesh01\" }" << endl;
   fout << "\t\tbase {" << endl;
   fout << "\t\t\torigin { (0 0 0) }" << endl;
   fout << "\t\t}" << endl;
   fout << "\t\tmaterial { \" -- default --\" }" << endl;
   fout << "\t\tsmoothangle { 45 }" << endl;
   fout << "\t\tmateriallist {" << endl;
   fout << "\t\t\tmaterialname { \" -- default --\" }" << endl;
   fout << "\t\t}" << endl;
   fout << "\t\tpoints{" << endl;

   //now whe spit out the points
   int x = 0;
   int y = 0;
   while(x < size)
   {
     while(y < size)
     {
       fout << "\t\t\t( ";
       fout << x * 10 << " ";
       fout << y * 10 << " ";
       fout << (rand()%rough) << " ";
       fout << ")" << endl;
       y++;
     }
     x++;
     y = 0;
   }
   fout << "\t\t}" << endl;

   //now we hook up the points to faces
   //need to make triangle faces so we can have
   //planar sufaces
   fout << "\t\tfaces {" << endl;
   //recycle x and y
   x = 0;
   y = 0;
   while(x < (size-1))
   {
     while(y < (size-1))
     {
        fout << "\t\t\t3 1 0 -1 ( (" << y+(size*x) << ") (" << y+(size*x)+size
          << ") (" << y+(size*x)+size+1 << ") )" << endl;
        fout << "\t\t\t3 1 0 -1 ( (" << y+(size*x) << ") (" << y+(size*x)+1
          << ") (" << y+(size*x)+size+1 << ") )" << endl;
        y++;
     }
     y = 0;
     x++;
   }
   fout << "\t\t}" << endl;
   fout << "\t}" << endl;
   fout << "}" << endl;

   //close up and go home
   fout.close();
   return(0);
}
