#include "physicsdebugnode.h"
#include "SparrowRenderer/mesh.h"
#include "SparrowRenderer/pbrmaterial.h"

#include "scene/scenetree.h"

PhysicsDebugNode::PhysicsDebugNode()
{
    Mesh* mesh = new Mesh();
    setMesh(mesh);
    mesh->setMaterial(new PBRMaterial());
    mesh->setPrimitiveType(GL_LINES);
    mesh->setWireframe(true);

    glm::vec3 pos1(0, 1, 0);
    glm::vec3 pos2(0, 0, 0);
    glm::vec3 color(1, 0, 0);
    mesh->addVertex(pos1, color);
    mesh->addVertex(pos2, color);
    mesh->updateFlags();
}

PhysicsDebugNode::~PhysicsDebugNode()
{
    delete getMesh();
}

void PhysicsDebugNode::drawLine(const btVector3& from, const btVector3& to, const btVector3& color)
{
    glm::vec3 glColor(color.x(), color.y(), color.z());
    getMesh()->addVertex(glm::vec3(from.x(), from.y(), from.z()), glColor);
    getMesh()->addVertex(glm::vec3(to.x(), to.y(), to.z()), glColor);
}

void PhysicsDebugNode::flushLines()
{
    // a little heavy, but it's a debug mode so it shouldn't be a problem
    getMesh()->initGL();
}

void PhysicsDebugNode::clearBuffers()
{
    getMesh()->m_positions3D.clear();
    getMesh()->m_normals.clear();
}
