/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program 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 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */
#ifndef KEOS_MODEL_H
#define KEOS_MODEL_H

#include "KeosPrerequisites.h"
#include "KeosResource.h"
#include "KeosDeclaration.h"
#include "KeosBuffer.h"
#include "KeosTexture.h"

namespace Keos
{
    //-----------------------------------------------------------------------
    /** Models. 
     *  Contains one vertex buffer, one index buffer, one CDeclaration
     *  and one function to render the model.
     */
    class KEOS_EXPORT CModel : public IResource
    {
    public :

        //! Vertex type for models
        struct TVertex
        {
            TVector3F     Position;
            TVector3F     Normal;
            ulong         Color;
            TVector2F     TexCoords;
        };
        //! Indice type for models
        typedef ushort TIndex;

        /** Default constructor.
         *  @param pVertices Pointer on vertex array.
         *  @param nVerticesCount Number of vertices.
         *  @param pIndices Pointer on index array.
         *  @param nIndicesCount Number of indices.
         *  @param strTexture Path of the model texture.
         */
        CModel(const TVertex* pVertices, ulong nVerticesCount, const TIndex* pIndices, ulong nIndicesCount, const String& strTexture);

        CModel(const TVertex* pVertices, ulong nVerticesCount, const TIndex* pIndices, ulong nIndicesCount, CImage* pImage);

        /** Destructor.
         */
        ~CModel();

        /** Render the model.
         */
        void Render() const;

        /** Return the texture of the model.
         *  @return Reference on the texture.
         */
        CTexture* GetTexture()
        { return m_pTexture; }

        /** Set the texture of the model.
         *  @param strTexture Path of the model texture.
         */
        void SetTexture(const String& strTexture)
        { 
            if (strTexture != "")
            {
                KEOS_DELETE(m_pTexture);
                m_pTexture = new CTexture();
                m_pTexture->CreateFromFile(strTexture, PXF_A8R8G8B8);
            }
        }

    private :

        //! Vertex declaration
        CDeclarationPtr  m_pDeclaration;
        //! Vertex buffer of the model
        CBuffer<TVertex> m_VertexBuffer;
        //! Index buffer du model
        CBuffer<TIndex>  m_IndexBuffer;
        //! Texture of the model
        CTexture*        m_pTexture;
    };

    //! Smart pointer for CModel
    typedef CSmartPtr<CModel, CCOMRefCounted> TModelPtr;

} // namespace Keos


#endif // KEOS_MODEL_H
