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