/*****************************************************************
  parse.cpp
  by Dave Pape
  14 April 2003

*****************************************************************/
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <dms/dms.h>

using namespace dms;

void createScene(char *,Object& root);
void parseLine(char *line, Object& root);
void drawEverything(void);

void key(unsigned char k, int x, int y);
void specialkey(int k, int x, int y);
void idle(void);


PerspCamera camera;
Light light;
Object root;

int main(int argc, char *argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(512,512);
    glutCreateWindow(argv[0]);
    
    glutDisplayFunc(drawEverything);
    glutKeyboardFunc(key);
    glutSpecialFunc(specialkey);
    glutIdleFunc(idle);
    
    camera.setPosition(0, 0, 10);
    light.setInfinitePosition(-5, 4, 1);
    if (argc > 1)
        createScene(argv[1], root);
    else
        {
        cerr << "You must provide the name of a scene file\n";
        exit(1);
        }
    
    glutMainLoop();
    return 0;
    }


void createScene(char * filename, Object& root)
    {
    FILE *fp = fopen(filename, "r");
    if (!fp)
        {
        cerr << "Failed to open file: " << filename << endl;
        perror(filename);
        exit(1);
        }
    char line[512];
    while (fgets(line, sizeof(line), fp))
        parseLine(line, root);
    fclose(fp);
    }


void parseLine(char *line, Object& root)
    {
    if (line[0] == '#')
        return;
    char command[256];
    if (sscanf(line, "%s", command) < 1)
        return;
    if (strcasecmp(command, "sphere") == 0)
        {
        // 'sphere' command format:
        //    sphere x y z radius r g b texture
        // (texture is optional)
        float x, y, z, radius, r, g, b;
        char textureName[256];
        textureName[0] = 0;
        sscanf(line, "%s %f %f %f %f %f %f %f %s", command, &x, &y, &z, &radius,
                &r, &g, &b, textureName);
        QuadricObject * sphere = new QuadricObject;
        sphere->makeSphere(radius, 16, 12);
        SimpleTransform * xform = new SimpleTransform;
        xform->setTranslation(x, y, z);
        sphere->setTransform(*xform);
        Material *mat = new Material;
        mat->setAmbientAndDiffuse(Vector4(r, g, b, 1));
        sphere->setMaterial(*mat);
        if (textureName[0] != 0)
            {
            Texture2D * texture = new Texture2D(textureName);
            sphere->setTexture(*texture);
            sphere->setUseTexture(GL_TRUE);
            }
        root.attach(*sphere);
        }
    else if (strcasecmp(command, "square") == 0)
        {
        //  square  minx miny  maxx maxy  r g b
        float minx, miny, maxx, maxy, r, g, b;
        sscanf(line, "%s %f %f %f %f %f %f %f", command, &minx, &miny,
                &maxx, &maxy, &r, &g, &b);
        Square * square;
        square = new Square(minx, miny, maxx, maxy, DMS_Y);
        Material *mat = new Material;
        mat->setAmbientAndDiffuse(Vector4(r, g, b, 1));
        square->setMaterial(*mat);
        root.attach(*square);
        }
    else
        cout << "Unrecognized command: " << command << endl;
    }


void drawEverything(void)
    {
    glClearColor(0.5, 0.7, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    
    camera.apply();
    
    light.apply();

    root.drawAll();

    glutSwapBuffers();

    checkGLError("end-of-frame");
    }


void key(unsigned char k, int x, int y)
    {
    if (k == 27)
        exit(0);
    }


void specialkey(int k, int x, int y)
    {
    if (k == GLUT_KEY_LEFT)
        camera.turn(3);
    else if (k == GLUT_KEY_RIGHT)
        camera.turn(-3);
    else if (k == GLUT_KEY_UP)
        camera.pitch(2);
    else if (k == GLUT_KEY_DOWN)
        camera.pitch(-2);
    else if (k == GLUT_KEY_HOME)
        camera.moveForward(0.25);
    else if (k == GLUT_KEY_END)
        camera.moveForward(-0.25);
    else if (k == GLUT_KEY_PAGE_UP)
        camera.zoom(-1);
    else if (k == GLUT_KEY_PAGE_DOWN)
        camera.zoom(1);
    }


void idle(void)
    {
    root.updateAll();
    glutPostRedisplay();
    }
