/src/ogre/OgreMain/include/OgreMaterialManager.h
Line | Count | Source |
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-2014 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 "OgreHeaderPrefix.h" |
36 | | #include "OgreTextureUnitState.h" |
37 | | |
38 | | namespace Ogre { |
39 | | |
40 | | class MaterialSerializer; |
41 | | |
42 | | |
43 | | /** \addtogroup Core |
44 | | * @{ |
45 | | */ |
46 | | /** \addtogroup Materials |
47 | | * @{ |
48 | | */ |
49 | | |
50 | | /// Default material scheme name |
51 | | _OgreExport extern const String MSN_DEFAULT; |
52 | | /// Material scheme of the shader generator |
53 | | _OgreExport extern const String MSN_SHADERGEN; |
54 | | /// Material scheme for shadow casters |
55 | | _OgreExport extern const String MSN_SHADOWCASTER; |
56 | | |
57 | | /** Class for managing Material settings for %Ogre. |
58 | | |
59 | | Materials control the eventual surface rendering properties of geometry. This class |
60 | | manages the library of materials, dealing with programmatic registrations and lookups, |
61 | | as well as loading predefined Material settings from scripts. |
62 | | |
63 | | When loaded from a script, a Material is in an 'unloaded' state and only stores the settings |
64 | | required. It does not at that stage load any textures. This is because the material settings may be |
65 | | loaded 'en masse' from bulk material script files, but only a subset will actually be required. |
66 | | |
67 | | Because this is a subclass of ResourceManager, any files loaded will be searched for in any path or |
68 | | archive added to the resource paths/archives. See ResourceManager for details. |
69 | | |
70 | | For a definition of the material script format, see @ref Material-Scripts. |
71 | | |
72 | | Ogre comes configured with a set of defaults for newly created |
73 | | materials. If you wish to have a different set of defaults, |
74 | | simply call getDefaultSettings() and change the returned Material's |
75 | | settings. All materials created from then on will be configured |
76 | | with the new defaults you have specified. |
77 | | */ |
78 | | class _OgreExport MaterialManager : public ResourceManager, public Singleton<MaterialManager> |
79 | | { |
80 | | public: |
81 | | /** Listener on any general material events. |
82 | | @see MaterialManager::addListener |
83 | | */ |
84 | | class Listener |
85 | | { |
86 | | public: |
87 | | /** Virtual destructor needed as class has virtual methods. */ |
88 | 0 | virtual ~Listener() { } |
89 | | /** Called if a technique for a given scheme is not found within a material, |
90 | | allows the application to specify a Technique instance manually. |
91 | | |
92 | | Material schemes allow you to switch wholesale between families of |
93 | | techniques on a material. However they require you to define those |
94 | | schemes on the materials up-front, which might not be possible or |
95 | | desirable for all materials, particular if, for example, you wanted |
96 | | a simple way to replace all materials with another using a scheme. |
97 | | @par |
98 | | This callback allows you to handle the case where a scheme is requested |
99 | | but the material doesn't have an entry for it. You can return a |
100 | | Technique pointer from this method to specify the material technique |
101 | | you'd like to be applied instead, which can be from another material |
102 | | entirely (and probably will be). Note that it is critical that you |
103 | | only return a Technique that is supported on this hardware; there are |
104 | | utility methods like Material::getBestTechnique to help you with this. |
105 | | @param schemeIndex The index of the scheme that was requested - all |
106 | | schemes have a unique index when created that does not alter. |
107 | | @param schemeName The friendly name of the scheme being requested |
108 | | @param originalMaterial The material that is being processed, that |
109 | | didn't have a specific technique for this scheme |
110 | | @param lodIndex The material level-of-detail that was being asked for, |
111 | | in case you need to use it to determine a technique. |
112 | | @param rend Pointer to the Renderable that is requesting this technique |
113 | | to be used, so this may influence your choice of Technique. May be |
114 | | null if the technique isn't being requested in that context. |
115 | | @return A pointer to the technique to be used, or NULL if you wish to |
116 | | use the default technique for this material |
117 | | */ |
118 | | virtual Technique* handleSchemeNotFound(unsigned short schemeIndex, |
119 | | const String& schemeName, Material* originalMaterial, unsigned short lodIndex, |
120 | | const Renderable* rend) = 0; |
121 | | |
122 | | /** Called right after illuminated passes were created, |
123 | | so that owner of runtime generated technique can handle this. |
124 | | @return True if notification is handled and should not be propagated further. |
125 | | */ |
126 | 0 | virtual bool afterIlluminationPassesCreated(Technique* technique) { return false; } |
127 | | |
128 | | /** Called right before illuminated passes would be removed, |
129 | | so that owner of runtime generated technique can handle this. |
130 | | @return True if notification is handled and should not be propagated further. |
131 | | */ |
132 | 0 | virtual bool beforeIlluminationPassesCleared(Technique* technique) { return false; } |
133 | | }; |
134 | | |
135 | | private: |
136 | | /// Default settings |
137 | | MaterialPtr mDefaultSettings; |
138 | | |
139 | | Resource* createImpl(const String& name, ResourceHandle handle, |
140 | | const String& group, bool isManual, ManualResourceLoader* loader, |
141 | | const NameValuePairList* params) override; |
142 | | |
143 | | /// Scheme name -> index. Never shrinks! Should be pretty static anyway |
144 | | typedef std::map<String, unsigned short> SchemeMap; |
145 | | /// List of material schemes |
146 | | SchemeMap mSchemes; |
147 | | /// Current material scheme |
148 | | String mActiveSchemeName; |
149 | | /// Current material scheme |
150 | | unsigned short mActiveSchemeIndex; |
151 | | |
152 | | /// The list of per-scheme (and general) material listeners |
153 | | typedef std::list<Listener*> ListenerList; |
154 | | typedef std::map<String, ListenerList> ListenerMap; |
155 | | ListenerMap mListenerMap; |
156 | | |
157 | | public: |
158 | | /// same as @ref MSN_DEFAULT |
159 | | static String DEFAULT_SCHEME_NAME; |
160 | | |
161 | | /// Create a new material |
162 | | /// @see ResourceManager::createResource |
163 | | MaterialPtr create (const String& name, const String& group, |
164 | | bool isManual = false, ManualResourceLoader* loader = 0, |
165 | | const NameValuePairList* createParams = 0); |
166 | | |
167 | | /// Get a material by name. For example, to get a MaterialPtr to a material defined in .material script. |
168 | | /// @see ResourceManager::getResourceByName |
169 | | MaterialPtr getByName(const String& name, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME) const; |
170 | | |
171 | | /// Get a default material that is always available even when no resources were loaded |
172 | | /// @param useLighting whether the material should be lit |
173 | | MaterialPtr getDefaultMaterial(bool useLighting = true); |
174 | | |
175 | | /** Default constructor. |
176 | | */ |
177 | | MaterialManager(); |
178 | | |
179 | | /** Default destructor. |
180 | | */ |
181 | | virtual ~MaterialManager(); |
182 | | |
183 | | /** Initialises the material manager, which also triggers it to |
184 | | * parse all available .program and .material scripts. */ |
185 | | void initialise(void); |
186 | | |
187 | | /** Sets the default texture filtering to be used for loaded textures, for when textures are |
188 | | loaded automatically (e.g. by Material class) or when 'load' is called with the default |
189 | | parameters by the application. |
190 | | @note |
191 | | The default value is TFO_BILINEAR. |
192 | | */ |
193 | | virtual void setDefaultTextureFiltering(TextureFilterOptions fo); |
194 | | /** Sets the default texture filtering to be used for loaded textures, for when textures are |
195 | | loaded automatically (e.g. by Material class) or when 'load' is called with the default |
196 | | parameters by the application. |
197 | | */ |
198 | | virtual void setDefaultTextureFiltering(FilterType ftype, FilterOptions opts); |
199 | | /** Sets the default texture filtering to be used for loaded textures, for when textures are |
200 | | loaded automatically (e.g. by Material class) or when 'load' is called with the default |
201 | | parameters by the application. |
202 | | */ |
203 | | virtual void setDefaultTextureFiltering(FilterOptions minFilter, FilterOptions magFilter, FilterOptions mipFilter); |
204 | | |
205 | | /// Get the default texture filtering |
206 | | virtual FilterOptions getDefaultTextureFiltering(FilterType ftype) const; |
207 | | |
208 | | /** Sets the default anisotropy level to be used for loaded textures, for when textures are |
209 | | loaded automatically (e.g. by Material class) or when 'load' is called with the default |
210 | | parameters by the application. |
211 | | @note |
212 | | The default value is 1 (no anisotropy). |
213 | | */ |
214 | | void setDefaultAnisotropy(unsigned int maxAniso); |
215 | | /// Get the default maxAnisotropy |
216 | | unsigned int getDefaultAnisotropy() const; |
217 | | |
218 | | /** Returns a pointer to the default Material settings. |
219 | | |
220 | | The default settings begin as a single Technique with a single, non-programmable Pass: |
221 | | |
222 | | - ambient = ColourValue::White |
223 | | - diffuse = ColourValue::White |
224 | | - specular = ColourValue::Black |
225 | | - emissive = ColourValue::Black |
226 | | - shininess = 0 (not shiny) |
227 | | - SourceBlendFactor = Ogre::SBF_ONE |
228 | | - DestBlendFactor = Ogre::SBF_ZERO (no blend, replace with new colour) |
229 | | - Depth buffer checking on |
230 | | - Depth buffer writing on |
231 | | - Depth buffer comparison function = Ogre::CMPF_LESS_EQUAL |
232 | | - Colour buffer writing on for all channels |
233 | | - Culling mode = Ogre::CULL_CLOCKWISE |
234 | | - Ambient lighting = ColourValue(0.5, 0.5, 0.5) (mid-grey) |
235 | | - Dynamic lighting enabled |
236 | | - Gourad shading mode |
237 | | - No texture unit settings (& hence no textures) |
238 | | */ |
239 | 0 | virtual MaterialPtr getDefaultSettings(void) const { return mDefaultSettings; } |
240 | | |
241 | | /** Internal method - returns index for a given material scheme name. |
242 | | @see Technique::setSchemeName |
243 | | */ |
244 | | virtual unsigned short _getSchemeIndex(const String& name); |
245 | | /** Internal method - returns name for a given material scheme index. |
246 | | @see Technique::setSchemeName |
247 | | */ |
248 | | virtual const String& _getSchemeName(unsigned short index); |
249 | | /** Internal method - returns the active scheme index. |
250 | | @see Technique::setSchemeName |
251 | | */ |
252 | 0 | unsigned short _getActiveSchemeIndex(void) const { return mActiveSchemeIndex; } |
253 | | |
254 | | /** Returns the name of the active material scheme. |
255 | | @see Technique::setSchemeName |
256 | | */ |
257 | 0 | const String& getActiveScheme(void) const { return mActiveSchemeName; } |
258 | | |
259 | | /** Sets the name of the active material scheme. |
260 | | @see Technique::setSchemeName |
261 | | */ |
262 | | virtual void setActiveScheme(const String& schemeName); |
263 | | |
264 | | /** |
265 | | Add a listener to handle material events. |
266 | | If schemeName is supplied, the listener will only receive events for that certain scheme. |
267 | | */ |
268 | | virtual void addListener(Listener* l, const Ogre::String& schemeName = BLANKSTRING); |
269 | | |
270 | | /** |
271 | | Remove a listener handling material events. |
272 | | If the listener was added with a custom scheme name, it needs to be supplied here as well. |
273 | | */ |
274 | | virtual void removeListener(Listener* l, const Ogre::String& schemeName = BLANKSTRING); |
275 | | |
276 | | /// Internal method for sorting out missing technique for a scheme |
277 | | virtual Technique* _arbitrateMissingTechniqueForActiveScheme( |
278 | | Material* mat, unsigned short lodIndex, const Renderable* rend); |
279 | | |
280 | | /// Internal method for sorting out illumination passes for a scheme |
281 | | virtual void _notifyAfterIlluminationPassesCreated(Technique* mat); |
282 | | |
283 | | /// Internal method for sorting out illumination passes for a scheme |
284 | | virtual void _notifyBeforeIlluminationPassesCleared(Technique* mat); |
285 | | |
286 | | |
287 | | /// @copydoc Singleton::getSingleton() |
288 | | static MaterialManager& getSingleton(void); |
289 | | /// @copydoc Singleton::getSingleton() |
290 | | static MaterialManager* getSingletonPtr(void); |
291 | | |
292 | | }; |
293 | | /** @} */ |
294 | | /** @} */ |
295 | | |
296 | | } |
297 | | |
298 | | #include "OgreHeaderSuffix.h" |
299 | | |
300 | | #endif |