#ifndef __OSGCAL__SUBMESH_H__
#define __OSGCAL__SUBMESH_H__

#include <osg/Drawable>
#include <cal3d/cal3d.h>

namespace osgCal {


/** @class osgCal::SubMesh
 * @brief Adapter that converts cal3d submeshes into osg::Drawables
 *
 * The easy way would be to draw all the character (CalModel) in a single
 * Drawable, but this approach lacks from state sorting. Each submesh of
 * each mesh of a model can have different state attributes. With the current
 * approach, if you have 1000 soldiers with two different textures each one,
 * there will be only two state changes per frame, and not 2000.
 *
 * Users of the osgCal library doesn't need to know about this class, it is
 * internal.
 */
class SubMesh: public osg::Drawable {
public:
    SubMesh();

    /**
     * Creates a submesh for one model given the mesh and submesh of this mesh
     */
    SubMesh(CalModel *model, unsigned mesh, unsigned submesh);

    /**
     * Draws the geometry.
     */
    virtual void drawImplementation(osg::State& state) const;


    virtual osg::Object* cloneType() const { return new SubMesh(); }
    virtual osg::Object* clone(const osg::CopyOp&) const;
    bool computeBound() const;
private:
    void setUpMaterial();
    unsigned mesh, submesh;
    mutable CalModel *model;
};

}; //namespace osgCal

#endif
