[692] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #include "OgreD3D9VertexDeclaration.h"
|
---|
| 26 | #include "OgreD3D9Mappings.h"
|
---|
| 27 | #include "OgreException.h"
|
---|
| 28 | #include "OgreRoot.h"
|
---|
| 29 |
|
---|
| 30 | namespace Ogre {
|
---|
| 31 |
|
---|
| 32 | //-----------------------------------------------------------------------
|
---|
| 33 | D3D9VertexDeclaration::D3D9VertexDeclaration(LPDIRECT3DDEVICE9 device)
|
---|
| 34 | : mlpD3DDevice(device), mlpD3DDecl(NULL), mNeedsRebuild(true)
|
---|
| 35 | {
|
---|
| 36 | }
|
---|
| 37 | //-----------------------------------------------------------------------
|
---|
| 38 | D3D9VertexDeclaration::~D3D9VertexDeclaration()
|
---|
| 39 | {
|
---|
| 40 | SAFE_RELEASE(mlpD3DDecl);
|
---|
| 41 | }
|
---|
| 42 | //-----------------------------------------------------------------------
|
---|
| 43 | const VertexElement& D3D9VertexDeclaration::addElement(unsigned short source,
|
---|
| 44 | size_t offset, VertexElementType theType,
|
---|
| 45 | VertexElementSemantic semantic, unsigned short index)
|
---|
| 46 | {
|
---|
| 47 | mNeedsRebuild = true;
|
---|
| 48 | return VertexDeclaration::addElement(source, offset, theType, semantic, index);
|
---|
| 49 | }
|
---|
| 50 | //-----------------------------------------------------------------------------
|
---|
| 51 | const VertexElement& D3D9VertexDeclaration::insertElement(unsigned short atPosition,
|
---|
| 52 | unsigned short source, size_t offset, VertexElementType theType,
|
---|
| 53 | VertexElementSemantic semantic, unsigned short index)
|
---|
| 54 | {
|
---|
| 55 | mNeedsRebuild = true;
|
---|
| 56 | return VertexDeclaration::insertElement(atPosition, source, offset, theType, semantic, index);
|
---|
| 57 | }
|
---|
| 58 | //-----------------------------------------------------------------------
|
---|
| 59 | void D3D9VertexDeclaration::removeElement(unsigned short elem_index)
|
---|
| 60 | {
|
---|
| 61 | VertexDeclaration::removeElement(elem_index);
|
---|
| 62 | mNeedsRebuild = true;
|
---|
| 63 | }
|
---|
| 64 | //-----------------------------------------------------------------------
|
---|
| 65 | void D3D9VertexDeclaration::removeElement(VertexElementSemantic semantic, unsigned short index)
|
---|
| 66 | {
|
---|
| 67 | VertexDeclaration::removeElement(semantic, index);
|
---|
| 68 | mNeedsRebuild = true;
|
---|
| 69 | }
|
---|
| 70 | //-----------------------------------------------------------------------
|
---|
| 71 | void D3D9VertexDeclaration::removeAllElements(void)
|
---|
| 72 | {
|
---|
| 73 | VertexDeclaration::removeAllElements();
|
---|
| 74 | mNeedsRebuild = true;
|
---|
| 75 | }
|
---|
| 76 | //-----------------------------------------------------------------------
|
---|
| 77 | void D3D9VertexDeclaration::modifyElement(unsigned short elem_index,
|
---|
| 78 | unsigned short source, size_t offset, VertexElementType theType,
|
---|
| 79 | VertexElementSemantic semantic, unsigned short index)
|
---|
| 80 | {
|
---|
| 81 | VertexDeclaration::modifyElement(elem_index, source, offset, theType, semantic, index);
|
---|
| 82 | mNeedsRebuild = true;
|
---|
| 83 | }
|
---|
| 84 | //-----------------------------------------------------------------------
|
---|
| 85 | LPDIRECT3DVERTEXDECLARATION9 D3D9VertexDeclaration::getD3DVertexDeclaration(void)
|
---|
| 86 | {
|
---|
| 87 | if (mNeedsRebuild)
|
---|
| 88 | {
|
---|
| 89 | SAFE_RELEASE(mlpD3DDecl);
|
---|
| 90 | // Create D3D elements
|
---|
| 91 | D3DVERTEXELEMENT9* d3delems = new D3DVERTEXELEMENT9[mElementList.size() + 1];
|
---|
| 92 |
|
---|
| 93 | VertexElementList::const_iterator i, iend;
|
---|
| 94 | unsigned int idx;
|
---|
| 95 | iend = mElementList.end();
|
---|
| 96 | for (idx = 0, i = mElementList.begin(); i != iend; ++i, ++idx)
|
---|
| 97 | {
|
---|
| 98 | d3delems[idx].Method = D3DDECLMETHOD_DEFAULT;
|
---|
| 99 | d3delems[idx].Offset = static_cast<WORD>(i->getOffset());
|
---|
| 100 | d3delems[idx].Stream = i->getSource();
|
---|
| 101 | d3delems[idx].Type = D3D9Mappings::get(i->getType());
|
---|
| 102 | d3delems[idx].Usage = D3D9Mappings::get(i->getSemantic());
|
---|
| 103 | // NB force index if colours since D3D uses the same usage for
|
---|
| 104 | // diffuse & specular
|
---|
| 105 | if (i->getSemantic() == VES_SPECULAR)
|
---|
| 106 | {
|
---|
| 107 | d3delems[idx].UsageIndex = 1;
|
---|
| 108 | }
|
---|
| 109 | else if (i->getSemantic() == VES_DIFFUSE)
|
---|
| 110 | {
|
---|
| 111 | d3delems[idx].UsageIndex = 0;
|
---|
| 112 | }
|
---|
| 113 | else
|
---|
| 114 | {
|
---|
| 115 | d3delems[idx].UsageIndex = i->getIndex();
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | // Add terminator
|
---|
| 119 | d3delems[idx].Stream = 0xff;
|
---|
| 120 | d3delems[idx].Offset = 0;
|
---|
| 121 | d3delems[idx].Type = D3DDECLTYPE_UNUSED;
|
---|
| 122 | d3delems[idx].Method = 0;
|
---|
| 123 | d3delems[idx].Usage = 0;
|
---|
| 124 | d3delems[idx].UsageIndex = 0;
|
---|
| 125 |
|
---|
| 126 | HRESULT hr = mlpD3DDevice->CreateVertexDeclaration(d3delems, &mlpD3DDecl);
|
---|
| 127 |
|
---|
| 128 | if (FAILED(hr))
|
---|
| 129 | {
|
---|
| 130 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
|
---|
| 131 | "Cannot create D3D9 vertex declaration: " +
|
---|
| 132 | Root::getSingleton().getErrorDescription(hr),
|
---|
| 133 | "Direct3D9VertexDeclaration::getD3DVertexDeclaration");
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | delete [] d3delems;
|
---|
| 137 |
|
---|
| 138 | mNeedsRebuild = false;
|
---|
| 139 |
|
---|
| 140 | }
|
---|
| 141 | return mlpD3DDecl;
|
---|
| 142 | }
|
---|
| 143 | //-----------------------------------------------------------------------
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | }
|
---|
| 147 |
|
---|