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