Updated all files to new format
[platform/core/uifw/dali-demo.git] / examples / mesh-morph / mesh-morph-example.cpp
1 /*
2  * Copyright (c) 2021 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 // EXTERNAL INCLUDES
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali/devel-api/actors/actor-devel.h>
21
22 // INTERNAL INCLUDES
23 #include "generated/mesh-morph-frag.h"
24 #include "generated/mesh-morph-vert.h"
25 #include "shared/view.h"
26
27 using namespace Dali;
28
29 namespace
30 {
31 Geometry CreateGeometry()
32 {
33   // Create vertices
34   struct VertexPosition
35   {
36     Vector2 position;
37   };
38   struct VertexColor
39   {
40     Vector3 color;
41   };
42
43   VertexPosition quad[] = {
44     // yellow
45     {Vector2(-.5, -.5)},
46     {Vector2(.0, .0)},
47     {Vector2(-.5, .5)},
48
49     // green
50     {Vector2(-.5, -.5)},
51     {Vector2(.5, -.5)},
52     {Vector2(.0, .0)},
53
54     // blue
55     {Vector2(.5, -.5)},
56     {Vector2(.5, .0)},
57     {Vector2(.25, -.25)},
58
59     // red
60     {Vector2(.25, -.25)},
61     {Vector2(.5, .0)},
62     {Vector2(.25, .25)},
63     {Vector2(.25, .25)},
64     {Vector2(.0, .0)},
65     {Vector2(.25, -.25)},
66
67     // cyan
68     {Vector2(.0, .0)},
69     {Vector2(.25, .25)},
70     {Vector2(-.25, .25)},
71
72     // magenta
73     {Vector2(-.25, .25)},
74     {Vector2(.25, .25)},
75     {Vector2(.0, .5)},
76     {Vector2(.0, .5)},
77     {Vector2(-.5, .5)},
78     {Vector2(-.25, .25)},
79
80     // orange
81     {Vector2(.5, .0)},
82     {Vector2(.5, .5)},
83     {Vector2(.0, .5)},
84   };
85
86   float bigSide = 0.707106781;
87   float side    = bigSide * .5f;
88   // float smallSide = side * .5f;
89
90   Vector2 pA  = Vector2(side, .25);
91   Vector2 pB  = pA + Vector2(0., bigSide);
92   Vector2 pC  = pB + Vector2(-bigSide, 0.);
93   Vector2 pD  = pA + Vector2(-.5, -.5);
94   Vector2 pE  = pD + Vector2(.0, 1.);
95   Vector2 pF  = pD + Vector2(-side, side);
96   Vector2 pF2 = pD + Vector2(0., bigSide);
97   Vector2 pG  = pD + Vector2(-.25, .25);
98   Vector2 pH  = pD + Vector2(-.5, .0);
99   Vector2 pI  = pD + Vector2(-.25, -.25);
100   Vector2 pJ  = pD + Vector2(0., -.5);
101   Vector2 pK  = pD + Vector2(-.5, -.5);
102   Vector2 pL  = pB + Vector2(0, -side);
103   Vector2 pM  = pL + Vector2(side, -side);
104   Vector2 pN  = pB + Vector2(side, -side);
105
106   VertexPosition cat[] = {
107     // yellow
108     {pA},
109     {pB},
110     {pC},
111
112     // green
113     {pD},
114     {pA},
115     {pE},
116
117     // blue
118     {pJ},
119     {pD},
120     {pI},
121
122     // red
123     {pI},
124     {pD},
125     {pG},
126     {pG},
127     {pH},
128     {pI},
129
130     // cyan
131     {pI},
132     {pH},
133     {pK},
134
135     // magenta
136     {pL},
137     {pM},
138     {pN},
139     {pN},
140     {pB},
141     {pL},
142
143     // orange
144     {pD},
145     {pF2},
146     {pF},
147   };
148
149   VertexColor colors[] = {
150     // yellow
151     {Vector3(1., 1., 0.)},
152     {Vector3(1., 1., 0.)},
153     {Vector3(1., 1., 0.)},
154
155     // green
156     {Vector3(0., 1., 0.)},
157     {Vector3(0., 1., 0.)},
158     {Vector3(0., 1., 0.)},
159
160     // blue
161     {Vector3(0., 0., 1.)},
162     {Vector3(0., 0., 1.)},
163     {Vector3(0., 0., 1.)},
164
165     // red
166     {Vector3(1., 0., 0.)},
167     {Vector3(1., 0., 0.)},
168     {Vector3(1., 0., 0.)},
169     {Vector3(1., 0., 0.)},
170     {Vector3(1., 0., 0.)},
171     {Vector3(1., 0., 0.)},
172
173     // cyan
174     {Vector3(0., 1., 1.)},
175     {Vector3(0., 1., 1.)},
176     {Vector3(0., 1., 1.)},
177
178     // magenta
179     {Vector3(1., 0., 1.)},
180     {Vector3(1., 0., 1.)},
181     {Vector3(1., 0., 1.)},
182     {Vector3(1., 0., 1.)},
183     {Vector3(1., 0., 1.)},
184     {Vector3(1., 0., 1.)},
185
186     // orange
187     {Vector3(1., 0.5, 0.)},
188     {Vector3(1., 0.5, 0.)},
189     {Vector3(1., 0.5, 0.)},
190
191   };
192
193   unsigned int numberOfVertices = sizeof(quad) / sizeof(VertexPosition);
194
195   Property::Map initialPositionVertexFormat;
196   initialPositionVertexFormat["aInitPos"] = Property::VECTOR2;
197   VertexBuffer initialPositionVertices    = VertexBuffer::New(initialPositionVertexFormat);
198   initialPositionVertices.SetData(quad, numberOfVertices);
199
200   Property::Map finalPositionVertexFormat;
201   finalPositionVertexFormat["aFinalPos"] = Property::VECTOR2;
202   VertexBuffer finalPositionVertices     = VertexBuffer::New(finalPositionVertexFormat);
203   finalPositionVertices.SetData(cat, numberOfVertices);
204
205   Property::Map colorVertexFormat;
206   colorVertexFormat["aColor"] = Property::VECTOR3;
207   VertexBuffer colorVertices  = VertexBuffer::New(colorVertexFormat);
208   colorVertices.SetData(colors, numberOfVertices);
209
210   // Create the geometry object
211   Geometry texturedQuadGeometry = Geometry::New();
212   texturedQuadGeometry.AddVertexBuffer(initialPositionVertices);
213   texturedQuadGeometry.AddVertexBuffer(finalPositionVertices);
214   texturedQuadGeometry.AddVertexBuffer(colorVertices);
215
216   return texturedQuadGeometry;
217 }
218
219 inline float StationarySin(float progress) ///< Single revolution
220 {
221   float val = cosf(progress * 2.0f * Math::PI) + .5f;
222   val       = val > 1.f ? 1.f : val;
223   val       = val < 0.f ? 0.f : val;
224   return val;
225 }
226
227 } // anonymous namespace
228
229 // This example shows how to use a simple mesh
230 //
231 class ExampleController : public ConnectionTracker
232 {
233 public:
234   /**
235    * The example controller constructor.
236    * @param[in] application The application instance
237    */
238   ExampleController(Application& application)
239   : mApplication(application)
240   {
241     // Connect to the Application's Init signal
242     mApplication.InitSignal().Connect(this, &ExampleController::Create);
243   }
244
245   /**
246    * The example controller destructor
247    */
248   ~ExampleController()
249   {
250     // Nothing to do here;
251   }
252
253   /**
254    * Invoked upon creation of application
255    * @param[in] application The application instance
256    */
257   void Create(Application& application)
258   {
259     Window window = application.GetWindow();
260     window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
261
262     mWindowSize = window.GetSize();
263
264     // The Init signal is received once (only) during the Application lifetime
265
266     mShader   = Shader::New(SHADER_MESH_MORPH_VERT, SHADER_MESH_MORPH_FRAG);
267     mGeometry = CreateGeometry();
268     mRenderer = Renderer::New(mGeometry, mShader);
269
270     mMeshActor = Actor::New();
271     mMeshActor.AddRenderer(mRenderer);
272     mMeshActor.SetProperty(Actor::Property::SIZE, Vector2(400, 400));
273     mMeshActor.SetProperty(DevelActor::Property::UPDATE_SIZE_HINT, Vector2(480, 700));
274
275     Property::Index morphDeltaIndex = mMeshActor.RegisterProperty("uDelta", 0.f);
276
277     mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
278
279     mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
280     mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
281     window.Add(mMeshActor);
282
283     Animation animation = Animation::New(10);
284     animation.AnimateTo(Property(mMeshActor, morphDeltaIndex), 1.f, StationarySin);
285     animation.SetLooping(true);
286     animation.Play();
287
288     window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
289   }
290
291   /**
292    * Invoked whenever the quit button is clicked
293    * @param[in] button the quit button
294    */
295   bool OnQuitButtonClicked(Toolkit::Button button)
296   {
297     // quit the application
298     mApplication.Quit();
299     return true;
300   }
301
302   void OnKeyEvent(const KeyEvent& event)
303   {
304     if(event.GetState() == KeyEvent::DOWN)
305     {
306       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
307       {
308         mApplication.Quit();
309       }
310     }
311   }
312
313 private:
314   Application& mApplication; ///< Application instance
315   Vector3      mWindowSize;  ///< The size of the window
316
317   Shader   mShader;
318   Geometry mGeometry;
319   Renderer mRenderer;
320   Actor    mMeshActor;
321   Timer    mMorphTimer;
322 };
323
324 int DALI_EXPORT_API main(int argc, char** argv)
325 {
326   Application       application = Application::New(&argc, &argv);
327   ExampleController test(application);
328   application.MainLoop();
329   return 0;
330 }