Merge "Combine Internal::ProxyObject & Internal::Object" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / modeling / material-impl.h
1 #ifndef __DALI_INTERNAL_MATERIAL_H__
2 #define __DALI_INTERNAL_MATERIAL_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32 class Image;
33
34 namespace Internal
35 {
36 class Material;
37 class EventToUpdate;
38
39 typedef std::vector<Dali::Material>       MaterialContainer;
40 typedef MaterialContainer::iterator       MaterialIter;
41 typedef MaterialContainer::const_iterator MaterialConstIter;
42
43 namespace SceneGraph
44 {
45 class Material;
46 class UpdateManager;
47 }
48
49
50 struct MaterialProperties
51 {
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
64
65   MaterialProperties();
66
67   MaterialProperties(const MaterialProperties& rhs);
68
69   MaterialProperties& operator=(const MaterialProperties& rhs);
70 };
71
72 /**
73  * @copydoc Dali::Material
74  *
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.
78  *
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.
85   */
86 class Material : public BaseObject
87 {
88 public:
89
90   enum SHADING_MODE
91   {
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
102   };
103
104   enum MAPPING_MODE
105   {
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
110   };
111
112   enum ImageType
113   {
114     DIFFUSE_TEXTURE,
115     OPACITY_TEXTURE,
116     NORMAL_MAP
117   };
118
119   static Material* New(const std::string& name);
120
121   Material(const std::string& name);
122
123 private:
124   /**
125    * Don't allow copy constructor
126    */
127   Material(const Material& rhs);
128
129   /**
130    * Don't allow use of copy operator
131    */
132   Material& operator=(const Material& rhs);
133
134   virtual ~Material();
135
136 public:
137   /**
138    * @copydoc Dali::Material::SetName
139    */
140   void SetName(const std::string& name)
141   {
142     mName = name;
143   }
144
145   /**
146    * @copydoc Dali::Material::GetName
147    */
148   const std::string& GetName() const
149   {
150     return mName;
151   }
152
153   /**
154    * @copydoc Dali::Material::SetOpacity
155    */
156   void SetOpacity(const float opacity);
157
158   /**
159    * @copydoc Dali::Material::GetOpacity
160    */
161   float GetOpacity() const;
162
163   /**
164    * @copydoc Dali::Material::SetShininess
165    */
166   void SetShininess(const float shininess);
167
168   /**
169    * @copydoc Dali::Material::GetShininess
170    */
171   float GetShininess() const;
172
173   /**
174    * @copydoc Dali::Material::SetAmbientColor
175    */
176   void SetAmbientColor(const Vector4& color);
177
178   /**
179    * @copydoc Dali::Material::GetAmbientColor
180    */
181   const Vector4& GetAmbientColor() const;
182
183   /**
184    * @copydoc Dali::Material::SetDiffuseColor
185    */
186   void SetDiffuseColor(const Vector4& color);
187
188   /**
189    * @copydoc Dali::Material::GetDiffuseColor
190    */
191   const Vector4& GetDiffuseColor() const;
192
193   /**
194    * @copydoc Dali::Material::SetSpecularColor
195    */
196   void SetSpecularColor(const Vector4& color);
197
198   /**
199    * @copydoc Dali::Material::GetSpecularColor
200    */
201   const Vector4& GetSpecularColor() const;
202
203   /**
204    * @copydoc Dali::Material::SetEmissiveColor
205    */
206   void SetEmissiveColor(const Vector4& color);
207
208   /**
209    * @copydoc Dali::Material::GetEmissiveColor
210    */
211   const Vector4& GetEmissiveColor() const;
212
213   /**
214    * @copydoc Dali::Material::SetDiffuseTexture(Dali::Image)
215    */
216   void SetDiffuseTexture(Dali::Image image);
217
218   /**
219    * @copydoc Dali::Material::SetDiffuseTextureFileName()
220    */
221   void SetDiffuseTextureFileName(const std::string filename);
222
223   /**
224    * @copydoc Dali::Material::GetDiffuseTexture
225    */
226   Dali::Image GetDiffuseTexture() const;
227
228   /**
229    * @copydoc Dali::Material::GetDiffuseFileName
230    */
231   const std::string& GetDiffuseTextureFileName() const;
232
233   /**
234    * @copydoc Dali::Material::SetOpacityTexture
235    */
236   void SetOpacityTexture(Dali::Image image);
237   void SetOpacityTextureFileName(const std::string filename);
238
239   /**
240    * @copydoc Dali::Material::GetOpacityTexture
241    */
242   Dali::Image GetOpacityTexture() const;
243   const std::string& GetOpacityTextureFileName() const;
244
245   /**
246    * @copydoc Dali::Material::SetNormalMap
247    */
248   void SetNormalMap(Dali::Image image);
249   void SetNormalMapFileName(const std::string image);
250
251   /**
252    * @copydoc Dali::Material::GetNormalMap
253    */
254   Dali::Image GetNormalMap() const;
255   const std::string& GetNormalMapFileName() const;
256
257   /**
258    * @copydoc Dali::Material::SetMapU
259    */
260   void SetMapU(const unsigned int map);
261
262   /**
263    * @copydoc Dali::Material::GetMapU
264    */
265   unsigned int GetMapU() const;
266
267   /**
268    * @copydoc Dali::Material::SetMapV
269    */
270   void SetMapV(const unsigned int map);
271
272   /**
273    * @copydoc Dali::Material::GetMapV
274    */
275   unsigned int GetMapV() const;
276
277   /**
278    * @copydoc Dali::Material::SetDiffuseUVIndex
279    */
280   void SetDiffuseUVIndex(const int index);
281
282   /**
283    * @copydoc Dali::Material::GetDiffuseUVIndex
284    */
285   unsigned int GetDiffuseUVIndex() const;
286
287   /**
288    * @copydoc Dali::Material::SetOpacityUVIndex
289    */
290   void SetOpacityUVIndex(const int index);
291
292   /**
293    * @copydoc Dali::Material::GetOpacityUVIndex
294    */
295   unsigned int GetOpacityUVIndex() const;
296
297   /**
298    * @copydoc Dali::Material::SetNormalUVIndex
299    */
300   void SetNormalUVIndex(const int index);
301
302   /**
303    * @copydoc Dali::Material::GetNormalUVIndex
304    */
305   unsigned int GetNormalUVIndex() const;
306
307   /**
308    * @copydoc Dali::Material::SetHasHeightMap
309    */
310   void SetHasHeightMap(const bool flag);
311
312   /**
313    * @copydoc Dali::Material::GetHasHeightMap
314    */
315   bool GetHasHeightMap() const;
316
317   const MaterialProperties& GetProperties() const
318   {
319     return mProperties;
320   }
321
322   void SetProperties(const MaterialProperties& properties)
323   {
324     mProperties = properties;
325   }
326
327   /**
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
331    */
332   const SceneGraph::Material* GetSceneObject();
333
334   /**
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)
337    */
338   void Connect();
339
340   /**
341    * Disconnect scene graph object from mesh/attachment. Disconnect from stage when this count
342    * reaches zero
343    */
344   void Disconnect();
345
346   /**
347    * Set or send message to set properties on the scene object
348    */
349   void SendPropertiesToSceneObject();
350
351   /**
352    * Set or send message to set diffuse texture image on the scene object
353    */
354   void SendDiffuseImageToSceneObject(Dali::Image image);
355
356   /**
357    * Set or send message to set opacity image on the scene object
358    */
359   void SendOpacityImageToSceneObject(Dali::Image image);
360
361   /**
362    * Set or send message to set normal map on the scene object
363    */
364   void SendNormalMapToSceneObject(Dali::Image image);
365
366
367 private:
368   /**
369    * Create the scene object and return it.
370    * @return the scene object
371    */
372   Internal::SceneGraph::Material* CreateSceneObject();
373
374   // Attributes
375 private:
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
381
382   std::string        mDiffuseName;       ///< Diffuse texture file name
383   std::string        mOpacityName;       ///< Opacity Texture file name
384   std::string        mNormalMapName;     ///< Normap map file name
385
386 private:
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
391 }; // class Material
392
393 } // namespace Internal
394
395 // Helpers for public-api forwarding methods
396
397 inline Internal::Material& GetImplementation(Dali::Material& object)
398 {
399   DALI_ASSERT_ALWAYS( object && "Material handle is empty" );
400
401   BaseObject& handle = object.GetBaseObject();
402
403   return static_cast<Internal::Material&>(handle);
404 }
405
406 inline const Internal::Material& GetImplementation(const Dali::Material& object)
407 {
408   DALI_ASSERT_ALWAYS( object && "Material handle is empty" );
409
410   const BaseObject& handle = object.GetBaseObject();
411
412   return static_cast<const Internal::Material&>(handle);
413 }
414
415 } // namespace Dali
416
417 #endif // __DALI_INTERNAL_MATERIAL_H__