#include <WFX/scene/SceneGraphTransformer.h>
#include <WFX/math/Matrix4.h>

using namespace WFX::scene;
using namespace WFX::math;

SceneGraphTransformer::SceneGraphTransformer() {
}

void SceneGraphTransformer::visitNode(Node& n) {
    n.updateDown();
    n.LocalRotation = math::Matrix3::IDENTITY;
    n.LocalTranslation = math::Vector3::ZERO;
    n.LocalScale = 1.0f;
    n.WorldRotation = math::Matrix3::IDENTITY;
    n.WorldTranslation = math::Vector3::ZERO;
    n.WorldScale = 1.0f;
    std::list<Spatial*>::iterator it;
    for (it = n.Children.begin(); it != n.Children.end(); it++) {
        (*it)->accept(*this);
    }
}

void SceneGraphTransformer::visitTriMesh(TriMesh& t) {
    Matrix4 trans(t.WorldRotation, t.WorldTranslation, t.WorldScale);
    for (UInt i=0; i<t.VertexCount; i++) {
        t.Vertices[i] = trans * t.Vertices[i];
    }
    t.LocalRotation = math::Matrix3::IDENTITY;
    t.LocalTranslation = math::Vector3::ZERO;
    t.LocalScale = 1.0f;
    t.WorldRotation = math::Matrix3::IDENTITY;
    t.WorldTranslation = math::Vector3::ZERO;
    t.WorldScale = 1.0f;
    t.calcModelBound();
}

void SceneGraphTransformer::visitOctreeNode(OctreeNode& o) {
    THROW("not yet implemented: visitOctreeNode");
}

