
#include "Tfontaine.h"


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

Tfontaine::Tfontaine()
{
	//
}

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

Tfontaine::~Tfontaine()
{
    //
}


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

// code null / px py pz radius / 1 null 0 / fontaine_0/fontaine_0.wav 99
void Tfontaine::addDetecteur(std::string flux)
{
    detecteur.create(flux);
}


void Tfontaine::addEau(std::string flux)
{
    std::vector<std::string> strList;
    std::string sep;
    std::string file;
    float px,py;

    m_eau_exist = false;
    strList = ECO_strCut(flux, ' ');

    file = strList[0];
    // pos ripple
    px = ECO_strToFloat(strList[2]);
    py = ECO_strToFloat(strList[3]);

    // ---

    OMesh_obj::P_setPath(PATH_MESH);

    if (m_path != "null")
    {
        file = m_path + file;
    }
    else
    {
        file = file;
    }
    if (!mesh_eau.create(file))
    {
        ECO_error_set("echec chargement %s >Tfontaine::_composition", file.c_str());
        //return;
    }

    m_eau_exist = true;

    sh_ripple.enable();
        sh_ripple.setModel( m_model );
        sh_ripple.setCircle_center(px,1.0f-py); // avec gltf > px,py ??? > flipV de l'image
    sh_ripple.disable();
}

void Tfontaine::addJet(std::string flux)
{
    OVec3_f pos;

    // code path file src.xywh / max generation_nbr gen_loop / angleH.min.max angleV.min.max /
    // vitesse.min.max / zone.radius zone.yz zone.type.w / size.min.max life.min.max /
    // color.rgb.min color.rgb.max / var_air var_gravite var_color /
    // "jet0 images/ particles.png 0 0 32 32 / 1000 10 9999 / -5 5 270 285 / 0.1 0.5 / 0.02 0 0 0 / 0.01 0.03 3 3.5 / 1 1 1 0.5 1 1 1 0.5 / 0.9 -0.1 0 /"
    prtJet.create(flux);
    pos = m_pos;
    // embouchure
    pos.x += 0.0f;
    pos.y += -0.5f;
    pos.z += 1.15f;
    prtJet.start(pos);
}



//f0001 fontaine_0/ fontaine_0a.glb 1 / 22 -24 0.75 0 0 0 1 1 1 /"
void Tfontaine::create(std::string flux)
{
    std::vector<std::string> strList;
    std::string sep;

    strList = ECO_strCut(flux, ' ');

	m_code = strList[0];
	m_path = strList[1];
	m_file = strList[2];
	m_cull_face = ECO_strToInt(strList[3]);
	// 4
	m_pos.x = ECO_strToFloat(strList[5]);
	m_pos.y = ECO_strToFloat(strList[6]);
	m_pos.z = ECO_strToFloat(strList[7]);
	m_rot.x = ECO_strToFloat(strList[8]);
	m_rot.y = ECO_strToFloat(strList[9]);
	m_rot.z = ECO_strToFloat(strList[10]);
    //m_scale.x = ECO_strToFloat(strList[11]);
	//m_scale.y = ECO_strToFloat(strList[12]);
	//m_scale.z = ECO_strToFloat(strList[13]);
    //

    sh_ripple.create();
    m_time = 0.0f;
    m_eau_exist = false;

    _composition();
    _setModel();
}

void Tfontaine::_composition()
{
    std::string file;

    OMesh_obj::P_setPath(PATH_MESH);

    if (m_path != "null")
    {
        file = m_path + m_file;
    }
    else
    {
        file = m_file;
    }
    if (!mesh_bassin.create(file))
    {
        ECO_error_set("echec chargement %s >Tfontaine::_composition", m_file.c_str());
        //return;
    }
}



// muret
void Tfontaine::draw(ODshGeo& dshGeo)
{
    m_time += 1.0f;

    if (m_cull_face)
        glEnable(GL_CULL_FACE);
    else
        glDisable(GL_CULL_FACE);

    dshGeo.enable();
    dshGeo.setModel(m_model);

    for (size_t part = 0; part < mesh_bassin.getPart_size(); part++)
    {
        dshGeo.setTextures( mesh_bassin.getId_diffuseMap(part), mesh_bassin.getId_normalMap(part), mesh_bassin.getId_specularMap(part));
        mesh_bassin.draw(part);
    }
    mesh_bassin.end_draw_part();
}

// hors defered shader > transparence
void Tfontaine::draw_water(const OMat4& vp, float ambient)
{
    if (!m_eau_exist) return;

    glDisable(GL_CULL_FACE);

    // !!! attention au point de base / origine, qui doit etre (le même que la structure) en 0,0,0
    sh_ripple.enable();
        sh_ripple.setVP(vp);
        //sh_ripple.setModel( m_model );
        sh_ripple.setTime(m_time);
        //sh_ripple.setCircle_center();
        sh_ripple.setAmbient(ambient);

        sh_ripple.setTexture( mesh_eau.getId_diffuseMap(0));
        mesh_eau.draw();

        //mesh_eau.draw(0);
        //mesh_eau.end_draw_part();

    sh_ripple.disable();
}

void Tfontaine::draw_particle()
{
    prtJet.generation();
    prtJet.draw();
}





void Tfontaine::getIn(OVec3_f& srcpos)
{
    detecteur.getIn(srcpos);
}




void Tfontaine::_setModel()
{
    m_model.loadIdentity();
    m_model.translate(m_pos);
    m_model.rotate(m_rot.z, 0, 0, 1);
    m_model.rotate(m_rot.y, 0, 1, 0);
    m_model.rotate(m_rot.x, 1, 0, 0);
    //m_model.scale(m_scale);

}




void Tfontaine::update(float ambient)
{
    prtJet.update(ambient);
}



