////////////////////////////////////////////////////////////////////////////
//Tom Valesky
//CS-752
//Dr. Chen, instructor
//main.cpp -- main() function, OpenGL init routine, display function
///////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include "glhdr.h"
#include "point.h"
#include "explosion.h"
#include "sceneobject.h"
#include "scene.h"
#include "util.h"

Scene *myScene;

float camera_x = 40.0, camera_y = 40.0, camera_z = 40.0;
char *camera_filename = "camera.txt";

void display(void)
{
        int more;
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();

   more = myScene->move();
   if (!more)
        {
        printf("points all gone\n");
		delete (myScene);
        exit(0);
        }
   myScene->draw();
   myScene->check_intersection();
   glFlush();
   auxSwapBuffers();
}

void read_camera_position(void)
{
        FILE *fp = NULL;
        char buf[BUFSIZE];

        fp = fopen(camera_filename, "r");
        if (fp == NULL)
                {
                printf("Can't open file %s to read\n", camera_filename);
                exit(0);
                }

	    fgets(buf, BUFSIZE - 1, fp);
        sscanf(buf, "%f %f %f", &camera_x, &camera_y, &camera_z);
        printf("camera position: %f %f %f\n", &camera_x, &camera_y, &camera_z);




		fclose(fp);
        

}


void myinit(void)
{

//   GLfloat light_position[] = {10.0, 10.0, 10.0, 0.0};
//   GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
//   GLfloat mat_shininess[] = {50.0};

   glClearColor(0.0, 0.0, 0.0, 1.0);
   //glColor3f(1.0, 1.0, 1.0);
   read_camera_position();


   //lighting
//   light_position[0] = camera_x;
//   light_position[1] = camera_y;
//   light_position[2] = camera_z;
//   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
//   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//   glEnable(GL_LIGHTING);
//   glEnable(GL_LIGHT0);

   //shading
   glShadeModel(GL_SMOOTH);

   //fog
  // glEnable(GL_FOG);

	//depth test
   glDepthFunc(GL_LEQUAL);
   glEnable(GL_DEPTH_TEST);

	//blending (for fading)
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);

   printf("creating new scene\n");
   myScene = new Scene();
}

void myReshape(GLsizei w, GLsizei h)
{

   glViewport (0, 0, w, h);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity();
   if (w<=h)
      {
      glOrtho(-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w,
         50.0*(GLfloat)w/(GLfloat)h, -100.0, 100.0);
      }
   else
      {
      glOrtho(50.0*(GLfloat)w/(GLfloat)h, 50.0*(GLfloat)w/(GLfloat)h,
         -50.0, 50.0, -100.0, 100.0);
      }
   gluLookAt(camera_x, camera_y, camera_z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}


int WINAPI WinMain(
    HINSTANCE hInstance,	// handle to current instance
    HINSTANCE hPrevInstance,	// handle to previous instance
    LPSTR lpCmdLine,	// pointer to command line
    int nCmdShow 	// show state of window
   )
{
   FILE *fp = fopen ("output.txt", "w");
   if (fp == NULL)
   {
	   exit(0);
   }
   *stdout = *fp; //set stdout to point to log file

   auxInitDisplayMode(AUX_DOUBLE | AUX_RGBA |AUX_DEPTH);
   auxInitPosition(0, 0, 600, 600);
   auxInitWindow("Explosion Simulator");
   myinit();


   auxReshapeFunc((AUXRESHAPEPROC)myReshape);
   auxIdleFunc((AUXIDLEPROC)display);
   auxMainLoop((AUXMAINPROC)display);
   return 1;
}
