/*****************************************************************
  anim-planet.cpp
  by Dave Pape
  16 March 2003

  This program demonstrates a flipbook animation of textures.
  It loads a sequence of texture images (the planet*.tif images
  from ftp.cs.tu-berlin.de), and applies them to a Square object;
  the texture that is used is changed on each frame.  The flipbook
  runs at a rate of one cycle through all the images per second.

*****************************************************************/
#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 createScene(Object& root);
void drawEverything(void);
void updateTexture(dms::Object& object,void *data);

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

/*************** CODE OF INTEREST ******************************/
/* Define an array of textures, to form the flipbook.          */
/*                                                             */
#define NUM_TEXTURES 12
Texture2D * textures[NUM_TEXTURES];


PerspCamera camera;
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, 5);
    createScene(root);
    
    glutMainLoop();
    return 0;
    }


/*************** CODE OF INTEREST ******************************/
/* Create the set of texture objects, and load a different     */
/* image into each one.  Create the object that the textures   */
/* will be applied to, and apply the first texture.            */
/*                                                             */
void createScene(Object& root)
    {
    Square *square = new Square;
    int i;
    int *textureNum = new int(0);
    for (i=0; i < NUM_TEXTURES; i++)
        {
        char filename[256];
        sprintf(filename,"planet/planet%d.tif",i);
        textures[i] = new Texture2D(filename);
        }
    square->setTexture(*textures[*textureNum]);
    square->setUpdateCallback(updateTexture, textureNum);
    root.attach(*square);
    }


/*************** CODE OF INTEREST ******************************/
/* Animate the texture.  This function determines the number of*/
/* the next texture to apply (based on the time), and applies  */
/* it to the object.                                           */
/*                                                             */
void updateTexture(dms::Object& object,void *data)
    {
    int * textureNum = (int *)data;
    *textureNum = (int)(fmod(currentTime(),1.0) * NUM_TEXTURES);
    object.setTexture(*textures[*textureNum]);
    }


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();
    
    Color::White.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();
    }
