Alpha function changes
[platform/core/uifw/dali-demo.git] / examples / animated-shapes / animated-shapes-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20
21 #include "shared/view.h"
22
23 using namespace Dali;
24
25 namespace
26 {
27 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-gradient.jpg" );
28
29 }
30
31 // This example shows resolution independent rendering and animation of curves using the gpu.
32 //
33 class AnimatedShapesExample : public ConnectionTracker
34 {
35 public:
36
37   AnimatedShapesExample( Application& application )
38 : mApplication( application )
39 {
40     // Connect to the Application's Init signal
41     mApplication.InitSignal().Connect( this, &AnimatedShapesExample::Create );
42 }
43
44   ~AnimatedShapesExample()
45   {
46     // Nothing to do here;
47   }
48
49   // The Init signal is received once (only) during the Application lifetime
50   void Create( Application& application )
51   {
52     DemoHelper::RequestThemeChange();
53
54     // Get a handle to the stage
55     Stage stage = Stage::GetCurrent();
56
57     //Create a view
58     mView = Dali::Toolkit::Control::New();
59     mView.SetAnchorPoint( Dali::AnchorPoint::CENTER );
60     mView.SetParentOrigin( Dali::ParentOrigin::CENTER );
61     mView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
62     stage.Add( mView );
63
64     //Set background image for the view
65     mView.SetBackgroundImage( ResourceImage::New( BACKGROUND_IMAGE ) );
66
67     CreateTriangleMorph(Vector3( stage.GetSize().x*0.5f,stage.GetSize().y*0.15f,0.0f), 100.0f );
68     CreateCircleMorph( Vector3( stage.GetSize().x*0.5f,stage.GetSize().y*0.85f,0.0f), 60.0f );
69     CreatePathMorph( Vector3( stage.GetSize().x*0.5f,stage.GetSize().y*0.5f,0.0f), 55.0f );
70
71
72     stage.KeyEventSignal().Connect(this, &AnimatedShapesExample::OnKeyEvent);
73   }
74
75   void CreateCircleMorph( Vector3 center, float radius )
76   {
77     Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(16, true);
78
79     shader.SetPoint(0, Vector3(-radius,-radius,0.0f));
80     shader.SetPoint(1, Vector3( 0.0f,-radius,0.0f));
81     shader.SetPoint(2, Vector3(radius,-radius,0.0f));
82
83     shader.SetPoint(3, Vector3(radius,-radius,0.0f));
84     shader.SetPoint(4, Vector3( radius,0.0f,0.0f));
85     shader.SetPoint(5, Vector3(radius,radius,0.0f));
86
87     shader.SetPoint(6, Vector3(radius,radius,0.0f));
88     shader.SetPoint(7, Vector3( 0.0f,radius,0.0f));
89     shader.SetPoint(8, Vector3( -radius,radius,0.0f));
90
91     shader.SetPoint(9,  Vector3( -radius,radius,0.0f));
92     shader.SetPoint(10, Vector3( -radius,0.0f,0.0f));
93     shader.SetPoint(11, Vector3(-radius,-radius,0.0f));
94
95     shader.SetPoint(12, Vector3(-radius,-radius,0.0f));
96     shader.SetPoint(13, Vector3(radius,-radius,0.0f));
97     shader.SetPoint(14, Vector3(radius,radius,0.0f));
98     shader.SetPoint(15, Vector3( -radius,radius,0.0f));
99
100     shader.SetColor(Vector4(1.0f,0.0f,0.0f,1.0f) );
101     shader.SetLineWidth(2.0f);
102
103     ////Generate the mesh
104     Dali::MeshData::VertexContainer vertices;
105     for( unsigned int i(0); i<12; i+=3 )
106     {
107       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,0.0f,i)  ));
108       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.5f,0.0f,i+1)));
109       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(1.0f,1.0f,i+2)));
110     }
111     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,12)  ));
112     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,13)));
113     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,14)));
114     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,15)));
115
116     short unsigned int indexArray[] = { 0,2,1, 3,5,4,6,8,7, 9, 11, 10, 12,15,14,12,14,13};
117     Dali::MeshData::FaceIndices index( indexArray, indexArray + sizeof(indexArray)/sizeof(short unsigned int) );
118
119     //Material
120     Dali::Material material = Material::New("Material");
121     material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
122
123     //Create the Mesh object
124     Dali::MeshData data;
125     data.SetVertices(vertices);
126     data.SetFaceIndices( index );
127     data.SetMaterial( material );
128     data.SetHasNormals( true );
129     Mesh mesh = Mesh::New( data );
130
131     //Create the mesh actor
132     MeshActor meshActor = MeshActor::New(mesh);
133     meshActor.SetAnchorPoint( AnchorPoint::CENTER );
134     meshActor.SetShaderEffect(shader);
135     meshActor.SetPosition( center );
136     meshActor.SetBlendMode(BlendingMode::ON );
137     mView.Add( meshActor );
138
139
140     //Animation
141     Animation animation = Animation::New(5.0f);
142     KeyFrames k0 = KeyFrames::New();
143     k0.Add( 0.0f, Vector3( 0.0f,-radius, 0.0f) );
144     k0.Add( 0.5f, Vector3(0.0f, -radius*4.0f, 0.0f));
145     k0.Add( 1.0f, Vector3( 0.0f,-radius, 0.0f) );
146     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(1)),k0,AlphaFunction::EASE_IN_OUT );
147
148     k0 = KeyFrames::New();
149     k0.Add( 0.0f, Vector3( radius, 0.0f, 0.0f) );
150     k0.Add( 0.5f, Vector3(radius*4.0f,0.0f, 0.0f));
151     k0.Add( 1.0f, Vector3( radius,0.0f, 0.0f));
152     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(4)),k0,AlphaFunction::EASE_IN_OUT );
153
154     k0 = KeyFrames::New();
155     k0.Add( 0.0f, Vector3(0.0f,radius, 0.0f) );
156     k0.Add( 0.5f, Vector3(0.0f,radius*4.0f, 0.0f));
157     k0.Add( 1.0f, Vector3(0.0f,radius, 0.0f) );
158     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(7)),k0,AlphaFunction::EASE_IN_OUT );
159
160     k0 = KeyFrames::New();
161     k0.Add( 0.0f, Vector3( -radius,  0.0f, 0.0f) );
162     k0.Add( 0.5f, Vector3(-radius*4.0f,0.0f, 0.0f));
163     k0.Add( 1.0f, Vector3( -radius,  0.0f, 0.0f) );
164     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(10)),k0,AlphaFunction::EASE_IN_OUT );
165
166     animation.AnimateBy( Property( meshActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(90.0f) ), Vector3::ZAXIS ) );
167     animation.SetLooping( true );
168     animation.Play();
169   }
170
171   void CreateTriangleMorph( Vector3 center, float side )
172   {
173     float h = (side *0.5f)/0.866f;
174
175     Vector3 v0 = Vector3(-h,h,0.0f);
176     Vector3 v1 = Vector3(0.0f,-(side*0.366f),0.0f );
177     Vector3 v2 = Vector3(h,h,0.0f);
178
179     Vector3 v3 = v0 + ((v1-v0) * 0.5f);
180     Vector3 v4 = v1 + ((v2-v1) * 0.5f);
181     Vector3 v5 = v2 + ((v0-v2) * 0.5f);
182
183     Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(12, true);
184
185     shader.SetPoint(0,v0);
186     shader.SetPoint(1,v3);
187     shader.SetPoint(2,v1);
188
189     shader.SetPoint(3,v1);
190     shader.SetPoint(4,v4);
191     shader.SetPoint(5,v2);
192
193     shader.SetPoint(6,v2);
194     shader.SetPoint(7,v5);
195     shader.SetPoint(8,v0);
196
197     shader.SetPoint(9, v0);
198     shader.SetPoint(10,v1);
199     shader.SetPoint(11,v2);
200
201     shader.SetColor(Vector4(0.0f,1.0f,0.0f,1.0f));
202     shader.SetLineWidth(2.0f);
203
204     ////Generate the mesh
205     Dali::MeshData::VertexContainer vertices;
206     for( unsigned int i(0);i<9;i+=3 )
207     {
208       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,0.0f,i)) );
209       vertices.push_back(  MeshData::Vertex( Vector3::ZERO, Vector2::ZERO,Vector3(0.5f,0.0f,i+1) ) );
210       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(1.0f,1.0f,i+2)  ) );
211     }
212
213     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,9)) );
214     vertices.push_back(  MeshData::Vertex( Vector3::ZERO, Vector2::ZERO,Vector3(0.0f,1.0f,10) ) );
215     vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,1.0f,11)  ) );
216
217     short unsigned int indexArray[] = { 0,2,1,3,5,4,6,8,7,9,11,10 };
218     Dali::MeshData::FaceIndices index( indexArray, indexArray + sizeof(indexArray)/sizeof(short unsigned int) );
219
220     //Material
221     Dali::Material material = Material::New("Material");
222     material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
223
224     //Create the Mesh object
225     Dali::MeshData data;
226     data.SetVertices(vertices);
227     data.SetFaceIndices( index );
228     data.SetMaterial( material );
229     data.SetHasNormals( true );
230     Mesh mesh = Mesh::New( data );
231
232 //    //Create the mesh actor
233     MeshActor meshActor = MeshActor::New(mesh);
234     meshActor.SetAnchorPoint( AnchorPoint::CENTER );
235     meshActor.SetShaderEffect(shader);
236     meshActor.SetPosition( center );
237     meshActor.SetBlendMode(BlendingMode::ON );
238     mView.Add( meshActor );
239
240     //Animation
241     Animation animation = Animation::New(5.0f);
242
243     KeyFrames k0 = KeyFrames::New();
244     k0.Add( 0.0f,v3  );
245     k0.Add( 0.5f, v3 + Vector3(-200.0f,-200.0f,0.0f));
246     k0.Add( 1.0f, v3 );
247     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(1)),k0,AlphaFunction::EASE_IN_OUT );
248
249     k0 = KeyFrames::New();
250     k0.Add( 0.0f,v4  );
251     k0.Add( 0.5f, v4 + Vector3(200.0f,-200.0f,0.0f));
252     k0.Add( 1.0f, v4 );
253     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(4)),k0,AlphaFunction::EASE_IN_OUT );
254
255     k0 = KeyFrames::New();
256     k0.Add( 0.0f,v5  );
257     k0.Add( 0.5f, v5 + Vector3(0.0,200.0f,0.0f));
258     k0.Add( 1.0f, v5 );
259     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(7)),k0,AlphaFunction::EASE_IN_OUT );
260     animation.SetLooping( true );
261     animation.Play();
262   }
263
264   void CreatePathMorph( Vector3 center, float radius )
265   {
266     Toolkit::QuadraticBezier shader = Toolkit::QuadraticBezier::New(12, false);
267
268     shader.SetPoint(0, Vector3(-radius,-radius,0.0f));
269     shader.SetPoint(1, Vector3( 0.0f,-radius,0.0f));
270     shader.SetPoint(2, Vector3(radius,-radius,0.0f));
271
272     shader.SetPoint(3, Vector3(radius,-radius,0.0f));
273     shader.SetPoint(4, Vector3( radius,0.0f,0.0f));
274     shader.SetPoint(5, Vector3(radius,radius,0.0f));
275
276     shader.SetPoint(6, Vector3(radius,radius,0.0f));
277     shader.SetPoint(7, Vector3( 0.0f,radius,0.0f));
278     shader.SetPoint(8, Vector3( -radius,radius,0.0f));
279
280     shader.SetPoint(9,  Vector3( -radius,radius,0.0f));
281     shader.SetPoint(10, Vector3( -radius,0.0f,0.0f));
282     shader.SetPoint(11, Vector3(-radius,-radius,0.0f));
283
284     shader.SetColor(Vector4(1.0f,1.0f,0.0f,1.0f) );
285     shader.SetLineWidth(1.5f);
286
287     ////Generate the mesh/S
288     Dali::MeshData::VertexContainer vertices;
289     for( unsigned int i(0); i<12; i+=3 )
290     {
291       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.0f,0.0f,i)  ));
292       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(0.5f,0.0f,i+1)));
293       vertices.push_back( MeshData::Vertex( Vector3::ZERO, Vector2::ZERO, Vector3(1.0f,1.0f,i+2)));
294     }
295
296
297     short unsigned int indexArray[] = { 0,2,1, 3,5,4,6,8,7, 9, 11, 10 };
298     Dali::MeshData::FaceIndices index( indexArray, indexArray + sizeof(indexArray)/sizeof(short unsigned int) );
299
300     //Material
301     Dali::Material material = Material::New("Material");
302     material.SetDiffuseColor( Vector4(1.0f,1.0f,1.0f,1.0f));
303
304     //Create the Mesh object
305     Dali::MeshData data;
306     data.SetVertices(vertices);
307     data.SetFaceIndices( index );
308     data.SetMaterial( material );
309     data.SetHasNormals( true );
310     Mesh mesh = Mesh::New( data );
311
312     //Create the mesh actor
313     MeshActor meshActor = MeshActor::New(mesh);
314     meshActor.SetAnchorPoint( AnchorPoint::CENTER );
315     meshActor.SetShaderEffect(shader);
316     meshActor.SetPosition( center );
317     meshActor.SetBlendMode(BlendingMode::ON );
318     mView.Add( meshActor );
319
320
321     //Animation
322     Animation animation = Animation::New(5.0f);
323     KeyFrames k0 = KeyFrames::New();
324     k0.Add( 0.0f, Vector3( 0.0f,-radius*2.0, 0.0f) );
325     k0.Add( 0.5f, Vector3(-radius*2.0, -radius*3.0f, 0.0f));
326     k0.Add( 1.0f, Vector3( 0.0f,-radius*2.0, 0.0f) );
327     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(1)),k0,AlphaFunction::EASE_IN_OUT );
328
329     k0 = KeyFrames::New();
330     k0.Add( 0.0f, Vector3( radius*2.0, 0.0f, 0.0f) );
331     k0.Add( 0.5f, Vector3(radius*3.0f,-radius*2.0, 0.0f));
332     k0.Add( 1.0f, Vector3( radius*2.0,0.0f, 0.0f));
333     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(4)),k0,AlphaFunction::EASE_IN_OUT );
334
335     k0 = KeyFrames::New();
336     k0.Add( 0.0f, Vector3(0.0f,radius*2.0, 0.0f) );
337     k0.Add( 0.5f, Vector3(radius*2.0,radius*3.0f, 0.0f));
338     k0.Add( 1.0f, Vector3(0.0f,radius*2.0, 0.0f) );
339     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(7)),k0,AlphaFunction::EASE_IN_OUT );
340
341     k0 = KeyFrames::New();
342     k0.Add( 0.0f, Vector3( -radius*2.0,  0.0f, 0.0f) );
343     k0.Add( 0.5f, Vector3(-radius*3.0f,radius*2.0, 0.0f));
344     k0.Add( 1.0f, Vector3( -radius*2.0,  0.0f, 0.0f) );
345     animation.AnimateBetween( Property(shader, shader.GetPointPropertyName(10)),k0,AlphaFunction::EASE_IN_OUT );
346
347     animation.AnimateBy( Property( meshActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(-90.0f) ), Vector3::ZAXIS ) );
348     animation.SetLooping( true );
349     animation.Play();
350   }
351
352   /**
353    * Main key event handler
354    */
355   void OnKeyEvent(const KeyEvent& event)
356   {
357     if( event.state == KeyEvent::Down && (IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ))  )
358     {
359       mApplication.Quit();
360     }
361   }
362
363 private:
364   Application&                mApplication;
365   Toolkit::Control            mView;
366 };
367
368 void RunTest( Application& application )
369 {
370   AnimatedShapesExample test( application );
371   application.MainLoop();
372 }
373
374 int main( int argc, char **argv )
375 {
376   Application application = Application::New( &argc, &argv );
377   RunTest( application );
378
379   return 0;
380 }