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