/*****************************************************************
  spheremap.cpp
  by Dave Pape
  30 Mar 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>

using namespace dms;

void createScene(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;
Texture2D contourTex("flowers.tif");
bool spin = true;


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, 8);
    light.setInfinitePosition(-1, 1, 2);
    createScene(root);
    
    glutMainLoop();
    return 0;
    }


void createScene(Object& root)
    {
    QuadricObject *ball = new QuadricObject;
    ball->makeSphere(1.0, 32, 16);
    ball->setMaterial(*(new Material(Color::White)));
    ball->setTexture(contourTex);
    root.attach(*ball);
    
    QuadricObject *cyl = new QuadricObject;
    cyl->makeCylinder(1.0, 1.0, 1.0, 32, 16, DMS_Y);
    cyl->setTexture(contourTex);
    SimpleTransform *xform = new SimpleTransform;
    xform->setTranslation(0, 1, 0);
    cyl->setTransform(*xform);
    root.attach(*cyl);
    
    QuadricObject *cone = new QuadricObject;
    cone->makeCone(1.0, 1.0, 32, 16, DMS_Y);
    cone->setTexture(contourTex);
    xform = new SimpleTransform;
    xform->setTranslation(0, 2, 0);
    cone->setTransform(*xform);
    root.attach(*cone);
    
    Square *square = new Square(-1, -2, 1, -1);
    square->setTexture(contourTex);
    root.attach(*square);
       
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    }


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

    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);

    if (spin)
        glRotatef(currentTime()*30, 1, 1, 1);
    root.drawAll();

    glDisable(GL_TEXTURE_GEN_S);
    glDisable(GL_TEXTURE_GEN_T);

    glutSwapBuffers();

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


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


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