2 * Copyright (c) 2016 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/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>
31 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.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 )
48 Dali::BitmapLoader loader = Dali::BitmapLoader::New( imageUrl );
50 PixelData pixelData = loader.GetPixelData();
53 texture = Texture::New( TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() );
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.
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" );
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 )
102 const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" );
103 const char * const STAGE_OFFSET_UNIFORM_NAME( "uStageOffset" );
106 //If a shader requires certain textures, they must be listed in order,
107 //as detailed in the TextureIndex enum documentation.
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
125 vec4 normalisedVertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n
126 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
127 vertexPosition = uMvpMatrix * vertexPosition;\n
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
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
137 float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
138 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
140 gl_Position = vertexPosition;\n
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
152 gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a );\n
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
175 vec4 normalisedVertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n
176 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
177 vertexPosition = uMvpMatrix * vertexPosition;\n
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
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
187 vec3 viewDirection = normalize( -mvVertexPosition.xyz );
189 float lightDiffuse = dot( vectorToLight, normal );\n
190 lightDiffuse = max( 0.0,lightDiffuse );\n
191 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
193 vec3 reflectDirection = reflect( -vectorToLight, normal );
194 vSpecular = pow( max( dot( reflectDirection, viewDirection ), 0.0 ), 4.0 );
196 vTexCoord = aTexCoord;\n
197 gl_Position = vertexPosition;\n
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
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
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
238 vec4 normalisedVertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n
239 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
240 vertexPosition = uMvpMatrix * vertexPosition;\n
242 vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
244 vec3 tangent = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aTangent );
245 vec3 binormal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aBiNormal );
246 vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );
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 );
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 );
261 vTexCoord = aTexCoord;\n
262 gl_Position = vertexPosition;\n
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
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
283 float lightDiffuse = max( 0.0, dot( normal, normalize( vLightDirection ) ) );\n
284 lightDiffuse = lightDiffuse * 0.5 + 0.5;\n
286 float shininess = pow ( max ( dot ( normalize( vHalfVector ), normal ), 0.0 ), 16.0 ) ;
288 gl_FragColor = vec4( texture.rgb * uColor.rgb * lightDiffuse + shininess * glossMap.rgb, texture.a * uColor.a );\n
292 } // unnamed namespace
294 MeshVisualPtr MeshVisual::New( VisualFactoryCache& factoryCache )
296 return new MeshVisual( factoryCache );
299 MeshVisual::MeshVisual( VisualFactoryCache& factoryCache )
300 : Visual::Base( factoryCache ),
301 mShadingMode( Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING ),
303 mUseMipmapping( true ),
304 mUseSoftNormals( true )
308 MeshVisual::~MeshVisual()
312 void MeshVisual::DoSetProperties( const Property::Map& propertyMap )
314 Property::Value* objectUrl = propertyMap.Find( Toolkit::MeshVisual::Property::OBJECT_URL, OBJECT_URL_NAME );
315 if( !objectUrl || !objectUrl->Get( mObjectUrl ) )
317 DALI_LOG_ERROR( "Fail to provide object URL to the MeshVisual object.\n" );
320 Property::Value* materialUrl = propertyMap.Find( Toolkit::MeshVisual::Property::MATERIAL_URL, MATERIAL_URL_NAME );
321 if( !materialUrl || !materialUrl->Get( mMaterialUrl ) || mMaterialUrl.empty() )
326 Property::Value* imagesUrl = propertyMap.Find( Toolkit::MeshVisual::Property::TEXTURES_PATH, TEXTURES_PATH_NAME );
327 if( !imagesUrl || !imagesUrl->Get( mTexturesPath ) )
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();
334 Property::Value* shadingMode = propertyMap.Find( Toolkit::MeshVisual::Property::SHADING_MODE, SHADING_MODE_NAME );
337 Scripting::GetEnumerationProperty( *shadingMode, SHADING_MODE_TABLE, SHADING_MODE_TABLE_COUNT, mShadingMode );
340 Property::Value* useMipmapping = propertyMap.Find( Toolkit::MeshVisual::Property::USE_MIPMAPPING, USE_MIPMAPPING_NAME );
343 useMipmapping->Get( mUseMipmapping );
346 Property::Value* useSoftNormals = propertyMap.Find( Toolkit::MeshVisual::Property::USE_SOFT_NORMALS, USE_SOFT_NORMALS_NAME );
349 useSoftNormals->Get( mUseSoftNormals );
352 Property::Value* lightPosition = propertyMap.Find( Toolkit::MeshVisual::Property::LIGHT_POSITION, LIGHT_POSITION_NAME );
355 if( !lightPosition->Get( mLightPosition ) )
357 DALI_LOG_ERROR( "Invalid value passed for light position in MeshRenderer object.\n" );
358 mLightPosition = Vector3::ZERO;
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();
367 mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 );
371 void MeshVisual::SetSize( const Vector2& size )
373 Visual::Base::SetSize( size );
375 // ToDo: renderer responds to the size change
378 void MeshVisual::DoSetOnStage( Actor& actor )
380 InitializeRenderer();
382 actor.AddRenderer( mImpl->mRenderer );
385 void MeshVisual::DoCreatePropertyMap( Property::Map& map ) const
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 );
398 void MeshVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
403 Dali::Property::Value MeshVisual::DoGetProperty( Dali::Property::Index index )
406 return Dali::Property::Value();
409 void MeshVisual::InitializeRenderer()
411 //Try to load the geometry from the file.
412 if( !LoadGeometry() )
414 SupplyEmptyGeometry();
418 //If a texture is used by the obj file, load the supplied material file.
419 if( mObjLoader.IsTexturePresent() && !mMaterialUrl.empty() )
421 if( !LoadMaterial() )
423 SupplyEmptyGeometry();
428 //Now that the required parts are loaded, create the geometry for the object.
429 if( !CreateGeometry() )
431 SupplyEmptyGeometry();
437 //Load the various texture files supplied by the material file.
438 if( !LoadTextures() )
440 SupplyEmptyGeometry();
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 );
450 void MeshVisual::SupplyEmptyGeometry()
452 mGeometry = Geometry::New();
453 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
454 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
456 DALI_LOG_ERROR( "Initialisation error in mesh visual.\n" );
459 void MeshVisual::UpdateShaderUniforms()
461 Stage stage = Stage::GetCurrent();
462 float width = stage.GetSize().width;
463 float height = stage.GetSize().height;
466 scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
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 );
473 void MeshVisual::CreateShader()
475 if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING )
477 mShader = Shader::New( NORMAL_MAP_VERTEX_SHADER, NORMAL_MAP_FRAGMENT_SHADER );
479 else if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING )
481 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
485 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
488 UpdateShaderUniforms();
491 bool MeshVisual::CreateGeometry()
493 //Determine if we need to use a simpler shader to handle the provided data
494 if( !mUseTexture || !mObjLoader.IsDiffuseMapPresent() )
496 mShadingMode = Toolkit::MeshVisual::ShadingMode::TEXTURELESS_WITH_DIFFUSE_LIGHTING;
498 else if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING && (!mObjLoader.IsNormalMapPresent() || !mObjLoader.IsSpecularMapPresent()) )
500 mShadingMode = Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING;
503 int objectProperties = 0;
505 if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING ||
506 mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING )
508 objectProperties |= ObjLoader::TEXTURE_COORDINATES;
511 if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING )
513 objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
516 //Create geometry with attributes required by shader.
517 mGeometry = mObjLoader.CreateGeometry( objectProperties, mUseSoftNormals );
524 DALI_LOG_ERROR( "Failed to load geometry in mesh visual.\n" );
528 bool MeshVisual::LoadGeometry()
530 std::streampos fileSize;
531 Dali::Vector<char> fileContent;
533 if( FileLoader::ReadFile( mObjectUrl, fileSize, fileContent, FileLoader::TEXT ) )
535 mObjLoader.ClearArrays();
536 mObjLoader.LoadObject( fileContent.Begin(), fileSize );
538 //Get size information from the obj loaded
539 mSceneCenter = mObjLoader.GetCenter();
540 mSceneSize = mObjLoader.GetSize();
545 DALI_LOG_ERROR( "Failed to find object to load in mesh visual.\n" );
549 bool MeshVisual::LoadMaterial()
551 std::streampos fileSize;
552 Dali::Vector<char> fileContent;
554 if( FileLoader::ReadFile( mMaterialUrl, fileSize, fileContent, FileLoader::TEXT ) )
556 //Load data into obj (usable) form
557 mObjLoader.LoadMaterial( fileContent.Begin(), fileSize, mDiffuseTextureUrl, mNormalTextureUrl, mGlossTextureUrl );
561 DALI_LOG_ERROR( "Failed to find texture set to load in mesh visual.\n" );
566 bool MeshVisual::LoadTextures()
568 mTextureSet = TextureSet::New();
570 if( mShadingMode != Toolkit::MeshVisual::ShadingMode::TEXTURELESS_WITH_DIFFUSE_LIGHTING )
572 Sampler sampler = Sampler::New();
575 sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR );
578 if( !mDiffuseTextureUrl.empty() )
580 std::string imageUrl = mTexturesPath + mDiffuseTextureUrl;
583 Texture diffuseTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
586 mTextureSet.SetTexture( DIFFUSE_INDEX, diffuseTexture );
587 mTextureSet.SetSampler( DIFFUSE_INDEX, sampler );
591 DALI_LOG_ERROR( "Failed to load diffuse map texture in mesh visual.\n");
596 if( !mNormalTextureUrl.empty() && ( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING ) )
598 std::string imageUrl = mTexturesPath + mNormalTextureUrl;
601 Texture normalTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
604 mTextureSet.SetTexture( NORMAL_INDEX, normalTexture );
605 mTextureSet.SetSampler( NORMAL_INDEX, sampler );
609 DALI_LOG_ERROR( "Failed to load normal map texture in mesh visual.\n");
614 if( !mGlossTextureUrl.empty() && ( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING ) )
616 std::string imageUrl = mTexturesPath + mGlossTextureUrl;
619 Texture glossTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
622 mTextureSet.SetTexture( GLOSS_INDEX, glossTexture );
623 mTextureSet.SetSampler( GLOSS_INDEX, sampler );
627 DALI_LOG_ERROR( "Failed to load gloss map texture in mesh visual.\n");
635 } // namespace Internal
637 } // namespace Toolkit