/***********************
 * assignment2.c shell *
 *****************************************************************
 * written 1/13/95                                               *
 * by Andy Johnson                                               *
 *****************************************************************
 * this is the shell for the second EECS 488 Spring 96 assignment*
 * written in Mesa (OpenGL.)                                     *
 *                                                               *
 * this program has a VERY primitive window interface to try     *
 * and keep things as simple as possible, so while you can move  *
 * the window, trying to resize it will not do you much good     *
 *****************************************************************/
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "glaux.h"


/****************************************************************/

#define WIDTH  400 /* width of the window */
#define HEIGHT 300 /* height of the window  */

/* the borders of the clipping rectangle */

#define CLIPLEFT 10
#define CLIPRIGHT (WIDTH-10)
#define CLIPBOTTOM 10
#define CLIPTOP (HEIGHT-10)

/****************************************************************/

static void initializeGraphics(char *);
static void drawScene(void);
static void key_esc(void);
static void reshape(int, int);
static void display(void);

/****************************************************************
 * Global Variables
 * 
 * YES,  THESE ARE BAD, SHOULDN'T USE THEM, BAD FOR ALL KINDS OF
 * REASONS,  BUT THIS PROGRAM IS NASTY ENOUGH WITHOUT PASSING
 * ARRAYS AROUND,  AND HEY I HAVE A PHD SO I'M ALLOWED TO USE THEM.
 * 
 * YOU WILL PROBABLY WANT TO DECLARE SOME MATRICIES HERE
 ****************************************************************/


/****************************************************************
 * INITIALIZEGRAPHICS
 * 
 * function to initialize the graphics
 * 
 * input: windowName - name of window to be opened
 * no outputs
 ****************************************************************/

static void initializeGraphics(char * windowName)
    {
    /* create a single-buffered RGBA window of size WIDTH by HEIGHT
     * located at 0, 0. If this window can not be created then exit
     * the program
     */

    auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);
    auxInitPosition( 0,0, WIDTH, HEIGHT);
    if (auxInitWindow(windowName) == GL_FALSE)
	{
	fprintf(stderr, "could not open a window\n");
	auxQuit();
	}


    /* clear the background of the window to black and set up
     * the window for orthographics viewing. The window is opened
     * at a size of HEIGHT X WIDTH, you are responsible for clipping
     * to a rectangle 10 pixels smaller on each side than the window
     */

    glClearColor( 0.0, 0.0, 0.0, 0.0);
    glClear( GL_COLOR_BUFFER_BIT );
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( 0, WIDTH, 0, HEIGHT, -1.0, 1.0 );
    }
    
/****************************************************************/

static void drawScene(void)
    {
    int temp;
   
   /* clear the window to black
    * 
    */
     
    glClearColor( 0.0, 0.0, 0.0, 0.0);
    glClear( GL_COLOR_BUFFER_BIT );

   /* draw the x axis
    * 
    * YOU WILL NEED TO DEAL WITH THE VIEWING TRANSFORMATION
    * AND CLIPPING HERE
    * 
    * YOU MAY USE glColor3f() HERE
    * YOU MAY USE glBegin(GL_LINE_STRIP), glVertex2i(), glEnd() HERE
    */


   /* draw the y axis
    * 
    * YOU WILL NEED TO DEAL WITH THE VIEWING TRANSFORMATION
    * AND CLIPPING HERE
    * 
    * YOU MAY USE glColor3f() HERE
    * YOU MAY USE glBegin(GL_LINE_STRIP), glVertex2i(), glEnd() HERE
    */


   /* draw the polygon
    * 
    * YOU WILL NEED TO DEAL WITH THE OBJECT TRANSFORMATION, 
    * VIEWING TRANSFORMATION, AND CLIPPING HERE
    * 
    * YOU MAY USE glColor3f() HERE
    * YOU MAY USE glBegin(GL_LINE_STRIP), glVertex2i(), glEnd() HERE
    */
    

    /* force all unfinished graphics commands to execute
    * 
    */

    glFlush();
    }

/****************************************************************
 YOU WILL PROBABLY WANT TO WRITE SEVERAL FUNCTIONS HERE TO HANDLE
 THE VARIOUS POSSIBLE KEY-PRESSES
 ****************************************************************/


/****************************************************************
 * KEY_ESC
 * 
 * whenever the escape key is pressed while focus is on this
 * process' window the program will halt
 * 
 * no inputs
 * no outputs
 ****************************************************************/

static void key_esc()
    {
    auxQuit();
    }

/****************************************************************
 * RESHAPE
 * 
 * whenever the window is shrunk or expanded this function is
 * called to update the contents of the window. In this case this
 * function does nothing when the window is reshaped
 * 
 * inputs: new width and height of the window
 * no outputs
 ****************************************************************/

static void reshape( int width, int height )
    {
    /* not doing anything when the window is reshaped */
    }
    
/****************************************************************
 * DISPLAY
 * 
 * the function call within is called to update the 
 * contents of the window. In this case it calls the
 * function which draws the scene
 * 
 * no inputs
 * no outputs
 ****************************************************************/

static void display( void )
    {	
    drawScene();
    }

/****************************************************************
 * MAIN
 * 
 * 
 * inputs: command line arguments
 * outputs: 0 if program exited properly
 ****************************************************************/

int main( int argc, char **argv )
    {
    initializeGraphics(argv[0]);
 
    /* YOU WILL PROBABLY WANT SOME FUNCTION CALL HERE TO READ
     * IN THE VERTICES FROM THE DATA FILE. THE FUNCTION YOU WROTE
     * FOR THE FIRST ASSIGNMENT SHOULD WORK NICELY
     */
   

    /* display the window until the user kills it
     * 
     * THIS CODE ALLOWS INTERATION WITH THE WINDOWING SYSTEM
     * DECLARING WHICH FUNCTIONS SHOULD BE CALLED WHEN THE WINDOW
     * MUST BE UPDATED OR THERE IS INPUT FROM THE KEYBOARD OR MOUSE
     * 
     * here you can include: auxKeyFunc( AUX_?, functionName);
     * where ? is a key on the keyboard and functionName() is the
     *    function to call when that key is pressed.
     * 
     * ? includes a, b, c ... z,  A, B, C ... Z, UP, DOWN, LEFT, RIGHT, SPACE
     *
     * e.g. in the first homework "auxKeyFunc( AUX_SPACE, changeView );"
     * was used to make the spacebar the trigger for drawing the filled
     * polygon rather than the outline of the polygon.
     */
    
    auxExposeFunc(reshape);
    auxReshapeFunc(reshape);

    /* FUNCTION TO UPDATE THE CONTENTS OF THE WINDOW */

    auxMainLoop( display );
 
    return 0;
}



