source: OGRE/trunk/ogrenew/RenderSystems/Direct3D9/src/OgreD3D9VertexDeclaration.cpp @ 657

Revision 657, 5.3 KB checked in by mattausch, 19 years ago (diff)

added ogre dependencies and patched ogre sources

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://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
30namespace 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::modifyElement(unsigned short elem_index,
66        unsigned short source, size_t offset, VertexElementType theType,
67        VertexElementSemantic semantic, unsigned short index)
68    {
69        VertexDeclaration::modifyElement(elem_index, source, offset, theType, semantic, index);
70        mNeedsRebuild = true;
71    }
72    //-----------------------------------------------------------------------
73    LPDIRECT3DVERTEXDECLARATION9 D3D9VertexDeclaration::getD3DVertexDeclaration(void)
74    {
75        if (mNeedsRebuild)
76        {
77            SAFE_RELEASE(mlpD3DDecl);
78            // Create D3D elements
79            D3DVERTEXELEMENT9* d3delems = new D3DVERTEXELEMENT9[mElementList.size() + 1];
80
81            VertexElementList::const_iterator i, iend;
82            unsigned int idx;
83            iend = mElementList.end();
84            for (idx = 0, i = mElementList.begin(); i != iend; ++i, ++idx)
85            {
86                d3delems[idx].Method = D3DDECLMETHOD_DEFAULT;
87                d3delems[idx].Offset = static_cast<WORD>(i->getOffset());
88                d3delems[idx].Stream = i->getSource();
89                                d3delems[idx].Type = D3D9Mappings::get(i->getType());
90                                d3delems[idx].Usage = D3D9Mappings::get(i->getSemantic());
91                                // NB force index if colours since D3D uses the same usage for
92                                // diffuse & specular
93                                if (i->getSemantic() == VES_SPECULAR)
94                                {
95                                        d3delems[idx].UsageIndex = 1;
96                                }
97                                else if (i->getSemantic() == VES_DIFFUSE)
98                                {
99                                        d3delems[idx].UsageIndex = 0;
100                                }
101                                else
102                                {
103                                        d3delems[idx].UsageIndex = i->getIndex();
104                                }
105            }
106            // Add terminator
107                    d3delems[idx].Stream = 0xff;
108                    d3delems[idx].Offset = 0;
109                    d3delems[idx].Type = D3DDECLTYPE_UNUSED;
110                    d3delems[idx].Method = 0;
111                    d3delems[idx].Usage = 0;
112                    d3delems[idx].UsageIndex = 0;
113           
114            HRESULT hr = mlpD3DDevice->CreateVertexDeclaration(d3delems, &mlpD3DDecl);
115
116            if (FAILED(hr))
117            {
118                OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
119                    "Cannot create D3D9 vertex declaration: " +
120                    Root::getSingleton().getErrorDescription(hr),
121                    "Direct3D9VertexDeclaration::getD3DVertexDeclaration");
122            }
123
124            delete [] d3delems;
125
126                        mNeedsRebuild = false;
127
128        }
129        return mlpD3DDecl;
130    }
131    //-----------------------------------------------------------------------
132
133
134}
135
Note: See TracBrowser for help on using the repository browser.