/*****************************************************************
  texTranslate.cpp
  by Dave Pape
  22 March 2003

 This program demonstrates applying a translation to an object's
 texture coordinates, using the GL_TEXTURE transformation matrix.
 
 It draws a square with a texture applied.  The texture coordinates
 are (0,0), (1,0), (1,1), and (0,1); their values remain constant.
 The texture transformation matrix continually changes, causing the
 texture to scroll across the surface of 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 drawSquare(void);
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;
Light light;
Texture2D texture("mrblpapr.tif");

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);
    light.setInfinitePosition(-5, 4, 1);
    
    glutMainLoop();
    return 0;
    }


void drawEverything(void)
    {
    dms::beginFrame();
    glClearColor(0.5, 0.7, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    
    camera.apply();
    
    light.apply();

    drawSquare();

    glutSwapBuffers();

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


void drawSquare(void)
    {
/*************** CODE OF INTEREST ******************************/
/* Switch matrix modes, to tell OpenGL that we are now applying*/
/* texture transformations.  Do the translation.  Then switch  */
/* back to ModelView matrix mode, so that it is left in a state*/
/* that other code would expect.                               */
/* As with ModelView, the texture transformation matrix        */
/* accumulates the results of everything applied to it (unless */
/* we call glLoadIdentity()).                                  */
/*                                                             */
    glMatrixMode(GL_TEXTURE);
     glTranslatef(0.001, 0.0005, 0);
    glMatrixMode(GL_MODELVIEW);
    
    texture.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();
    texture.disable();
    }


void key(unsigned char k, int x, int y)
    {
    if (k == 27)
        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)
    {
    glutPostRedisplay();
    }
