
#include "Tbarrel_pbr.h"

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

Tbarrel_pbr::Tbarrel_pbr()
{
	//
}

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

Tbarrel_pbr::~Tbarrel_pbr()
{
    //
}

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


// code path file 1 / 0 0 0 0 0 0
void Tbarrel_pbr::create(std::string flux)
{
    std::vector<std::string> strList;

    ECO_strCut(strList, 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]);
    // 14

    // ---

    _composition();

}

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

    //OMesh_obj::P_setPath(PATH_MESH);
    OMesh_gltf::P_setPath(PATH_MESH);

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

    _setModel();
}






void Tbarrel_pbr::draw(OShLight& shLight)
{
    shLight.enable();
        shLight.setModel(m_model);

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

    for (size_t i_part = 0; i_part < mesh.getPart_size(); i_part++)
    {
        shLight.setTextures( mesh.getId_diffuseMap(i_part), mesh.getId_normalMap(i_part) );
        mesh.draw(i_part);
    }
    mesh.end_draw_part();
}

void Tbarrel_pbr::draw(OShLight_pbr& shLight_pbr)
{
    shLight_pbr.enable();
        shLight_pbr.setModel(m_model);

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

    for (size_t i_part = 0; i_part < mesh.getPart_size(); i_part++)
    {
        shLight_pbr.setTextures( mesh.getId_albedoMap(i_part),
                                 mesh.getId_normalMap(i_part),
                                 mesh.getId_metallicMap(i_part),
                                 mesh.getId_roughnessMap(i_part),
                                 mesh.getId_AOMap(i_part)  );
        mesh.draw(i_part);
    }
    mesh.end_draw_part();
}






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

    // gltf > pour ma verticale z
    switch(mesh.getVerticale())
    {
    case 0:
        m_model.rotate(180, 1,0,0);
        break;
    case 1:
        m_model.rotate(90, 1,0,0);
        break;
    }
}



