X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=examples%2Fmetaball-refrac%2Fmetaball-refrac-example.cpp;h=e66ecdea2a4b1d166b66d8353f23e7d02fbdcf41;hb=708a5e016f973900a20a885914d3b70741fa80d9;hp=94fd5b6bb1e3890d44944deaa571821fdfd006c1;hpb=17730e15a6ab2b7fe4292c0455c3772e87b2fffc;p=platform%2Fcore%2Fuifw%2Fdali-demo.git diff --git a/examples/metaball-refrac/metaball-refrac-example.cpp b/examples/metaball-refrac/metaball-refrac-example.cpp index 94fd5b6..e66ecde 100644 --- a/examples/metaball-refrac/metaball-refrac-example.cpp +++ b/examples/metaball-refrac/metaball-refrac-example.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 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. @@ -15,31 +15,36 @@ * */ -#include -#include -#include - +// EXTERNAL INCLUDES #include #include +#include // uint32_t, uint16_t etc + +#include +#include +#include +#include + +// INTERNAL INCLUDES +#include "shared/utility.h" // DemoHelper::LoadTexture using namespace Dali; -using namespace Dali::Toolkit; -namespace +namespace // unnamed namespace for constants { -const char * const BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-2.jpg" ); -const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" ); - +const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-2.jpg" ); const float GRAVITY_X(0); const float GRAVITY_Y(-0.09); -} -#define METABALL_NUMBER 4 +// number of metaballs +constexpr uint32_t METABALL_NUMBER = 6; -const char*const METABALL_VERTEX_SHADER = DALI_COMPOSE_SHADER ( +/** + * Vertex shader for metaballs + */ +const char* const METABALL_VERTEX_SHADER = DALI_COMPOSE_SHADER ( attribute mediump vec2 aPosition;\n attribute mediump vec2 aTexture;\n - attribute mediump vec3 aNormal;\n uniform mediump mat4 uMvpMatrix;\n uniform mediump vec3 uSize;\n uniform lowp vec4 uColor;\n @@ -54,8 +59,10 @@ const char*const METABALL_VERTEX_SHADER = DALI_COMPOSE_SHADER ( }\n ); - -const char*const METABALL_FRAG_SHADER = DALI_COMPOSE_SHADER ( +/** + * Fragment shader for metaballs + */ +const char* const METABALL_FRAG_SHADER = DALI_COMPOSE_SHADER ( precision mediump float;\n varying vec2 vTexCoord;\n uniform vec2 uPositionMetaball;\n @@ -95,7 +102,10 @@ const char*const METABALL_FRAG_SHADER = DALI_COMPOSE_SHADER ( }\n ); -const char*const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER ( +/** + * Fragment shader code for metaball and background composition with refraction effect + */ +const char* const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER ( precision mediump float;\n varying vec2 vTexCoord;\n uniform sampler2D sTexture;\n @@ -109,7 +119,7 @@ const char*const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER ( {\n zoomCoords = ((vTexCoord - 0.5) * 0.95) + 0.5;\n }\n - else if (metaColor.r > 0.81)\n + else if (metaColor.r > 0.78)\n {\n float interpolation = mix(0.95, 1.05, (0.85 - metaColor.r) * 50.0);\n zoomCoords = ((vTexCoord - 0.5) * interpolation) + 0.5;\n @@ -124,7 +134,10 @@ const char*const REFRACTION_FRAG_SHADER = DALI_COMPOSE_SHADER ( }\n ); -const char*const FRAG_SHADER = DALI_COMPOSE_SHADER ( +/** + * Fragment shader code when there's no effect + */ +const char* const FRAG_SHADER = DALI_COMPOSE_SHADER ( precision mediump float;\n varying vec2 vTexCoord;\n uniform sampler2D sTexture;\n @@ -134,10 +147,11 @@ const char*const FRAG_SHADER = DALI_COMPOSE_SHADER ( }\n ); - +/** + * Metadata for each ball + */ struct MetaballInfo { - //ShaderEffect shader; Actor actor; Vector2 position; float radius; @@ -152,32 +166,50 @@ struct MetaballInfo Property::Index aspectIndex; }; +} // unnamed namespace -/***************************************************************************/ -/* Demo using Metaballs for Refraction when clicking the screen ************/ -/* The concept is similar to the Note 5 ScreenLock ************/ -/***************************************************************************/ +/** + * Demo using Metaballs + * + * When the metaball is clicked it starts to grow and fuses into the closest edge of screen + */ class MetaballRefracController : public ConnectionTracker { public: + + /** + * Constructor + * @param application + */ MetaballRefracController( Application& application ); - ~MetaballRefracController(); + /** + * Destructor + */ + virtual ~MetaballRefracController(); + + /** + * Creates the metaballs and initializes the scene + */ void Create( Application& app ); + + /** + * Touch handler, start the grow animation and creates additional metaballs + */ bool OnTouch( Actor actor, const TouchEvent& touch ); - void OnKeyEvent(const KeyEvent& event); - void SetGravity(const Vector2 & gravity); + /** + * Key event callback to quit the application on escape or back key + */ + void OnKeyEvent( const KeyEvent& event ); +private: // Data -private: Application& mApplication; Vector2 mScreenSize; - Layer mContentLayer; - - Image mBackImage; - FrameBufferImage mMetaballFBO; + Texture mBackgroundTexture; + FrameBuffer mMetaballFBO; Actor mMetaballRoot; MetaballInfo mMetaballs[METABALL_NUMBER]; @@ -185,7 +217,6 @@ private: Actor mCompositionActor; //Motion - bool mExitClick; Vector2 mCurrentTouchPosition; Vector2 mMetaballPosVariation; Vector2 mMetaballPosVariationFrom; @@ -196,10 +227,12 @@ private: Vector2 mGravityVar; Renderer mRendererRefraction; - Material mMaterialRefraction; - Material mMaterialNormal; + TextureSet mTextureSetRefraction; + Shader mShaderRefraction; + TextureSet mTextureSetNormal; + Shader mShaderNormal; - //Animations + // Animations Animation mGravityAnimation[METABALL_NUMBER]; Animation mRadiusDecAnimation[METABALL_NUMBER]; Animation mRadiusIncFastAnimation[METABALL_NUMBER]; @@ -207,32 +240,69 @@ private: Animation mRadiusVarAnimation[METABALL_NUMBER]; Animation mPositionVarAnimation[METABALL_NUMBER]; - //Private functions - Geometry CreateGeometry(); - Geometry CreateGeometryComposition(); - - void CreateMetaballActors(); - void CreateMetaballImage(); - void AddRefractionImage(); - void CreateAnimations(); - - void LaunchRadiusIncSlowAnimations(Animation &source); - void LaunchGetBackToPositionAnimation(Animation &source); - - void StopClickAnimations(); - void StopAfterClickAnimations(); - - void ResetMetaballsState(); + // Private Helper functions + + /** + * Create a mesh data with the geometry for the metaball rendering + * @param aspectMappedTexture whether texture coords should be mapped based on aspect ratio + */ + Geometry CreateGeometry( bool aspectMappedTexture = true ); + + /** + * Create a actor for the metaballs + */ + void CreateMetaballActors(); + + /** + * Create the render task and FBO to render the metaballs into a texture + */ + void CreateMetaballImage(); + + /** + * Create the the final composition + */ + void CreateComposition(); + + /** + * Create all the metaballs animations (gravity, movement, size, etc.) + */ + void CreateAnimations(); + + /** + * Function to launch the grow slow radius for the metaballs, and also the small variations for metaball[2] and [3] + */ + void LaunchRadiusIncSlowAnimations( Animation& source ); + + /** + * Function to launch the animation to get the metaball[1] back to the center + */ + void LaunchGetBackToPositionAnimation( Animation& source ); + + /** + * Function to stop all animations related to the click of the user in the screen + */ + void StopClickAnimations(); + + /** + * Function to stop all animations related to the after click of the user in the screen + */ + void StopAfterClickAnimations(); + + /** + * Function that resets the sate of the different Metaballs + */ + void ResetMetaballsState(); + + /** + * Function to set the actual position of the metaballs when the user clicks the screen + */ + void SetPositionToMetaballs( const Vector2& metaballCenter ); - void SetPositionToMetaballs(Vector2 & metaballCenter); }; - -//----------------------------------------------------------------------------------------------- -// -// IMPLEMENTATION -// -//---------------- +/** + * Implementation + */ MetaballRefracController::MetaballRefracController( Application& application ) : mApplication( application ) @@ -246,347 +316,204 @@ MetaballRefracController::~MetaballRefracController() // Nothing to do here; } -/* - * Setter function for gravity - */ -void MetaballRefracController::SetGravity(const Vector2 & gravity) -{ - mGravity = gravity; -} - -/** - * Main create function, it creates the metaballs and all the - */ void MetaballRefracController::Create( Application& app ) { - Stage stage = Stage::GetCurrent(); + Window window = app.GetWindow(); - stage.KeyEventSignal().Connect(this, &MetaballRefracController::OnKeyEvent); + window.KeyEventSignal().Connect( this, &MetaballRefracController::OnKeyEvent ); - mScreenSize = stage.GetSize(); + mScreenSize = window.GetSize(); - stage.SetBackgroundColor(Color::BLACK); + window.SetBackgroundColor(Color::BLACK); - //Set background image for the view - mBackImage = ResourceImage::New( BACKGROUND_IMAGE ); + // Load background texture + mBackgroundTexture = DemoHelper::LoadTexture( BACKGROUND_IMAGE ); mGravity = Vector2(GRAVITY_X,GRAVITY_Y); mGravityVar = Vector2(0,0); - //Create internal data CreateMetaballActors(); CreateMetaballImage(); - AddRefractionImage(); - + CreateComposition(); CreateAnimations(); // Connect the callback to the touch signal on the mesh actor - stage.GetRootLayer().TouchedSignal().Connect( this, &MetaballRefracController::OnTouch ); + window.GetRootLayer().TouchSignal().Connect( this, &MetaballRefracController::OnTouch ); } -/** - * Create a mesh data with the geometry for the metaball rendering - */ -Geometry MetaballRefracController::CreateGeometry() +Geometry MetaballRefracController::CreateGeometry( bool aspectMappedTexture ) { - float aspect = (float)mScreenSize.y / (float)mScreenSize.x; + const float aspect = mScreenSize.y / mScreenSize.x; // Create vertices and specify their color - float xsize = mScreenSize.x * 0.5; + const float xsize = mScreenSize.x * 0.5; - //We create the meshdata for the metaballs + // Create the meshdata for the metaballs struct VertexPosition { Vector2 position; }; struct VertexTexture { Vector2 texture; }; - struct VertexNormal { Vector3 normal; }; - - VertexPosition vertices[] = { - { Vector2( -xsize, -xsize * aspect) }, - { Vector2( xsize, -xsize * aspect) }, - { Vector2( -xsize, xsize * aspect) }, - { Vector2( xsize, xsize * aspect) } - }; - - VertexTexture textures[] = { - { Vector2(0.0f, 0.0f) }, - { Vector2(1.0f, 0.0f) }, - { Vector2(0.0f, 1.0f * aspect) }, - { Vector2(1.0f, 1.0f * aspect) } - }; - - VertexNormal normals [] = { - { Vector3(0.0f, 0.0f, 1.0f) }, - { Vector3(0.0f, 0.0f, 1.0f) }, - { Vector3(0.0f, 0.0f, 1.0f) }, - { Vector3(0.0f, 0.0f, 1.0f) } - }; - - int indices[] = { 0, 3, 1, 0, 2, 3 }; - - unsigned int numberOfVertices = sizeof(vertices)/sizeof(VertexPosition); - - //Vertices - Property::Map positionVertexFormat; - positionVertexFormat["aPosition"] = Property::VECTOR2; - PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat, numberOfVertices ); - positionVertices.SetData(vertices); - - //Textures - Property::Map textureVertexFormat; - textureVertexFormat["aTexture"] = Property::VECTOR2; - PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat, numberOfVertices ); - textureVertices.SetData(textures); - - //Normals - Property::Map normalVertexFormat; - normalVertexFormat["aNormal"] = Property::VECTOR3; - PropertyBuffer normalVertices = PropertyBuffer::New( normalVertexFormat, numberOfVertices ); - normalVertices.SetData(normals); - //Indices - Property::Map indicesVertexFormat; - indicesVertexFormat["aIndices"] = Property::INTEGER; - PropertyBuffer indicesToVertices = PropertyBuffer::New( indicesVertexFormat, 6 ); - indicesToVertices.SetData(indices); - - - // Create the geometry object - Geometry texturedQuadGeometry = Geometry::New(); - texturedQuadGeometry.AddVertexBuffer( positionVertices ); - texturedQuadGeometry.AddVertexBuffer( textureVertices ); - texturedQuadGeometry.AddVertexBuffer( normalVertices ); - - texturedQuadGeometry.SetIndexBuffer ( indicesToVertices ); - - return texturedQuadGeometry; -} - -/** - * Create a mesh data with the geometry for the metaball rendering - */ -Geometry MetaballRefracController::CreateGeometryComposition() -{ - float aspect = (float)mScreenSize.y / (float)mScreenSize.x; - - // Create vertices and specify their color - float xsize = mScreenSize.x * 0.5; - - //We create the meshdata for the metaballs - struct VertexPosition { Vector2 position; }; - struct VertexTexture { Vector2 texture; }; - struct VertexNormal { Vector3 normal; }; - - VertexPosition vertices[] = { - { Vector2( -xsize, -xsize * aspect) }, - { Vector2( xsize, -xsize * aspect) }, - { Vector2( -xsize, xsize * aspect) }, - { Vector2( xsize, xsize * aspect) } - }; - - VertexTexture textures[] = { - { Vector2(0.0f, 0.0f) }, - { Vector2(1.0f, 0.0f) }, - { Vector2(0.0f, 1.0f) }, - { Vector2(1.0f, 1.0f) } + VertexPosition vertices[] = + { + { Vector2( -xsize, -xsize * aspect ) }, + { Vector2( xsize, -xsize * aspect ) }, + { Vector2( -xsize, xsize * aspect ) }, + { Vector2( xsize, xsize * aspect ) } }; - VertexNormal normals [] = { - { Vector3(0.0f, 0.0f, 1.0f) }, - { Vector3(0.0f, 0.0f, 1.0f) }, - { Vector3(0.0f, 0.0f, 1.0f) }, - { Vector3(0.0f, 0.0f, 1.0f) } + const float textureAspect = (aspectMappedTexture) ? aspect : 1.0f; + VertexTexture textures[] = + { + { Vector2( 0.0f, 0.0f ) }, + { Vector2( 1.0f, 0.0f ) }, + { Vector2( 0.0f, 1.0f * textureAspect ) }, + { Vector2( 1.0f, 1.0f * textureAspect ) } }; - int indices[] = { 0, 3, 1, 0, 2, 3 }; - - unsigned int numberOfVertices = sizeof(vertices)/sizeof(VertexPosition); + uint32_t numberOfVertices = sizeof(vertices)/sizeof(VertexPosition); - //Vertices + // Vertices Property::Map positionVertexFormat; positionVertexFormat["aPosition"] = Property::VECTOR2; - PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat, numberOfVertices ); - positionVertices.SetData(vertices); + PropertyBuffer positionVertices = PropertyBuffer::New( positionVertexFormat ); + positionVertices.SetData( vertices, numberOfVertices ); - //Textures + // Textures Property::Map textureVertexFormat; textureVertexFormat["aTexture"] = Property::VECTOR2; - PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat, numberOfVertices ); - textureVertices.SetData(textures); - - //Normals - Property::Map normalVertexFormat; - normalVertexFormat["aNormal"] = Property::VECTOR3; - PropertyBuffer normalVertices = PropertyBuffer::New( normalVertexFormat, numberOfVertices ); - normalVertices.SetData(normals); - - //Indices - Property::Map indicesVertexFormat; - indicesVertexFormat["aIndices"] = Property::INTEGER; - PropertyBuffer indicesToVertices = PropertyBuffer::New( indicesVertexFormat, 6 ); - indicesToVertices.SetData(indices); + PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat ); + textureVertices.SetData( textures, numberOfVertices ); + // Indices + const uint16_t indices[] = { 0, 3, 1, 0, 2, 3 }; // Create the geometry object Geometry texturedQuadGeometry = Geometry::New(); texturedQuadGeometry.AddVertexBuffer( positionVertices ); texturedQuadGeometry.AddVertexBuffer( textureVertices ); - texturedQuadGeometry.AddVertexBuffer( normalVertices ); - texturedQuadGeometry.SetIndexBuffer ( indicesToVertices ); + texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) ); return texturedQuadGeometry; } -/** - * Create a mesh actor for the metaballs - */ void MetaballRefracController::CreateMetaballActors() { - //We create metaball structures - //With MeshData Textured - float aspect = (float)mScreenSize.y / (float)mScreenSize.x; - - //Create the shader for the metaballs - - Shader shader = Shader::New( METABALL_VERTEX_SHADER, METABALL_FRAG_SHADER ); - - Material material = Material::New( shader ); - material.SetBlendMode(BlendingMode::ON ); - material.SetBlendFunc(BlendingFactor::ONE, BlendingFactor::ONE, BlendingFactor::ONE, BlendingFactor::ONE); - - Geometry metaballGeom = CreateGeometry(); - - //Each metaball has a different radius + const float aspect = mScreenSize.y / mScreenSize.x; + + // Create the renderer for the metaballs + Shader shader = Shader::New( METABALL_VERTEX_SHADER, METABALL_FRAG_SHADER, Shader::Hint::MODIFIES_GEOMETRY ); + Geometry metaballGeometry = CreateGeometry(); + Renderer renderer = Renderer::New( metaballGeometry, shader ); + renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON ); + renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE ); + renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ONE ); + renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE ); + renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ONE ); + + // Each metaball has a different radius mMetaballs[0].radius = mMetaballs[0].initRadius = 0.0145f; mMetaballs[1].radius = mMetaballs[1].initRadius = 0.012f; mMetaballs[2].radius = mMetaballs[2].initRadius = 0.0135f; mMetaballs[3].radius = mMetaballs[3].initRadius = 0.0135f; - //Initialization of each of the metaballs - for (int i = 0 ; i < METABALL_NUMBER ; i++) + // Initialization of each of the metaballs + for( uint32_t i = 0 ; i < METABALL_NUMBER ; i++ ) { mMetaballs[i].position = Vector2(0.0f, 0.0f); - mMetaballs[i].actor = Actor::New( ); - mMetaballs[i].actor.SetName("Metaball"); - mMetaballs[i].actor.SetScale( 1.0f ); - mMetaballs[i].actor.SetParentOrigin( ParentOrigin::CENTER ); + mMetaballs[i].actor = Actor::New(); + mMetaballs[i].actor.SetProperty( Dali::Actor::Property::NAME, "Metaball" ); + mMetaballs[i].actor.SetProperty( Actor::Property::SCALE, 1.0f ); + mMetaballs[i].actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); + - Renderer renderer = Renderer::New( metaballGeom, material ); mMetaballs[i].actor.AddRenderer( renderer ); mMetaballs[i].positionIndex = mMetaballs[i].actor.RegisterProperty( "uPositionMetaball", mMetaballs[i].position ); - mMetaballs[i].positionVarIndex = mMetaballs[i].actor.RegisterProperty( "uPositionVar", Vector2(0.f,0.f) ); - mMetaballs[i].gravityIndex = mMetaballs[i].actor.RegisterProperty( "uGravityVector", Vector2(0.f,0.f) ); - mMetaballs[i].radiusIndex = mMetaballs[i].actor.RegisterProperty( "uRadius", mMetaballs[i].radius ); - mMetaballs[i].radiusVarIndex = mMetaballs[i].actor.RegisterProperty( "uRadiusVar", 0.f ); - mMetaballs[i].aspectIndex = mMetaballs[i].actor.RegisterProperty( "uAspect", aspect ); - - mMetaballs[i].actor.SetSize(400, 400); } //Root creation mMetaballRoot = Actor::New(); - mMetaballRoot.SetParentOrigin( ParentOrigin::CENTER ); - for (int i = 0 ; i < METABALL_NUMBER ; i++) + mMetaballRoot.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); + for( uint32_t i = 0 ; i < METABALL_NUMBER ; i++ ) { mMetaballRoot.Add( mMetaballs[i].actor ); } - - //Initialization of variables related to metaballs - mMetaballPosVariation = Vector2(0,0); - mMetaballPosVariationFrom = Vector2(0,0); - mMetaballPosVariationTo = Vector2(0,0); - mCurrentTouchPosition = Vector2(0,0); } -/** - * Create the render task and FBO to render the metaballs into a texture - */ void MetaballRefracController::CreateMetaballImage() { - //We create an FBO and a render task to create to render the metaballs with a fragment shader - Stage stage = Stage::GetCurrent(); - mMetaballFBO = FrameBufferImage::New(mScreenSize.x, mScreenSize.y ); + // Create an FBO and a render task to create to render the metaballs with a fragment shader + Window window = mApplication.GetWindow(); + mMetaballFBO = FrameBuffer::New( mScreenSize.x, mScreenSize.y ); - stage.Add(mMetaballRoot); + window.Add(mMetaballRoot); //Creation of the render task used to render the metaballs - RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList(); + RenderTaskList taskList = window.GetRenderTaskList(); RenderTask task = taskList.CreateTask(); task.SetRefreshRate( RenderTask::REFRESH_ALWAYS ); task.SetSourceActor( mMetaballRoot ); - task.SetExclusive(true); + task.SetExclusive( true ); task.SetClearColor( Color::BLACK ); task.SetClearEnabled( true ); - task.SetTargetFrameBuffer( mMetaballFBO ); + task.SetFrameBuffer( mMetaballFBO ); } -/** - * Create a mesh image to render the final composition - */ -void MetaballRefracController::AddRefractionImage() +void MetaballRefracController::CreateComposition() { - //Creation of the composition image - - //Create geometry - Geometry metaballGeom = CreateGeometryComposition(); - - //Create Refraction shader and renderer - Shader shader = Shader::New( METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER ); - //Create new material - mMaterialRefraction = Material::New( shader ); + // Create Refraction shader and renderer + mShaderRefraction = Shader::New( METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER ); - //Add Textures - mMaterialRefraction.AddTexture(mBackImage, "sTexture"); - mMaterialRefraction.AddTexture(mMetaballFBO, "sEffect"); + // Create new texture set + mTextureSetRefraction = TextureSet::New(); + mTextureSetRefraction.SetTexture( 0u, mBackgroundTexture ); + mTextureSetRefraction.SetTexture( 1u, mMetaballFBO.GetColorTexture() ); - //Create normal shader - Shader shaderNormal = Shader::New( METABALL_VERTEX_SHADER, FRAG_SHADER ); - //Create new material - mMaterialNormal = Material::New( shaderNormal ); + // Create normal shader + mShaderNormal = Shader::New( METABALL_VERTEX_SHADER, FRAG_SHADER ); - //Add samplers - mMaterialNormal.AddTexture(mBackImage, "sTexture"); + // Create new texture set + mTextureSetNormal = TextureSet::New(); + mTextureSetNormal.SetTexture( 0u, mBackgroundTexture ); - - //Create actor + // Create actor mCompositionActor = Actor::New( ); - mCompositionActor.SetParentOrigin(ParentOrigin::CENTER); - mCompositionActor.SetPosition(Vector3(0.0f, 0.0f, 0.0f)); - mCompositionActor.SetSize(mScreenSize.x, mScreenSize.y); - - - mRendererRefraction = Renderer::New( metaballGeom, mMaterialNormal ); + mCompositionActor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER); + mCompositionActor.SetProperty( Actor::Property::POSITION, Vector3(0.0f, 0.0f, 0.0f)); + mCompositionActor.SetProperty( Actor::Property::SIZE, Vector2(mScreenSize.x, mScreenSize.y) ); + + // Create geometry + Geometry metaballGeometry = CreateGeometry( false ); + mRendererRefraction = Renderer::New( metaballGeometry, mShaderNormal ); + mRendererRefraction.SetTextures( mTextureSetNormal ); mCompositionActor.AddRenderer( mRendererRefraction ); - Stage stage = Stage::GetCurrent(); - stage.Add( mCompositionActor ); + Window window = mApplication.GetWindow(); + window.Add( mCompositionActor ); } -/** - * Creation of all the metaballs animations (gravity, movement, size, etc.) - */ void MetaballRefracController::CreateAnimations() { - int i = 0; + uint32_t i = 0; float key; - mPositionVarAnimation[1] = Animation::New(2.f); + mPositionVarAnimation[1] = Animation::New( 2.f ); mPositionVarAnimation[1].SetLooping( false ); mPositionVarAnimation[1].Pause(); mPositionVarAnimation[1].FinishedSignal().Connect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation ); KeyFrames keySinCosVariation = KeyFrames::New(); Vector2 sinCosVariation(0,0); - for ( i = 0 ; i < 360 ; i++) + for( i = 0 ; i < 360; i++ ) { - sinCosVariation.x = 0.05f * (-sin(i * Math::PI/180.f) + cos(i * Math::PI/180.f)); - sinCosVariation.y = 0.05f * (sin(i * Math::PI/180.f) - cos(i * Math::PI/180.f)); + sinCosVariation.x = 0.05f * ( -sinf(i * Math::PI_OVER_180) + cosf(i * Math::PI_OVER_180) ); + sinCosVariation.y = 0.05f * ( sinf(i * Math::PI_OVER_180) - cosf(i * Math::PI_OVER_180) ); key = i/360.f; keySinCosVariation.Add(key, sinCosVariation); } @@ -598,10 +525,10 @@ void MetaballRefracController::CreateAnimations() KeyFrames keyCosSinVariation = KeyFrames::New(); Vector2 cosSinVariation(0,0); - for ( i = 0 ; i < 360 ; i++) + for( i = 0 ; i < 360; i++ ) { - cosSinVariation.x = 0.05f * (-sin(i * Math::PI/180.f) - cos(i * Math::PI/180.f)); - cosSinVariation.y = 0.05f * (sin(i * Math::PI/180.f) + cos(i * Math::PI/180.f)); + cosSinVariation.x = 0.05f * ( -sinf(i * Math::PI_OVER_180) - cosf(i * Math::PI_OVER_180) ); + cosSinVariation.y = 0.05f * ( sinf(i * Math::PI_OVER_180) + cosf(i * Math::PI_OVER_180) ); key = i/360.f; keyCosSinVariation.Add(key, cosSinVariation); } @@ -612,46 +539,46 @@ void MetaballRefracController::CreateAnimations() mPositionVarAnimation[3].Pause(); //Animations for gravity - for ( i = 0 ; i < METABALL_NUMBER ; i++) + for( i = 0 ; i < METABALL_NUMBER; i++ ) { - mGravityAnimation[i] = Animation::New(25.f); - mGravityAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].gravityIndex ), mGravity * 25.f * 3.f); + mGravityAnimation[i] = Animation::New( 25.f ); + mGravityAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].gravityIndex ), mGravity * 25.f * 3.f ); mGravityAnimation[i].SetLooping( false ); mGravityAnimation[i].Pause(); } //Animation to decrease size of metaballs when there is no click - for ( i = 0 ; i < METABALL_NUMBER ; i++) + for( i = 0 ; i < METABALL_NUMBER; i++ ) { - mRadiusDecAnimation[i] = Animation::New(25.f); - mRadiusDecAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), -0.004f * 25.f * 3.f); + mRadiusDecAnimation[i] = Animation::New( 25.f ); + mRadiusDecAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), -0.004f * 25.f * 3.f ); mRadiusDecAnimation[i].SetLooping( false ); mRadiusDecAnimation[i].Pause(); } - //Animation to grow the size of the metaballs the first second of the click - for ( i = 0 ; i < METABALL_NUMBER ; i++) + // Animation to grow the size of the metaballs the first second of the click + for( i = 0 ; i < METABALL_NUMBER; i++ ) { - mRadiusIncFastAnimation[i] = Animation::New(0.3f); - mRadiusIncFastAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), 0.06f); + mRadiusIncFastAnimation[i] = Animation::New( 0.3f ); + mRadiusIncFastAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), 0.06f ); mRadiusIncFastAnimation[i].SetLooping( false ); mRadiusIncFastAnimation[i].Pause(); } mRadiusIncFastAnimation[0].FinishedSignal().Connect( this, &MetaballRefracController::LaunchRadiusIncSlowAnimations ); - //Animation to grow the size of the metaballs afterwards - for ( i = 0 ; i < METABALL_NUMBER ; i++) + // Animation to grow the size of the metaballs afterwards + for( i = 0 ; i < METABALL_NUMBER; i++ ) { - mRadiusIncSlowAnimation[i] = Animation::New(20.f); - mRadiusIncSlowAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), 0.04f); + mRadiusIncSlowAnimation[i] = Animation::New( 20.f ); + mRadiusIncSlowAnimation[i].AnimateBy( Property( mMetaballs[i].actor, mMetaballs[i].radiusIndex ), 0.04f ); mRadiusIncSlowAnimation[i].SetLooping( false ); mRadiusIncSlowAnimation[i].Pause(); } - //keyframes of a sin function + // Keyframes of a sin function KeyFrames keySin = KeyFrames::New(); float val; - for ( i = 0 ; i < 360 ; i++) + for( i = 0 ; i < 360; i++ ) { val = 0.01f * sin(i * Math::PI/180.f); key = i/360.f; @@ -659,13 +586,13 @@ void MetaballRefracController::CreateAnimations() } //Animation to change the size of the metaball - mRadiusVarAnimation[2] = Animation::New(8.f); - mRadiusVarAnimation[2].AnimateBetween(Property( mMetaballs[2].actor, mMetaballs[2].radiusVarIndex ), keySin); + mRadiusVarAnimation[2] = Animation::New( 8.f ); + mRadiusVarAnimation[2].AnimateBetween( Property( mMetaballs[2].actor, mMetaballs[2].radiusVarIndex ), keySin ); mRadiusVarAnimation[2].SetLooping( true ); - //keyframes of a cos function + // Keyframes of a cos function KeyFrames keyCos = KeyFrames::New(); - for ( i = 0 ; i < 360 ; i++) + for( i = 0 ; i < 360; i++ ) { val = 0.01f * cos(i * Math::PI/180.f); key = i/360.f; @@ -673,30 +600,24 @@ void MetaballRefracController::CreateAnimations() } //Animation to change the size of the metaball - mRadiusVarAnimation[3] = Animation::New(8.f); - mRadiusVarAnimation[3].AnimateBetween(Property( mMetaballs[3].actor, mMetaballs[3].radiusVarIndex ), keyCos); + mRadiusVarAnimation[3] = Animation::New( 8.f ); + mRadiusVarAnimation[3].AnimateBetween( Property( mMetaballs[3].actor, mMetaballs[3].radiusVarIndex ), keyCos ); mRadiusVarAnimation[3].SetLooping( true ); } -/** - * Function to launch the animation to get the metaball[1] back to the center - */ -void MetaballRefracController::LaunchGetBackToPositionAnimation(Animation &source) +void MetaballRefracController::LaunchGetBackToPositionAnimation( Animation& source ) { mMetaballPosVariationTo = Vector2(0,0); - mPositionVarAnimation[1] = Animation::New(1.f); + mPositionVarAnimation[1] = Animation::New( 1.f ); mPositionVarAnimation[1].SetLooping( false ); - mPositionVarAnimation[1].AnimateTo(Property( mMetaballs[1].actor, mMetaballs[1].positionVarIndex ), Vector2(0,0)); + mPositionVarAnimation[1].AnimateTo(Property( mMetaballs[1].actor, mMetaballs[1].positionVarIndex ), Vector2(0,0) ); mPositionVarAnimation[1].Play(); } -/** - * Function to launch the gro slow radius for the metaballs, and also the small variations for metaball[2] and [3] - */ -void MetaballRefracController::LaunchRadiusIncSlowAnimations(Animation &source) +void MetaballRefracController::LaunchRadiusIncSlowAnimations( Animation& source ) { - for (int i = 0 ; i < METABALL_NUMBER ; i++) + for( uint32_t i = 0 ; i < METABALL_NUMBER; i++ ) { mRadiusIncSlowAnimation[i].Play(); } @@ -704,12 +625,9 @@ void MetaballRefracController::LaunchRadiusIncSlowAnimations(Animation &source) mPositionVarAnimation[3].Play(); } -/** - * Function to stop all animations related to the click of the user in the screen - */ void MetaballRefracController::StopClickAnimations() { - for (int i = 0 ; i < METABALL_NUMBER ; i++) + for( uint32_t i = 0 ; i < METABALL_NUMBER; i++ ) { mRadiusIncSlowAnimation[i].Stop(); mRadiusIncFastAnimation[i].Stop(); @@ -719,34 +637,29 @@ void MetaballRefracController::StopClickAnimations() mPositionVarAnimation[3].Stop(); } -/** - * Function to stop all animations related to the after click of the user in the screen - */ void MetaballRefracController::StopAfterClickAnimations() { - for (int i = 0 ; i < METABALL_NUMBER ; i++) + for( uint32_t i = 0 ; i < METABALL_NUMBER; i++ ) { mGravityAnimation[i].Stop(); mRadiusDecAnimation[i].Stop(); mMetaballs[i].radius = mMetaballs[i].initRadius; - mMetaballs[i].actor.SetProperty(mMetaballs[i].gravityIndex, Vector2(0,0)); - mMetaballs[i].actor.SetProperty(mMetaballs[i].radiusIndex, mMetaballs[i].radius); - mMetaballs[i].actor.SetProperty(mMetaballs[i].radiusVarIndex, 0.f); + mMetaballs[i].actor.SetProperty( mMetaballs[i].gravityIndex, Vector2(0,0) ); + mMetaballs[i].actor.SetProperty( mMetaballs[i].radiusIndex, mMetaballs[i].radius ); + mMetaballs[i].actor.SetProperty( mMetaballs[i].radiusVarIndex, 0.f ); } mRadiusVarAnimation[2].Stop(); mRadiusVarAnimation[3].Stop(); } -/* - * Function that resets the sate of the different Metaballs - */ void MetaballRefracController::ResetMetaballsState() { - mRendererRefraction.SetMaterial(mMaterialNormal); + mRendererRefraction.SetTextures( mTextureSetNormal ); + mRendererRefraction.SetShader( mShaderNormal ); - for (int i = 0 ; i < METABALL_NUMBER ; i++) + for( uint32_t i = 0 ; i < METABALL_NUMBER; i++ ) { mMetaballs[i].radius = mMetaballs[i].initRadius; } @@ -754,85 +667,88 @@ void MetaballRefracController::ResetMetaballsState() mMetaballPosVariationTo = Vector2(0,0); mMetaballPosVariationFrom = Vector2(0,0); mMetaballPosVariation = Vector2(0,0); - mGravityVar = Vector2(0,0); } -/** - * Function to set the actual position of the metaballs when the user clicks the screen - */ -void MetaballRefracController::SetPositionToMetaballs(Vector2 & metaballCenter) +void MetaballRefracController::SetPositionToMetaballs( const Vector2& metaballCenter ) { //We set the position for the metaballs based on click position - for (int i = 0 ; i < METABALL_NUMBER ; i++) + for( uint32_t i = 0 ; i < METABALL_NUMBER; i++ ) { mMetaballs[i].position = metaballCenter; - mMetaballs[i].actor.SetProperty(mMetaballs[i].positionIndex, mMetaballs[0].position); // 0 y no i ?!?!?! + mMetaballs[i].actor.SetProperty( mMetaballs[i].positionIndex, mMetaballs[i].position ); } } bool MetaballRefracController::OnTouch( Actor actor, const TouchEvent& touch ) { - const TouchPoint &point = touch.GetPoint(0); - float aspectR = mScreenSize.y / mScreenSize.x; - switch(point.state) + const float aspect = mScreenSize.y / mScreenSize.x; + switch( touch.GetState( 0 ) ) { - case TouchPoint::Down: + case PointState::DOWN: { StopAfterClickAnimations(); - for (int i = 0 ; i < METABALL_NUMBER ; i++) + for( uint32_t i = 0 ; i < METABALL_NUMBER; i++ ) + { mRadiusIncFastAnimation[i].Play(); + } mRadiusVarAnimation[2].Play(); mRadiusVarAnimation[3].Play(); //We draw with the refraction-composition shader - mRendererRefraction.SetMaterial(mMaterialRefraction); - - mCurrentTouchPosition = point.screen; + mRendererRefraction.SetTextures( mTextureSetRefraction ); + mRendererRefraction.SetShader( mShaderRefraction ); + mCurrentTouchPosition = touch.GetScreenPosition( 0 ); //we use the click position for the metaballs - Vector2 metaballCenter = Vector2((point.screen.x / mScreenSize.x) - 0.5, (aspectR * (mScreenSize.y - point.screen.y) / mScreenSize.y) - 0.5) * 2.0; + Vector2 metaballCenter = Vector2( (mCurrentTouchPosition.x / mScreenSize.x) - 0.5f, + (aspect * (mScreenSize.y - mCurrentTouchPosition.y) / mScreenSize.y) - 0.5f ) * 2.0f; SetPositionToMetaballs(metaballCenter); break; } - case TouchPoint::Motion: + case PointState::MOTION: { - Vector2 displacement = point.screen - mCurrentTouchPosition; - mCurrentTouchPosition = point.screen; + Vector2 screen = touch.GetScreenPosition( 0 ); + Vector2 displacement = screen - mCurrentTouchPosition; + mCurrentTouchPosition = screen; - mMetaballPosVariationTo.x += (displacement.x / mScreenSize.x) * 2.2; - mMetaballPosVariationTo.y += (- displacement.y / mScreenSize.y) * 2.2; + mMetaballPosVariationTo.x += ( displacement.x / mScreenSize.x ) * 2.2f; + mMetaballPosVariationTo.y += (-displacement.y / mScreenSize.y ) * 2.2f; if (mPositionVarAnimation[1]) { mPositionVarAnimation[1].FinishedSignal().Disconnect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation ); mPositionVarAnimation[1].Stop(); } - mPositionVarAnimation[1] = Animation::New(1.f); + mPositionVarAnimation[1] = Animation::New( 1.f ); mPositionVarAnimation[1].SetLooping( false ); - mPositionVarAnimation[1].AnimateTo(Property( mMetaballs[1].actor, mMetaballs[1].positionVarIndex ), mMetaballPosVariationTo); + mPositionVarAnimation[1].AnimateTo(Property( mMetaballs[1].actor, mMetaballs[1].positionVarIndex ), mMetaballPosVariationTo ); mPositionVarAnimation[1].FinishedSignal().Connect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation ); mPositionVarAnimation[1].Play(); //we use the click position for the metaballs - Vector2 metaballCenter = Vector2((point.screen.x / mScreenSize.x) - 0.5, (aspectR * (mScreenSize.y - point.screen.y) / mScreenSize.y) - 0.5) * 2.0; + Vector2 metaballCenter = Vector2( (screen.x / mScreenSize.x) - 0.5f, + (aspect * (mScreenSize.y - screen.y) / mScreenSize.y) - 0.5f) * 2.0f; SetPositionToMetaballs(metaballCenter); break; } - case TouchPoint::Up: - case TouchPoint::Leave: - case TouchPoint::Interrupted: + case PointState::UP: + case PointState::LEAVE: + case PointState::INTERRUPTED: { //Stop click animations StopClickAnimations(); //Launch out of screen animations - for (int i = 0 ; i < METABALL_NUMBER ; i++) + for( uint32_t i = 0 ; i < METABALL_NUMBER; i++ ) + { mGravityAnimation[i].Play(); + } - for (int i = 0 ; i < METABALL_NUMBER ; i++) + for( uint32_t i = 0 ; i < METABALL_NUMBER; i++ ) + { mRadiusDecAnimation[i].Play(); - + } break; } default: @@ -841,10 +757,9 @@ bool MetaballRefracController::OnTouch( Actor actor, const TouchEvent& touch ) return true; } - void MetaballRefracController::OnKeyEvent(const KeyEvent& event) { - if(event.state == KeyEvent::Down) + if( event.state == KeyEvent::Down ) { if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) ) { @@ -853,26 +768,15 @@ void MetaballRefracController::OnKeyEvent(const KeyEvent& event) } } - -// -// -//----------------------------------------------------------------------------------------------- - -void RunTest( Application& application ) -{ - MetaballRefracController test( application ); - - application.MainLoop(); -} - -// Entry point for Linux & Tizen applications -// -int main( int argc, char **argv ) +/** + * Main entry point + */ +int32_t DALI_EXPORT_API main( int argc, char **argv ) { Application application = Application::New( &argc, &argv ); - RunTest( application ); + MetaballRefracController test( application ); + application.MainLoop(); return 0; } -