/*****************************************************************
  forest.cpp
  by Dave Pape
  4 Mar 2003

 This program compares a uniform versus gaussian random distribution.
 It creates a forest of trees, as in forest.cpp.  The placement
 of the trees can either be uniformly distributed, or in a
 gaussian distribution - press the 'u' and 'g' keys to switch
 between the two options.

*****************************************************************/
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <dms/dms.h>
#include "Square.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);

#define FOREST_SIZE 40.0

#define NUM_TREES 25

PerspCamera camera;
Object root;
Object * trees[NUM_TREES];

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, 20, 50);
    createScene(root);
    
    glutMainLoop();
    return 0;
    }


/*************** CODE OF INTEREST ******************************/
/* Uniform distribution.  This function is the same as         */
/* positionTreesRandomly in forest.cpp.                        */
/*                                                             */
void positionTreesUniformly(void)
    {
    for (int i=0; i < NUM_TREES; i++)
        {
        float x = drand48() * FOREST_SIZE - FOREST_SIZE/2.0;
        float z = drand48() * FOREST_SIZE - FOREST_SIZE/2.0;
        ((SimpleTransform&)trees[i]->transform()).setTranslation(x,6,z);
        }
    }

/*************** CODE OF INTEREST ******************************/
/* Gaussian distribution.  This function picks the X & Z values*/
/* by adding together three random numbers, to give a rough    */
/* gaussian (bell curve) distribution, centered around (0,0).  */
/*                                                             */
void positionTreesGaussianly(void)
    {
    for (int i=0; i < NUM_TREES; i++)
        {
        float x = (drand48() + drand48() + drand48()) / 3.0;
        float z = (drand48() + drand48() + drand48()) / 3.0;
        x = x * FOREST_SIZE - FOREST_SIZE/2.0;
        z = z * FOREST_SIZE - FOREST_SIZE/2.0;
        ((SimpleTransform&)trees[i]->transform()).setTranslation(x,6,z);
        }
    }


void createScene(Object& root)
    {
    Texture2D *grassTex = new Texture2D("grass.jpg");
    Texture2D *treeTex = new Texture2D("tree.sgi");
    Object * ground, * tree;
    SimpleTransform * xform;
    ground = new Square;
    ground->setTexture(*grassTex);
    xform = new SimpleTransform;
    xform->setRotation(-90.0, 1, 0, 0);
    xform->setScaling(50);
    ground->setTransform(*xform);
    root.attach(*ground);
    for (int i=0; i < NUM_TREES; i++)
        {
        tree = new Square;
        tree->setTexture(*treeTex);
        xform = new SimpleTransform;
        xform->setScaling(3, 6, 1);
        tree->setTransform(*xform);
        tree->setTransparency(Transparency::AlphaTestZero);
        root.attach(*tree);
        trees[i] = tree;
        }
    positionTreesUniformly();
    }


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);
    root.drawAll();
    glutSwapBuffers();
    checkGLError("end-of-frame");
    }


void key(unsigned char k, int x, int y)
    {
    if (k == 27)
        exit(0);
    else if (k == 'u')
        positionTreesUniformly();
    else if (k == 'g')
        positionTreesGaussianly();
    }


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