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

#include "group_node.h"
#include "../../Includes/OpenGL.h"

BEGIN_TEG_NAMESPACE();

GroupNode::GroupNode() : ISceneNode(GROUP_NODE) {}

GroupNode::GroupNode(const std::vector< SmartPointer<ISceneNode> >& nodes)
    : ISceneNode(GROUP_NODE), nodes_(nodes) {}

GroupNode::~GroupNode() {
  nodes_.clear();
}

void GroupNode::apply() {
  // Do a recursive call to apply of each node in the group.
  glPushMatrix();
    const int kNumberOfNodes = static_cast<int>(nodes_.size());
    for (int i = 0; i < kNumberOfNodes; ++i) nodes_[i]->apply();
  glPopMatrix();
}

SmartPointer<ISceneNode> GroupNode::copy() {
  // Create a copy of each SceneNode in the current group.
  const int kNumberOfNodes = static_cast<int> (nodes_.size());
  std::vector< SmartPointer<ISceneNode> > nodes_copy(kNumberOfNodes);
  for (int i = 0; i < kNumberOfNodes; ++i) nodes_copy[i] = nodes_[i]->copy();

  // Create a new instance of GroupNode with the same values.
  return SmartPointer<ISceneNode>(new GroupNode(nodes_copy));
}

END_TEG_NAMESPACE();
