/*****************************************************************
  fmod0.cpp
  by Dave Pape
  25 Feb 2003

 An example of using fmod for playing sounds.
 
 This program plays a 'boing' sound whenever the ball bounces.
 It also loads two MP3 streams for background music, and switches
 between them (and silence) whenever the spacebar is hit.  It
 uses the Pause-Channel feature to pause each MP3 wherever it
 currently is, rather than stopping them and restarting them at
 the beginning each time.
 
*****************************************************************/
#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 <fmod.h>
#include <fmod_errors.h>

void initializeSound(void);
void drawEverything(void);
void updateBall(dms::Object&,void*);
void drawFloorMesh(dms::Object&,void*);
void drawTeapot(dms::Object&,void*);
void updateTeapot(dms::Object&,void*);

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


dms::PerspCamera camera;
dms::Light light;

dms::Vector4 green(0, 0.8, 0.1, 1);
dms::Vector4 red(0.8, 0, 0, 1);
dms::Vector4 white(1, 1, 1, 1);
dms::Vector4 grey(0.6, 0.6, 0.6, 1);
dms::Vector4 cyan(0, 1, 1, 1);
    
dms::Material floorMaterial(green);
dms::Material teapotMaterial(red, white, 90);
dms::Material pedestalMaterial(grey, white, 30);
dms::Material ballMaterial(cyan);

dms::Texture2D texture("texture.tif");

dms::SimpleTransform ballTransform;
dms::SimpleTransform pedestalTransform;
dms::SimpleTransform pedestalCapTransform;
dms::SimpleTransform teapotTransform;

dms::QuadricObject ball;
dms::QuadricObject pedestal;
dms::QuadricObject pedestalCap;
dms::Object teapot;
dms::Object floormesh;

    
/*************** CODE OF INTEREST ******************************/
/* The variables for our sample sound, and the channels where  */
/* our MP3s will play.                                         */
/*                                                             */
FSOUND_SAMPLE *samplesound;
int mp3channel[3] = { -1, -1 -1 };
int currentMP3 = 0;

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

    floormesh.setMaterial(floorMaterial);
    floormesh.setDrawCallback(drawFloorMesh, (void*)32);

    pedestal.makeCylinder(4.0, 4.0, 4.0, 16, 1, DMS_Y);
    pedestal.setMaterial(pedestalMaterial);
    pedestalTransform.setTranslation(2.0, -2.0, -1.0);
    pedestal.setTransform(pedestalTransform);
    
    pedestalCap.makeDisk(0.0, 4.0, 16, 1, DMS_Y);
    pedestalCap.setMaterial(pedestalMaterial);
    pedestalCapTransform.setTranslation(4 * dms::Vector3::Y_Axis);
    pedestalCap.setTransform(pedestalCapTransform);
    
    teapot.setMaterial(teapotMaterial);
    teapot.material().diffuse()[3] = 0.75;
    teapot.setTexture(texture);
    teapot.setTransparency(dms::Transparency::StandardBlend);
    teapotTransform.setTranslation(5.5 * dms::Vector3::Y_Axis);
    teapot.setTransform(teapotTransform);
    teapot.setDrawCallback(drawTeapot);
    teapot.setUpdateCallback(updateTeapot);
    
    ball.makeSphere(2.0, 16, 8);
    ball.setMaterial(ballMaterial);
    ballTransform.setTranslation(-10.0, 0.0, -15.0);
    ball.setTransform(ballTransform);
    ball.setUpdateCallback(updateBall);

    floormesh.attach(ball);
    floormesh.attach(pedestal);
    pedestal.attach(pedestalCap);
    pedestal.attach(teapot);

    initializeSound();

    glutMainLoop();
    return 0;
    }


