/*
 * 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.
 *
 */

#include "KeosModel.h"
#include "KeosException.h"
#include "KeosRoot.h"
#include "KeosRenderSystem.h"
#include "KeosTexture.h"
#include "KeosImage.h"

namespace Keos
{
    //@todo hummm.....
    #define Renderer CRoot::Instance().GetRenderSystem()


    //=======================================================================
    // CModel implementation
    //=======================================================================

    //-----------------------------------------------------------------------
    CModel::CModel(const CModel::TVertex* pVertices, ulong nVerticesCount, 
                   const CModel::TIndex* pIndices, ulong nIndicesCount, 
                   const String& strTexture)
    {
        Assert(pVertices != NULL);
        Assert(pIndices != NULL);

        TDeclarationElement Elements[] =
        {
            {0, ELT_USAGE_POSITION,     ELT_TYPE_FLOAT3},
            {0, ELT_USAGE_NORMAL,       ELT_TYPE_FLOAT3},
            {0, ELT_USAGE_DIFFUSE,      ELT_TYPE_COLOR},
            {0, ELT_USAGE_TEXCOORD0,    ELT_TYPE_FLOAT2}
        };

        m_pDeclaration  = Renderer->CreateVertexDeclaration(Elements);
        m_VertexBuffer  = Renderer->CreateVertexBuffer(nVerticesCount, 0, pVertices);
        m_IndexBuffer   = Renderer->CreateIndexBuffer(nIndicesCount, 0, pIndices);

        m_pTexture = NULL;
        if (strTexture != "")
        {
            m_pTexture = new CTexture();
            m_pTexture->CreateFromFile(strTexture, PXF_A8R8G8B8);
        }
    }

    CModel::CModel(const TVertex* pVertices, ulong nVerticesCount, 
                   const TIndex* pIndices, ulong nIndicesCount, CImage* pImage)
    {
        Assert(pVertices != NULL);
        Assert(pIndices != NULL);

        TDeclarationElement Elements[] =
        {
            {0, ELT_USAGE_POSITION,     ELT_TYPE_FLOAT3},
            {0, ELT_USAGE_NORMAL,       ELT_TYPE_FLOAT3},
            {0, ELT_USAGE_DIFFUSE,      ELT_TYPE_COLOR},
            {0, ELT_USAGE_TEXCOORD0,    ELT_TYPE_FLOAT2}
        };

        m_pDeclaration  = Renderer->CreateVertexDeclaration(Elements);
        m_VertexBuffer  = Renderer->CreateVertexBuffer(nVerticesCount, 0, pVertices);
        m_IndexBuffer   = Renderer->CreateIndexBuffer(nIndicesCount, 0, pIndices);

        m_pTexture = NULL;
        if (pImage != NULL)
        {
            m_pTexture = new CTexture();
            m_pTexture->CreateFromImage(*pImage, pImage->GetFormat());
        }
    }

    //-----------------------------------------------------------------------
    CModel::~CModel()
    {
        KEOS_DELETE(m_pTexture);
    }

    //-----------------------------------------------------------------------
    void CModel::Render() const
    {
        if (m_pTexture)
            Renderer->SetTexture(0, m_pTexture->GetTexture());
        Renderer->SetDeclaration(m_pDeclaration);
        Renderer->SetVertexBuffer(0, m_VertexBuffer);
        Renderer->SetIndexBuffer(m_IndexBuffer);
        Renderer->DrawIndexedPrimitives(PT_TRIANGLELIST, 0, m_IndexBuffer.GetCount() / 3);
    }

} // namespace Keos
