Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# KeosModel.cpp

Go to the documentation of this file.

    00001 /*
    00002  * This source file is part of KEOS (Free 3D Engine)
    00003  * For the latest info, see http://www.keosengine.org/
    00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it under
    00007  * the terms of the GNU Lesser General Public License as published by the Free Software
    00008  * Foundation; either version 2 of the License, or (at your option) any later
    00009  * version.
    00010  *
    00011  * This program is distributed in the hope that it will be useful, but WITHOUT
    00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU Lesser General Public License along with
    00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00018  * http://www.gnu.org/copyleft/lesser.txt.
    00019  *
    00020  */
    00021
    00022 #include "KeosModel.h"
    00023 #include "KeosException.h"
    00024 #include "KeosRoot.h"
    00025 #include "KeosRenderSystem.h"
    00026 #include "KeosTexture.h"
    00027 #include "KeosImage.h"
    00028
    00029 namespace Keos
    00030 {
    00031     //@todo hummm.....
    00032     #define Renderer CRoot::Instance().GetRenderSystem()
    00033
    00034
    00035     //=======================================================================
    00036     // CModel implementation
    00037     //=======================================================================
    00038
    00039     //-----------------------------------------------------------------------
    00040     CModel::CModel(const CModel::TVertex* pVertices, ulong nVerticesCount,
    00041                    const CModel::TIndex* pIndices, ulong nIndicesCount,
    00042                    const String& strTexture)
    00043     {
    00044         Assert(pVertices != NULL);
    00045         Assert(pIndices != NULL);
    00046
    00047         TDeclarationElement Elements[] =
    00048         {
    00049             {0, ELT_USAGE_POSITION,     ELT_TYPE_FLOAT3},
    00050             {0, ELT_USAGE_NORMAL,       ELT_TYPE_FLOAT3},
    00051             {0, ELT_USAGE_DIFFUSE,      ELT_TYPE_COLOR},
    00052             {0, ELT_USAGE_TEXCOORD0,    ELT_TYPE_FLOAT2}
    00053         };
    00054
    00055         m_pDeclaration  = Renderer->CreateVertexDeclaration(Elements);
    00056         m_VertexBuffer  = Renderer->CreateVertexBuffer(nVerticesCount, 0, pVertices);
    00057         m_IndexBuffer   = Renderer->CreateIndexBuffer(nIndicesCount, 0, pIndices);
    00058
    00059         m_pTexture = NULL;
    00060         if (strTexture != "")
    00061         {
    00062             m_pTexture = new CTexture();
    00063             m_pTexture->CreateFromFile(strTexture, PXF_A8R8G8B8);
    00064         }
    00065     }
    00066
    00067     CModel::CModel(const TVertex* pVertices, ulong nVerticesCount,
    00068                    const TIndex* pIndices, ulong nIndicesCount, CImage* pImage)
    00069     {
    00070         Assert(pVertices != NULL);
    00071         Assert(pIndices != NULL);
    00072
    00073         TDeclarationElement Elements[] =
    00074         {
    00075             {0, ELT_USAGE_POSITION,     ELT_TYPE_FLOAT3},
    00076             {0, ELT_USAGE_NORMAL,       ELT_TYPE_FLOAT3},
    00077             {0, ELT_USAGE_DIFFUSE,      ELT_TYPE_COLOR},
    00078             {0, ELT_USAGE_TEXCOORD0,    ELT_TYPE_FLOAT2}
    00079         };
    00080
    00081         m_pDeclaration  = Renderer->CreateVertexDeclaration(Elements);
    00082         m_VertexBuffer  = Renderer->CreateVertexBuffer(nVerticesCount, 0, pVertices);
    00083         m_IndexBuffer   = Renderer->CreateIndexBuffer(nIndicesCount, 0, pIndices);
    00084
    00085         m_pTexture = NULL;
    00086         if (pImage != NULL)
    00087         {
    00088             m_pTexture = new CTexture();
    00089             m_pTexture->CreateFromImage(*pImage, pImage->GetFormat());
    00090         }
    00091     }
    00092
    00093     //-----------------------------------------------------------------------
    00094     CModel::~CModel()
    00095     {
    00096         KEOS_DELETE(m_pTexture);
    00097     }
    00098
    00099     //-----------------------------------------------------------------------
    00100     void CModel::Render() const
    00101     {
    00102         if (m_pTexture)
    00103             Renderer->SetTexture(0, m_pTexture->GetTexture());
    00104         Renderer->SetDeclaration(m_pDeclaration);
    00105         Renderer->SetVertexBuffer(0, m_VertexBuffer);
    00106         Renderer->SetIndexBuffer(m_IndexBuffer);
    00107         Renderer->DrawIndexedPrimitives(PT_TRIANGLELIST, 0, m_IndexBuffer.GetCount() / 3);
    00108     }
    00109
    00110 } // namespace Keos