f6186d33d1444b2bc6856d3e371c10930a948bf3
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / mesh / mesh-renderer.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "mesh-renderer.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/images/resource-image.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
26 #include <dali/devel-api/adaptor-framework/file-loader.h>
27 #include <fstream>
28
29 //INTERNAL INCLUDES
30 #include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
31 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
32 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
33 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
34
35 namespace Dali
36 {
37
38 namespace
39 {
40   /**
41    * @brief Loads a texture from a file
42    * @param[in] imageUrl The url of the file
43    * @param[in] generateMipmaps Indicates whether to generate mipmaps for the texture
44    * @return A texture if loading succeeds, an empty handle otherwise
45    */
46   Texture LoadTexture( const char* imageUrl, bool generateMipmaps )
47   {
48     Texture texture;
49     Dali::BitmapLoader loader = Dali::BitmapLoader::New( imageUrl );
50     loader.Load();
51     PixelData pixelData = loader.GetPixelData();
52     if( pixelData )
53     {
54       texture = Texture::New( TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() );
55       texture.Upload( pixelData );
56
57       if( generateMipmaps )
58       {
59         texture.GenerateMipmaps();
60       }
61     }
62
63     return texture;
64   }
65 }// unnamed namespace
66
67 namespace Toolkit
68 {
69
70 namespace Internal
71 {
72
73 namespace
74 {
75
76 //Defines ordering of textures for shaders.
77 //All shaders, if including certain texture types, must include them in the same order.
78 //Within the texture set for the renderer, textures are ordered in the same manner.
79 enum TextureIndex
80 {
81   DIFFUSE_INDEX = 0u,
82   NORMAL_INDEX = 1u,
83   GLOSS_INDEX = 2u
84 };
85
86 const char * const RENDERER_TYPE_VALUE( "mesh" ); //String label for which type of control renderer this is.
87 const char * const LIGHT_POSITION( "uLightPosition" ); //Shader property
88 const char * const OBJECT_MATRIX( "uObjectMatrix" ); //Shader property
89
90 const char * const SHADER_TYPE_TEXTURELESS( "TEXTURELESS" );
91 const char * const SHADER_TYPE_DIFFUSE_TEXTURE( "DIFFUSE_TEXTURE" );
92 const char * const SHADER_TYPE_ALL_TEXTURES( "ALL_TEXTURES" );
93
94 //Shaders
95 //If a shader requires certain textures, they must be listed in order,
96 //as detailed in the TextureIndex enum documentation.
97
98 //A basic shader that doesn't use textures at all.
99 const char* SIMPLE_VERTEX_SHADER = DALI_COMPOSE_SHADER(
100   attribute highp vec3 aPosition;\n
101   attribute highp vec3 aNormal;\n
102   varying mediump vec3 vIllumination;\n
103   uniform mediump vec3 uSize;\n
104   uniform mediump mat4 uMvpMatrix;\n
105   uniform mediump mat4 uModelView;\n
106   uniform mediump mat3 uNormalMatrix;
107   uniform mediump mat4 uObjectMatrix;\n
108   uniform mediump vec3 uLightPosition;\n
109
110   void main()\n
111   {\n
112     vec4 vertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n
113     vertexPosition = uObjectMatrix * vertexPosition;\n
114     vertexPosition = uMvpMatrix * vertexPosition;\n
115
116     //Illumination in Model-View space - Transform attributes and uniforms\n
117     vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
118     vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
119     vec4 centre = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
120     vec4 lightPos = vec4( centre.x, centre.y, uLightPosition.z, 1.0 );\n
121     vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
122
123     float lightDiffuse = max( dot( vecToLight, normal ), 0.0 );\n
124     vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
125
126     gl_Position = vertexPosition;\n
127   }\n
128 );
129
130 //Fragment shader corresponding to the texture-less shader.
131 const char* SIMPLE_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
132   precision mediump float;\n
133   varying mediump vec3 vIllumination;\n
134   uniform lowp vec4 uColor;\n
135
136   void main()\n
137   {\n
138     gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a );\n
139   }\n
140 );
141
142 //Diffuse and specular illumination shader with albedo texture. Texture is index 0.
143 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
144   attribute highp vec3 aPosition;\n
145   attribute highp vec2 aTexCoord;\n
146   attribute highp vec3 aNormal;\n
147   varying mediump vec2 vTexCoord;\n
148   varying mediump vec3 vIllumination;\n
149   varying mediump float vSpecular;\n
150   uniform mediump vec3 uSize;\n
151   uniform mediump mat4 uMvpMatrix;\n
152   uniform mediump mat4 uModelView;
153   uniform mediump mat3 uNormalMatrix;
154   uniform mediump mat4 uObjectMatrix;\n
155   uniform mediump vec3 uLightPosition;\n
156
157   void main()
158   {\n
159     vec4 vertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n
160     vertexPosition = uObjectMatrix * vertexPosition;\n
161     vertexPosition = uMvpMatrix * vertexPosition;\n
162
163     //Illumination in Model-View space - Transform attributes and uniforms\n
164     vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
165     vec4 centre = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
166     vec4 lightPos = vec4( centre.x, centre.y, uLightPosition.z, 1.0 );\n
167     vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );\n
168
169     vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
170     vec3 viewDir = normalize( -vertPos.xyz );
171
172     vec3 halfVector = normalize( viewDir + vecToLight );
173
174     float lightDiffuse = dot( vecToLight, normal );\n
175     lightDiffuse = max( 0.0,lightDiffuse );\n
176     vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
177
178     vec3 reflectDir = reflect( -vecToLight, normal );
179     vSpecular = pow( max( dot( reflectDir, viewDir ), 0.0 ), 4.0 );
180
181     vTexCoord = aTexCoord;\n
182     gl_Position = vertexPosition;\n
183   }\n
184 );
185
186 //Fragment shader corresponding to the diffuse and specular illumination shader with albedo texture
187 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
188   precision mediump float;\n
189   varying mediump vec2 vTexCoord;\n
190   varying mediump vec3 vIllumination;\n
191   varying mediump float vSpecular;\n
192   uniform sampler2D sDiffuse;\n
193   uniform lowp vec4 uColor;\n
194
195   void main()\n
196   {\n
197     vec4 texture = texture2D( sDiffuse, vTexCoord );\n
198     gl_FragColor = vec4( vIllumination.rgb * texture.rgb * uColor.rgb + vSpecular * 0.3, texture.a * uColor.a );\n
199   }\n
200 );
201
202 //Diffuse and specular illumination shader with albedo texture, normal map and gloss map shader.
203 //Diffuse (albedo) texture is index 0, normal is 1, gloss is 2. They must be declared in this order.
204 const char* NORMAL_MAP_VERTEX_SHADER = DALI_COMPOSE_SHADER(
205   attribute highp vec3 aPosition;\n
206   attribute highp vec2 aTexCoord;\n
207   attribute highp vec3 aNormal;\n
208   attribute highp vec3 aTangent;\n
209   attribute highp vec3 aBiNormal;\n
210   varying mediump vec2 vTexCoord;\n
211   varying mediump vec3 vLightDirection;\n
212   varying mediump vec3 vHalfVector;\n
213   uniform mediump vec3 uSize;\n
214   uniform mediump mat4 uMvpMatrix;\n
215   uniform mediump mat4 uModelView;
216   uniform mediump mat3 uNormalMatrix;
217   uniform mediump mat4 uObjectMatrix;\n
218   uniform mediump vec3 uLightPosition;\n
219
220   void main()
221   {\n
222     vec4 vertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n
223     vertexPosition = uObjectMatrix * vertexPosition;\n
224     vertexPosition = uMvpMatrix * vertexPosition;\n
225
226     vec4 vertPos = uModelView * vec4( aPosition.xyz, 1.0 );\n
227     vec4 centre = uModelView * vec4( 0.0, 0.0, 0.0, 1.0 );\n
228     vec4 lightPos = vec4( centre.x, centre.y, uLightPosition.z, 1.0 );\n
229
230     vec3 tangent = normalize( uNormalMatrix * aTangent );
231     vec3 binormal = normalize( uNormalMatrix * aBiNormal );
232     vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );
233
234     vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
235     vLightDirection.x = dot( vecToLight, tangent );
236     vLightDirection.y = dot( vecToLight, binormal );
237     vLightDirection.z = dot( vecToLight, normal );
238
239     vec3 viewDir = normalize( -vertPos.xyz );
240     vec3 halfVector = normalize( viewDir + vecToLight );
241     vHalfVector.x = dot( halfVector, tangent );
242     vHalfVector.y = dot( halfVector, binormal );
243     vHalfVector.z = dot( halfVector, normal );
244
245     vTexCoord = aTexCoord;\n
246     gl_Position = vertexPosition;\n
247   }\n
248 );
249
250 //Fragment shader corresponding to the shader that uses all textures (diffuse, normal and gloss maps)
251 const char* NORMAL_MAP_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
252   precision mediump float;\n
253   varying mediump vec2 vTexCoord;\n
254   varying mediump vec3 vLightDirection;\n
255   varying mediump vec3 vHalfVector;\n
256   uniform sampler2D sDiffuse;\n
257   uniform sampler2D sNormal;\n
258   uniform sampler2D sGloss;\n
259   uniform lowp vec4 uColor;\n
260
261   void main()\n
262   {\n
263     vec4 texture = texture2D( sDiffuse, vTexCoord );\n
264     vec3 normal = normalize( texture2D( sNormal, vTexCoord ).xyz * 2.0 - 1.0 );\n
265     vec4 glossMap = texture2D( sGloss, vTexCoord );\n
266
267     float lightDiffuse = max( 0.0, dot( normal, normalize( vLightDirection ) ) );\n
268     lightDiffuse = lightDiffuse * 0.5 + 0.5;\n
269
270     float shininess = pow ( max ( dot ( normalize( vHalfVector ), normal ), 0.0 ), 16.0 )  ;
271
272     gl_FragColor = vec4( texture.rgb * uColor.rgb * lightDiffuse + shininess * glossMap.rgb, texture.a * uColor.a );\n
273   }\n
274 );
275
276 } // namespace
277
278 MeshRenderer::MeshRenderer( RendererFactoryCache& factoryCache )
279 : ControlRenderer( factoryCache ),
280   mShaderType( ALL_TEXTURES ),
281   mUseTexture( true ),
282   mUseMipmapping( true )
283 {
284 }
285
286 MeshRenderer::~MeshRenderer()
287 {
288 }
289
290 void MeshRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
291 {
292   Property::Value* objectUrl = propertyMap.Find( OBJECT_URL );
293   if( !objectUrl || !objectUrl->Get( mObjectUrl ) )
294   {
295     DALI_LOG_ERROR( "Fail to provide object URL to the MeshRenderer object.\n" );
296   }
297
298   Property::Value* materialUrl = propertyMap.Find( MATERIAL_URL );
299
300   if( !materialUrl || !materialUrl->Get( mMaterialUrl ) || mMaterialUrl.empty() )
301   {
302     mUseTexture = false;
303   }
304
305   Property::Value* imagesUrl = propertyMap.Find( TEXTURES_PATH );
306   if( !imagesUrl || !imagesUrl->Get( mTexturesPath ) )
307   {
308     //Default behaviour is to assume files are in the same directory,
309     // or have their locations detailed in full when supplied.
310     mTexturesPath.clear();
311   }
312
313   Property::Value* useMipmapping = propertyMap.Find( USE_MIPMAPPING );
314   if( useMipmapping )
315   {
316     useMipmapping->Get( mUseMipmapping );
317   }
318
319   Property::Value* shaderType = propertyMap.Find( SHADER_TYPE );
320   if( shaderType )
321   {
322     std::string shaderTypeString;
323     if( shaderType->Get( shaderTypeString ) )
324     {
325       if( shaderTypeString == SHADER_TYPE_TEXTURELESS )
326       {
327         mShaderType = TEXTURELESS;
328       }
329       else if( shaderTypeString == SHADER_TYPE_DIFFUSE_TEXTURE )
330       {
331         mShaderType = DIFFUSE_TEXTURE;
332       }
333       else if( shaderTypeString == SHADER_TYPE_ALL_TEXTURES )
334       {
335         mShaderType = ALL_TEXTURES;
336       }
337       else
338       {
339         DALI_LOG_ERROR( "Unknown shader type provided to the MeshRenderer object.\n");
340       }
341     }
342   }
343 }
344
345 void MeshRenderer::SetSize( const Vector2& size )
346 {
347   ControlRenderer::SetSize( size );
348
349   // ToDo: renderer responds to the size change
350 }
351
352 void MeshRenderer::SetClipRect( const Rect<int>& clipRect )
353 {
354   ControlRenderer::SetClipRect( clipRect );
355
356   //ToDo: renderer responds to the clipRect change
357 }
358
359 void MeshRenderer::SetOffset( const Vector2& offset )
360 {
361   //ToDo: renderer applies the offset
362 }
363
364 void MeshRenderer::DoSetOnStage( Actor& actor )
365 {
366   InitializeRenderer();
367 }
368
369 void MeshRenderer::DoCreatePropertyMap( Property::Map& map ) const
370 {
371   map.Clear();
372   map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
373   map.Insert( OBJECT_URL, mObjectUrl );
374   map.Insert( MATERIAL_URL, mMaterialUrl );
375   map.Insert( TEXTURES_PATH, mTexturesPath );
376
377   std::string shaderTypeString;
378   switch( mShaderType )
379   {
380     case ALL_TEXTURES:
381     {
382       shaderTypeString = SHADER_TYPE_ALL_TEXTURES;
383       break;
384     }
385
386     case DIFFUSE_TEXTURE:
387     {
388       shaderTypeString = SHADER_TYPE_DIFFUSE_TEXTURE;
389       break;
390     }
391
392     case TEXTURELESS:
393     {
394       shaderTypeString = SHADER_TYPE_TEXTURELESS;
395       break;
396     }
397   }
398   map.Insert( SHADER_TYPE, shaderTypeString );
399 }
400
401 void MeshRenderer::InitializeRenderer()
402 {
403   //Try to load the geometry from the file.
404   if( !LoadGeometry() )
405   {
406     SupplyEmptyGeometry();
407     return;
408   }
409
410   //If a texture is used by the obj file, load the supplied material file.
411   if( mObjLoader.IsTexturePresent() && !mMaterialUrl.empty() )
412   {
413     if( !LoadMaterial() )
414     {
415       SupplyEmptyGeometry();
416       return;
417     }
418   }
419
420   //Now that the required parts are loaded, create the geometry for the object.
421   if( !CreateGeometry() )
422   {
423     SupplyEmptyGeometry();
424     return;
425   }
426
427   CreateShader();
428
429   //Load the various texture files supplied by the material file.
430   if( !LoadTextures() )
431   {
432     SupplyEmptyGeometry();
433     return;
434   }
435
436   mImpl->mRenderer = Renderer::New( mGeometry, mShader );
437   mImpl->mRenderer.SetTextures( mTextureSet );
438   mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
439 }
440
441 void MeshRenderer::SupplyEmptyGeometry()
442 {
443   mGeometry = Geometry::New();
444   mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
445   mImpl->mRenderer = Renderer::New( mGeometry, mShader );
446
447   DALI_LOG_ERROR( "Initialisation error in mesh renderer.\n" );
448 }
449
450 void MeshRenderer::UpdateShaderUniforms()
451 {
452   Stage stage = Stage::GetCurrent();
453
454   Vector3 lightPosition( 0, 0, stage.GetSize().width );
455   mShader.RegisterProperty( LIGHT_POSITION, lightPosition );
456
457   Matrix scaleMatrix;
458   scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
459   mShader.RegisterProperty( OBJECT_MATRIX, scaleMatrix );
460 }
461
462 void MeshRenderer::CreateShader()
463 {
464   if( mShaderType == ALL_TEXTURES )
465   {
466     mShader = Shader::New( NORMAL_MAP_VERTEX_SHADER, NORMAL_MAP_FRAGMENT_SHADER );
467   }
468   else if( mShaderType == DIFFUSE_TEXTURE )
469   {
470     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
471   }
472   else //Textureless
473   {
474     mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
475   }
476
477   UpdateShaderUniforms();
478 }
479
480 bool MeshRenderer::CreateGeometry()
481 {
482   //Determine if we need to use a simpler shader to handle the provided data
483   if( !mUseTexture || !mObjLoader.IsDiffuseMapPresent() )
484   {
485     mShaderType = TEXTURELESS;
486   }
487   else if( mShaderType == ALL_TEXTURES && (!mObjLoader.IsNormalMapPresent() || !mObjLoader.IsSpecularMapPresent()) )
488   {
489     mShaderType = DIFFUSE_TEXTURE;
490   }
491
492   int objectProperties = 0;
493
494   if( mShaderType == DIFFUSE_TEXTURE ||
495       mShaderType == ALL_TEXTURES )
496   {
497     objectProperties |= ObjLoader::TEXTURE_COORDINATES;
498   }
499
500   if( mShaderType == ALL_TEXTURES )
501   {
502     objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
503   }
504
505   //Create geometry with attributes required by shader.
506   mGeometry = mObjLoader.CreateGeometry( objectProperties );
507
508   if( mGeometry )
509   {
510     return true;
511   }
512
513   DALI_LOG_ERROR( "Failed to load geometry in mesh renderer.\n" );
514   return false;
515 }
516
517 bool MeshRenderer::LoadGeometry()
518 {
519   std::streampos fileSize;
520   Dali::Vector<char> fileContent;
521
522   if( FileLoader::ReadFile( mObjectUrl, fileSize, fileContent, FileLoader::TEXT ) )
523   {
524     mObjLoader.ClearArrays();
525     mObjLoader.LoadObject( fileContent.Begin(), fileSize );
526
527     //Get size information from the obj loaded
528     mSceneCenter = mObjLoader.GetCenter();
529     mSceneSize = mObjLoader.GetSize();
530
531     return true;
532   }
533
534   DALI_LOG_ERROR( "Failed to find object to load in mesh renderer.\n" );
535   return false;
536 }
537
538 bool MeshRenderer::LoadMaterial()
539 {
540   std::streampos fileSize;
541   Dali::Vector<char> fileContent;
542
543   if( FileLoader::ReadFile( mMaterialUrl, fileSize, fileContent, FileLoader::TEXT ) )
544   {
545     //Load data into obj (usable) form
546     mObjLoader.LoadMaterial( fileContent.Begin(), fileSize, mDiffuseTextureUrl, mNormalTextureUrl, mGlossTextureUrl );
547     return true;
548   }
549
550   DALI_LOG_ERROR( "Failed to find texture set to load in mesh renderer.\n" );
551   mUseTexture = false;
552   return false;
553 }
554
555 bool MeshRenderer::LoadTextures()
556 {
557   mTextureSet = TextureSet::New();
558
559   if( mShaderType != TEXTURELESS )
560   {
561     Sampler sampler = Sampler::New();
562     if( mUseMipmapping )
563     {
564       sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR );
565     }
566
567     if( !mDiffuseTextureUrl.empty() )
568     {
569       std::string imageUrl = mTexturesPath + mDiffuseTextureUrl;
570
571       //Load textures
572       Texture diffuseTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
573       if( diffuseTexture )
574       {
575         mTextureSet.SetTexture( DIFFUSE_INDEX, diffuseTexture );
576         mTextureSet.SetSampler( DIFFUSE_INDEX, sampler );
577       }
578       else
579       {
580         DALI_LOG_ERROR( "Failed to load diffuse map texture in mesh renderer.\n");
581         return false;
582       }
583     }
584
585     if( !mNormalTextureUrl.empty() && ( mShaderType == ALL_TEXTURES ) )
586     {
587       std::string imageUrl = mTexturesPath + mNormalTextureUrl;
588
589       //Load textures
590       Texture normalTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
591       if( normalTexture )
592       {
593         mTextureSet.SetTexture( NORMAL_INDEX, normalTexture );
594         mTextureSet.SetSampler( NORMAL_INDEX, sampler );
595       }
596       else
597       {
598         DALI_LOG_ERROR( "Failed to load normal map texture in mesh renderer.\n");
599         return false;
600       }
601     }
602
603     if( !mGlossTextureUrl.empty() && ( mShaderType == ALL_TEXTURES ) )
604     {
605       std::string imageUrl = mTexturesPath + mGlossTextureUrl;
606
607       //Load textures
608       Texture glossTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
609       if( glossTexture )
610       {
611         mTextureSet.SetTexture( GLOSS_INDEX, glossTexture );
612         mTextureSet.SetSampler( GLOSS_INDEX, sampler );
613       }
614       else
615       {
616         DALI_LOG_ERROR( "Failed to load gloss map texture in mesh renderer.\n");
617         return false;
618       }
619     }
620   }
621   return true;
622 }
623
624 } // namespace Internal
625
626 } // namespace Toolkit
627
628 } // namespace Dali