1 #ifndef __DALI_INTERNAL_MATERIAL_H__
2 #define __DALI_INTERNAL_MATERIAL_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/object/ref-object.h>
23 #include <dali/public-api/common/constants.h>
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/images/image.h>
26 #include <dali/public-api/math/vector4.h>
27 #include <dali/public-api/modeling/material.h>
28 #include <dali/public-api/object/base-object.h>
39 typedef std::vector<Dali::Material> MaterialContainer;
40 typedef MaterialContainer::iterator MaterialIter;
41 typedef MaterialContainer::const_iterator MaterialConstIter;
50 struct MaterialProperties
52 float mOpacity; ///< opacity (0 = transparent, 1 = opaque [default: 1]
53 float mShininess; ///< value between 0 and 128 [default: 0]
54 Vector4 mAmbientColor; ///< Ambient color [default: 0.2 0.2 0.2 1.0]
55 Vector4 mDiffuseColor; ///< Diffuse color [default: 0.8 0.8 0.8 1.0]
56 Vector4 mSpecularColor; ///< Specular color [default: 0.0 0.0 0.0 1.0]
57 Vector4 mEmissiveColor; ///< Emissive color [default: 0.0 0.0 0.0 1.0]
58 unsigned int mMapU; ///< mapping mode for U texture coordinates
59 unsigned int mMapV; ///< mapping mode for V texture coordinates
60 unsigned int mDiffuseUVIndex; ///< Index into bound mesh's array of UV's for diffuse texture coordinates
61 unsigned int mOpacityUVIndex; ///< Index into bound mesh's array of UV's for opacity texture coordinates
62 unsigned int mNormalUVIndex; ///< Index into bound mesh's array of UV's for normal/height map texture coordinates
63 bool mHasHeightMap; ///< Determines if the normal map (if supplied) contains normals or height information
67 MaterialProperties(const MaterialProperties& rhs);
69 MaterialProperties& operator=(const MaterialProperties& rhs);
73 * @copydoc Dali::Material
75 * A Material object can be created by the Adaptor when loading a model
76 * resource, in which case it becomes part of the ModelData resource, or it can
77 * be created directly by an application.
79 * An Internal::Material is tracked by an Internal::MeshAttachment. When the
80 * mesh attachment puts its scene graph equivalent on the stage, the
81 * Internal::Material is informed through the Connect method. If this is the
82 * first connection, then this creates a SceneGraph::Material and passes
83 * ownership to the UpdateManager, keeping a pointer to it, and also returning a
84 * pointer to the MeshAttachment.
86 class Material : public BaseObject
92 SHADING_FLAT = 0x1, ///< Flat (faceted) shading.
93 SHADING_Gouraud = 0x2, ///< Simple gouraud shading.
94 SHADING_Phong = 0x3, ///< Phong shading
95 SHADING_Blinn = 0x4, ///< Phong-Blinn shading
96 SHADING_Toon = 0x5, ///< Toon (comic) shading
97 SHADING_OrenNayar = 0x6, ///< Extension to standard Lambertian shading, taking the roughness of the material into account
98 SHADING_Minnaert = 0x7, ///< Extension to standard Lambertian shading, taking the "darkness" of the material into account
99 SHADING_CookTorrance = 0x8, ///< Shader for metallic surfaces.
100 SHADING_None = 0x9, ///< No shading, constant light influence of 1.0.
101 SHADING_Fresnel = 0xa, ///< Fresnel shading
106 MAPPING_MODE_WRAP = 0x0, ///< A texture coordinate u|v is translated to u%1|v%1
107 MAPPING_MODE_CLAMP = 0x1, ///< Texture coordinates outside [0...1] are clamped to the nearest valid value
108 MAPPING_MODE_MIRROR = 0x2, ///< A texture coordinate u|v becomes u%1|v%1 if (u-(u%1))%2 is zero and 1-(u%1)|1-(v%1) otherwise
109 MAPPING_MODE_DECAL = 0x3, ///< If the texture coordinates for a pixel are outside [0...1] the texture is not applied to that pixel
119 static Material* New(const std::string& name);
121 Material(const std::string& name);
125 * Don't allow copy constructor
127 Material(const Material& rhs);
130 * Don't allow use of copy operator
132 Material& operator=(const Material& rhs);
138 * @copydoc Dali::Material::SetName
140 void SetName(const std::string& name)
146 * @copydoc Dali::Material::GetName
148 const std::string& GetName() const
154 * @copydoc Dali::Material::SetOpacity
156 void SetOpacity(const float opacity);
159 * @copydoc Dali::Material::GetOpacity
161 float GetOpacity() const;
164 * @copydoc Dali::Material::SetShininess
166 void SetShininess(const float shininess);
169 * @copydoc Dali::Material::GetShininess
171 float GetShininess() const;
174 * @copydoc Dali::Material::SetAmbientColor
176 void SetAmbientColor(const Vector4& color);
179 * @copydoc Dali::Material::GetAmbientColor
181 const Vector4& GetAmbientColor() const;
184 * @copydoc Dali::Material::SetDiffuseColor
186 void SetDiffuseColor(const Vector4& color);
189 * @copydoc Dali::Material::GetDiffuseColor
191 const Vector4& GetDiffuseColor() const;
194 * @copydoc Dali::Material::SetSpecularColor
196 void SetSpecularColor(const Vector4& color);
199 * @copydoc Dali::Material::GetSpecularColor
201 const Vector4& GetSpecularColor() const;
204 * @copydoc Dali::Material::SetEmissiveColor
206 void SetEmissiveColor(const Vector4& color);
209 * @copydoc Dali::Material::GetEmissiveColor
211 const Vector4& GetEmissiveColor() const;
214 * @copydoc Dali::Material::SetDiffuseTexture(Dali::Image)
216 void SetDiffuseTexture(Dali::Image image);
219 * @copydoc Dali::Material::SetDiffuseTextureFileName()
221 void SetDiffuseTextureFileName(const std::string filename);
224 * @copydoc Dali::Material::GetDiffuseTexture
226 Dali::Image GetDiffuseTexture() const;
229 * @copydoc Dali::Material::GetDiffuseFileName
231 const std::string& GetDiffuseTextureFileName() const;
234 * @copydoc Dali::Material::SetOpacityTexture
236 void SetOpacityTexture(Dali::Image image);
237 void SetOpacityTextureFileName(const std::string filename);
240 * @copydoc Dali::Material::GetOpacityTexture
242 Dali::Image GetOpacityTexture() const;
243 const std::string& GetOpacityTextureFileName() const;
246 * @copydoc Dali::Material::SetNormalMap
248 void SetNormalMap(Dali::Image image);
249 void SetNormalMapFileName(const std::string image);
252 * @copydoc Dali::Material::GetNormalMap
254 Dali::Image GetNormalMap() const;
255 const std::string& GetNormalMapFileName() const;
258 * @copydoc Dali::Material::SetMapU
260 void SetMapU(const unsigned int map);
263 * @copydoc Dali::Material::GetMapU
265 unsigned int GetMapU() const;
268 * @copydoc Dali::Material::SetMapV
270 void SetMapV(const unsigned int map);
273 * @copydoc Dali::Material::GetMapV
275 unsigned int GetMapV() const;
278 * @copydoc Dali::Material::SetDiffuseUVIndex
280 void SetDiffuseUVIndex(const int index);
283 * @copydoc Dali::Material::GetDiffuseUVIndex
285 unsigned int GetDiffuseUVIndex() const;
288 * @copydoc Dali::Material::SetOpacityUVIndex
290 void SetOpacityUVIndex(const int index);
293 * @copydoc Dali::Material::GetOpacityUVIndex
295 unsigned int GetOpacityUVIndex() const;
298 * @copydoc Dali::Material::SetNormalUVIndex
300 void SetNormalUVIndex(const int index);
303 * @copydoc Dali::Material::GetNormalUVIndex
305 unsigned int GetNormalUVIndex() const;
308 * @copydoc Dali::Material::SetHasHeightMap
310 void SetHasHeightMap(const bool flag);
313 * @copydoc Dali::Material::GetHasHeightMap
315 bool GetHasHeightMap() const;
317 const MaterialProperties& GetProperties() const
322 void SetProperties(const MaterialProperties& properties)
324 mProperties = properties;
328 * Returns an existing scene object, or if none exists, creates one and returns it.
329 * Note, the existing scene object may already be staged.
330 * @return The associated scene object
332 const SceneGraph::Material* GetSceneObject();
335 * Connect scene graph object to stage if it isn't already.
336 * Track number of connections (may have single material used by multiple meshes/attachments)
341 * Disconnect scene graph object from mesh/attachment. Disconnect from stage when this count
347 * Set or send message to set properties on the scene object
349 void SendPropertiesToSceneObject();
352 * Set or send message to set diffuse texture image on the scene object
354 void SendDiffuseImageToSceneObject(Dali::Image image);
357 * Set or send message to set opacity image on the scene object
359 void SendOpacityImageToSceneObject(Dali::Image image);
362 * Set or send message to set normal map on the scene object
364 void SendNormalMapToSceneObject(Dali::Image image);
369 * Create the scene object and return it.
370 * @return the scene object
372 Internal::SceneGraph::Material* CreateSceneObject();
376 std::string mName; ///< Material's name
377 MaterialProperties mProperties; ///< Material properties
378 Dali::Image mDiffuseImage; ///< Diffuse image handle
379 Dali::Image mOpacityImage; ///< opacity image handle
380 Dali::Image mNormalMap; ///< Normal/Height map image handle
382 std::string mDiffuseName; ///< Diffuse texture file name
383 std::string mOpacityName; ///< Opacity Texture file name
384 std::string mNormalMapName; ///< Normap map file name
387 SceneGraph::Material* mSceneObject; ///< The associated scene graph object
388 EventToUpdate* mEventToUpdate; ///< Used to send messages to update-thread
389 SceneGraph::UpdateManager* mUpdateManager; ///< The Update manager
390 unsigned int mConnectionCount; ///< Number of on-stage meshes connected to this material
393 } // namespace Internal
395 // Helpers for public-api forwarding methods
397 inline Internal::Material& GetImplementation(Dali::Material& object)
399 DALI_ASSERT_ALWAYS( object && "Material handle is empty" );
401 BaseObject& handle = object.GetBaseObject();
403 return static_cast<Internal::Material&>(handle);
406 inline const Internal::Material& GetImplementation(const Dali::Material& object)
408 DALI_ASSERT_ALWAYS( object && "Material handle is empty" );
410 const BaseObject& handle = object.GetBaseObject();
412 return static_cast<const Internal::Material&>(handle);
417 #endif // __DALI_INTERNAL_MATERIAL_H__