/* -*- c++ -*-
 *
 * Copyright (C) 2007 Loic Dachary <loic@dachary.org>
 * Copyright (C) 2004, 2006 Mekensleep
 *
 *	Mekensleep
 *	24 rue vieille du temple
 *	75004 Paris
 *       licensing@mekensleep.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Authors:
 *  Cedric PINSON <cpinson@freesheep.org>
 *  Loic Dachary <loic@gnu.org>
 *  Igor Kravtchenko <igor@tsarevitch.org>
 *
 */

#ifndef __OSGCAL__SUBMESHHARDWARE_H__
#define __OSGCAL__SUBMESHHARDWARE_H__

#include <osg/Version>
#include <osg/Geometry>
#include <osg/PrimitiveSet>

#include <cal3d/cal3d.h>
#include <osg/Texture2D>
#include <osg/VertexProgram>
#include <osgCal/Export>

namespace osgCal {
  class GlueCalHardwareModel;

class Model;

  class OSGCAL_EXPORT SubMeshHardware: public osg::Drawable //osg::Geometry 
  {
   
   protected:

    ~SubMeshHardware();


   public:
    typedef std::map<std::string, osg::ref_ptr<osg::Texture2D> > Name2Texture;

    SubMeshHardware();


    /**
     * Creates a submesh for one model given the mesh and submesh of this mesh
     */
    SubMeshHardware(Model *model, int mesh);

    osg::Object* cloneType() const { return new SubMeshHardware(); }

    virtual const char* className() const { return "SubMeshHardware"; }

    bool update(void) { return true; }

    /**
     * Init class for hardware skinning
     */
    void InitHardwareMesh(int nbIdxs,
                          osg::VertexProgram* vp3,
                          GlueCalHardwareModel* hardwareModel,
                          unsigned int* vbo,
                          unsigned int idxVbo,
                          int maxVertexIndex);

#if OSG_VERSION_MAJOR != 2
    void drawImplementation(osg::State& state) const;
#else // OSG_VERSION_MAJOR != 2
    void drawImplementation(osg::RenderInfo& renderInfo) const;
#endif // OSG_VERSION_MAJOR != 2

#if OSG_VERSION_RELEASE != 9 && OSG_VERSION_MAJOR != 1 && OSG_VERSION_MAJOR != 2
    bool computeBound() const;
#else // OSG_VERSION_RELEASE != 9 && OSG_VERSION_MAJOR != 1 && OSG_VERSION_MAJOR != 2
    osg::BoundingBox computeBound() const;
#endif // OSG_VERSION_RELEASE != 9 && OSG_VERSION_MAJOR != 1 && OSG_VERSION_MAJOR != 2
    osg::Object* clone(const osg::CopyOp&) const;

    const std::string& getName(void) const { return mName; }
    void setName(const std::string& name) { mName = name; }

	const std::string& getMaterialName(void) { return mMaterialName; }
	void setMaterialName(const std::string& materialName) { mMaterialName = materialName; }

    osg::VertexProgram* mVp;
    int mMeshId;

    osg::BoundingBox _staticbbox;

  protected:

    bool mVertexProgram;
    GlueCalHardwareModel* mHardwareModel;
    mutable Model* mModel;

    int _nbIdxs;
    GLuint* _vbo;
    int _indexVbo;
    int _maxVertexIndex;
    std::string mName;
    std::string mMaterialName;
  };

}; //namespace osgCal

#endif
