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

PhysicsDebugNode::PhysicsDebugNode() :
    MeshNode(new Mesh())
{
    m_geometry.mesh->setMaterial(new PBRMaterial());
    m_geometry.mesh->setPrimitiveType(GL_LINES);
    m_geometry.mesh->setWireframe(true);

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

PhysicsDebugNode::~PhysicsDebugNode()
{
    delete m_geometry.mesh;
}

void PhysicsDebugNode::drawLine(const btVector3& from, const btVector3& to, const btVector3& color)
{
    glm::vec3 glColor(color.x(), color.y(), color.z());
    m_geometry.mesh->addVertex(glm::vec3(from.x(), from.y(), from.z()), glColor);
    m_geometry.mesh->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
    m_geometry.mesh->initGL();
}

void PhysicsDebugNode::clearBuffers()
{
    m_geometry.mesh->positions3D.clear();
    m_geometry.mesh->normals.clear();
}
