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-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | #ifndef __MATERIALMANAGER_H__ |
---|
29 | #define __MATERIALMANAGER_H__ |
---|
30 | |
---|
31 | #include "OgrePrerequisites.h" |
---|
32 | |
---|
33 | #include "OgreSingleton.h" |
---|
34 | #include "OgreResourceManager.h" |
---|
35 | #include "OgreMaterial.h" |
---|
36 | #include "OgreStringVector.h" |
---|
37 | #include "OgreMaterialSerializer.h" |
---|
38 | #include "OgreHeaderPrefix.h" |
---|
39 | |
---|
40 | namespace Ogre { |
---|
41 | |
---|
42 | |
---|
43 | /** \addtogroup Core |
---|
44 | * @{ |
---|
45 | */ |
---|
46 | /** \addtogroup Materials |
---|
47 | * @{ |
---|
48 | */ |
---|
49 | /** Class for managing Material settings for Ogre. |
---|
50 | @remarks |
---|
51 | Materials control the eventual surface rendering properties of geometry. This class |
---|
52 | manages the library of materials, dealing with programmatic registrations and lookups, |
---|
53 | as well as loading predefined Material settings from scripts. |
---|
54 | @par |
---|
55 | When loaded from a script, a Material is in an 'unloaded' state and only stores the settings |
---|
56 | required. It does not at that stage load any textures. This is because the material settings may be |
---|
57 | loaded 'en masse' from bulk material script files, but only a subset will actually be required. |
---|
58 | @par |
---|
59 | Because this is a subclass of ResourceManager, any files loaded will be searched for in any path or |
---|
60 | archive added to the resource paths/archives. See ResourceManager for details. |
---|
61 | @par |
---|
62 | For a definition of the material script format, see the Tutorials/MaterialScript.html file. |
---|
63 | */ |
---|
64 | class _OgreExport MaterialManager : public ResourceManager, public Singleton<MaterialManager> |
---|
65 | { |
---|
66 | public: |
---|
67 | /** Listener on any general material events. |
---|
68 | @see MaterialManager::addListener |
---|
69 | */ |
---|
70 | class Listener |
---|
71 | { |
---|
72 | public: |
---|
73 | /** Virtual destructor needed as class has virtual methods. */ |
---|
74 | virtual ~Listener() { } |
---|
75 | /** Called if a technique for a given scheme is not found within a material, |
---|
76 | allows the application to specify a Technique instance manually. |
---|
77 | @remarks |
---|
78 | Material schemes allow you to switch wholesale between families of |
---|
79 | techniques on a material. However they require you to define those |
---|
80 | schemes on the materials up-front, which might not be possible or |
---|
81 | desirable for all materials, particular if, for example, you wanted |
---|
82 | a simple way to replace all materials with another using a scheme. |
---|
83 | @par |
---|
84 | This callback allows you to handle the case where a scheme is requested |
---|
85 | but the material doesn't have an entry for it. You can return a |
---|
86 | Technique pointer from this method to specify the material technique |
---|
87 | you'd like to be applied instead, which can be from another material |
---|
88 | entirely (and probably will be). Note that it is critical that you |
---|
89 | only return a Technique that is supported on this hardware; there are |
---|
90 | utility methods like Material::getBestTechnique to help you with this. |
---|
91 | @param schemeIndex The index of the scheme that was requested - all |
---|
92 | schemes have a unique index when created that does not alter. |
---|
93 | @param schemeName The friendly name of the scheme being requested |
---|
94 | @param originalMaterial The material that is being processed, that |
---|
95 | didn't have a specific technique for this scheme |
---|
96 | @param lodIndex The material level-of-detail that was being asked for, |
---|
97 | in case you need to use it to determine a technique. |
---|
98 | @param rend Pointer to the Renderable that is requesting this technique |
---|
99 | to be used, so this may influence your choice of Technique. May be |
---|
100 | null if the technique isn't being requested in that context. |
---|
101 | @return A pointer to the technique to be used, or NULL if you wish to |
---|
102 | use the default technique for this material |
---|
103 | */ |
---|
104 | virtual Technique* handleSchemeNotFound(unsigned short schemeIndex, |
---|
105 | const String& schemeName, Material* originalMaterial, unsigned short lodIndex, |
---|
106 | const Renderable* rend) = 0; |
---|
107 | |
---|
108 | }; |
---|
109 | |
---|
110 | protected: |
---|
111 | |
---|
112 | /// Default Texture filtering - minification |
---|
113 | FilterOptions mDefaultMinFilter; |
---|
114 | /// Default Texture filtering - magnification |
---|
115 | FilterOptions mDefaultMagFilter; |
---|
116 | /// Default Texture filtering - mipmapping |
---|
117 | FilterOptions mDefaultMipFilter; |
---|
118 | /// Default Texture filtering - comparison |
---|
119 | FilterOptions mDefaultCompare; |
---|
120 | |
---|
121 | bool mDefaultCompareEnabled; |
---|
122 | CompareFunction mDefaultCompareFunction; |
---|
123 | |
---|
124 | /// Default Texture anisotropy |
---|
125 | unsigned int mDefaultMaxAniso; |
---|
126 | /// Serializer - Hold instance per thread if necessary |
---|
127 | OGRE_THREAD_POINTER(MaterialSerializer, mSerializer); |
---|
128 | /// Default settings |
---|
129 | MaterialPtr mDefaultSettings; |
---|
130 | |
---|
131 | /// Overridden from ResourceManager |
---|
132 | Resource* createImpl(const String& name, ResourceHandle handle, |
---|
133 | const String& group, bool isManual, ManualResourceLoader* loader, |
---|
134 | const NameValuePairList* params); |
---|
135 | |
---|
136 | /// Scheme name -> index. Never shrinks! Should be pretty static anyway |
---|
137 | typedef map<String, unsigned short>::type SchemeMap; |
---|
138 | /// List of material schemes |
---|
139 | SchemeMap mSchemes; |
---|
140 | /// Current material scheme |
---|
141 | String mActiveSchemeName; |
---|
142 | /// Current material scheme |
---|
143 | unsigned short mActiveSchemeIndex; |
---|
144 | |
---|
145 | /// The list of per-scheme (and general) material listeners |
---|
146 | typedef list<Listener*>::type ListenerList; |
---|
147 | typedef map<String, ListenerList>::type ListenerMap; |
---|
148 | ListenerMap mListenerMap; |
---|
149 | |
---|
150 | public: |
---|
151 | /// Default material scheme |
---|
152 | static String DEFAULT_SCHEME_NAME; |
---|
153 | |
---|
154 | /// Create a new material |
---|
155 | /// @see ResourceManager::createResource |
---|
156 | MaterialPtr create (const String& name, const String& group, |
---|
157 | bool isManual = false, ManualResourceLoader* loader = 0, |
---|
158 | const NameValuePairList* createParams = 0); |
---|
159 | |
---|
160 | /// Get a resource by name |
---|
161 | /// @see ResourceManager::getResourceByName |
---|
162 | MaterialPtr getByName(const String& name, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); |
---|
163 | |
---|
164 | /** Default constructor. |
---|
165 | */ |
---|
166 | MaterialManager(); |
---|
167 | |
---|
168 | /** Default destructor. |
---|
169 | */ |
---|
170 | virtual ~MaterialManager(); |
---|
171 | |
---|
172 | /** Initialises the material manager, which also triggers it to |
---|
173 | * parse all available .program and .material scripts. */ |
---|
174 | void initialise(void); |
---|
175 | |
---|
176 | /** @see ScriptLoader::parseScript |
---|
177 | */ |
---|
178 | void parseScript(DataStreamPtr& stream, const String& groupName); |
---|
179 | |
---|
180 | |
---|
181 | /** Sets the default texture filtering to be used for loaded textures, for when textures are |
---|
182 | loaded automatically (e.g. by Material class) or when 'load' is called with the default |
---|
183 | parameters by the application. |
---|
184 | @note |
---|
185 | The default value is TFO_BILINEAR. |
---|
186 | */ |
---|
187 | virtual void setDefaultTextureFiltering(TextureFilterOptions fo); |
---|
188 | /** Sets the default texture filtering to be used for loaded textures, for when textures are |
---|
189 | loaded automatically (e.g. by Material class) or when 'load' is called with the default |
---|
190 | parameters by the application. |
---|
191 | */ |
---|
192 | virtual void setDefaultTextureFiltering(FilterType ftype, FilterOptions opts); |
---|
193 | /** Sets the default texture filtering to be used for loaded textures, for when textures are |
---|
194 | loaded automatically (e.g. by Material class) or when 'load' is called with the default |
---|
195 | parameters by the application. |
---|
196 | */ |
---|
197 | virtual void setDefaultTextureFiltering(FilterOptions minFilter, FilterOptions magFilter, FilterOptions mipFilter); |
---|
198 | |
---|
199 | /// Get the default texture filtering |
---|
200 | virtual FilterOptions getDefaultTextureFiltering(FilterType ftype) const; |
---|
201 | |
---|
202 | /** Sets the default anisotropy level to be used for loaded textures, for when textures are |
---|
203 | loaded automatically (e.g. by Material class) or when 'load' is called with the default |
---|
204 | parameters by the application. |
---|
205 | @note |
---|
206 | The default value is 1 (no anisotropy). |
---|
207 | */ |
---|
208 | void setDefaultAnisotropy(unsigned int maxAniso); |
---|
209 | /// Get the default maxAnisotropy |
---|
210 | unsigned int getDefaultAnisotropy() const; |
---|
211 | |
---|
212 | /** Returns a pointer to the default Material settings. |
---|
213 | @remarks |
---|
214 | Ogre comes configured with a set of defaults for newly created |
---|
215 | materials. If you wish to have a different set of defaults, |
---|
216 | simply call this method and change the returned Material's |
---|
217 | settings. All materials created from then on will be configured |
---|
218 | with the new defaults you have specified. |
---|
219 | @par |
---|
220 | The default settings begin as a single Technique with a single, non-programmable Pass: |
---|
221 | <ul> |
---|
222 | <li>ambient = ColourValue::White</li> |
---|
223 | <li>diffuse = ColourValue::White</li> |
---|
224 | <li>specular = ColourValue::Black</li> |
---|
225 | <li>emmissive = ColourValue::Black</li> |
---|
226 | <li>shininess = 0</li> |
---|
227 | <li>No texture unit settings (& hence no textures)</li> |
---|
228 | <li>SourceBlendFactor = SBF_ONE</li> |
---|
229 | <li>DestBlendFactor = SBF_ZERO (no blend, replace with new |
---|
230 | colour)</li> |
---|
231 | <li>Depth buffer checking on</li> |
---|
232 | <li>Depth buffer writing on</li> |
---|
233 | <li>Depth buffer comparison function = CMPF_LESS_EQUAL</li> |
---|
234 | <li>Colour buffer writing on for all channels</li> |
---|
235 | <li>Culling mode = CULL_CLOCKWISE</li> |
---|
236 | <li>Ambient lighting = ColourValue(0.5, 0.5, 0.5) (mid-grey)</li> |
---|
237 | <li>Dynamic lighting enabled</li> |
---|
238 | <li>Gourad shading mode</li> |
---|
239 | <li>Bilinear texture filtering</li> |
---|
240 | </ul> |
---|
241 | */ |
---|
242 | virtual MaterialPtr getDefaultSettings(void) const { return mDefaultSettings; } |
---|
243 | |
---|
244 | /** Internal method - returns index for a given material scheme name. |
---|
245 | @see Technique::setSchemeName |
---|
246 | */ |
---|
247 | virtual unsigned short _getSchemeIndex(const String& name); |
---|
248 | /** Internal method - returns name for a given material scheme index. |
---|
249 | @see Technique::setSchemeName |
---|
250 | */ |
---|
251 | virtual const String& _getSchemeName(unsigned short index); |
---|
252 | /** Internal method - returns the active scheme index. |
---|
253 | @see Technique::setSchemeName |
---|
254 | */ |
---|
255 | virtual unsigned short _getActiveSchemeIndex(void) const; |
---|
256 | |
---|
257 | /** Returns the name of the active material scheme. |
---|
258 | @see Technique::setSchemeName |
---|
259 | */ |
---|
260 | virtual const String& getActiveScheme(void) const; |
---|
261 | |
---|
262 | /** Sets the name of the active material scheme. |
---|
263 | @see Technique::setSchemeName |
---|
264 | */ |
---|
265 | virtual void setActiveScheme(const String& schemeName); |
---|
266 | |
---|
267 | /** |
---|
268 | Add a listener to handle material events. |
---|
269 | If schemeName is supplied, the listener will only receive events for that certain scheme. |
---|
270 | */ |
---|
271 | virtual void addListener(Listener* l, const Ogre::String& schemeName = StringUtil::BLANK); |
---|
272 | |
---|
273 | /** |
---|
274 | Remove a listener handling material events. |
---|
275 | If the listener was added with a custom scheme name, it needs to be supplied here as well. |
---|
276 | */ |
---|
277 | virtual void removeListener(Listener* l, const Ogre::String& schemeName = StringUtil::BLANK); |
---|
278 | |
---|
279 | /// Internal method for sorting out missing technique for a scheme |
---|
280 | virtual Technique* _arbitrateMissingTechniqueForActiveScheme( |
---|
281 | Material* mat, unsigned short lodIndex, const Renderable* rend); |
---|
282 | |
---|
283 | /** Override standard Singleton retrieval. |
---|
284 | @remarks |
---|
285 | Why do we do this? Well, it's because the Singleton |
---|
286 | implementation is in a .h file, which means it gets compiled |
---|
287 | into anybody who includes it. This is needed for the |
---|
288 | Singleton template to work, but we actually only want it |
---|
289 | compiled into the implementation of the class based on the |
---|
290 | Singleton, not all of them. If we don't change this, we get |
---|
291 | link errors when trying to use the Singleton-based class from |
---|
292 | an outside dll. |
---|
293 | @par |
---|
294 | This method just delegates to the template version anyway, |
---|
295 | but the implementation stays in this single compilation unit, |
---|
296 | preventing link errors. |
---|
297 | */ |
---|
298 | static MaterialManager& getSingleton(void); |
---|
299 | /** Override standard Singleton retrieval. |
---|
300 | @remarks |
---|
301 | Why do we do this? Well, it's because the Singleton |
---|
302 | implementation is in a .h file, which means it gets compiled |
---|
303 | into anybody who includes it. This is needed for the |
---|
304 | Singleton template to work, but we actually only want it |
---|
305 | compiled into the implementation of the class based on the |
---|
306 | Singleton, not all of them. If we don't change this, we get |
---|
307 | link errors when trying to use the Singleton-based class from |
---|
308 | an outside dll. |
---|
309 | @par |
---|
310 | This method just delegates to the template version anyway, |
---|
311 | but the implementation stays in this single compilation unit, |
---|
312 | preventing link errors. |
---|
313 | */ |
---|
314 | static MaterialManager* getSingletonPtr(void); |
---|
315 | |
---|
316 | }; |
---|
317 | /** @} */ |
---|
318 | /** @} */ |
---|
319 | |
---|
320 | } |
---|
321 | |
---|
322 | #include "OgreHeaderSuffix.h" |
---|
323 | |
---|
324 | #endif |
---|