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 #include "OgreException.h"
00028 #include "OgreRoot.h"
00029 
00030 namespace Ogre {
00031 
00032     //-----------------------------------------------------------------------
00033     D3D9VertexDeclaration::D3D9VertexDeclaration(LPDIRECT3DDEVICE9 device) 
00034         : mlpD3DDevice(device), mlpD3DDecl(NULL), mNeedsRebuild(true)
00035     {
00036     }
00037     //-----------------------------------------------------------------------
00038     D3D9VertexDeclaration::~D3D9VertexDeclaration()
00039     {
00040         SAFE_RELEASE(mlpD3DDecl);
00041     }
00042     //-----------------------------------------------------------------------
00043     const VertexElement& D3D9VertexDeclaration::addElement(unsigned short source, 
00044         size_t offset, VertexElementType theType,
00045         VertexElementSemantic semantic, unsigned short index)
00046     {
00047         mNeedsRebuild = true;
00048         return VertexDeclaration::addElement(source, offset, theType, semantic, index);
00049     }
00050     //-----------------------------------------------------------------------------
00051     const VertexElement& D3D9VertexDeclaration::insertElement(unsigned short atPosition,
00052         unsigned short source, size_t offset, VertexElementType theType,
00053         VertexElementSemantic semantic, unsigned short index)
00054     {
00055         mNeedsRebuild = true;
00056         return VertexDeclaration::insertElement(atPosition, source, offset, theType, semantic, index);
00057     }
00058     //-----------------------------------------------------------------------
00059     void D3D9VertexDeclaration::removeElement(unsigned short elem_index)
00060     {
00061         VertexDeclaration::removeElement(elem_index);
00062         mNeedsRebuild = true;
00063     }
00064     //-----------------------------------------------------------------------
00065     void D3D9VertexDeclaration::modifyElement(unsigned short elem_index, 
00066         unsigned short source, size_t offset, VertexElementType theType,
00067         VertexElementSemantic semantic, unsigned short index)
00068     {
00069         VertexDeclaration::modifyElement(elem_index, source, offset, theType, semantic, index);
00070         mNeedsRebuild = true;
00071     }
00072     //-----------------------------------------------------------------------
00073     LPDIRECT3DVERTEXDECLARATION9 D3D9VertexDeclaration::getD3DVertexDeclaration(void)
00074     {
00075         if (mNeedsRebuild)
00076         {
00077             SAFE_RELEASE(mlpD3DDecl);
00078             // Create D3D elements
00079             D3DVERTEXELEMENT9* d3delems = new D3DVERTEXELEMENT9[mElementList.size() + 1];
00080 
00081             VertexElementList::const_iterator i, iend;
00082             unsigned int idx;
00083             iend = mElementList.end();
00084             for (idx = 0, i = mElementList.begin(); i != iend; ++i, ++idx)
00085             {
00086                 d3delems[idx].Method = D3DDECLMETHOD_DEFAULT;
00087                 d3delems[idx].Offset = static_cast<WORD>(i->getOffset());
00088                 d3delems[idx].Stream = i->getSource();
00089                 d3delems[idx].Type = D3D9Mappings::get(i->getType());
00090                 d3delems[idx].Usage = D3D9Mappings::get(i->getSemantic());
00091                 // NB force index if colours since D3D uses the same usage for 
00092                 // diffuse & specular
00093                 if (i->getSemantic() == VES_SPECULAR)
00094                 {
00095                     d3delems[idx].UsageIndex = 1;
00096                 }
00097                 else if (i->getSemantic() == VES_DIFFUSE)
00098                 {
00099                     d3delems[idx].UsageIndex = 0;
00100                 }
00101                 else
00102                 {
00103                     d3delems[idx].UsageIndex = i->getIndex();
00104                 }
00105             }
00106             // Add terminator
00107             d3delems[idx].Stream = 0xff;
00108             d3delems[idx].Offset = 0;
00109             d3delems[idx].Type = D3DDECLTYPE_UNUSED;
00110             d3delems[idx].Method = 0;
00111             d3delems[idx].Usage = 0;
00112             d3delems[idx].UsageIndex = 0;
00113             
00114             HRESULT hr = mlpD3DDevice->CreateVertexDeclaration(d3delems, &mlpD3DDecl);
00115 
00116             if (FAILED(hr))
00117             {
00118                 Except(Exception::ERR_INTERNAL_ERROR, 
00119                     "Cannot create D3D9 vertex declaration: " + 
00120                     Root::getSingleton().getErrorDescription(hr), 
00121                     "Direct3D9VertexDeclaration::getD3DVertexDeclaration");
00122             }
00123 
00124             delete [] d3delems;
00125 
00126             mNeedsRebuild = false;
00127 
00128         }
00129         return mlpD3DDecl;
00130     }
00131     //-----------------------------------------------------------------------
00132 
00133 
00134 }
00135 

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:22 2004