/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
*
*/
-#include <dali/dali.h>
-#include <dali/devel-api/rendering/renderer.h>
-#include <dali-toolkit/dali-toolkit.h>
-
+// EXTERNAL INCLUDES
#include <cstdio>
#include <string>
+#include <cstdint> // uint32_t, uint16_t etc
+
+#include <dali/public-api/rendering/renderer.h>
+#include <dali/public-api/rendering/texture.h>
+#include <dali/public-api/rendering/texture-set.h>
+#include <dali/public-api/rendering/frame-buffer.h>
+
+// 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
}\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
}\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
{\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
}\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
}\n
);
-
+/**
+ * Metadata for each ball
+ */
struct MetaballInfo
{
- //ShaderEffect shader;
Actor actor;
Vector2 position;
float radius;
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 );
- bool OnTouch( Actor actor, const TouchEvent& touch );
- void OnKeyEvent(const KeyEvent& event);
- void SetGravity(const Vector2 & gravity);
+ /**
+ * Touch handler, start the grow animation and creates additional metaballs
+ */
+ bool OnTouch( Actor actor, const TouchData& touch );
+ /**
+ * 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;
+ Texture mMetaballFBOTexture;
Actor mMetaballRoot;
MetaballInfo mMetaballs[METABALL_NUMBER];
Actor mCompositionActor;
//Motion
- bool mExitClick;
Vector2 mCurrentTouchPosition;
Vector2 mMetaballPosVariation;
Vector2 mMetaballPosVariationFrom;
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];
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 )
// 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();
- stage.KeyEventSignal().Connect(this, &MetaballRefracController::OnKeyEvent);
+ stage.KeyEventSignal().Connect( this, &MetaballRefracController::OnKeyEvent );
mScreenSize = stage.GetSize();
stage.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 );
-}
-
-/**
- * Create a mesh data with the geometry for the metaball rendering
- */
-Geometry MetaballRefracController::CreateGeometry()
-{
- 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 * 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;
+ stage.GetRootLayer().TouchSignal().Connect( this, &MetaballRefracController::OnTouch );
}
-/**
- * Create a mesh data with the geometry for the metaball rendering
- */
-Geometry MetaballRefracController::CreateGeometryComposition()
+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) },
- { 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 = 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 );
+
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++)
+ 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
+ // 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 );
+ mMetaballFBO = FrameBuffer::New( mScreenSize.x, mScreenSize.y, FrameBuffer::Attachment::NONE );
+ mMetaballFBOTexture = Texture::New( Dali::TextureType::TEXTURE_2D,
+ Pixel::RGB888,
+ mScreenSize.x, mScreenSize.y );
+ mMetaballFBO.AttachColorTexture( mMetaballFBOTexture );
stage.Add(mMetaballRoot);
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
+ mShaderRefraction = Shader::New( METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER );
- //Create Refraction shader and renderer
- Shader shader = Shader::New( METABALL_VERTEX_SHADER, REFRACTION_FRAG_SHADER );
- //Create new material
- mMaterialRefraction = Material::New( shader );
+ // Create new texture set
+ mTextureSetRefraction = TextureSet::New();
+ mTextureSetRefraction.SetTexture( 0u, mBackgroundTexture );
+ mTextureSetRefraction.SetTexture( 1u, mMetaballFBOTexture );
- //Add Textures
- mMaterialRefraction.AddTexture(mBackImage, "sTexture");
- mMaterialRefraction.AddTexture(mMetaballFBO, "sEffect");
+ // Create normal shader
+ mShaderNormal = Shader::New( METABALL_VERTEX_SHADER, FRAG_SHADER );
- //Create normal shader
- Shader shaderNormal = Shader::New( METABALL_VERTEX_SHADER, FRAG_SHADER );
- //Create new material
- mMaterialNormal = Material::New( shaderNormal );
+ // Create new texture set
+ mTextureSetNormal = TextureSet::New();
+ mTextureSetNormal.SetTexture( 0u, mBackgroundTexture );
- //Add samplers
- mMaterialNormal.AddTexture(mBackImage, "sTexture");
-
-
- //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 );
+ // Create geometry
+ Geometry metaballGeometry = CreateGeometry( false );
+ mRendererRefraction = Renderer::New( metaballGeometry, mShaderNormal );
+ mRendererRefraction.SetTextures( mTextureSetNormal );
mCompositionActor.AddRenderer( mRendererRefraction );
Stage stage = Stage::GetCurrent();
stage.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);
}
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);
}
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;
}
//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;
}
//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();
}
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();
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;
}
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 TouchData& 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:
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) )
{
}
}
-
-//
-//
-//-----------------------------------------------------------------------------------------------
-
-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;
}
-