
#include "Tbillfire.h"

// -----------------------
// --- le constructeur ---
// -----------------------

Tbillfire::Tbillfire()
{
	m_texParam.id = 0;
}

// ----------------------
// --- le destructeur ---
// ----------------------

Tbillfire::~Tbillfire()
{
    if (m_texParam.id > 0) glDeleteTextures(1, &m_texParam.id);
}


// --------------------
// --- les méthodes ---
// --------------------

void Tbillfire::create()
{
    m_texParam = ECO_loadTexture("donnees/mesh/perso/flamme.png");

    float w = 0.25f, h = 0.25f;
    float ratioW, ratioH;
    float x0,y0,x1,y1;
    ORect_i src(0,0,64,64);

    m_vertex[0].set(0.0f, -w, -h);
    m_vertex[1].set(0.0f,  w, -h);
    m_vertex[2].set(0.0f,  w,  h);
    m_vertex[3].set(0.0f, -w,  h);

    m_normal[0].set(1, 0, 0);
    m_normal[1].set(1, 0, 0);
    m_normal[2].set(1, 0, 0);
    m_normal[3].set(1, 0, 0);

    ratioW = 1.0f / static_cast<float>(m_texParam.w);
    ratioH = 1.0f / static_cast<float>(m_texParam.h);

    for (int i = 0; i < 23; i++)
    {
        x0 = src.x * ratioW;
        y0 = (m_texParam.h - src.y - (src.h * i)) * ratioH;
        x1 = x0 + (src.w * ratioW);
        y1 = y0 + (src.h * ratioH);

        m_uv[i][0].set(x0, y0);
        m_uv[i][1].set(x1, y0);
        m_uv[i][2].set(x1, y1);
        m_uv[i][3].set(x0, y1);
    }
}




void Tbillfire::draw(ODshGeo& dshGeo, OVec3_f& camDir)
{
    OMat4 model;

    glDisable(GL_CULL_FACE);

    dshGeo.enable();
        dshGeo.setTextures(m_texParam.id, m_texParam.id, 0);

        glVertexAttribPointer(LOCATION_POSITION, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
        glEnableVertexAttribArray(LOCATION_POSITION);
        glVertexAttribPointer(LOCATION_NORMAL, 3, GL_FLOAT, GL_FALSE, 0, m_normal);
        glEnableVertexAttribArray(LOCATION_NORMAL);

    for (size_t idx = 0; idx < plane.size(); idx++)
    {
        model.loadIdentity();
        model.translate(plane[idx].pos);
        model.rotate(camDir.z, 0,0,1); // le plan, créé face à l'Est, se retrouve perpenticulairement au regard de la caméra
        model.rotate(camDir.y, 0,1,0);

        dshGeo.setModel(model);

        glVertexAttribPointer(LOCATION_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, m_uv[plane[idx].frame]);
        glEnableVertexAttribArray(LOCATION_TEXCOORD);

        glDrawArrays(GL_QUADS, 0, 4);
    }

    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
}

void Tbillfire::draw(OShLight& shLight, OVec3_f& camDir)
{
    OMat4 model;

    glDisable(GL_CULL_FACE);

    shLight.enable();
        shLight.setTextures(m_texParam.id, m_texParam.id);

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, m_normal);
        glEnableVertexAttribArray(2);

    for (size_t idx = 0; idx < plane.size(); idx++)
    {
        model.loadIdentity();
        model.translate(plane[idx].pos);
        model.rotate(camDir.z, 0,0,1); // le plan, créé face à l'Est, se retrouve perpenticulairement au regard de la caméra
        model.rotate(camDir.y, 0,1,0);

        shLight.setModel(model);

        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, m_uv[plane[idx].frame]);
        glEnableVertexAttribArray(1);

        glDrawArrays(GL_QUADS, 0, 4);
    }

    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
}




void Tbillfire::generate(OVec3_f& pos, int nbr)
{
    OVec3_f p2;
    float angle;
    size_t idx  = plane.size();

    for (int i = 0; i < nbr; i++)
    {
        angle = ECO_rnd_i(36) * 10;
        angle *= ECO_DEGTORAD;

        p2 = pos;
        p2.z += 0.5f + ECO_rnd_f(0.5f);
        p2.x += cos(angle) * 0.5f;
        p2.y += sin(angle) * 0.5f;

        plane.push_back(s_plane());

        plane[idx].pos = p2;
        plane[idx].frame = 0;
        plane[idx].nbr_frame = 23;
        plane[idx].cpt = 0;
        plane[idx].cpt_delay = 4;
        idx++;
    }
}

void Tbillfire::update()
{
    for (size_t idx = 0; idx < plane.size(); idx++)
    {
        plane[idx].pos.z += 0.01f;

        plane[idx].cpt++;
        if (plane[idx].cpt == plane[idx].cpt_delay)
        {
            plane[idx].cpt = 0;

            plane[idx].frame++;
            if (plane[idx].frame == plane[idx].nbr_frame)
            {
                plane[idx].frame = 0;
                plane[idx].pos.z = 1001.0f;
            }
        }
    }

    // ---

    // notez : int
    for (int idx = plane.size()-1; idx >= 0; idx--)
    {
        if (plane[idx].pos.z > 1000.0f)
        {
            plane.erase(plane.begin() + idx);
            break;
        }
    }
}
