/*****************************************************************
  object.cpp
  by Dave Pape
  18 Feb 2003

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

using namespace dms;

void drawEverything(void);

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

void drawTeapot(dms::Object&,void *data)
    {
    float * sizePtr = (float *)data;
    float size = *sizePtr;
    glutSolidTeapot(size);
    }

void updateTeapot(dms::Object&,void *data)
    {
    float * sizePtr = (float *)data;
    *sizePtr = sin(currentTime()) + 2;
    }

float teapotSize = 1;
float teapot2Size = 2;
PerspCamera camera;
Light light;
QuadricObject ball;
SimpleTransform xform;
Object teapot;
Object teapot2;
Square square;

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(0, 0, 1);

//    Material mat(Color::Yellow);
//    ball.setMaterial(mat);
    ball.setMaterial(*(new Material(Color::Yellow)));
    Texture2D tex("/home/dms/dave/data/Pixar/brick/blubrik.tif");
    ball.setTexture(tex);
    ball.setUseTexture(GL_TRUE);
    xform.setTranslation(0, 1, 0);
    ball.setTransform(xform);
    ball.makeSphere(0.2);

    square.setMaterial(*(new Material(Color::Grey90)));
    square.setTexture(tex);
    
    teapot.setMaterial(*(new Material(Color::Grey80)));
    teapot.setDrawCallback(drawTeapot, &teapotSize);
    
    SimpleTransform t2xform;
    t2xform.setTranslation(-2.0, 0.0, 0.0);
    teapot2.setTransform(t2xform);
    teapot2.setMaterial(*(new Material(Color::Grey80)));
    teapot2.setDrawCallback(drawTeapot, &teapot2Size);
    teapot2.setUpdateCallback(updateTeapot, &teapot2Size);
        
    glutMainLoop();
    return 0;
    }



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();

    ball.drawAll();
    square.drawAll();
    teapot.drawAll();
    teapot2.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)
    {
    xform.setTranslation(0, sin(currentTime()), 0);
    teapot2.updateAll();
    glutPostRedisplay();
    }

