/*****************************************************************
  txfText.cpp
  by Dave Pape
  22 April 2003

*****************************************************************/
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <dms/dms.h>
#include "TexFont.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;
OrthoCamera orthocamera(0, 1, 0, 1);
Light light;

TexFont * txfFont;
int oneCharWidth=1;


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, 40);
    light.setInfinitePosition(1, 1, 1);
    txfFont = txfLoadFont("lucida.txf");
//    txfFont = txfLoadFont("default.txf");
    txfEstablishTexture(txfFont, 0, GL_TRUE);
    int ascent, descent;
    txfGetStringMetrics(txfFont, "X", 1, &oneCharWidth, &ascent, &descent);
    
    glutMainLoop();
    return 0;
    }


void drawEverything(void)
    {
    float y = fabs(sin(currentTime()/2)) * 20;
    char str[256];
    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();
    glPushMatrix();
     glTranslatef(0, y, 0);
     glEnable(GL_LIGHTING);
     glutSolidTeapot(1);
     glDisable(GL_LIGHTING);
    glPopMatrix();
     
    glEnable(GL_TEXTURE_2D);
//    glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GEQUAL, 0.5);
    glPushMatrix();
     glRotatef(currentTime()*20, 0, 1, 0);
     glTranslatef(2, y, 0);
     glScalef(0.025, 0.025, 0.025);
     sprintf(str, "Y : %0.1f", y);
     glColor3f(0, 0, 0);
     txfRenderString(txfFont, str, strlen(str));
    glPopMatrix();


    orthocamera.apply();

    sprintf(str, "time : %.2f", currentTime());
    glColor3f(1, 1, 1);
    glPushMatrix();
     glTranslatef(0.01, 0.01, 0);
     glScalef(0.05/oneCharWidth, 0.05/oneCharWidth, 0.05/oneCharWidth);
     txfRenderString(txfFont, str, strlen(str));
    glPopMatrix();

    glColor3f(0.5, 0.4, 0.2);
    sprintf(str, "teapot height : %.2f",  y);
    glPushMatrix();
     glTranslatef(0.01, 0.08, 0);
     glScalef(0.05/oneCharWidth, 0.05/oneCharWidth, 0.05/oneCharWidth);
     txfRenderString(txfFont, str, strlen(str));
    glPopMatrix();

    glDisable(GL_TEXTURE_2D);
    glDisable(GL_ALPHA_TEST);
    glDisable(GL_BLEND);

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