// ### randface.C ###
// Raphael Clancy
// rafemonkey@yahoo.com
// makes a collection of squares
// with random size and orientation
// ### randface.C ###

#include <fstream.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   //check to make sure we have the right number of args
   if(argc != 2)
   {
      cerr << "Usage: randface <nuber of faces>" <<  endl;
      return(0);
   }

   //extract the correct number of faces from the args
   int faces = atoi(argv[1]);
   //seed the random number generator
   //I use the number of faces here,
   //so each object will be the same
   //as any other object withe same number of faces
   //this is cheesy, but I don't know how winders
   //deals with time() and I'm lazy
   srand(faces);

   //now we create a file and write header info to it
   ofstream fout;
   fout.open("mess.an8");
   if(fout.fail())
   {
      //file error
      cerr << "Unable to create mess.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;

   //whew! now we make the points... 4 at a time
   int temp = faces*4;
   while(temp > 0)
   {
      fout << "\t\t\t( ";
      fout << (rand()%100)-50 << " ";
      fout << (rand()%100)-50 << " ";
      fout << (rand()%100)-50 << " ";
      fout << ")" << endl;
      temp--;
   }
   fout << "\t\t}" << endl;

   //now we hook up the faces
   fout << "\t\tfaces {" << endl;
   temp = faces;
   int pt = 0;
   while(temp > 0)
   {
      fout << "\t\t\t4 0 0 -1 ( (" << pt << ") (" << (pt+1) << ") (" << (pt+2) << ") ("
              << (pt+3) <<  ") )" << endl;
      temp--;
      pt+=4;
   }
   fout << "\t\t}" << endl;
   fout << "\t}" << endl;
   fout << "}" << endl;

   //close up and go home
   fout.close();
   return(0);
}
