| 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-2006 Torus Knot Software Ltd |
|---|
| 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 | You may alternatively use this source under the terms of a specific version of |
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
|---|
| 26 | Torus Knot Software Ltd. |
|---|
| 27 | ----------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | #include "OgreD3D9VertexDeclaration.h" |
|---|
| 30 | #include "OgreD3D9Mappings.h" |
|---|
| 31 | #include "OgreException.h" |
|---|
| 32 | #include "OgreRoot.h" |
|---|
| 33 | |
|---|
| 34 | namespace Ogre { |
|---|
| 35 | |
|---|
| 36 | //----------------------------------------------------------------------- |
|---|
| 37 | D3D9VertexDeclaration::D3D9VertexDeclaration(LPDIRECT3DDEVICE9 device) |
|---|
| 38 | : mlpD3DDevice(device), mlpD3DDecl(NULL), mNeedsRebuild(true) |
|---|
| 39 | { |
|---|
| 40 | } |
|---|
| 41 | //----------------------------------------------------------------------- |
|---|
| 42 | D3D9VertexDeclaration::~D3D9VertexDeclaration() |
|---|
| 43 | { |
|---|
| 44 | SAFE_RELEASE(mlpD3DDecl); |
|---|
| 45 | } |
|---|
| 46 | //----------------------------------------------------------------------- |
|---|
| 47 | const VertexElement& D3D9VertexDeclaration::addElement(unsigned short source, |
|---|
| 48 | size_t offset, VertexElementType theType, |
|---|
| 49 | VertexElementSemantic semantic, unsigned short index) |
|---|
| 50 | { |
|---|
| 51 | mNeedsRebuild = true; |
|---|
| 52 | return VertexDeclaration::addElement(source, offset, theType, semantic, index); |
|---|
| 53 | } |
|---|
| 54 | //----------------------------------------------------------------------------- |
|---|
| 55 | const VertexElement& D3D9VertexDeclaration::insertElement(unsigned short atPosition, |
|---|
| 56 | unsigned short source, size_t offset, VertexElementType theType, |
|---|
| 57 | VertexElementSemantic semantic, unsigned short index) |
|---|
| 58 | { |
|---|
| 59 | mNeedsRebuild = true; |
|---|
| 60 | return VertexDeclaration::insertElement(atPosition, source, offset, theType, semantic, index); |
|---|
| 61 | } |
|---|
| 62 | //----------------------------------------------------------------------- |
|---|
| 63 | void D3D9VertexDeclaration::removeElement(unsigned short elem_index) |
|---|
| 64 | { |
|---|
| 65 | VertexDeclaration::removeElement(elem_index); |
|---|
| 66 | mNeedsRebuild = true; |
|---|
| 67 | } |
|---|
| 68 | //----------------------------------------------------------------------- |
|---|
| 69 | void D3D9VertexDeclaration::removeElement(VertexElementSemantic semantic, unsigned short index) |
|---|
| 70 | { |
|---|
| 71 | VertexDeclaration::removeElement(semantic, index); |
|---|
| 72 | mNeedsRebuild = true; |
|---|
| 73 | } |
|---|
| 74 | //----------------------------------------------------------------------- |
|---|
| 75 | void D3D9VertexDeclaration::removeAllElements(void) |
|---|
| 76 | { |
|---|
| 77 | VertexDeclaration::removeAllElements(); |
|---|
| 78 | mNeedsRebuild = true; |
|---|
| 79 | } |
|---|
| 80 | //----------------------------------------------------------------------- |
|---|
| 81 | void D3D9VertexDeclaration::modifyElement(unsigned short elem_index, |
|---|
| 82 | unsigned short source, size_t offset, VertexElementType theType, |
|---|
| 83 | VertexElementSemantic semantic, unsigned short index) |
|---|
| 84 | { |
|---|
| 85 | VertexDeclaration::modifyElement(elem_index, source, offset, theType, semantic, index); |
|---|
| 86 | mNeedsRebuild = true; |
|---|
| 87 | } |
|---|
| 88 | //----------------------------------------------------------------------- |
|---|
| 89 | LPDIRECT3DVERTEXDECLARATION9 D3D9VertexDeclaration::getD3DVertexDeclaration(void) |
|---|
| 90 | { |
|---|
| 91 | if (mNeedsRebuild) |
|---|
| 92 | { |
|---|
| 93 | SAFE_RELEASE(mlpD3DDecl); |
|---|
| 94 | // Create D3D elements |
|---|
| 95 | D3DVERTEXELEMENT9* d3delems = new D3DVERTEXELEMENT9[mElementList.size() + 1]; |
|---|
| 96 | |
|---|
| 97 | VertexElementList::const_iterator i, iend; |
|---|
| 98 | unsigned int idx; |
|---|
| 99 | iend = mElementList.end(); |
|---|
| 100 | for (idx = 0, i = mElementList.begin(); i != iend; ++i, ++idx) |
|---|
| 101 | { |
|---|
| 102 | d3delems[idx].Method = D3DDECLMETHOD_DEFAULT; |
|---|
| 103 | d3delems[idx].Offset = static_cast<WORD>(i->getOffset()); |
|---|
| 104 | d3delems[idx].Stream = i->getSource(); |
|---|
| 105 | d3delems[idx].Type = D3D9Mappings::get(i->getType()); |
|---|
| 106 | d3delems[idx].Usage = D3D9Mappings::get(i->getSemantic()); |
|---|
| 107 | // NB force index if colours since D3D uses the same usage for |
|---|
| 108 | // diffuse & specular |
|---|
| 109 | if (i->getSemantic() == VES_SPECULAR) |
|---|
| 110 | { |
|---|
| 111 | d3delems[idx].UsageIndex = 1; |
|---|
| 112 | } |
|---|
| 113 | else if (i->getSemantic() == VES_DIFFUSE) |
|---|
| 114 | { |
|---|
| 115 | d3delems[idx].UsageIndex = 0; |
|---|
| 116 | } |
|---|
| 117 | else |
|---|
| 118 | { |
|---|
| 119 | d3delems[idx].UsageIndex = i->getIndex(); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | // Add terminator |
|---|
| 123 | d3delems[idx].Stream = 0xff; |
|---|
| 124 | d3delems[idx].Offset = 0; |
|---|
| 125 | d3delems[idx].Type = D3DDECLTYPE_UNUSED; |
|---|
| 126 | d3delems[idx].Method = 0; |
|---|
| 127 | d3delems[idx].Usage = 0; |
|---|
| 128 | d3delems[idx].UsageIndex = 0; |
|---|
| 129 | |
|---|
| 130 | HRESULT hr = mlpD3DDevice->CreateVertexDeclaration(d3delems, &mlpD3DDecl); |
|---|
| 131 | |
|---|
| 132 | if (FAILED(hr)) |
|---|
| 133 | { |
|---|
| 134 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, |
|---|
| 135 | "Cannot create D3D9 vertex declaration: " + |
|---|
| 136 | Root::getSingleton().getErrorDescription(hr), |
|---|
| 137 | "Direct3D9VertexDeclaration::getD3DVertexDeclaration"); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | delete [] d3delems; |
|---|
| 141 | |
|---|
| 142 | mNeedsRebuild = false; |
|---|
| 143 | |
|---|
| 144 | } |
|---|
| 145 | return mlpD3DDecl; |
|---|
| 146 | } |
|---|
| 147 | //----------------------------------------------------------------------- |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | } |
|---|
| 151 | |
|---|