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

 This program demonstrates applying a rotation to an object's
 texture coordinates, using the GL_TEXTURE transformation matrix.
 
 One important difference between this and texTranslate.cpp is
 that glLoadIdentity() is used to reset the transformation matrix
 on each frame, before applying the rotation.
  
*****************************************************************/
#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 to texture matrix mode.  Reset the matrix, then apply*/
/* a new rotation.  Return to ModelViw matrix mode.            */
/*                                                             */
    glMatrixMode(GL_TEXTURE);
     glLoadIdentity();
     glRotatef(20.0 * dms::currentTime(), 0, 0, 1);
    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();
    }
