
#include "Tbassin.h"


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

Tbassin::Tbassin()
{
	//
}

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

Tbassin::~Tbassin()
{
    //
}


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

// nom obj
void Tbassin::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 = m_path + strList[0];
    // pos ripple
    //px = ECO_strToFloat(strList[2]);
    //py = ECO_strToFloat(strList[3]);

    // ---

    OMesh_obj::P_setPath(PATH_MESH);

    if (!mesh_eau.create(file))
    {
        ECO_error_set("echec chargement %s >Tbassin::addEau", m_file.c_str());
        //return;
    }

    m_eau_exist = true;

}



//f0001 fontaine_0/ fontaine_0a.glb 1 / 22 -24 0.75 0 0 0 1 1 1 /"
void Tbassin::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_rain.create();
    m_time = 0.0f;

    _composition();
    _setModel();
}

void Tbassin::_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 >Tbassin::_composition", m_file.c_str());
        //return;
    }

}




void Tbassin::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 Tbassin::draw_water(const OMat4& vp, float ambient)
{
    if (!m_eau_exist) return;

    glDisable(GL_CULL_FACE);

    m_time++;

    // !!! attention au point de base / origine, qui doit etre (le même que la structure) en 0,0,0
    sh_rain.enable();
        sh_rain.setVP(vp);
        sh_rain.setModel( m_model); // eau placée par rapport à bassin ou mesh_eau[0].getModel(0) );
        sh_rain.setTime(m_time);
        sh_rain.setAmbient(ambient);
        sh_rain.setTexture( mesh_eau.getId_diffuseMap(0));

        mesh_eau.draw();
    sh_rain.disable();
}




void Tbassin::_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);

}





