#include <fstream>

#include "client/rendering/Mesh.h"

Mesh::Mesh() {
    vertexBuffer.bind();
    vertexBuffer.setFloatAttribute(0, 3, 0, 11);
    vertexBuffer.setFloatAttribute(1, 2, 3, 11);
    vertexBuffer.setFloatAttribute(2, 3, 5, 11);
    vertexBuffer.setFloatAttribute(3, 3, 8, 11);
}

void Mesh::add(const Triangle& data) {
    buffer.add(data);
}

void Mesh::clear() {
    buffer.clear();
}

void Mesh::build() {
    vertexBuffer.bindBuffer();
    vertexBuffer.setData(sizeof (Triangle) * buffer.getLength(), buffer.getData());
}

void Mesh::draw() const {
    vertexBuffer.bindArray();
    vertexBuffer.draw(buffer.getLength() * 3);
}

void Mesh::save() {
    std::ofstream out;
    out.open("scene");
    for(Triangle& t : buffer) {
        out << t.a.position[0] << ", ";
        out << t.a.position[1] << ", ";
        out << t.a.position[2] << ", ";
        out << t.a.texture[0] << ", ";
        out << t.a.texture[1] << "\n";
        out << t.b.position[0] << ", ";
        out << t.b.position[1] << ", ";
        out << t.b.position[2] << ", ";
        out << t.b.texture[0] << ", ";
        out << t.b.texture[1] << "\n";
        out << t.c.position[0] << ", ";
        out << t.c.position[1] << ", ";
        out << t.c.position[2] << ", ";
        out << t.c.texture[0] << ", ";
        out << t.c.texture[1] << "\n";
    }
}