X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fmesh%2Fmesh-visual.cpp;h=f34f66c8319021b486dd589a3acf7061763148c2;hp=5f8d4e0012949112497051a0381ec18671e63622;hb=0e96d3c3debf43ce2c02883bc269f087f8286528;hpb=02318dd256a2edbef78a37cd40e153279385cc94 diff --git a/dali-toolkit/internal/visuals/mesh/mesh-visual.cpp b/dali-toolkit/internal/visuals/mesh/mesh-visual.cpp index 5f8d4e0..f34f66c 100644 --- a/dali-toolkit/internal/visuals/mesh/mesh-visual.cpp +++ b/dali-toolkit/internal/visuals/mesh/mesh-visual.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,15 +20,17 @@ // EXTERNAL INCLUDES #include -#include -#include +#include +#include #include #include #include -#include //INTERNAL INCLUDES +#include #include +#include +#include namespace Dali { @@ -44,12 +46,12 @@ namespace Texture LoadTexture( const char* imageUrl, bool generateMipmaps ) { Texture texture; - Dali::BitmapLoader loader = Dali::BitmapLoader::New( imageUrl ); - loader.Load(); - PixelData pixelData = loader.GetPixelData(); - if( pixelData ) + + Devel::PixelBuffer pixelBuffer = LoadImageFromFile( imageUrl ); + if( pixelBuffer ) { - texture = Texture::New( TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() ); + texture = Texture::New( TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight() ); + PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer ); texture.Upload( pixelData ); if( generateMipmaps ) @@ -81,14 +83,6 @@ enum TextureIndex GLOSS_INDEX = 2u }; -//Property names -const char * const OBJECT_URL_NAME( "objectUrl" ); -const char * const MATERIAL_URL_NAME( "materialUrl" ); -const char * const TEXTURES_PATH_NAME( "texturesPath" ); -const char * const SHADING_MODE_NAME( "shadingMode" ); -const char * const USE_MIPMAPPING_NAME( "useMipmapping" ); -const char * const USE_SOFT_NORMALS_NAME( "useSoftNormals" ); -const char * const LIGHT_POSITION_NAME( "lightPosition" ); //Shading mode DALI_ENUM_TO_STRING_TABLE_BEGIN( SHADING_MODE ) @@ -101,197 +95,17 @@ DALI_ENUM_TO_STRING_TABLE_END( SHADING_MODE ) const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" ); const char * const STAGE_OFFSET_UNIFORM_NAME( "uStageOffset" ); -//Shaders -//If a shader requires certain textures, they must be listed in order, -//as detailed in the TextureIndex enum documentation. - -//A basic shader that doesn't use textures at all. -const char* SIMPLE_VERTEX_SHADER = DALI_COMPOSE_SHADER( - attribute highp vec3 aPosition;\n - attribute highp vec3 aNormal;\n - varying mediump vec3 vIllumination;\n - uniform mediump vec3 uSize;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump mat4 uModelView;\n - uniform mediump mat4 uViewMatrix;\n - uniform mediump mat3 uNormalMatrix; - uniform mediump mat4 uObjectMatrix;\n - uniform mediump vec3 lightPosition;\n - uniform mediump vec2 uStageOffset;\n - - void main()\n - {\n - vec4 normalisedVertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n - vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n - vertexPosition = uMvpMatrix * vertexPosition;\n - - //Illumination in Model-View space - Transform attributes and uniforms\n - vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n - vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n - - vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n - mvLightPosition = uViewMatrix * mvLightPosition;\n - vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n - - float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n - vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n - - gl_Position = vertexPosition;\n - }\n -); - -//Fragment shader corresponding to the texture-less shader. -const char* SIMPLE_FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - precision mediump float;\n - varying mediump vec3 vIllumination;\n - uniform lowp vec4 uColor;\n - - void main()\n - {\n - gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a );\n - }\n -); - -//Diffuse and specular illumination shader with albedo texture. Texture is index 0. -const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( - attribute highp vec3 aPosition;\n - attribute highp vec2 aTexCoord;\n - attribute highp vec3 aNormal;\n - varying mediump vec2 vTexCoord;\n - varying mediump vec3 vIllumination;\n - varying mediump float vSpecular;\n - uniform mediump vec3 uSize;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump mat4 uModelView; - uniform mediump mat4 uViewMatrix;\n - uniform mediump mat3 uNormalMatrix; - uniform mediump mat4 uObjectMatrix;\n - uniform mediump vec3 lightPosition;\n - uniform mediump vec2 uStageOffset;\n - - void main() - {\n - vec4 normalisedVertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n - vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n - vertexPosition = uMvpMatrix * vertexPosition;\n - - //Illumination in Model-View space - Transform attributes and uniforms\n - vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n - vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal );\n - - vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n - mvLightPosition = uViewMatrix * mvLightPosition;\n - vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n - - vec3 viewDirection = normalize( -mvVertexPosition.xyz ); - - float lightDiffuse = dot( vectorToLight, normal );\n - lightDiffuse = max( 0.0,lightDiffuse );\n - vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n - - vec3 reflectDirection = reflect( -vectorToLight, normal ); - vSpecular = pow( max( dot( reflectDirection, viewDirection ), 0.0 ), 4.0 ); - - vTexCoord = aTexCoord;\n - gl_Position = vertexPosition;\n - }\n -); - -//Fragment shader corresponding to the diffuse and specular illumination shader with albedo texture -const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - precision mediump float;\n - varying mediump vec2 vTexCoord;\n - varying mediump vec3 vIllumination;\n - varying mediump float vSpecular;\n - uniform sampler2D sDiffuse;\n - uniform lowp vec4 uColor;\n - - void main()\n - {\n - vec4 texture = texture2D( sDiffuse, vTexCoord );\n - gl_FragColor = vec4( vIllumination.rgb * texture.rgb * uColor.rgb + vSpecular * 0.3, texture.a * uColor.a );\n - }\n -); - -//Diffuse and specular illumination shader with albedo texture, normal map and gloss map shader. -//Diffuse (albedo) texture is index 0, normal is 1, gloss is 2. They must be declared in this order. -const char* NORMAL_MAP_VERTEX_SHADER = DALI_COMPOSE_SHADER( - attribute highp vec3 aPosition;\n - attribute highp vec2 aTexCoord;\n - attribute highp vec3 aNormal;\n - attribute highp vec3 aTangent;\n - attribute highp vec3 aBiNormal;\n - varying mediump vec2 vTexCoord;\n - varying mediump vec3 vLightDirection;\n - varying mediump vec3 vHalfVector;\n - uniform mediump vec3 uSize;\n - uniform mediump mat4 uMvpMatrix;\n - uniform mediump mat4 uModelView; - uniform mediump mat4 uViewMatrix;\n - uniform mediump mat3 uNormalMatrix; - uniform mediump mat4 uObjectMatrix;\n - uniform mediump vec3 lightPosition;\n - uniform mediump vec2 uStageOffset;\n - void main() - {\n - vec4 normalisedVertexPosition = vec4( aPosition * min( uSize.x, uSize.y ), 1.0 );\n - vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n - vertexPosition = uMvpMatrix * vertexPosition;\n - - vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n - - vec3 tangent = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aTangent ); - vec3 binormal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aBiNormal ); - vec3 normal = normalize( uNormalMatrix * mat3( uObjectMatrix ) * aNormal ); - - vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n - mvLightPosition = uViewMatrix * mvLightPosition;\n - vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n - vLightDirection.x = dot( vectorToLight, tangent ); - vLightDirection.y = dot( vectorToLight, binormal ); - vLightDirection.z = dot( vectorToLight, normal ); - - vec3 viewDirection = normalize( -mvVertexPosition.xyz ); - vec3 halfVector = normalize( viewDirection + vectorToLight ); - vHalfVector.x = dot( halfVector, tangent ); - vHalfVector.y = dot( halfVector, binormal ); - vHalfVector.z = dot( halfVector, normal ); - - vTexCoord = aTexCoord;\n - gl_Position = vertexPosition;\n - }\n -); - -//Fragment shader corresponding to the shader that uses all textures (diffuse, normal and gloss maps) -const char* NORMAL_MAP_FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - precision mediump float;\n - varying mediump vec2 vTexCoord;\n - varying mediump vec3 vLightDirection;\n - varying mediump vec3 vHalfVector;\n - uniform sampler2D sDiffuse;\n - uniform sampler2D sNormal;\n - uniform sampler2D sGloss;\n - uniform lowp vec4 uColor;\n - - void main()\n - {\n - vec4 texture = texture2D( sDiffuse, vTexCoord );\n - vec3 normal = normalize( texture2D( sNormal, vTexCoord ).xyz * 2.0 - 1.0 );\n - vec4 glossMap = texture2D( sGloss, vTexCoord );\n - - float lightDiffuse = max( 0.0, dot( normal, normalize( vLightDirection ) ) );\n - lightDiffuse = lightDiffuse * 0.5 + 0.5;\n - - float shininess = pow ( max ( dot ( normalize( vHalfVector ), normal ), 0.0 ), 16.0 ) ; - - gl_FragColor = vec4( texture.rgb * uColor.rgb * lightDiffuse + shininess * glossMap.rgb, texture.a * uColor.a );\n - }\n -); - -} // namespace +} // unnamed namespace + +MeshVisualPtr MeshVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties ) +{ + MeshVisualPtr meshVisualPtr( new MeshVisual( factoryCache ) ); + meshVisualPtr->SetProperties( properties ); + return meshVisualPtr; +} MeshVisual::MeshVisual( VisualFactoryCache& factoryCache ) -: Visual::Base( factoryCache ), +: Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::MESH ), mShadingMode( Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING ), mUseTexture( true ), mUseMipmapping( true ), @@ -303,77 +117,140 @@ MeshVisual::~MeshVisual() { } -void MeshVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap ) +void MeshVisual::DoSetProperties( const Property::Map& propertyMap ) { - Property::Value* objectUrl = propertyMap.Find( Toolkit::MeshVisual::Property::OBJECT_URL, OBJECT_URL_NAME ); - if( !objectUrl || !objectUrl->Get( mObjectUrl ) ) + for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter ) { - DALI_LOG_ERROR( "Fail to provide object URL to the MeshVisual object.\n" ); + KeyValuePair keyValue = propertyMap.GetKeyValue( iter ); + if( keyValue.first.type == Property::Key::INDEX ) + { + DoSetProperty( keyValue.first.indexKey, keyValue.second ); + } + else + { + if( keyValue.first == OBJECT_URL_NAME ) + { + DoSetProperty( Toolkit::MeshVisual::Property::OBJECT_URL, keyValue.second ); + } + else if( keyValue.first == MATERIAL_URL_NAME ) + { + DoSetProperty( Toolkit::MeshVisual::Property::MATERIAL_URL, keyValue.second ); + } + else if( keyValue.first == TEXTURES_PATH_NAME ) + { + DoSetProperty( Toolkit::MeshVisual::Property::TEXTURES_PATH, keyValue.second ); + } + else if( keyValue.first == SHADING_MODE_NAME ) + { + DoSetProperty( Toolkit::MeshVisual::Property::SHADING_MODE, keyValue.second ); + } + else if( keyValue.first == USE_MIPMAPPING_NAME ) + { + DoSetProperty( Toolkit::MeshVisual::Property::USE_MIPMAPPING, keyValue.second ); + } + else if( keyValue.first == USE_SOFT_NORMALS_NAME ) + { + DoSetProperty( Toolkit::MeshVisual::Property::USE_SOFT_NORMALS, keyValue.second ); + } + else if( keyValue.first == LIGHT_POSITION_NAME ) + { + DoSetProperty( Toolkit::MeshVisual::Property::LIGHT_POSITION, keyValue.second ); + } + } } - Property::Value* materialUrl = propertyMap.Find( Toolkit::MeshVisual::Property::MATERIAL_URL, MATERIAL_URL_NAME ); - if( !materialUrl || !materialUrl->Get( mMaterialUrl ) || mMaterialUrl.empty() ) + if( mMaterialUrl.empty() ) { mUseTexture = false; } - Property::Value* imagesUrl = propertyMap.Find( Toolkit::MeshVisual::Property::TEXTURES_PATH, TEXTURES_PATH_NAME ); - if( !imagesUrl || !imagesUrl->Get( mTexturesPath ) ) - { - //Default behaviour is to assume files are in the same directory, - // or have their locations detailed in full when supplied. - mTexturesPath.clear(); - } - - Property::Value* shadingMode = propertyMap.Find( Toolkit::MeshVisual::Property::SHADING_MODE, SHADING_MODE_NAME ); - if( shadingMode ) - { - Scripting::GetEnumerationProperty( *shadingMode, SHADING_MODE_TABLE, SHADING_MODE_TABLE_COUNT, mShadingMode ); - } - - Property::Value* useMipmapping = propertyMap.Find( Toolkit::MeshVisual::Property::USE_MIPMAPPING, USE_MIPMAPPING_NAME ); - if( useMipmapping ) + if( mLightPosition == Vector3::ZERO ) { - useMipmapping->Get( mUseMipmapping ); - } + // Default behaviour is to place the light directly in front of the object, + // at a reasonable distance to light everything on screen. + Stage stage = Stage::GetCurrent(); - Property::Value* useSoftNormals = propertyMap.Find( Toolkit::MeshVisual::Property::USE_SOFT_NORMALS, USE_SOFT_NORMALS_NAME ); - if( useSoftNormals ) - { - useSoftNormals->Get( mUseSoftNormals ); + mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 ); } +} - Property::Value* lightPosition = propertyMap.Find( Toolkit::MeshVisual::Property::LIGHT_POSITION, LIGHT_POSITION_NAME ); - if( lightPosition ) +void MeshVisual::DoSetProperty( Property::Index index, const Property::Value& value ) +{ + switch( index ) { - if( !lightPosition->Get( mLightPosition ) ) + case Toolkit::MeshVisual::Property::OBJECT_URL: { - DALI_LOG_ERROR( "Invalid value passed for light position in MeshRenderer object.\n" ); - mLightPosition = Vector3::ZERO; + if( !value.Get( mObjectUrl ) ) + { + DALI_LOG_ERROR("MeshVisual: property objectUrl is the wrong type, use STRING\n"); + } + break; + } + case Toolkit::MeshVisual::Property::MATERIAL_URL: + { + if( ! value.Get( mMaterialUrl ) ) + { + DALI_LOG_ERROR("MeshVisual: property materialUrl is the wrong type, use STRING\n"); + } + break; + } + case Toolkit::MeshVisual::Property::TEXTURES_PATH: + { + if( ! value.Get( mTexturesPath ) ) + { + mTexturesPath.clear(); + } + break; + } + case Toolkit::MeshVisual::Property::SHADING_MODE: + { + Scripting::GetEnumerationProperty( value, SHADING_MODE_TABLE, SHADING_MODE_TABLE_COUNT, mShadingMode ); + break; + } + case Toolkit::MeshVisual::Property::USE_MIPMAPPING: + { + if( !value.Get( mUseMipmapping ) ) + { + DALI_LOG_ERROR("MeshVisual: property useMipmapping is the wrong type, use BOOLEAN\n"); + } + break; + } + case Toolkit::MeshVisual::Property::USE_SOFT_NORMALS: + { + if( !value.Get( mUseSoftNormals ) ) + { + DALI_LOG_ERROR("MeshVisual: property useSoftNormals is the wrong type, use BOOLEAN\n"); + } + break; + } + case Toolkit::MeshVisual::Property::LIGHT_POSITION: + { + if( !value.Get( mLightPosition ) ) + { + mLightPosition = Vector3::ZERO; + DALI_LOG_ERROR("MeshVisual: property lightPosition is the wrong type, use VECTOR3\n"); + } + break; } - } - else - { - //Default behaviour is to place the light directly in front of the object, - // at a reasonable distance to light everything on screen. - Stage stage = Stage::GetCurrent(); - - mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 ); } } -void MeshVisual::SetSize( const Vector2& size ) +void MeshVisual::OnSetTransform() { - Visual::Base::SetSize( size ); - - // ToDo: renderer responds to the size change + if( mImpl->mRenderer ) + { + mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT ); + } } -void MeshVisual::DoSetOnStage( Actor& actor ) +void MeshVisual::DoSetOnScene( Actor& actor ) { InitializeRenderer(); actor.AddRenderer( mImpl->mRenderer ); + + // Mesh loaded and ready to display + ResourceReady( Toolkit::Visual::ResourceStatus::READY ); } void MeshVisual::DoCreatePropertyMap( Property::Map& map ) const @@ -389,15 +266,9 @@ void MeshVisual::DoCreatePropertyMap( Property::Map& map ) const map.Insert( Toolkit::MeshVisual::Property::LIGHT_POSITION, mLightPosition ); } -void MeshVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue ) +void MeshVisual::DoCreateInstancePropertyMap( Property::Map& map ) const { - // TODO -} - -Dali::Property::Value MeshVisual::DoGetProperty( Dali::Property::Index index ) -{ - // TODO - return Dali::Property::Value(); + // Do nothing } void MeshVisual::InitializeRenderer() @@ -439,12 +310,15 @@ void MeshVisual::InitializeRenderer() mImpl->mRenderer.SetTextures( mTextureSet ); mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON ); mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON ); + + //Register transform properties + mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT ); } void MeshVisual::SupplyEmptyGeometry() { mGeometry = Geometry::New(); - mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER ); + mShader = Shader::New( SHADER_MESH_VISUAL_SIMPLE_SHADER_VERT, SHADER_MESH_VISUAL_SIMPLE_SHADER_FRAG ); mImpl->mRenderer = Renderer::New( mGeometry, mShader ); DALI_LOG_ERROR( "Initialisation error in mesh visual.\n" ); @@ -468,15 +342,15 @@ void MeshVisual::CreateShader() { if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING ) { - mShader = Shader::New( NORMAL_MAP_VERTEX_SHADER, NORMAL_MAP_FRAGMENT_SHADER ); + mShader = Shader::New( SHADER_MESH_VISUAL_NORMAL_MAP_SHADER_VERT, SHADER_MESH_VISUAL_NORMAL_MAP_SHADER_FRAG ); } else if( mShadingMode == Toolkit::MeshVisual::ShadingMode::TEXTURED_WITH_SPECULAR_LIGHTING ) { - mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ); + mShader = Shader::New( SHADER_MESH_VISUAL_SHADER_VERT, SHADER_MESH_VISUAL_SHADER_FRAG ); } else //Textureless { - mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER ); + mShader = Shader::New( SHADER_MESH_VISUAL_SIMPLE_SHADER_VERT, SHADER_MESH_VISUAL_SIMPLE_SHADER_FRAG ); } UpdateShaderUniforms();