/*****************************************************************
  multitex0.cpp
  by Dave Pape
  2 April 2003

  This program is a preliminary example for multitexturing.  It
  does *not* use multitexturing, but instead attempts to apply
  two textures to a single polygon.  The result is that only
  the last texture that is bound will be used when drawing the
  square.

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

using namespace dms;

void createScene(Object& root);
void drawEverything(void);

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


PerspCamera camera;

/*************** CODE OF INTEREST ******************************/
/* Create the two textures.                                    */
/*                                                             */
Texture2D texture1("zebra.tif");
Texture2D texture2("macbeth.tif");

bool showTex1=true, showTex2=false;


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, 15);
    
    glutMainLoop();
    return 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();

    glColor4f(1, 1, 1, 1);
    
/*************** CODE OF INTEREST ******************************/
/* Apply one or both of the textures.                          */
/*                                                             */
    if (showTex1)
        texture1.apply();
    if (showTex2)
        texture2.apply();
    
    glBegin(GL_QUADS);
     glTexCoord2f(0, 0);
     glVertex3f(-5, -5, 0);
     glTexCoord2f(1, 0);
     glVertex3f(5, -5, 0);
     glTexCoord2f(1, 1);
     glVertex3f(5, 5, 0);
     glTexCoord2f(0, 1);
     glVertex3f(-5, 5, 0);
    glEnd();
    
    if (showTex1)
        texture1.disable();
    if (showTex2)
        texture2.disable();
    
    glutSwapBuffers();

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


void key(unsigned char k, int x, int y)
    {
    if (k == 27)
        exit(0);
/*************** CODE OF INTEREST ******************************/
/* Turn the textures on or off in response to keyboard input.  */
/* The '1' key toggles the first texture, the '2' key toggles  */
/* the second texture.                                         */
/*                                                             */
    else if (k == '1')
        showTex1 = !showTex1;
    else if (k == '2')
        showTex2 = !showTex2;
    }


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