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 "OgreStableHeaders.h" |
---|
30 | #include "OgreMaterialManager.h" |
---|
31 | |
---|
32 | #include "OgreMaterial.h" |
---|
33 | #include "OgreStringVector.h" |
---|
34 | #include "OgreLogManager.h" |
---|
35 | #include "OgreArchive.h" |
---|
36 | #include "OgreStringConverter.h" |
---|
37 | #include "OgreBlendMode.h" |
---|
38 | #include "OgreTechnique.h" |
---|
39 | #include "OgrePass.h" |
---|
40 | #include "OgreTextureUnitState.h" |
---|
41 | #include "OgreException.h" |
---|
42 | #include "OgreMaterialScriptCompiler.h" |
---|
43 | |
---|
44 | /** Set this to 0 if having problems with the new Material Script Compiler and want to use the original one. |
---|
45 | */ |
---|
46 | #ifndef OGRE_MATERIAL_SCRIPT_COMPILER |
---|
47 | #define OGRE_MATERIAL_SCRIPT_COMPILER 0 |
---|
48 | #endif |
---|
49 | |
---|
50 | namespace Ogre { |
---|
51 | |
---|
52 | //----------------------------------------------------------------------- |
---|
53 | template<> MaterialManager* Singleton<MaterialManager>::ms_Singleton = 0; |
---|
54 | MaterialManager* MaterialManager::getSingletonPtr(void) |
---|
55 | { |
---|
56 | return ms_Singleton; |
---|
57 | } |
---|
58 | MaterialManager& MaterialManager::getSingleton(void) |
---|
59 | { |
---|
60 | assert( ms_Singleton ); return ( *ms_Singleton ); |
---|
61 | } |
---|
62 | String MaterialManager::DEFAULT_SCHEME_NAME = "Default"; |
---|
63 | //----------------------------------------------------------------------- |
---|
64 | MaterialManager::MaterialManager() |
---|
65 | { |
---|
66 | mDefaultMinFilter = FO_LINEAR; |
---|
67 | mDefaultMagFilter = FO_LINEAR; |
---|
68 | mDefaultMipFilter = FO_POINT; |
---|
69 | mDefaultMaxAniso = 1; |
---|
70 | |
---|
71 | // Create primary thread copies of script compiler / serializer |
---|
72 | // other copies for other threads may also be instantiated |
---|
73 | #if OGRE_MATERIAL_SCRIPT_COMPILER |
---|
74 | OGRE_THREAD_POINTER_SET(mScriptCompiler, new MaterialScriptCompiler()); |
---|
75 | #endif |
---|
76 | OGRE_THREAD_POINTER_SET(mSerializer, new MaterialSerializer()); |
---|
77 | |
---|
78 | // Loading order |
---|
79 | mLoadOrder = 100.0f; |
---|
80 | // Scripting is supported by this manager |
---|
81 | mScriptPatterns.push_back("*.program"); |
---|
82 | mScriptPatterns.push_back("*.material"); |
---|
83 | ResourceGroupManager::getSingleton()._registerScriptLoader(this); |
---|
84 | |
---|
85 | // Resource type |
---|
86 | mResourceType = "Material"; |
---|
87 | |
---|
88 | // Register with resource group manager |
---|
89 | ResourceGroupManager::getSingleton()._registerResourceManager(mResourceType, this); |
---|
90 | |
---|
91 | // Default scheme |
---|
92 | mActiveSchemeIndex = 0; |
---|
93 | mActiveSchemeName = DEFAULT_SCHEME_NAME; |
---|
94 | mSchemes[mActiveSchemeName] = 0; |
---|
95 | |
---|
96 | } |
---|
97 | //----------------------------------------------------------------------- |
---|
98 | MaterialManager::~MaterialManager() |
---|
99 | { |
---|
100 | mDefaultSettings.setNull(); |
---|
101 | // Resources cleared by superclass |
---|
102 | // Unregister with resource group manager |
---|
103 | ResourceGroupManager::getSingleton()._unregisterResourceManager(mResourceType); |
---|
104 | ResourceGroupManager::getSingleton()._unregisterScriptLoader(this); |
---|
105 | |
---|
106 | // delete primary thread instances directly, other threads will delete |
---|
107 | // theirs automatically when the threads end (part of boost::thread_specific_ptr) |
---|
108 | #if OGRE_MATERIAL_SCRIPT_COMPILER |
---|
109 | OGRE_THREAD_POINTER_DELETE(mScriptCompiler); |
---|
110 | #endif |
---|
111 | OGRE_THREAD_POINTER_DELETE(mSerializer); |
---|
112 | |
---|
113 | } |
---|
114 | //----------------------------------------------------------------------- |
---|
115 | Resource* MaterialManager::createImpl(const String& name, ResourceHandle handle, |
---|
116 | const String& group, bool isManual, ManualResourceLoader* loader, |
---|
117 | const NameValuePairList* params) |
---|
118 | { |
---|
119 | return new Material(this, name, handle, group, isManual, loader); |
---|
120 | } |
---|
121 | //----------------------------------------------------------------------- |
---|
122 | void MaterialManager::initialise(void) |
---|
123 | { |
---|
124 | // Set up default material - don't use name contructor as we want to avoid applying defaults |
---|
125 | mDefaultSettings = create("DefaultSettings", ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME); |
---|
126 | // Add a single technique and pass, non-programmable |
---|
127 | mDefaultSettings->createTechnique()->createPass(); |
---|
128 | |
---|
129 | // Set up a lit base white material |
---|
130 | create("BaseWhite", ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME); |
---|
131 | // Set up an unlit base white material |
---|
132 | MaterialPtr baseWhiteNoLighting = create("BaseWhiteNoLighting", |
---|
133 | ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME); |
---|
134 | baseWhiteNoLighting->setLightingEnabled(false); |
---|
135 | |
---|
136 | } |
---|
137 | //----------------------------------------------------------------------- |
---|
138 | void MaterialManager::parseScript(DataStreamPtr& stream, const String& groupName) |
---|
139 | { |
---|
140 | // Delegate to serializer |
---|
141 | #if OGRE_MATERIAL_SCRIPT_COMPILER |
---|
142 | #if OGRE_THREAD_SUPPORT |
---|
143 | // check we have an instance for this thread (should always have one for main thread) |
---|
144 | if (!mScriptCompiler.get()) |
---|
145 | { |
---|
146 | // create a new instance for this thread - will get deleted when |
---|
147 | // the thread dies |
---|
148 | mScriptCompiler.reset(new MaterialScriptCompiler()); |
---|
149 | } |
---|
150 | #endif |
---|
151 | mScriptCompiler->parseScript(stream, groupName); |
---|
152 | #else |
---|
153 | #if OGRE_THREAD_SUPPORT |
---|
154 | // check we have an instance for this thread (should always have one for main thread) |
---|
155 | if (!mSerializer.get()) |
---|
156 | { |
---|
157 | // create a new instance for this thread - will get deleted when |
---|
158 | // the thread dies |
---|
159 | mSerializer.reset(new MaterialSerializer()); |
---|
160 | } |
---|
161 | #endif |
---|
162 | mSerializer->parseScript(stream, groupName); |
---|
163 | #endif |
---|
164 | } |
---|
165 | //----------------------------------------------------------------------- |
---|
166 | void MaterialManager::setDefaultTextureFiltering(TextureFilterOptions fo) |
---|
167 | { |
---|
168 | switch (fo) |
---|
169 | { |
---|
170 | case TFO_NONE: |
---|
171 | setDefaultTextureFiltering(FO_POINT, FO_POINT, FO_NONE); |
---|
172 | break; |
---|
173 | case TFO_BILINEAR: |
---|
174 | setDefaultTextureFiltering(FO_LINEAR, FO_LINEAR, FO_POINT); |
---|
175 | break; |
---|
176 | case TFO_TRILINEAR: |
---|
177 | setDefaultTextureFiltering(FO_LINEAR, FO_LINEAR, FO_LINEAR); |
---|
178 | break; |
---|
179 | case TFO_ANISOTROPIC: |
---|
180 | setDefaultTextureFiltering(FO_ANISOTROPIC, FO_ANISOTROPIC, FO_LINEAR); |
---|
181 | break; |
---|
182 | } |
---|
183 | } |
---|
184 | //----------------------------------------------------------------------- |
---|
185 | void MaterialManager::setDefaultAnisotropy(unsigned int maxAniso) |
---|
186 | { |
---|
187 | mDefaultMaxAniso = maxAniso; |
---|
188 | } |
---|
189 | //----------------------------------------------------------------------- |
---|
190 | unsigned int MaterialManager::getDefaultAnisotropy() const |
---|
191 | { |
---|
192 | return mDefaultMaxAniso; |
---|
193 | } |
---|
194 | //----------------------------------------------------------------------- |
---|
195 | void MaterialManager::setDefaultTextureFiltering(FilterType ftype, FilterOptions opts) |
---|
196 | { |
---|
197 | switch (ftype) |
---|
198 | { |
---|
199 | case FT_MIN: |
---|
200 | mDefaultMinFilter = opts; |
---|
201 | break; |
---|
202 | case FT_MAG: |
---|
203 | mDefaultMagFilter = opts; |
---|
204 | break; |
---|
205 | case FT_MIP: |
---|
206 | mDefaultMipFilter = opts; |
---|
207 | break; |
---|
208 | } |
---|
209 | } |
---|
210 | //----------------------------------------------------------------------- |
---|
211 | void MaterialManager::setDefaultTextureFiltering(FilterOptions minFilter, |
---|
212 | FilterOptions magFilter, FilterOptions mipFilter) |
---|
213 | { |
---|
214 | mDefaultMinFilter = minFilter; |
---|
215 | mDefaultMagFilter = magFilter; |
---|
216 | mDefaultMipFilter = mipFilter; |
---|
217 | } |
---|
218 | //----------------------------------------------------------------------- |
---|
219 | FilterOptions MaterialManager::getDefaultTextureFiltering(FilterType ftype) const |
---|
220 | { |
---|
221 | switch (ftype) |
---|
222 | { |
---|
223 | case FT_MIN: |
---|
224 | return mDefaultMinFilter; |
---|
225 | case FT_MAG: |
---|
226 | return mDefaultMagFilter; |
---|
227 | case FT_MIP: |
---|
228 | return mDefaultMipFilter; |
---|
229 | } |
---|
230 | // to keep compiler happy |
---|
231 | return mDefaultMinFilter; |
---|
232 | } |
---|
233 | //----------------------------------------------------------------------- |
---|
234 | unsigned short MaterialManager::_getSchemeIndex(const String& schemeName) |
---|
235 | { |
---|
236 | unsigned short ret = 0; |
---|
237 | SchemeMap::iterator i = mSchemes.find(schemeName); |
---|
238 | if (i != mSchemes.end()) |
---|
239 | { |
---|
240 | ret = i->second; |
---|
241 | } |
---|
242 | else |
---|
243 | { |
---|
244 | // Create new |
---|
245 | ret = static_cast<unsigned short>(mSchemes.size()); |
---|
246 | mSchemes[schemeName] = ret; |
---|
247 | } |
---|
248 | return ret; |
---|
249 | |
---|
250 | } |
---|
251 | //----------------------------------------------------------------------- |
---|
252 | const String& MaterialManager::_getSchemeName(unsigned short index) |
---|
253 | { |
---|
254 | for (SchemeMap::iterator i = mSchemes.begin(); i != mSchemes.end(); ++i) |
---|
255 | { |
---|
256 | if (i->second == index) |
---|
257 | return i->first; |
---|
258 | } |
---|
259 | return DEFAULT_SCHEME_NAME; |
---|
260 | } |
---|
261 | //----------------------------------------------------------------------- |
---|
262 | unsigned short MaterialManager::_getActiveSchemeIndex(void) const |
---|
263 | { |
---|
264 | return mActiveSchemeIndex; |
---|
265 | } |
---|
266 | //----------------------------------------------------------------------- |
---|
267 | const String& MaterialManager::getActiveScheme(void) const |
---|
268 | { |
---|
269 | return mActiveSchemeName; |
---|
270 | } |
---|
271 | //----------------------------------------------------------------------- |
---|
272 | void MaterialManager::setActiveScheme(const String& schemeName) |
---|
273 | { |
---|
274 | SchemeMap::iterator i = mSchemes.find(schemeName); |
---|
275 | if (i == mSchemes.end()) |
---|
276 | { |
---|
277 | // Invalid scheme, use default |
---|
278 | mActiveSchemeName = DEFAULT_SCHEME_NAME; |
---|
279 | mActiveSchemeIndex = 0; |
---|
280 | } |
---|
281 | else |
---|
282 | { |
---|
283 | mActiveSchemeName = schemeName; |
---|
284 | mActiveSchemeIndex = i->second; |
---|
285 | } |
---|
286 | |
---|
287 | } |
---|
288 | //----------------------------------------------------------------------- |
---|
289 | } |
---|