3e58869e600533ebd005a6d07659349f78667c68
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-material.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDER_MATERIAL_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDER_MATERIAL_H__
3 /*
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/math/vector4.h>
22 #include <dali/internal/common/message.h>
23 #include <dali/internal/event/effects/shader-declarations.h>
24 #include <dali/internal/render/shaders/custom-uniform.h>
25 #include <dali/internal/render/gl-resources/texture-observer.h>
26 #include <dali/internal/render/shaders/program.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 class Texture;
35
36 namespace SceneGraph
37 {
38
39 class TextureCache;
40 class RenderMaterial;
41
42 /**
43  * Helper class to manage uniforms for RenderMaterial. There is a 1-1
44  * correspondance between a helper object and a Renderer instance,
45  * whereas there is a 1-many relationship between RenderMaterial and
46  * Renderer instances.
47  */
48 class RenderMaterialUniforms
49 {
50 public:
51   /**
52    * If the owner's program changes, then this should be called to reset the uniform locations
53    */
54   void ResetCustomUniforms();
55
56   /**
57    * Set the uniform values from the renderMaterial onto the program
58    * @param[in] renderMaterial The material from which to get the values
59    * @param[in] program The program for which to set the values
60    * @param[in] shaderType The sub type of shader program
61    */
62   void SetUniforms( const RenderMaterial& renderMaterial, Program& program, ShaderSubTypes shaderType );
63
64 private:
65   static const unsigned int mNumberOfCustomUniforms = 6; ///< Number of material uniforms
66   mutable CustomUniform mCustomUniform[SHADER_SUBTYPE_LAST][ mNumberOfCustomUniforms ]; ///< The uniform locations
67 };
68
69
70 /**
71  * Holds values to be stored into uniforms by the renderer
72  * Holds texture pointers for binding by the renderer
73  */
74 class RenderMaterial : public TextureObserver
75 {
76 public:
77   /**
78    * Constructor
79    */
80   RenderMaterial();
81
82   /**
83    * Destructor
84    */
85   virtual ~RenderMaterial();
86
87   /**
88    * Secondary stage initialization
89    * @param[in] textureCache The GL Texture cache
90    */
91   void Initialize(TextureCache& textureCache);
92
93   // Message setters
94
95   /**
96    * Set the diffuse texture resource id
97    * @param[in] textureId The resource id of the texture
98    */
99   void SetDiffuseTextureId( unsigned int textureId );
100
101   /**
102    * Set the opacity texture id
103    * @param[in] textureId The resource id of the texture
104    */
105   void SetOpacityTextureId( unsigned int textureId );
106
107   /**
108    * Set the normal / height map texture id
109    * @param[in] textureId The resource id of the texture
110    */
111   void SetNormalMapTextureId( unsigned int textureId );
112
113   /**
114    * Set the opacity
115    * @param[in] opacity The opacity
116    */
117   void SetOpacity(float opacity);
118
119   /**
120    * Set the shininess
121    * @param[in] shininess The shininess
122    */
123   void SetShininess(float shininess);
124
125   /**
126    * Set the ambient color
127    * @param[in] color The color
128    */
129   void SetAmbientColor( const Vector4& color );
130
131   /**
132    * Set the diffuse color
133    * @param[in] color The color
134    */
135   void SetDiffuseColor( const Vector4& color );
136
137   /**
138    * Set the specular color
139    * @param[in] color The color
140    */
141   void SetSpecularColor( const Vector4& color );
142
143   /**
144    * Set the emissive color
145    * @param[in] color The color
146    */
147   void SetEmissiveColor( const Vector4& color );
148
149   /**
150    * @return true if a texture has been set on this material, false otherwise
151    */
152   bool HasTexture() const;
153
154   // Rendering
155
156   /**
157    * Set the uniform values for the locations defined in the uniforms structure.
158    * @param[in] uniforms The material uniforms
159    * @param[in] program The shader program for which to set the uniform values
160    * @param[in] shaderType The shader type
161    */
162   void SetUniforms( RenderMaterialUniforms& uniforms, Program& program, ShaderSubTypes shaderType ) const;
163
164   /**
165    * Bind all the valid textures. Will also store valid texture pointer
166    * @param[in] program The shader program with which to bind the textures
167    */
168   void BindTextures( Program& program );
169
170 protected: // TextureObserver implementation
171   /**
172    * @copydoc Dali::Internal::TextureObserver::TextureDiscarded()
173    */
174   virtual void TextureDiscarded( unsigned int textureId );
175
176 private:
177   /**
178    * Bind a texture.
179    * @param[in] program The shader program with which to bind the texture
180    * @param[in] textureId the texture id to bind
181    * @param[in] texture The texture to bind
182    * @param[in] textureUnit Texture unit ( should be a value between 0 and the max number of samplers - 1 )
183    * @param[in] samplerIndex The index of the uniform to bind the texture sampler
184    */
185   void BindTexture( Program& program, unsigned int textureId, Texture* texture, unsigned int textureUnit, Program::UniformType samplerIndex ) const;
186
187 private:
188   TextureCache*  mTextureCache;       ///< Texture cache of GL resources
189
190   unsigned int   mDiffuseTextureId;   ///< Diffuse texture id
191   unsigned int   mOpacityTextureId;   ///< opacity texture id
192   unsigned int   mNormalMapTextureId; ///< Normal/Height map texture id
193
194   Texture*       mDiffuseTexture;     ///< Pointer to ready texture
195   Texture*       mOpacityTexture;     ///< Pointer to ready texture
196   Texture*       mNormalMapTexture;   ///< Pointer to ready texture
197
198   float          mOpacity;            ///< opacity (0 = transparent, 1 = opaque)
199   float          mShininess;          ///< value between 0 and 128
200
201   Vector4        mAmbientColor;       ///< Ambient color
202   Vector4        mDiffuseColor;       ///< Diffuse color
203   Vector4        mSpecularColor;      ///< Specular color
204   Vector4        mEmissiveColor;      ///< Emissive color
205
206   friend class RenderMaterialUniforms;
207 };
208
209 } // SceneGraph
210
211 } // Internal
212
213 } // Dali
214
215 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_MATERIAL_H__