import java.awt.Color;
import java.io.*;
import java.util.*;

/**
 * A driver that can be used to test the Rasterizer3D class
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class PA3Driver
{
    /**
     * The entry point of the application
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args) throws Exception
    {
       BufferedReader   in;       
       Color[]          colors;       
       double           phi, theta;
       double[][]       coords, p;       
       FrameBuffer      fb;       
       GraphicsWindow   window;
       int[][]          vertices;       
       Rasterizer3D     rasterizer;
       String           line, name;
       StringTokenizer  st;
       TriFileReader    reader;
       
       name = null;
       if ((args != null)    && (args.length > 0) && 
           (args[0] != null) && (!args[0].equals("")))
       {
          name = args[0];          
       }
       
       // Construct the window
       window = new GraphicsWindow();

       // Construct the rasterizer
       fb = window.getFrameBuffer();
       rasterizer = new Rasterizer3D(fb);

       // Read the file
       reader = new TriFileReader(name);
       coords = reader.getCoordinates();
       colors = reader.getFrontColors();
       vertices = reader.getTriangles();

       // Scale and translate the coordinates
       p = rasterizer.scaleAndTranslate(coords);
          

       // Prompt for input and rasterize
       in = new BufferedReader(new InputStreamReader(System.in));       
       System.out.println("Enter the x and y rotation angles: ");
       System.out.flush();
       
       while ((line = in.readLine()) != null)
       {
          st = new StringTokenizer(line,", \t");          
          try
          {
             phi   = Double.parseDouble(st.nextToken());
             theta = Double.parseDouble(st.nextToken());
          }
          catch (Exception e)
          {
             phi   = 0.0;
             theta = 0.0;             
          }
          
          
          fb.clear(Color.GRAY);          
          rasterizer.useTrimetricView(phi, theta);
          rasterizer.draw(colors, vertices, p);
          fb.show();

          System.out.println("Enter the x and y rotation angles: " );
          System.out.flush();
       }       
    }
}
