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

 This program demonstrates the use of randomness in creating a
 3D scene.  It draws a forest of 25 trees.  Initially, the
 trees are positioned in an easy-to-calculate regular grid
 pattern.  If the 'r' key is hit, then a random X/Z position is
 chosen for each tree.

*****************************************************************/
#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 ******************************/
/* Lay out the trees in a simple grid.  This non-random method */
/* simply spaces the trees evenly in a 5x5 grid over the 40 x  */
/* 40 unit area.                                               */
/*                                                             */
void positionTreesInGrid(void)
    {
    int gridSize = (int)(sqrt(NUM_TREES) + 0.5);
    float treeSpacing = FOREST_SIZE / gridSize;
    for (int i=0; i < NUM_TREES; i++)
        {
        float x = (i % gridSize) * treeSpacing - FOREST_SIZE/2.0;
        float z = (i / gridSize) * treeSpacing - FOREST_SIZE/2.0;
        ((SimpleTransform&)trees[i]->transform()).setTranslation(x,6,z);
        }
    }


/*************** CODE OF INTEREST ******************************/
/* Lay out the trees randomly.  This picks a random X value    */
/* and a random Z value for each tree, and sets its translation*/
/* to those values.  It also picks a random scale factor, and  */
/* a random rotation.                                          */
/*                                                             */
void positionTreesRandomly(void)
    {
    float height;
    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()).setRotation(drand48() * 360.0, 0, 1, 0);
        height = drand48() * 10.0 + 3.0;
        ((SimpleTransform&)trees[i]->transform()).setScaling(height/2, height, 1);
        ((SimpleTransform&)trees[i]->transform()).setTranslation(x,height,z);
        }
    }


/*************** CODE OF INTEREST ******************************/
/* Create everything needed for the scene.  This creates the   */
/* ground plane and 25 trees as objects of the 'Square' class  */
/* (created in Class 11).  It also creates the necessary       */
/* texture and transformation objects.                         */
/*                                                             */
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;
        }
    positionTreesInGrid();
    }


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 == 'g')
        positionTreesInGrid();
    else if (k == 'r')
        positionTreesRandomly();
    }


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

