// Author: Juan Carlos De Abreu (jcabreur@gmail.com)

#include "rotate_node.h"
#include "../../../Includes/OpenGL.h"
#include "../../../Math/Quaternion/Quaternion-inl.h"

BEGIN_TEG_NAMESPACE();

RotateNode::RotateNode() : ITransformationNode(ROTATE_NODE, 0, 0, 0),
                           angle_(0) {}

RotateNode::RotateNode(float angle, float x, float y, float z)
    : ITransformationNode(ROTATE_NODE, x, y, z), angle_(angle) {}

RotateNode::~RotateNode() {}

void RotateNode::apply() {
  // Create a quaternion with the rotation, then create a rotation matrix
  // related to this quaternion and finally mult the rotation matrix by the
  // modelview matrix.
  const math::Quaternion<float> kRotation = rotate(-angle_, Vec3f(x_, y_, z_));
  const Mat4f kRotationMatrix = math::transpose(kRotation.createMatrix());
  glMultMatrixf(kRotationMatrix.m);
}

SmartPointer<ISceneNode> RotateNode::copy() {
  // Create a new instance of RotateNode with the same values.
  return SmartPointer<ISceneNode>(new RotateNode(angle_, x_, y_, z_));
}

END_TEG_NAMESPACE();
