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

  This program demonstrates the combination of multitexturing and
  lightmapping.  The first texture unit is used for a 'normal'
  color texture, while the second unit is used for a lightmap
  that will vary the brightness of the color from the first texture,
  to give a complex lighting effect.

  The scene consists of a wall with a window in it, which is 
  drawn normally, not using multitexturing, and a floor that is
  multitexured.  The color texture on the floor is a carpet image
  that is repeated several times.  The lightmap texture is a
  greyscale image constructed from the data for the wall; it is
  bright in the region that would receive light from the window,
  and dark elsewhere.

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

using namespace dms;

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 that will be combined on the floor. */
/*                                                             */
Texture2D floorTex("carpet.tif");
Texture2D shadowTex("slantedShadow.tif", GL_CLAMP);

/*************** CODE OF INTEREST ******************************/
/* 'wall' and 'wallTex' are an additional part of the scene    */
/* that is not multitextured.                                  */
/*                                                             */
Square wall(-10, -0.5, 10, 10, DMS_Z);
Texture2D wallTex("wall.tiff");


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, 3, 15);
    wall.setTexture(wallTex);
    wall.setTransparency(Transparency::AlphaTestZero);
    
    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();

    wall.drawAll();

/*************** CODE OF INTEREST ******************************/
/* Apply the two textures.                                     */
/*                                                             */
    floorTex.apply();
    glActiveTextureARB(GL_TEXTURE1_ARB);
    shadowTex.apply();
    glActiveTextureARB(GL_TEXTURE0_ARB);
    
/*************** CODE OF INTEREST ******************************/
/* Draw the square for the floor, with appropriate texture     */
/* coordinates for the two textures.                           */
/*                                                             */
    glBegin(GL_QUADS);
     glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 0);
     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0, 0);
     glVertex3f(-10, 0, 0);
     
     glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 20, 0);
     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1, 0);
     glVertex3f(10, 0, 0);
     
     glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 20, 10);
     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1, 1);
     glVertex3f(10, 0, 10);
     
     glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 10);
     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0, 1);
     glVertex3f(-10, 0, 10);

    glEnd();

/*************** CODE OF INTEREST ******************************/
/* Disable the two textures, so they don't affect the wall.    */
/*                                                             */
    glActiveTextureARB(GL_TEXTURE1_ARB);
    shadowTex.disable();
    glActiveTextureARB(GL_TEXTURE0_ARB);
    floorTex.disable();

    glutSwapBuffers();

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


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

