#include <ogl2d.h>
#include "block.h"
#include "spectrum.h"

Spectrum::Spectrum(unsigned int _size, unsigned int _height):size(_size),height(_height)
{
    data = new unsigned int[size];
    for (unsigned int i = 0; i<size; i++)
    {
        data[i] = rand()%height;
    }
}

Spectrum::~Spectrum()
{
    delete [] data;
}

void Spectrum::step()
{
    for (unsigned int i = 0; i<size; i++)
    {
        data[i] += rand()%3-1;
        if (data[i] > height)
            data[i] = height;
        if (data[i] < 0) 
            data[i] = 0;
    }
}

void Spectrum::render()
{
    float r,g,b;
    unsigned int i, j, x, y;
    const unsigned int h = height*::size/3;

    //glPushMatrix();
    //glEnable(GL_DEPTH_TEST);
    //glTranslatef(0,0,-10.f);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE,GL_SRC_ALPHA);
    glBegin(GL_QUADS);
    x = -1;
    for (i = 0; i<size; i++)
    {
        y = h - 1;
        for (j = height - 1; j > data[i]; j--)
        {
            y -= ::size/3;
            r = (1 - (float)j / ((float)height)) * .3f;
            g = (float)j / ((float)height) * .3f;
            b = 0;//(float)data[i] / (float)height;
            glColor4f(r,g,b,.99f);

            glVertex2d(x+2,y+2);
            glVertex2d(x+::size,y+2);
            glVertex2d(x+::size,y+::size/3);
            glVertex2d(x+2,y+::size/3);
        }
        x += ::size;
    }
    glEnd();
    //glDisable(GL_DEPTH_TEST);
    //glPopMatrix();
}