/*************** CODE OF INTEREST ******************************/
/* Initialize fmod.                                            */
/* Create an FMOD sample-sound object for the 'boing' sound.   */
/* Create stream-sound objects for the two MP3s, and start     */
/* them playing, in a paused state.                            */
/* Only the channel numbers for the stream sounds are made     */
/* global, because that's all that we need in order to control */
/* them, once they've been started.                            */
/*                                                             */
void initializeSound(void)
    {
    if (FSOUND_GetVersion() < FMOD_VERSION)
        {
        printf("Error: You have the wrong FMOD version -"
               " you should be using FMOD %.02f\n", FMOD_VERSION);
        exit(1);
        }

    FSOUND_SetOutput(FSOUND_OUTPUT_OSS);

    if (!FSOUND_Init(44100, 4, FSOUND_INIT_USEDEFAULTMIDISYNTH))
        {
        printf("Error: %s\n", FMOD_ErrorString(FSOUND_GetError()));
        exit(1);
        }

    samplesound = FSOUND_Sample_Load(FSOUND_UNMANAGED, "boing.wav",
                                     FSOUND_NORMAL, 0);
    if (!samplesound)
        {
        printf("Error: %s\n", FMOD_ErrorString(FSOUND_GetError()));
        FSOUND_Close();
        exit(1);
        }
    FSOUND_Sample_SetMode(samplesound, FSOUND_LOOP_OFF);
    
    FSOUND_STREAM *mp3sound;
    mp3sound = FSOUND_Stream_OpenFile("tarotplane.mp3",
                                    FSOUND_NORMAL, 0);
    if (!mp3sound)
        {
        printf("Error: %s\n", FMOD_ErrorString(FSOUND_GetError()));
        FSOUND_Close();
        exit(1);
        }
    mp3channel[1] = FSOUND_Stream_PlayEx(FSOUND_FREE, mp3sound, NULL, 1);
    mp3sound = FSOUND_Stream_OpenFile("wtc.mp3",
                                    FSOUND_NORMAL, 0);
    if (!mp3sound)
        {
        printf("Error: %s\n", FMOD_ErrorString(FSOUND_GetError()));
        FSOUND_Close();
        exit(1);
        }
    mp3channel[2] = FSOUND_Stream_PlayEx(FSOUND_FREE, mp3sound, NULL, 1);
    }
    
   
void drawFloorMesh(dms::Object&,void* data)
    {
    int resolution = (int)data;
    int i,j;
    GLfloat x,z;
    glNormal3f(0.0, 1.0, 0.0);
    for (j = 0; j < resolution; j++)
        {
        glBegin(GL_TRIANGLE_STRIP);
        for (i = 0; i < resolution; i++)
            {
            x = (((float)i) / resolution) * 40.0 - 20.0;
            z = (((float)j) / resolution) * 40.0 - 20.0;
            glVertex3f(x, -2.0, z);
            z = (((float)j+1) / resolution) * 40.0 - 20.0;
            glVertex3f(x, -2.0, z);
            }
        glEnd();
        }
    }


void drawTeapot(dms::Object&,void *)
    {
    glutSolidTeapot(2.0);
    }


void updateTeapot(dms::Object& object,void *)
    {
    dms::SimpleTransform& xform = (dms::SimpleTransform&)object.transform();
    xform.setRotation(dms::currentTime() * 30.0, 0.0, 1.0, 0.0);
    }


void updateBall(dms::Object&,void *)
    {
/*************** CODE OF INTEREST ******************************/
/* Compute the new position of the ball.  Remember the old     */
/* position of from the previous update, and when we detect    */
/* that it has bounced, play the bounce sound.                 */
/*                                                             */
    static float prevY = 0;
    float y = sin(dms::currentTime()) * 3.0;
    if (((y > 0) && (prevY < 0)) || ((y < 0) && (prevY > 0)))
        FSOUND_PlaySound(FSOUND_FREE, samplesound);
    prevY = y;
    ballTransform.setTranslation(-10.0, fabs(y), -15.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();
    glEnable(GL_LIGHTING);
    light.apply();
    floormesh.drawAll();
    glDisable(GL_LIGHTING);

    glutSwapBuffers();

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


void key(unsigned char k, int x, int y)
    {
    if (k == 27)
        {
/*************** CODE OF INTEREST ******************************/
/* Call FSOUND_Close() before exiting, to make sure it shuts   */
/* down cleanly.                                               */
/*                                                             */
        FSOUND_Close();
        exit(0);
        }
/*************** CODE OF INTEREST ******************************/
/* Whenever the spacebar is hit, switch the MP3 stream that is */
/* playing - pause the current one (if any), increment the     */
/* 'currentMP3' number, and start the new one (if any)         */
/*                                                             */
    else if (k == ' ')
        {
        if (mp3channel[currentMP3] > -1)
            FSOUND_SetPaused(mp3channel[currentMP3], 1);
        currentMP3 = (currentMP3 + 1) % 3;
        if (mp3channel[currentMP3] > -1)
            FSOUND_SetPaused(mp3channel[currentMP3], 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)
    {
    floormesh.updateAll();
    glutPostRedisplay();
    }

