/// This file is part of the X3DToolKit library
/// Copyright (C) 2002-2004  Yannick Le Goc (legoc@imag.fr)

/// http://artis.imag.fr/Members/Yannick.Legoc/X3D/

/// This library is free software; you can redistribute it and/or
/// modify it under the terms of the GNU Lesser General Public
/// License as published by the Free Software Foundation; either
/// version 2.1 of the License, or (at your option) any later version.

/// This library is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
/// Lesser General Public License for more details.

/// You should have received a copy of the GNU Lesser General Public
/// License along with this library; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#ifndef X3DTK_GL_INDEXEDFACESET_H
#define X3DTK_GL_INDEXEDFACESET_H

#include "GL_X3DComposedGeometryNode.h"

#include <vector>
#include <utility>

namespace X3DTK {
namespace GL {

/*! \brief Class declared in <b><X3DTK/GL/scenegraph.h></b> providing the GL implementation
 *  of X3D::IndexedFaceSet.
 *
 *  \ingroup GLSceneGraph
 */
 
class IndexedFaceSet : public X3DComposedGeometryNode
{
public:
  /// Constructor.
  IndexedFaceSet();
  /// Destructor.
  virtual ~IndexedFaceSet();

  /// Sets the N3F_V3F vertexArray.
  void setN3F_V3F_vertexArray(const std::vector<N3F_V3F> &N3F_V3FvertexArray);
  /// Sets the C4F_N3F_V3F vertexArray.
  void setC4F_N3F_V3F_vertexArray(const std::vector<C4F_N3F_V3F> &C4F_N3F_V3FvertexArray);
  /// Sets the T2F_N3F_V3F vertexArray.
  void setT2F_N3F_V3F_vertexArray(const std::vector<T2F_N3F_V3F> &T2F_N3F_V3F_vertexArray);
  /// Sets the T2F_C4F_N3F_V3F vertexArray.
  void setT2F_C4F_N3F_V3F_vertexArray(const std::vector<T2F_C4F_N3F_V3F> &T2F_C4F_N3F_V3F_vertexArray);
  /// Sets the indexFaceArray.
  void setIndexArray(const MFInt32 &indexArray);

  /// Gets the N3F_V3F vertexArray.
  inline std::vector<N3F_V3F> &N3F_V3F_vertexArray() {return _N3F_V3F_vertexArray;};
  /// Gets the C4F_N3F_V3F vertexArray.
  inline std::vector<C4F_N3F_V3F> &C4F_N3F_V3F_vertexArray() {return _C4F_N3F_V3F_vertexArray;};
  /// Gets the T2F_N3F_V3F vertexArray.
  inline std::vector<T2F_N3F_V3F> &T2F_N3F_V3F_vertexArray() {return _T2F_N3F_V3F_vertexArray;};
  /// Gets the T2F_C4F_N3F_V3F vertexArray.
  inline std::vector<T2F_C4F_N3F_V3F> &T2F_C4F_N3F_V3F_vertexArray() {return _T2F_C4F_N3F_V3F_vertexArray;};
  /// Gets the indexFaceArray.
  inline MFInt32 &indexArray() {return _indexArray;};
  /// Gets the X3DToGLIndex.
  inline const std::vector<MFInt32> &getX3DToGLIndex() const {return _X3DtoGLIndex;};
  /// Computes the normals.
  virtual void computeNormals();

  /// Updates the attributes of the node.
  virtual void update();
  /// Renders the node in an openGL context.
  virtual void draw() const;

private:   
  /// N3F_V3F vertexArray.
  std::vector<N3F_V3F> _N3F_V3F_vertexArray;
  /// C4F_N3F_V3F vertexArray.
  std::vector<C4F_N3F_V3F> _C4F_N3F_V3F_vertexArray;
  /// T2F_N3F_V3F vertexArray.
  std::vector<T2F_N3F_V3F> _T2F_N3F_V3F_vertexArray;
  /// T2F_C4F_N3F_V3F vertexArray.
  std::vector<T2F_C4F_N3F_V3F> _T2F_C4F_N3F_V3F_vertexArray;
  /// indexFaceArray.
  MFInt32 _indexArray;
  /// Relation between X3D vertex array and GL vertex array.
  std::vector<MFInt32> _X3DtoGLIndex;
  
  /// temporary list of index.
  std::list<std::pair<SFInt32, N3F_V3F> > _N3F_V3F_tesselatedFace;
  /// temporary list of index.
  std::list<std::pair<SFInt32, C4F_N3F_V3F> > _C4F_N3F_V3F_tesselatedFace;
  /// temporary list of index.
  std::list<std::pair<SFInt32, T2F_N3F_V3F> > _T2F_N3F_V3F_tesselatedFace;
  /// temporary list of index.
  std::list<std::pair<SFInt32, T2F_C4F_N3F_V3F> > _T2F_C4F_N3F_V3F_tesselatedFace;
      
  /// Empties the arrays.
  void emptyArrays();
  ///  Computes the vertex Array with no color and no texCoord.
  void computeNoColorNoTexCoord(const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex);
  /// Computes the vertex Array with color and no texCoord.
  void computeColorRGBANoTexCoord(const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex);
  /// Computes the vertex Array with color and no texCoord.
  void computeColorNoTexCoord(const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex);
  /// Computes the vertex Array with no color and texCoord.
  void computeNoColorTexCoord(const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex);
  /// Computes the vertex Array with color and texCoord.
  void computeColorRGBATexCoord(const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex);
  /// Computes the vertex Array with color and texCoord.
  void computeColorTexCoord(const MFVec3f &vertexArray, const MFVec3f &normalArray, const MFInt32 &coordIndex);
  
  /// Tesselate the face.
  void tesselateNoColorNoTexCoord(const std::list<std::pair<SFInt32, N3F_V3F> > &N3F_V3F_face);
  /// Tesselate the face.
  void tesselateColorNoTexCoord(const std::list<std::pair<SFInt32, C4F_N3F_V3F> > &C4F_N3F_V3F_face);
  /// Tesselate the face.
  void tesselateNoColorTexCoord(const std::list<std::pair<SFInt32, T2F_N3F_V3F> > &T2F_N3F_V3F_face);
  /// Tesselate the face.
  void tesselateColorTexCoord(const std::list<std::pair<SFInt32, T2F_C4F_N3F_V3F> > &T2F_C4F_N3F_V3F_face);
  
  /// Computes the normals.
  void computeNoColorNoTexCoordNormals();
  /// Computes the normals.
  void computeColorNoTexCoordNormals();
  /// Computes the normals.
  void computeNoColorTexCoordNormals();
  /// Computes the normals.
  void computeColorTexCoordNormals();
  
};

}
}

#endif
