/* -*- c++ -*-
 *
 * Copyright (C) 2004 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@ozos.net>
 *
 */

#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 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(CalModel *model, int mesh);

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

    bool update(void) { return true; }

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

    void drawImplementation(osg::State& state) const;

#if OSG_VERSION_RELEASE != 9
    bool computeBound() const;
#else // OSG_VERSION_RELEASE != 9
    osg::BoundingBox computeBound() const;
#endif // OSG_VERSION_RELEASE != 9
    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;

  private:

    bool mVertexProgram;
    CalHardwareModel* mHardwareModel;
    mutable CalModel* mModel;
/*
    osg::ref_ptr<osg::Vec3Array> mVarray;
    osg::ref_ptr<osg::Vec4Array> mWarray;
    osg::ref_ptr<osg::Vec3Array> mNarray;
    osg::ref_ptr<osg::Vec4Array> mMarray;
    osg::ref_ptr<osg::Vec2Array> mTarray;
    */
    osg::ref_ptr<osg::Array> mIarray;


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


}; //namespace osgCal

#endif
