X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=examples%2Fmetaball-refrac%2Fmetaball-refrac-example.cpp;h=1d1c0466bc09fbeb8e167044656d68afb21ea11b;hb=1b19fd140ff139b5854a1a62447faf31b175d8f6;hp=e472593ea2ce2a3873c651849cd8be4c4e1e517c;hpb=1bb19325c728169dbf3994aefdc12a8a5f00ca02;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 e472593..1d1c046 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,38 @@ * */ -#include -#include -#include - +// EXTERNAL INCLUDES +#include // uint32_t, uint16_t etc #include #include +#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( DEMO_IMAGE_DIR "background-2.jpg" ); -const char * const TOOLBAR_IMAGE( DEMO_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); -const float GRAVITY_X(0); -const float GRAVITY_Y(-0.09); -} +// number of metaballs +constexpr uint32_t METABALL_NUMBER = 6; -#define METABALL_NUMBER 4 +// clang-format off -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 +61,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 +104,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 +121,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 +136,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 @@ -133,11 +148,13 @@ const char*const FRAG_SHADER = DALI_COMPOSE_SHADER ( gl_FragColor = texture2D(sTexture, vTexCoord);\n }\n ); +// clang-format on - +/** + * Metadata for each ball + */ struct MetaballInfo { - //ShaderEffect shader; Actor actor; Vector2 position; float radius; @@ -152,93 +169,146 @@ 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: - MetaballRefracController( Application& application ); - ~MetaballRefracController(); - - void Create( Application& app ); - bool OnTouch( Actor actor, const TouchEvent& touch ); + /** + * Constructor + * @param application + */ + MetaballRefracController(Application& application); + + /** + * 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); + + /** + * Key event callback to quit the application on escape or back key + */ void OnKeyEvent(const KeyEvent& event); - void SetGravity(const Vector2 & gravity); - - -private: - Application& mApplication; - Vector2 mScreenSize; +private: // Data + Application& mApplication; + Vector2 mScreenSize; - Layer mContentLayer; + Texture mBackgroundTexture; + FrameBuffer mMetaballFBO; - Image mBackImage; - FrameBufferImage mMetaballFBO; + Actor mMetaballRoot; + MetaballInfo mMetaballs[METABALL_NUMBER]; - Actor mMetaballRoot; - MetaballInfo mMetaballs[METABALL_NUMBER]; - - Actor mCompositionActor; + Actor mCompositionActor; //Motion - bool mExitClick; - Vector2 mCurrentTouchPosition; - Vector2 mMetaballPosVariation; - Vector2 mMetaballPosVariationFrom; - Vector2 mMetaballPosVariationTo; - Vector2 mMetaballCenter; - - Vector2 mGravity; - Vector2 mGravityVar; - - Renderer mRendererRefraction; - Material mMaterialRefraction; - Material mMaterialNormal; - - //Animations - Animation mGravityAnimation[METABALL_NUMBER]; - Animation mRadiusDecAnimation[METABALL_NUMBER]; - Animation mRadiusIncFastAnimation[METABALL_NUMBER]; - Animation mRadiusIncSlowAnimation[METABALL_NUMBER]; - 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(); - - void SetPositionToMetaballs(Vector2 & metaballCenter); + Vector2 mCurrentTouchPosition; + Vector2 mMetaballPosVariation; + Vector2 mMetaballPosVariationFrom; + Vector2 mMetaballPosVariationTo; + Vector2 mMetaballCenter; + + Vector2 mGravity; + Vector2 mGravityVar; + + Renderer mRendererRefraction; + TextureSet mTextureSetRefraction; + Shader mShaderRefraction; + TextureSet mTextureSetNormal; + Shader mShaderNormal; + + // Animations + Animation mGravityAnimation[METABALL_NUMBER]; + Animation mRadiusDecAnimation[METABALL_NUMBER]; + Animation mRadiusIncFastAnimation[METABALL_NUMBER]; + Animation mRadiusIncSlowAnimation[METABALL_NUMBER]; + Animation mRadiusVarAnimation[METABALL_NUMBER]; + Animation mPositionVarAnimation[METABALL_NUMBER]; + + // 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); }; +/** + * Implementation + */ -//----------------------------------------------------------------------------------------------- -// -// IMPLEMENTATION -// -//---------------- - -MetaballRefracController::MetaballRefracController( Application& application ) - : mApplication( application ) +MetaballRefracController::MetaballRefracController(Application& application) +: mApplication(application) { // Connect to the Application's Init signal - mApplication.InitSignal().Connect( this, &MetaballRefracController::Create ); + mApplication.InitSignal().Connect(this, &MetaballRefracController::Create); } MetaballRefracController::~MetaballRefracController() @@ -246,456 +316,311 @@ 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 ) +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); + 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().TouchedSignal().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; - - //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 * 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 ); - positionVertices.SetData( vertices, numberOfVertices ); - - //Textures - Property::Map textureVertexFormat; - textureVertexFormat["aTexture"] = Property::VECTOR2; - PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat ); - textureVertices.SetData( textures, numberOfVertices ); - - //Normals - Property::Map normalVertexFormat; - normalVertexFormat["aNormal"] = Property::VECTOR3; - PropertyBuffer normalVertices = PropertyBuffer::New( normalVertexFormat ); - normalVertices.SetData( normals, numberOfVertices ); - - //Indices - Property::Map indicesVertexFormat; - indicesVertexFormat["aIndices"] = Property::INTEGER; - PropertyBuffer indicesToVertices = PropertyBuffer::New( indicesVertexFormat ); - indicesToVertices.SetData( indices, 6 ); + const float xsize = mScreenSize.x * 0.5; - - // 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) } + // Create the meshdata for the metaballs + struct VertexPosition + { + Vector2 position; }; - - VertexTexture textures[] = { - { Vector2(0.0f, 0.0f) }, - { Vector2(1.0f, 0.0f) }, - { Vector2(0.0f, 1.0f) }, - { Vector2(1.0f, 1.0f) } + struct VertexTexture + { + Vector2 texture; }; - 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) } - }; + VertexPosition vertices[] = + { + {Vector2(-xsize, -xsize * aspect)}, + {Vector2(xsize, -xsize * aspect)}, + {Vector2(-xsize, xsize * aspect)}, + {Vector2(xsize, xsize * aspect)}}; - int indices[] = { 0, 3, 1, 0, 2, 3 }; + 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)}}; - 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 ); - positionVertices.SetData( vertices, numberOfVertices ); + VertexBuffer positionVertices = VertexBuffer::New(positionVertexFormat); + positionVertices.SetData(vertices, numberOfVertices); - //Textures + // Textures Property::Map textureVertexFormat; textureVertexFormat["aTexture"] = Property::VECTOR2; - PropertyBuffer textureVertices = PropertyBuffer::New( textureVertexFormat ); - textureVertices.SetData( textures, numberOfVertices ); + VertexBuffer textureVertices = VertexBuffer::New(textureVertexFormat); + textureVertices.SetData(textures, numberOfVertices); - //Normals - Property::Map normalVertexFormat; - normalVertexFormat["aNormal"] = Property::VECTOR3; - PropertyBuffer normalVertices = PropertyBuffer::New( normalVertexFormat ); - normalVertices.SetData( normals, numberOfVertices ); - - //Indices - Property::Map indicesVertexFormat; - indicesVertexFormat["aIndices"] = Property::INTEGER; - PropertyBuffer indicesToVertices = PropertyBuffer::New( indicesVertexFormat ); - indicesToVertices.SetData( indices, 6 ); + // 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.AddVertexBuffer(positionVertices); + texturedQuadGeometry.AddVertexBuffer(textureVertices); - 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 ); - - 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 ); - - Renderer renderer = Renderer::New( metaballGeom, material ); - renderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON ); - renderer.SetBlendFunc(BlendingFactor::ONE, BlendingFactor::ONE, BlendingFactor::ONE, BlendingFactor::ONE); - 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].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); - mMetaballs[i].aspectIndex = mMetaballs[i].actor.RegisterProperty( "uAspect", aspect ); + mMetaballs[i].actor.AddRenderer(renderer); - mMetaballs[i].actor.SetSize(400, 400); + 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); } //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 ); + 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(); - RenderTask task = taskList.CreateTask(); - task.SetRefreshRate( RenderTask::REFRESH_ALWAYS ); - task.SetSourceActor( mMetaballRoot ); + RenderTaskList taskList = window.GetRenderTaskList(); + RenderTask task = taskList.CreateTask(); + task.SetRefreshRate(RenderTask::REFRESH_ALWAYS); + task.SetSourceActor(mMetaballRoot); task.SetExclusive(true); - task.SetClearColor( Color::BLACK ); - task.SetClearEnabled( true ); - task.SetTargetFrameBuffer( mMetaballFBO ); + task.SetClearColor(Color::BLACK); + task.SetClearEnabled(true); + 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 ); - - //Add Textures - mMaterialRefraction.AddTexture(mBackImage, "sTexture"); - mMaterialRefraction.AddTexture(mMetaballFBO, "sEffect"); - - //Create normal shader - Shader shaderNormal = Shader::New( METABALL_VERTEX_SHADER, FRAG_SHADER ); - //Create new material - mMaterialNormal = Material::New( shaderNormal ); - - //Add samplers - mMaterialNormal.AddTexture(mBackImage, "sTexture"); - - - //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.AddRenderer( mRendererRefraction ); - - Stage stage = Stage::GetCurrent(); - stage.Add( mCompositionActor ); + // Create Refraction shader and renderer + mShaderRefraction = Shader::New(METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER); + + // Create new texture set + mTextureSetRefraction = TextureSet::New(); + mTextureSetRefraction.SetTexture(0u, mBackgroundTexture); + mTextureSetRefraction.SetTexture(1u, mMetaballFBO.GetColorTexture()); + + // Create normal shader + mShaderNormal = Shader::New(METABALL_VERTEX_SHADER, FRAG_SHADER); + + // Create new texture set + mTextureSetNormal = TextureSet::New(); + mTextureSetNormal.SetTexture(0u, mBackgroundTexture); + + // Create actor + mCompositionActor = Actor::New(); + 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); + + Window window = mApplication.GetWindow(); + window.Add(mCompositionActor); } -/** - * Creation of all the metaballs animations (gravity, movement, size, etc.) - */ void MetaballRefracController::CreateAnimations() { - int i = 0; - float key; + uint32_t i = 0; + float key; mPositionVarAnimation[1] = Animation::New(2.f); - mPositionVarAnimation[1].SetLooping( false ); + mPositionVarAnimation[1].SetLooping(false); mPositionVarAnimation[1].Pause(); - mPositionVarAnimation[1].FinishedSignal().Connect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation ); + mPositionVarAnimation[1].FinishedSignal().Connect(this, &MetaballRefracController::LaunchGetBackToPositionAnimation); KeyFrames keySinCosVariation = KeyFrames::New(); - Vector2 sinCosVariation(0,0); - for ( i = 0 ; i < 360 ; i++) + Vector2 sinCosVariation(0, 0); + 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)); - key = i/360.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); } mPositionVarAnimation[2] = Animation::New(6.f); - mPositionVarAnimation[2].AnimateBetween(Property( mMetaballs[2].actor, mMetaballs[2].positionVarIndex ), keySinCosVariation); - mPositionVarAnimation[2].SetLooping( true ); + mPositionVarAnimation[2].AnimateBetween(Property(mMetaballs[2].actor, mMetaballs[2].positionVarIndex), keySinCosVariation); + mPositionVarAnimation[2].SetLooping(true); mPositionVarAnimation[2].Pause(); KeyFrames keyCosSinVariation = KeyFrames::New(); - Vector2 cosSinVariation(0,0); - for ( i = 0 ; i < 360 ; i++) + Vector2 cosSinVariation(0, 0); + 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)); - key = i/360.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); } mPositionVarAnimation[3] = Animation::New(6.f); - mPositionVarAnimation[3].AnimateBetween(Property( mMetaballs[3].actor, mMetaballs[3].positionVarIndex ), keyCosSinVariation); - mPositionVarAnimation[3].SetLooping( true ); + mPositionVarAnimation[3].AnimateBetween(Property(mMetaballs[3].actor, mMetaballs[3].positionVarIndex), keyCosSinVariation); + mPositionVarAnimation[3].SetLooping(true); 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].SetLooping( false ); + 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].SetLooping( false ); + 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].SetLooping( false ); + 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 ); + 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].SetLooping( false ); + 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++) + float val; + for(i = 0; i < 360; i++) { - val = 0.01f * sin(i * Math::PI/180.f); - key = i/360.f; + val = 0.01f * sin(i * Math::PI / 180.f); + key = i / 360.f; keySin.Add(key, val); } //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].SetLooping( true ); + 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; + val = 0.01f * cos(i * Math::PI / 180.f); + key = i / 360.f; keyCos.Add(key, val); } //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].SetLooping( true ); + 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); + mMetaballPosVariationTo = Vector2(0, 0); 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].SetLooping(false); + 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(); } @@ -703,12 +628,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(); @@ -718,19 +640,16 @@ 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].gravityIndex, Vector2(0, 0)); mMetaballs[i].actor.SetProperty(mMetaballs[i].radiusIndex, mMetaballs[i].radius); mMetaballs[i].actor.SetProperty(mMetaballs[i].radiusVarIndex, 0.f); } @@ -738,139 +657,131 @@ void MetaballRefracController::StopAfterClickAnimations() 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; } - mMetaballPosVariationTo = Vector2(0,0); - mMetaballPosVariationFrom = Vector2(0,0); - mMetaballPosVariation = Vector2(0,0); - - mGravityVar = Vector2(0,0); + 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 ) +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]) + if(mPositionVarAnimation[1]) { - mPositionVarAnimation[1].FinishedSignal().Disconnect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation ); + mPositionVarAnimation[1].FinishedSignal().Disconnect(this, &MetaballRefracController::LaunchGetBackToPositionAnimation); mPositionVarAnimation[1].Stop(); } mPositionVarAnimation[1] = Animation::New(1.f); - mPositionVarAnimation[1].SetLooping( false ); - mPositionVarAnimation[1].AnimateTo(Property( mMetaballs[1].actor, mMetaballs[1].positionVarIndex ), mMetaballPosVariationTo); - mPositionVarAnimation[1].FinishedSignal().Connect( this, &MetaballRefracController::LaunchGetBackToPositionAnimation ); + mPositionVarAnimation[1].SetLooping(false); + 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: break; - } + } return true; } - void MetaballRefracController::OnKeyEvent(const KeyEvent& event) { - if(event.state == KeyEvent::Down) + if(event.GetState() == KeyEvent::DOWN) { - if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) ) + if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK)) { mApplication.Quit(); } } } - -// -// -//----------------------------------------------------------------------------------------------- - -void RunTest( Application& application ) +/** + * Main entry point + */ +int32_t DALI_EXPORT_API main(int argc, char** argv) { - MetaballRefracController test( application ); + Application application = Application::New(&argc, &argv); + MetaballRefracController test(application); application.MainLoop(); -} - -// Entry point for Linux & Tizen applications -// -int main( int argc, char **argv ) -{ - Application application = Application::New( &argc, &argv ); - - RunTest( application ); return 0; }