#include "Mosaic.h"

Mosaic::Mosaic() : m_text( 0x0 )
{
    //ctor
}

Mosaic::~Mosaic()
{
    if ( m_text ) delete m_text;
}


int Mosaic::charger( std::string const src, float vertMulti )
{
    /// Création des attributs
    // Chargement du sprite
    SDL_Surface* image = SDL_LoadBMP( src.c_str() );
    if ( !image ) {
        std::cout << "Erreur lors du chargement de " << src << std::endl;
    }
    unsigned char* pixels = (unsigned char*) image->pixels;
    SDL_LockSurface(image);

    // Création de la texture
    m_text = new Texture(src);
    m_textID = m_text->getID();

    // Tableaux
    std::vector<float> v_coord(0); // Coordonnées des vertices
    std::vector<float> v_norm(0); // Normales
    std::vector<float> v_text(0); // Coordonnées de texture des vertices

    /// Attributions
    // Tableaux
    v_coord.push_back(-0.5f);//1
    v_coord.push_back(0.5f);
    v_coord.push_back(0.0f);
    v_coord.push_back(-0.5f);//2
    v_coord.push_back(-0.5f);
    v_coord.push_back(0.0f);
    v_coord.push_back(0.5f);//3
    v_coord.push_back(0.5f);
    v_coord.push_back(0.0f);
    v_coord.push_back(0.5f);//4
    v_coord.push_back(0.5f);
    v_coord.push_back(0.0f);
    v_coord.push_back(-0.5f);//5
    v_coord.push_back(-0.5f);
    v_coord.push_back(0.0f);
    v_coord.push_back(0.5f);//6
    v_coord.push_back(-0.5f);
    v_coord.push_back(0.0f);

    v_norm.push_back(0.0f);//1
    v_norm.push_back(0.0f);
    v_norm.push_back(1.0f);
    v_norm.push_back(0.0f);//2
    v_norm.push_back(0.0f);
    v_norm.push_back(1.0f);
    v_norm.push_back(0.0f);//3
    v_norm.push_back(0.0f);
    v_norm.push_back(1.0f);
    v_norm.push_back(0.0f);//4
    v_norm.push_back(0.0f);
    v_norm.push_back(1.0f);
    v_norm.push_back(0.0f);//5
    v_norm.push_back(0.0f);
    v_norm.push_back(1.0f);
    v_norm.push_back(0.0f);//6
    v_norm.push_back(0.0f);
    v_norm.push_back(1.0f);

    v_text.push_back(0.0f);//1
    v_text.push_back(1.0f);
    v_text.push_back(0.0f);//2
    v_text.push_back(0.0f);
    v_text.push_back(1.0f);//3
    v_text.push_back(1.0f);
    v_text.push_back(1.0f);//4
    v_text.push_back(1.0f);
    v_text.push_back(0.0f);//5
    v_text.push_back(0.0f);
    v_text.push_back(1.0f);//6
    v_text.push_back(0.0f);

    // Création des tableaux openGL
    m_nbVec = v_coord.size() / 3;

    m_tailleVerticesBytes = v_coord.size() * sizeof(float);
    m_vertices = new float[v_coord.size()];

    m_tailleNormalesBytes = v_norm.size() * sizeof(float);
    m_normales = new float[v_norm.size()];

    m_tailleCoordTextureBytes = v_text.size() * sizeof(float);
    m_coordTexture = new float[v_text.size()];

    // Remplissage des tableaux openGL
    for ( uint32_t i; i < v_coord.size(); i++ )
        m_vertices[i] = v_coord[i];
    for ( uint32_t i; i < v_norm.size(); i++ )
        m_vertices[i] = v_norm[i];
    for ( uint32_t i; i < v_text.size(); i++ )
        m_vertices[i] = v_text[i];

    /// Destruction de l'image
    SDL_FreeSurface( image );

    /// Chargement Vertex Buffer Object
    // Destruction d'un éventuel ancien VBO
    if(glIsBuffer(m_vboID) == GL_TRUE)
        glDeleteBuffers(1, &m_vboID);

    // Génération de l'ID
    glGenBuffers(1, &m_vboID);

    // Verrouillage du VBO
    glBindBuffer(GL_ARRAY_BUFFER, m_vboID);

        // Allocation de la mémoire vidéo
        glBufferData(GL_ARRAY_BUFFER, m_tailleVerticesBytes + m_tailleNormalesBytes + m_tailleCoordTextureBytes, 0, GL_STATIC_DRAW);

        // Transfert des données
        glBufferSubData(GL_ARRAY_BUFFER, 0, m_tailleVerticesBytes, m_vertices);
        glBufferSubData(GL_ARRAY_BUFFER, m_tailleVerticesBytes, m_tailleNormalesBytes, m_normales);
        glBufferSubData(GL_ARRAY_BUFFER, m_tailleVerticesBytes+m_tailleNormalesBytes, m_tailleCoordTextureBytes, m_coordTexture);

    // Déverrouillage de l'objet
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    /// Chargement Vertex Array Object
    // Destruction d'un éventuel ancien VAO
    if(glIsVertexArray(m_vaoID) == GL_TRUE)
        glDeleteVertexArrays(1, &m_vaoID);

    // Génération de l'ID du VAO
    glGenVertexArrays(1, &m_vaoID);

    // Verrouillage du VAO
    glBindVertexArray(m_vaoID);

        // Verrouillage du VBO
        glBindBuffer(GL_ARRAY_BUFFER, m_vboID);

            // Accès aux vertices dans la mémoire vidéo
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
            glEnableVertexAttribArray(0);

            // Accès aux normales dans la mémoire vidéo
            glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(m_tailleVerticesBytes));
            glEnableVertexAttribArray(1);

            // Accès aux coordonnées de texture dans la mémoire vidéo
            glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(m_tailleVerticesBytes+m_tailleNormalesBytes));
            glEnableVertexAttribArray(2);

        // Déverrouillage du VBO
        glBindBuffer(GL_ARRAY_BUFFER, 0);

    // Déverrouillage du VAO
    glBindVertexArray(0);

    /// Fin
    return 0;
}
