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

 This program is an example of a GLUT program converted to a
 CAVElib one.
 The world is that of example4, from class 11.

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


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[])
    {
/*************** CODE OF INTEREST ******************************/
/* Call the CAVE functions, in place of GLUT functions.        */
/*                                                             */
    CAVEConfigure(&argc,argv,NULL);
    CAVEInit();  
    
    CAVEInitApplication(initGraphics,0);  
    CAVEFrameFunction(updateData,0);  
    CAVEDisplay(drawEverything,0);
    
/*************** CODE OF INTEREST ******************************/
/* Here we have to explicitly loop, & watch for the Escape     */
/* key, rather than calling glutMainLoop() and expecting it    */
/* to never return.                                            */
/*                                                             */
    while (!CAVEgetbutton(CAVE_ESCKEY))
        usleep(10000);
    CAVEExit();
    }


/*************** CODE OF INTEREST ******************************/
/* This initialization code had to be moved into a function    */
/* that is passed to CAVEInitApplication(), rather than        */
/* running it directly in main().                              */
/*                                                             */
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);
    }


/*************** CODE OF INTEREST ******************************/
/* In the draw function, we removed the camera code, and the   */
/* glutSwapBuffers call.                                       */
/*                                                             */
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);
    
    glEnable(GL_LIGHTING);
    light.apply();
    floormesh.drawAll();
    glDisable(GL_LIGHTING);

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




/*************** CODE OF INTEREST ******************************/
/* This update function is roughly equivalent to GLUT's idle   */
/* function.                                                   */
/*                                                             */
void updateData(void)
    {
    floormesh.updateAll();
    }

