source: OGRE/trunk/ogrenew/OgreMain/src/OgreSubMesh.cpp @ 692

Revision 692, 8.0 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

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 "OgreStableHeaders.h"
26#include "OgreSubMesh.h"
27
28#include "OgreMesh.h"
29#include "OgreException.h"
30#include "OgreMeshManager.h"
31#include "OgreMaterialManager.h"
32#include "OgreStringConverter.h"
33
34namespace Ogre {
35    //-----------------------------------------------------------------------
36    SubMesh::SubMesh()
37        : useSharedVertices(true)
38        , operationType(RenderOperation::OT_TRIANGLE_LIST)
39        , vertexData(0)
40        , mMatInitialised(false)
41        , mBoneAssignmentsOutOfDate(false)
42                , mVertexAnimationType(VAT_NONE)
43    {
44                indexData = new IndexData();
45    }
46    //-----------------------------------------------------------------------
47    SubMesh::~SubMesh()
48    {
49        delete vertexData;
50                delete indexData;
51
52                removeLodLevels();
53    }
54
55    //-----------------------------------------------------------------------
56    void SubMesh::setMaterialName(const String& name)
57    {
58        mMaterialName = name;
59        mMatInitialised = true;
60    }
61    //-----------------------------------------------------------------------
62    const String& SubMesh::getMaterialName() const
63    {
64        return mMaterialName;
65    }
66    //-----------------------------------------------------------------------
67    bool SubMesh::isMatInitialised(void) const
68    {
69        return mMatInitialised;
70
71    }
72    //-----------------------------------------------------------------------
73    void SubMesh::_getRenderOperation(RenderOperation& ro, ushort lodIndex)
74    {
75
76                // SubMeshes always use indexes
77        ro.useIndexes = true;
78                if (lodIndex > 0 && static_cast< size_t >( lodIndex - 1 ) < mLodFaceList.size())
79                {
80                        // lodIndex - 1 because we don't store full detail version in mLodFaceList
81                        ro.indexData = mLodFaceList[lodIndex-1];
82        }
83        else
84        {
85                ro.indexData = indexData;
86        }
87                ro.operationType = operationType;
88                ro.vertexData = useSharedVertices? parent->sharedVertexData : vertexData;
89
90    }
91    //-----------------------------------------------------------------------
92    void SubMesh::addBoneAssignment(const VertexBoneAssignment& vertBoneAssign)
93    {
94        if (useSharedVertices)
95        {
96            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "This SubMesh uses shared geometry,  you "
97                "must assign bones to the Mesh, not the SubMesh", "SubMesh.addBoneAssignment");
98        }
99        mBoneAssignments.insert(
100            VertexBoneAssignmentList::value_type(vertBoneAssign.vertexIndex, vertBoneAssign));
101        mBoneAssignmentsOutOfDate = true;
102    }
103    //-----------------------------------------------------------------------
104    void SubMesh::clearBoneAssignments(void)
105    {
106        mBoneAssignments.clear();
107        mBoneAssignmentsOutOfDate = true;
108    }
109
110    //-----------------------------------------------------------------------
111    void SubMesh::_compileBoneAssignments(void)
112    {
113        unsigned short maxBones =
114            parent->_rationaliseBoneAssignments(vertexData->vertexCount, mBoneAssignments);
115
116        if (maxBones != 0)
117        {
118            parent->compileBoneAssignments(mBoneAssignments, maxBones,
119                blendIndexToBoneIndexMap, vertexData);
120        }
121
122        mBoneAssignmentsOutOfDate = false;
123    }
124    //---------------------------------------------------------------------
125    SubMesh::BoneAssignmentIterator SubMesh::getBoneAssignmentIterator(void)
126    {
127        return BoneAssignmentIterator(mBoneAssignments.begin(),
128            mBoneAssignments.end());
129    }
130    //---------------------------------------------------------------------
131    SubMesh::AliasTextureIterator SubMesh::getAliasTextureIterator(void) const
132    {
133        return AliasTextureIterator(mTextureAliases.begin(),
134            mTextureAliases.end());
135    }
136    //---------------------------------------------------------------------
137    void SubMesh::addTextureAlias(const String& aliasName, const String& textureName)
138    {
139        mTextureAliases[aliasName] = textureName;
140    }
141    //---------------------------------------------------------------------
142    void SubMesh::removeTextureAlias(const String& aliasName)
143    {
144        mTextureAliases.erase(aliasName);
145    }
146    //---------------------------------------------------------------------
147    void SubMesh::removeAllTextureAliases(void)
148    {
149        mTextureAliases.clear();
150    }
151    //---------------------------------------------------------------------
152    bool SubMesh::updateMaterialUsingTextureAliases(void)
153    {
154        bool newMaterialCreated = false;
155        // if submesh has texture aliases
156        // ask the material manager if the current summesh material exists
157        if (hasTextureAliases() && MaterialManager::getSingleton().resourceExists(mMaterialName))
158        {
159            // get the current submesh material
160            MaterialPtr material = MaterialManager::getSingleton().getByName( mMaterialName );
161            // get test result for if change will occur when the texture aliases are applied
162            if (material->applyTextureAliases(mTextureAliases, false))
163            {
164                // material textures will be changed so copy material,
165                // new material name is old material name + index
166                // check with material manager and find a unique name
167                size_t index = 0;
168                String newMaterialName = mMaterialName + "_" + StringConverter::toString(index);
169                while (MaterialManager::getSingleton().resourceExists(newMaterialName))
170                {
171                    // increment index for next name
172                    newMaterialName = mMaterialName + "_" + StringConverter::toString(++index);
173                }
174
175                Ogre::MaterialPtr newMaterial = Ogre::MaterialManager::getSingleton().create(
176                    newMaterialName, material->getGroup());
177                // copy parent material details to new material
178                material->copyDetailsTo(newMaterial);
179                // apply texture aliases to new material
180                newMaterial->applyTextureAliases(mTextureAliases);
181                // place new material name in submesh
182                setMaterialName(newMaterialName);
183                newMaterialCreated = true;
184            }
185        }
186
187        return newMaterialCreated;
188    }
189    //---------------------------------------------------------------------
190    void SubMesh::removeLodLevels(void)
191    {
192        ProgressiveMesh::LODFaceList::iterator lodi, lodend;
193                lodend = mLodFaceList.end();
194                for (lodi = mLodFaceList.begin(); lodi != lodend; ++lodi)
195                {
196                        delete *lodi;
197                }
198
199        mLodFaceList.clear();
200
201    }
202        //---------------------------------------------------------------------
203        VertexAnimationType SubMesh::getVertexAnimationType(void) const
204        {
205                if(parent->_getAnimationTypesDirty())
206                {
207                        parent->_determineAnimationTypes();
208                }
209                return mVertexAnimationType;
210        }
211
212
213}
214
Note: See TracBrowser for help on using the repository browser.