/* -*- 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors:
 *  Cedric PINSON <cpinson@freesheep.org>
 *  Loic Dachary <loic@gnu.org>
 *
 */

#ifndef __OSGCAL__SUBMESHHARDWARE_H__
#define __OSGCAL__SUBMESHHARDWARE_H__

#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(); }

    void update(void) {}

    /**
     * Init class for hardware skinning
     */
    void InitHardwareMesh(osg::ref_ptr<osg::Vec3Array> vertices,
                          osg::ref_ptr<osg::Vec4Array> weights,
                          osg::ref_ptr<osg::Vec3Array> normals,
                          osg::ref_ptr<osg::Vec4Array> matrixs,
                          osg::ref_ptr<osg::Vec2Array> uvs,
                          CalIndex* idx,
                          int nbIdxs,
                          osg::VertexProgram* vp3,
                          CalHardwareModel* hardwareModel,
                          unsigned int* vbo,
                          unsigned int idxVbo);

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

    bool computeBound() const;
    osg::Object* clone(const osg::CopyOp&) const;

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

    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::UIntArray> mIarray;


    GLuint* mVbo;
    int mIndexVbo;
    std::string mName;
  };


}; //namespace osgCal

#endif
