Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreD3D9VertexDeclaration.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2003 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreD3D9VertexDeclaration.h"
00026 #include "OgreD3D9Mappings.h"
00027 
00028 namespace Ogre {
00029 
00030     //-----------------------------------------------------------------------
00031     D3D9VertexDeclaration::D3D9VertexDeclaration(LPDIRECT3DDEVICE9 device) 
00032         : mlpD3DDevice(device), mlpD3DDecl(NULL), mNeedsRebuild(true)
00033     {
00034     }
00035     //-----------------------------------------------------------------------
00036     D3D9VertexDeclaration::~D3D9VertexDeclaration()
00037     {
00038         SAFE_RELEASE(mlpD3DDecl);
00039     }
00040     //-----------------------------------------------------------------------
00041     const VertexElement& D3D9VertexDeclaration::addElement(unsigned short source, 
00042         size_t offset, VertexElementType theType,
00043         VertexElementSemantic semantic, unsigned short index)
00044     {
00045         mNeedsRebuild = true;
00046         return VertexDeclaration::addElement(source, offset, theType, semantic, index);
00047     }
00048     //-----------------------------------------------------------------------
00049     void D3D9VertexDeclaration::removeElement(unsigned short elem_index)
00050     {
00051         VertexDeclaration::removeElement(elem_index);
00052         mNeedsRebuild = true;
00053     }
00054     //-----------------------------------------------------------------------
00055     void D3D9VertexDeclaration::modifyElement(unsigned short elem_index, 
00056         unsigned short source, size_t offset, VertexElementType theType,
00057         VertexElementSemantic semantic, unsigned short index)
00058     {
00059         VertexDeclaration::modifyElement(elem_index, source, offset, theType, semantic, index);
00060         mNeedsRebuild = true;
00061     }
00062     //-----------------------------------------------------------------------
00063     LPDIRECT3DVERTEXDECLARATION9 D3D9VertexDeclaration::getD3DVertexDeclaration(void)
00064     {
00065         if (mNeedsRebuild)
00066         {
00067             SAFE_RELEASE(mlpD3DDecl);
00068             // Create D3D elements
00069             D3DVERTEXELEMENT9* d3delems = new D3DVERTEXELEMENT9[mElementList.size() + 1];
00070 
00071             VertexElementList::const_iterator i, iend;
00072             unsigned int idx;
00073             iend = mElementList.end();
00074             for (idx = 0, i = mElementList.begin(); i != iend; ++i, ++idx)
00075             {
00076                 d3delems[idx].Method = D3DDECLMETHOD_DEFAULT;
00077                 d3delems[idx].Offset = static_cast<WORD>(i->getOffset());
00078                 d3delems[idx].Stream = i->getSource();
00079                 d3delems[idx].Type = D3D9Mappings::get(i->getType());
00080                 d3delems[idx].Usage = D3D9Mappings::get(i->getSemantic());
00081                 // NB force index if colours since D3D uses the same usage for 
00082                 // diffuse & specular
00083                 if (i->getSemantic() == VES_SPECULAR)
00084                 {
00085                     d3delems[idx].UsageIndex = 1;
00086                 }
00087                 else if (i->getSemantic() == VES_DIFFUSE)
00088                 {
00089                     d3delems[idx].UsageIndex = 0;
00090                 }
00091                 else
00092                 {
00093                     d3delems[idx].UsageIndex = i->getIndex();
00094                 }
00095             }
00096             // Add terminator
00097             d3delems[idx].Stream = 0xff;
00098             d3delems[idx].Offset = 0;
00099             d3delems[idx].Type = D3DDECLTYPE_UNUSED;
00100             d3delems[idx].Method = 0;
00101             d3delems[idx].Usage = 0;
00102             d3delems[idx].UsageIndex = 0;
00103             
00104             HRESULT hr = mlpD3DDevice->CreateVertexDeclaration(d3delems, &mlpD3DDecl);
00105 
00106             delete [] d3delems;
00107 
00108             mNeedsRebuild = false;
00109 
00110         }
00111         return mlpD3DDecl;
00112     }
00113     //-----------------------------------------------------------------------
00114 
00115 
00116 }
00117 

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:09 2004