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

 This program creates an animation of fire, using a flipbook
 sequence of textures with alpha transparency.

*****************************************************************/
#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);

#define NUM_TEXTURES 84
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 scene.  This consists of a 'log' - a cylinder    */
/* with a normal, static texture applied to it, and an attached*/
/* Square object that the animated texture is applied to.      */
/* Alpha-blended transparency is applied to the fire object,   */
/* to make use of the alpha channel in the texture images.     */
/*                                                             */
void createScene(Object& root)
    {
    QuadricObject *log = new QuadricObject;
    log->makeCylinder(0.25, 0.25, 2.5, 12, 1, DMS_X);
    log->setTexture(*(new Texture2D("figsepel.tif")));
    log->setUseTexture(GL_TRUE);
    SimpleTransform *logxform = new SimpleTransform;
    logxform->setTranslation(-1.25, -1, 0);
    log->setTransform(*logxform);
    root.attach(*log);
    
    Square *square = new Square;
    int i;
    int *textureNum = new int(0);
    for (i=0; i < NUM_TEXTURES; i++)
        {
        char filename[256];
        sprintf(filename,"fire/woodfire%02d.sgi",i);
        textures[i] = new Texture2D(filename,GL_CLAMP);
        }
    square->setTexture(*textures[*textureNum]);
    square->setUpdateCallback(updateTexture, textureNum);
    square->setTransparency(Transparency::StandardBlend);
    SimpleTransform *xform = new SimpleTransform;
    xform->setTranslation(1.25, 1.25, 0);
    square->setTransform(*xform);
    log->attach(*square);
    }


void updateTexture(dms::Object& object,void *data)
    {
    int * textureNum = (int *)data;
    *textureNum = (int)(fmod(currentTime(),4.0)/4.0 * NUM_TEXTURES);
    object.setTexture(*textures[*textureNum]);
    }


void drawEverything(void)
    {
    glClearColor(0.0, 0.0, 0.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();
    }
