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

 An fmod program using 3D sounds; similar to bergen1.cpp
 
 This plays a boing sound whenever the ball bounces, at the position
 of the ball.  It also plays a continuously looping squeak sound,
 at the position of the teapot.
 The FMOD listener position is continually updated using the
 current camera position.

 One important thing to be aware of: fmod uses a left-handed
 coordinate system, as opposed to OpenGL's right-handed system.
 Therefore, we need to invert the Z value of all 3D positions
 & vectors that we pass to fmod.
  
*****************************************************************/
#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;

    
FSOUND_SAMPLE *samplesound1, *samplesound2;

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

   
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, 1024, FSOUND_INIT_USEDEFAULTMIDISYNTH))
        {
        printf("Error: %s\n", FMOD_ErrorString(FSOUND_GetError()));
        exit(1);
        }

    samplesound1 = FSOUND_Sample_Load(FSOUND_UNMANAGED, "boing.wav",
                                     FSOUND_NORMAL, 0);
    if (!samplesound1)
        {
        printf("Error: %s\n", FMOD_ErrorString(FSOUND_GetError()));
        FSOUND_Close();
        exit(1);
        }
    FSOUND_Sample_SetMode(samplesound1, FSOUND_LOOP_OFF);
/*************** CODE OF INTEREST ******************************/
/* Set Min/Max distance for spatialization, to control how     */
/* quickly the sound amplitude falls off with distance.        */
/*                                                             */
    FSOUND_Sample_SetMinMaxDistance(samplesound1, 5.0, 1000.0);    

    samplesound2 = FSOUND_Sample_Load(FSOUND_UNMANAGED, "squeak.wav",
                                     FSOUND_NORMAL, 0);
    if (!samplesound2)
        {
        printf("Error: %s\n", FMOD_ErrorString(FSOUND_GetError()));
        FSOUND_Close();
        exit(1);
        }
    FSOUND_Sample_SetMode(samplesound2, FSOUND_LOOP_NORMAL);
    FSOUND_Sample_SetMinMaxDistance(samplesound2, 1.0, 1000.0);    
    int channel = FSOUND_PlaySound(FSOUND_FREE, samplesound2);
/*************** CODE OF INTEREST ******************************/
/* Set a position for the squeak sound, using the teapot's     */
/* transformation data.                                        */
/*                                                             */
    dms::Vector3 pos = teapotTransform.translation();
    pos[2] = -pos[2];
    FSOUND_3D_SetAttributes(channel, pos.vec, NULL);
    }
    
   
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 *)
    {
    static float prevY = 0;
    float y = sin(dms::currentTime()) * 3.0;
    ballTransform.setTranslation(-10.0, fabs(y), -15.0);
    if (((y > 0) && (prevY < 0)) || ((y < 0) && (prevY > 0)))
        {
        int channel = FSOUND_PlaySound(FSOUND_FREE, samplesound1);
/*************** CODE OF INTEREST ******************************/
/* Set a position for the boing sound, using the ball's        */
/* transformation data.                                        */
/*                                                             */
        dms::Vector3 pos = ballTransform.translation();
        pos[2] = -pos[2];
        FSOUND_3D_SetAttributes(channel, pos.vec, NULL);
        }
    prevY = y;
    }


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)
        {
        FSOUND_Close();
        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)
    {
/*************** CODE OF INTEREST ******************************/
/* Update the Listener data, using the position & vectors from */
/* the camera.  Note that FSOUND_Update() has to be called for */
/* this change to have an effect.                              */
/*                                                             */
    dms::Vector3 position, forward, up;
    position = camera.position();
    forward = camera.forward();
    up = camera.up();
    position[2] = -position[2];
    forward[2] = -forward[2];
    up[2] = -up[2];
    FSOUND_3D_Listener_SetAttributes(position.vec, NULL,
                forward[0], forward[1], forward[2], up[0], up[1], up[2]);
    FSOUND_Update();
    floormesh.updateAll();
    glutPostRedisplay();
    }
