/*****************************************************************
  navigate.cpp
  by Dave Pape
  22 Feb 2003

 This program is an example of navigation in a CAVElib program.
 It is built from cave1.cpp, just adding CAVENav function calls.

 The navigation controls are:
    - pushing the joystick forward moves you in the direction
    that the wand is pointing
    - pushing the joystick backward moves you the opposite direction
    - pushing the joystick left or right turns you in that
    direction

*****************************************************************/
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#include <cave_ogl.h>
#include <GL/gl.h>
#include <dms/dms.h>

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

void initGraphics(void);
void updateData(void);
void navigate(void);


dms::PerspCamera camera;
dms::OrthoCamera orthoCamera;
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;


int main(int argc, char *argv[])
    {
    CAVEConfigure(&argc,argv,NULL);
    CAVEInit();  
    
    CAVEInitApplication(initGraphics,0);  
    CAVEFrameFunction(updateData,0);  
    CAVEDisplay(drawEverything,0);
    
    while (!CAVEgetbutton(CAVE_ESCKEY))
        usleep(10000);
    CAVEExit();
    }


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

   
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 *)
    {
    ballTransform.setTranslation(-10.0, fabs(sin(dms::currentTime())) * 3.0, -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);
    
/*************** CODE OF INTEREST ******************************/
/* Call CAVENavTransform() to apply the navigation             */
/* transformation to everything that's drawn.                  */
/*                                                             */
    CAVENavTransform();

    glEnable(GL_LIGHTING);
    light.apply();
    floormesh.drawAll();
    glDisable(GL_LIGHTING);

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


void updateData(void)
    {
    floormesh.updateAll();
    navigate();
    }


#define SPEED 5.0f  /* Max navigation speed in feet per second */
    
/*************** CODE OF INTEREST ******************************/
/* navigate() performs the navigation calculations.  If the    */
/* joystick is being pushed forward or back, we get the current*/
/* direction that the wand is pointing, and do a translation   */
/* in that direction.  The amount of translation is scaled by  */
/* the joystick value, so that pushing harder moves the user   */
/* faster.                                                     */
/* If the joystick is pushed left or right, then we rotate     */
/* around the Y axis (also scaled by the joystick value).      */
/*                                                             */
void navigate(void)
    {
    float jx=CAVE_JOYSTICK_X, jy=CAVE_JOYSTICK_Y;
    float dt, t;
    static float prevtime = 0;
    t = CAVEGetTime();
    dt = t - prevtime;
    prevtime = t;
    if (fabs(jy)>0.2)
        {
        float wandFront[3];
        CAVEGetVector(CAVE_WAND_FRONT, wandFront);
        CAVENavTranslate(wandFront[0]*jy*SPEED*dt, wandFront[1]*jy*SPEED*dt,
                         wandFront[2]*jy*SPEED*dt);
        }
    if (fabs(jx)>0.2)
        CAVENavRot(-jx*90.0f*dt, 'y');
    }
