2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "mesh-visual.h"
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/common/stage.h>
24 #include <dali/devel-api/adaptor-framework/image-loading.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>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
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
45 Texture LoadTexture( const char* imageUrl, bool generateMipmaps )
49 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( imageUrl );
52 texture = Texture::New( TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight() );
53 PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
54 texture.Upload( pixelData );
58 texture.GenerateMipmaps();
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.
87 DALI_ENUM_TO_STRING_TABLE_BEGIN( SHADING_MODE )
88 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::MeshVisual::ShadingMode, TEXTURELESS_WITH_DIFFUSE_LIGHTING )
89 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::MeshVisual::ShadingMode, TEXTURED_WITH_SPECULAR_LIGHTING )
90 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::MeshVisual::ShadingMode, TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING )
91 DALI_ENUM_TO_STRING_TABLE_END( SHADING_MODE )
94 const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" );
95 const char * const STAGE_OFFSET_UNIFORM_NAME( "uStageOffset" );
98 //If a shader requires certain textures, they must be listed in order,
99 //as detailed in the TextureIndex enum documentation.
101 //A basic shader that doesn't use textures at all.
102 const char* SIMPLE_VERTEX_SHADER = DALI_COMPOSE_SHADER(
103 attribute highp vec3 aPosition;\n
104 attribute highp vec3 aNormal;\n
105 varying mediump vec3 vIllumination;\n
106 uniform mediump vec3 uSize;\n
107 uniform mediump mat4 uMvpMatrix;\n
108 uniform mediump mat4 uModelView;\n
109 uniform mediump mat4 uViewMatrix;\n
110 uniform mediump mat3 uNormalMatrix;
111 uniform mediump mat4 uObjectMatrix;\n
112 uniform mediump vec3 lightPosition;\n
113 uniform mediump vec2 uStageOffset;\n
115 //Visual size and offset
116 uniform mediump vec2 offset;\n
117 uniform mediump vec2 size;\n
118 uniform mediump vec4 offsetSizeMode;\n
119 uniform mediump vec2 origin;\n
120 uniform mediump vec2 anchorPoint;\n
122 vec4 ComputeVertexPosition()\n
124 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
125 float scaleFactor = min( visualSize.x, visualSize.y );\n
126 vec3 originFlipY = vec3(origin.x, -origin.y, 0.0);
127 vec3 anchorPointFlipY = vec3( anchorPoint.x, -anchorPoint.y, 0.0);
128 vec3 offset = vec3( ( offset / uSize.xy ) * offsetSizeMode.xy + offset * (1.0-offsetSizeMode.xy), 0.0) * vec3(1.0,-1.0,1.0);\n
129 return vec4( (aPosition + anchorPointFlipY)*scaleFactor + (offset + originFlipY)*uSize, 1.0 );\n
134 vec4 normalisedVertexPosition = ComputeVertexPosition();\n
135 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
136 vertexPosition = uMvpMatrix * vertexPosition;\n
138 //Illumination in Model-View space - Transform attributes and uniforms\n
139 vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
140 vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
142 vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
143 mvLightPosition = uViewMatrix * mvLightPosition;\n
144 vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
146 float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
147 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
149 gl_Position = vertexPosition;\n
153 //Fragment shader corresponding to the texture-less shader.
154 const char* SIMPLE_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
155 precision mediump float;\n
156 varying mediump vec3 vIllumination;\n
157 uniform lowp vec4 uColor;\n
158 uniform lowp vec3 mixColor;\n
159 uniform lowp float preMultipliedAlpha;\n
163 gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a ) * vec4( mixColor, 1.0 );\n
167 //Diffuse and specular illumination shader with albedo texture. Texture is index 0.
168 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
169 attribute highp vec3 aPosition;\n
170 attribute highp vec2 aTexCoord;\n
171 attribute highp vec3 aNormal;\n
172 varying mediump vec2 vTexCoord;\n
173 varying mediump vec3 vIllumination;\n
174 varying mediump float vSpecular;\n
175 uniform mediump vec3 uSize;\n
176 uniform mediump mat4 uMvpMatrix;\n
177 uniform mediump mat4 uModelView;
178 uniform mediump mat4 uViewMatrix;\n
179 uniform mediump mat3 uNormalMatrix;
180 uniform mediump mat4 uObjectMatrix;\n
181 uniform mediump vec3 lightPosition;\n
182 uniform mediump vec2 uStageOffset;\n
184 //Visual size and offset
185 uniform mediump vec2 offset;\n
186 uniform mediump vec2 size;\n
187 uniform mediump vec4 offsetSizeMode;\n
188 uniform mediump vec2 origin;\n
189 uniform mediump vec2 anchorPoint;\n
191 vec4 ComputeVertexPosition()\n
193 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
194 float scaleFactor = min( visualSize.x, visualSize.y );\n
195 vec3 originFlipY = vec3(origin.x, -origin.y, 0.0);
196 vec3 anchorPointFlipY = vec3( anchorPoint.x, -anchorPoint.y, 0.0);
197 vec3 offset = vec3( ( offset / uSize.xy ) * offsetSizeMode.xy + offset * (1.0-offsetSizeMode.xy), 0.0) * vec3(1.0,-1.0,1.0);\n
198 return vec4( (aPosition + anchorPointFlipY)*scaleFactor + (offset + originFlipY)*uSize, 1.0 );\n
203 vec4 normalisedVertexPosition = ComputeVertexPosition();\n
204 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
205 vertexPosition = uMvpMatrix * vertexPosition;\n
207 //Illumination in Model-View space - Transform attributes and uniforms\n
208 vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
209 vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );\n
211 vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
212 mvLightPosition = uViewMatrix * mvLightPosition;\n
213 vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
215 vec3 viewDirection = normalize( -mvVertexPosition.xyz );
217 float lightDiffuse = dot( vectorToLight, normal );\n
218 lightDiffuse = max( 0.0,lightDiffuse );\n
219 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
221 vec3 reflectDirection = reflect( -vectorToLight, normal );
222 vSpecular = pow( max( dot( reflectDirection, viewDirection ), 0.0 ), 4.0 );
224 vTexCoord = aTexCoord;\n
225 gl_Position = vertexPosition;\n
229 //Fragment shader corresponding to the diffuse and specular illumination shader with albedo texture
230 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
231 precision mediump float;\n
232 varying mediump vec2 vTexCoord;\n
233 varying mediump vec3 vIllumination;\n
234 varying mediump float vSpecular;\n
235 uniform sampler2D sDiffuse;\n
236 uniform lowp vec4 uColor;\n
237 uniform lowp vec3 mixColor;\n
238 uniform lowp float preMultipliedAlpha;\n
242 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
243 vec4 visualMixColor = vec4( mixColor, 1.0 );\n
244 gl_FragColor = vec4( vIllumination.rgb * texture.rgb * uColor.rgb * visualMixColor.rgb + vSpecular * 0.3, texture.a * uColor.a * visualMixColor.a );\n
248 //Diffuse and specular illumination shader with albedo texture, normal map and gloss map shader.
249 //Diffuse (albedo) texture is index 0, normal is 1, gloss is 2. They must be declared in this order.
250 const char* NORMAL_MAP_VERTEX_SHADER = DALI_COMPOSE_SHADER(
251 attribute highp vec3 aPosition;\n
252 attribute highp vec2 aTexCoord;\n
253 attribute highp vec3 aNormal;\n
254 attribute highp vec3 aTangent;\n
255 attribute highp vec3 aBiNormal;\n
256 varying mediump vec2 vTexCoord;\n
257 varying mediump vec3 vLightDirection;\n
258 varying mediump vec3 vHalfVector;\n
259 uniform mediump vec3 uSize;\n
260 uniform mediump mat4 uMvpMatrix;\n
261 uniform mediump mat4 uModelView;
262 uniform mediump mat4 uViewMatrix;\n
263 uniform mediump mat3 uNormalMatrix;
264 uniform mediump mat4 uObjectMatrix;\n
265 uniform mediump vec3 lightPosition;\n
266 uniform mediump vec2 uStageOffset;\n
268 //Visual size and offset
269 uniform mediump vec2 offset;\n
270 uniform mediump vec2 size;\n
271 uniform mediump vec4 offsetSizeMode;\n
272 uniform mediump vec2 origin;\n
273 uniform mediump vec2 anchorPoint;\n
275 vec4 ComputeVertexPosition()\n
277 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
278 float scaleFactor = min( visualSize.x, visualSize.y );\n
279 vec3 originFlipY = vec3(origin.x, -origin.y, 0.0);
280 vec3 anchorPointFlipY = vec3( anchorPoint.x, -anchorPoint.y, 0.0);
281 vec3 offset = vec3( ( offset / uSize.xy ) * offsetSizeMode.xy + offset * (1.0-offsetSizeMode.xy), 0.0) * vec3(1.0,-1.0,1.0);\n
282 return vec4( (aPosition + anchorPointFlipY)*scaleFactor + (offset + originFlipY)*uSize, 1.0 );\n
287 vec4 normalisedVertexPosition = ComputeVertexPosition();\n
288 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
289 vertexPosition = uMvpMatrix * vertexPosition;\n
291 vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
293 vec3 tangent = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aTangent );
294 vec3 binormal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aBiNormal );
295 vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );
297 vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
298 mvLightPosition = uViewMatrix * mvLightPosition;\n
299 vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
300 vLightDirection.x = dot( vectorToLight, tangent );
301 vLightDirection.y = dot( vectorToLight, binormal );
302 vLightDirection.z = dot( vectorToLight, normal );
304 vec3 viewDirection = normalize( -mvVertexPosition.xyz );
305 vec3 halfVector = normalize( viewDirection + vectorToLight );
306 vHalfVector.x = dot( halfVector, tangent );
307 vHalfVector.y = dot( halfVector, binormal );
308 vHalfVector.z = dot( halfVector, normal );
310 vTexCoord = aTexCoord;\n
311 gl_Position = vertexPosition;\n
315 //Fragment shader corresponding to the shader that uses all textures (diffuse, normal and gloss maps)
316 const char* NORMAL_MAP_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
317 precision mediump float;\n
318 varying mediump vec2 vTexCoord;\n
319 varying mediump vec3 vLightDirection;\n
320 varying mediump vec3 vHalfVector;\n
321 uniform sampler2D sDiffuse;\n
322 uniform sampler2D sNormal;\n
323 uniform sampler2D sGloss;\n
324 uniform lowp vec4 uColor;\n
325 uniform lowp vec3 mixColor;\n
326 uniform lowp float preMultipliedAlpha;\n
330 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
331 vec3 normal = normalize( texture2D( sNormal, vTexCoord ).xyz * 2.0 - 1.0 );\n
332 vec4 glossMap = texture2D( sGloss, vTexCoord );\n
333 vec4 visualMixColor = vec4( mixColor, 1.0 );\n
335 float lightDiffuse = max( 0.0, dot( normal, normalize( vLightDirection ) ) );\n
336 lightDiffuse = lightDiffuse * 0.5 + 0.5;\n
338 float shininess = pow ( max ( dot ( normalize( vHalfVector ), normal ), 0.0 ), 16.0 ) ;
340 gl_FragColor = vec4( texture.rgb * uColor.rgb * visualMixColor.rgb * lightDiffuse + shininess * glossMap.rgb, texture.a * uColor.a * visualMixColor.a );\n
344 } // unnamed namespace
346 MeshVisualPtr MeshVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
348 MeshVisualPtr meshVisualPtr( new MeshVisual( factoryCache ) );
349 meshVisualPtr->SetProperties( properties );
350 return meshVisualPtr;
353 MeshVisual::MeshVisual( VisualFactoryCache& factoryCache )
354 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::MESH ),
355 mShadingMode( Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING ),
357 mUseMipmapping( true ),
358 mUseSoftNormals( true )
362 MeshVisual::~MeshVisual()
366 void MeshVisual::DoSetProperties( const Property::Map& propertyMap )
368 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
370 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
371 if( keyValue.first.type == Property::Key::INDEX )
373 DoSetProperty( keyValue.first.indexKey, keyValue.second );
377 if( keyValue.first == OBJECT_URL_NAME )
379 DoSetProperty( Toolkit::MeshVisual::Property::OBJECT_URL, keyValue.second );
381 else if( keyValue.first == MATERIAL_URL_NAME )
383 DoSetProperty( Toolkit::MeshVisual::Property::MATERIAL_URL, keyValue.second );
385 else if( keyValue.first == TEXTURES_PATH_NAME )
387 DoSetProperty( Toolkit::MeshVisual::Property::TEXTURES_PATH, keyValue.second );
389 else if( keyValue.first == SHADING_MODE_NAME )
391 DoSetProperty( Toolkit::MeshVisual::Property::SHADING_MODE, keyValue.second );
393 else if( keyValue.first == USE_MIPMAPPING_NAME )
395 DoSetProperty( Toolkit::MeshVisual::Property::USE_MIPMAPPING, keyValue.second );
397 else if( keyValue.first == USE_SOFT_NORMALS_NAME )
399 DoSetProperty( Toolkit::MeshVisual::Property::USE_SOFT_NORMALS, keyValue.second );
401 else if( keyValue.first == LIGHT_POSITION_NAME )
403 DoSetProperty( Toolkit::MeshVisual::Property::LIGHT_POSITION, keyValue.second );
408 if( mMaterialUrl.empty() )
413 if( mLightPosition == Vector3::ZERO )
415 // Default behaviour is to place the light directly in front of the object,
416 // at a reasonable distance to light everything on screen.
417 Stage stage = Stage::GetCurrent();
419 mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 );
423 void MeshVisual::DoSetProperty( Property::Index index, const Property::Value& value )
427 case Toolkit::MeshVisual::Property::OBJECT_URL:
429 if( !value.Get( mObjectUrl ) )
431 DALI_LOG_ERROR("MeshVisual: property objectUrl is the wrong type, use STRING\n");
435 case Toolkit::MeshVisual::Property::MATERIAL_URL:
437 if( ! value.Get( mMaterialUrl ) )
439 DALI_LOG_ERROR("MeshVisual: property materialUrl is the wrong type, use STRING\n");
443 case Toolkit::MeshVisual::Property::TEXTURES_PATH:
445 if( ! value.Get( mTexturesPath ) )
447 mTexturesPath.clear();
451 case Toolkit::MeshVisual::Property::SHADING_MODE:
453 Scripting::GetEnumerationProperty( value, SHADING_MODE_TABLE, SHADING_MODE_TABLE_COUNT, mShadingMode );
456 case Toolkit::MeshVisual::Property::USE_MIPMAPPING:
458 if( !value.Get( mUseMipmapping ) )
460 DALI_LOG_ERROR("MeshVisual: property useMipmapping is the wrong type, use BOOLEAN\n");
464 case Toolkit::MeshVisual::Property::USE_SOFT_NORMALS:
466 if( !value.Get( mUseSoftNormals ) )
468 DALI_LOG_ERROR("MeshVisual: property useSoftNormals is the wrong type, use BOOLEAN\n");
472 case Toolkit::MeshVisual::Property::LIGHT_POSITION:
474 if( !value.Get( mLightPosition ) )
476 mLightPosition = Vector3::ZERO;
477 DALI_LOG_ERROR("MeshVisual: property lightPosition is the wrong type, use VECTOR3\n");
484 void MeshVisual::OnSetTransform()
486 if( mImpl->mRenderer )
488 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
492 void MeshVisual::DoSetOnScene( Actor& actor )
494 InitializeRenderer();
496 actor.AddRenderer( mImpl->mRenderer );
498 // Mesh loaded and ready to display
499 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
502 void MeshVisual::DoCreatePropertyMap( Property::Map& map ) const
505 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::MESH );
506 map.Insert( Toolkit::MeshVisual::Property::OBJECT_URL, mObjectUrl );
507 map.Insert( Toolkit::MeshVisual::Property::MATERIAL_URL, mMaterialUrl );
508 map.Insert( Toolkit::MeshVisual::Property::TEXTURES_PATH, mTexturesPath );
509 map.Insert( Toolkit::MeshVisual::Property::SHADING_MODE, mShadingMode );
510 map.Insert( Toolkit::MeshVisual::Property::USE_MIPMAPPING, mUseMipmapping );
511 map.Insert( Toolkit::MeshVisual::Property::USE_SOFT_NORMALS, mUseSoftNormals );
512 map.Insert( Toolkit::MeshVisual::Property::LIGHT_POSITION, mLightPosition );
515 void MeshVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
520 void MeshVisual::InitializeRenderer()
522 //Try to load the geometry from the file.
523 if( !LoadGeometry() )
525 SupplyEmptyGeometry();
529 //If a texture is used by the obj file, load the supplied material file.
530 if( mObjLoader.IsTexturePresent() && !mMaterialUrl.empty() )
532 if( !LoadMaterial() )
534 SupplyEmptyGeometry();
539 //Now that the required parts are loaded, create the geometry for the object.
540 if( !CreateGeometry() )
542 SupplyEmptyGeometry();
548 //Load the various texture files supplied by the material file.
549 if( !LoadTextures() )
551 SupplyEmptyGeometry();
555 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
556 mImpl->mRenderer.SetTextures( mTextureSet );
557 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
558 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
560 //Register transform properties
561 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
564 void MeshVisual::SupplyEmptyGeometry()
566 mGeometry = Geometry::New();
567 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
568 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
570 DALI_LOG_ERROR( "Initialisation error in mesh visual.\n" );
573 void MeshVisual::UpdateShaderUniforms()
575 Stage stage = Stage::GetCurrent();
576 float width = stage.GetSize().width;
577 float height = stage.GetSize().height;
580 scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
582 mShader.RegisterProperty( STAGE_OFFSET_UNIFORM_NAME, Vector2( width, height ) / 2.0f );
583 mShader.RegisterProperty( LIGHT_POSITION_NAME, mLightPosition );
584 mShader.RegisterProperty( OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix );
587 void MeshVisual::CreateShader()
589 if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING )
591 mShader = Shader::New( NORMAL_MAP_VERTEX_SHADER, NORMAL_MAP_FRAGMENT_SHADER );
593 else if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING )
595 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
599 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
602 UpdateShaderUniforms();
605 bool MeshVisual::CreateGeometry()
607 //Determine if we need to use a simpler shader to handle the provided data
608 if( !mUseTexture || !mObjLoader.IsDiffuseMapPresent() )
610 mShadingMode = Toolkit::MeshVisual::ShadingMode::TEXTURELESS_WITH_DIFFUSE_LIGHTING;
612 else if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING && (!mObjLoader.IsNormalMapPresent() || !mObjLoader.IsSpecularMapPresent()) )
614 mShadingMode = Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING;
617 int objectProperties = 0;
619 if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING ||
620 mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING )
622 objectProperties |= ObjLoader::TEXTURE_COORDINATES;
625 if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING )
627 objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
630 //Create geometry with attributes required by shader.
631 mGeometry = mObjLoader.CreateGeometry( objectProperties, mUseSoftNormals );
638 DALI_LOG_ERROR( "Failed to load geometry in mesh visual.\n" );
642 bool MeshVisual::LoadGeometry()
644 std::streampos fileSize;
645 Dali::Vector<char> fileContent;
647 if( FileLoader::ReadFile( mObjectUrl, fileSize, fileContent, FileLoader::TEXT ) )
649 mObjLoader.ClearArrays();
650 mObjLoader.LoadObject( fileContent.Begin(), fileSize );
652 //Get size information from the obj loaded
653 mSceneCenter = mObjLoader.GetCenter();
654 mSceneSize = mObjLoader.GetSize();
659 DALI_LOG_ERROR( "Failed to find object to load in mesh visual.\n" );
663 bool MeshVisual::LoadMaterial()
665 std::streampos fileSize;
666 Dali::Vector<char> fileContent;
668 if( FileLoader::ReadFile( mMaterialUrl, fileSize, fileContent, FileLoader::TEXT ) )
670 //Load data into obj (usable) form
671 mObjLoader.LoadMaterial( fileContent.Begin(), fileSize, mDiffuseTextureUrl, mNormalTextureUrl, mGlossTextureUrl );
675 DALI_LOG_ERROR( "Failed to find texture set to load in mesh visual.\n" );
680 bool MeshVisual::LoadTextures()
682 mTextureSet = TextureSet::New();
684 if( mShadingMode != Toolkit::MeshVisual::ShadingMode::TEXTURELESS_WITH_DIFFUSE_LIGHTING )
686 Sampler sampler = Sampler::New();
689 sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR );
692 if( !mDiffuseTextureUrl.empty() )
694 std::string imageUrl = mTexturesPath + mDiffuseTextureUrl;
697 Texture diffuseTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
700 mTextureSet.SetTexture( DIFFUSE_INDEX, diffuseTexture );
701 mTextureSet.SetSampler( DIFFUSE_INDEX, sampler );
705 DALI_LOG_ERROR( "Failed to load diffuse map texture in mesh visual.\n");
710 if( !mNormalTextureUrl.empty() && ( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING ) )
712 std::string imageUrl = mTexturesPath + mNormalTextureUrl;
715 Texture normalTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
718 mTextureSet.SetTexture( NORMAL_INDEX, normalTexture );
719 mTextureSet.SetSampler( NORMAL_INDEX, sampler );
723 DALI_LOG_ERROR( "Failed to load normal map texture in mesh visual.\n");
728 if( !mGlossTextureUrl.empty() && ( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING ) )
730 std::string imageUrl = mTexturesPath + mGlossTextureUrl;
733 Texture glossTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
736 mTextureSet.SetTexture( GLOSS_INDEX, glossTexture );
737 mTextureSet.SetSampler( GLOSS_INDEX, sampler );
741 DALI_LOG_ERROR( "Failed to load gloss map texture in mesh visual.\n");
749 } // namespace Internal
751 } // namespace Toolkit