Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / textured-mesh / textured-mesh-example.cpp
1 /*
2  * Copyright (c) 2020 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
21 // INTERNAL INCLUDES
22 #include "shared/utility.h"
23 #include "shared/view.h"
24
25 using namespace Dali;
26
27 namespace
28 {
29 const char* MATERIAL_SAMPLE(DEMO_IMAGE_DIR "gallery-small-48.jpg");
30 const char* MATERIAL_SAMPLE2(DEMO_IMAGE_DIR "gallery-medium-19.jpg");
31
32 #define MAKE_SHADER(A) #A
33
34 const char* VERTEX_SHADER = MAKE_SHADER(
35   attribute mediump vec2 aPosition;
36   attribute highp vec2   aTexCoord;
37   varying mediump vec2   vTexCoord;
38   uniform mediump mat4   uMvpMatrix;
39   uniform mediump vec3   uSize;
40   uniform lowp vec4      uFadeColor;
41
42   void main() {
43     mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
44     vertexPosition.xyz *= uSize;
45     vertexPosition = uMvpMatrix * vertexPosition;
46     vTexCoord      = aTexCoord;
47     gl_Position    = vertexPosition;
48   });
49
50 const char* FRAGMENT_SHADER = MAKE_SHADER(
51   varying mediump vec2 vTexCoord;
52   uniform lowp vec4    uColor;
53   uniform sampler2D    sTexture;
54   uniform lowp vec4    uFadeColor;
55
56   void main() {
57     gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * uFadeColor;
58   });
59
60 /**
61  * Sinusoidal curve starting at zero with 2 cycles
62  */
63 float AlphaFunctionSineX2(float progress)
64 {
65   return 0.5f - cosf(progress * 4.0f * Math::PI) * 0.5f;
66 }
67
68 } // anonymous namespace
69
70 // This example shows how to use a simple mesh
71 //
72 class ExampleController : public ConnectionTracker
73 {
74 public:
75   /**
76    * The example controller constructor.
77    * @param[in] application The application instance
78    */
79   ExampleController(Application& application)
80   : mApplication(application)
81   {
82     // Connect to the Application's Init signal
83     mApplication.InitSignal().Connect(this, &ExampleController::Create);
84   }
85
86   /**
87    * The example controller destructor
88    */
89   ~ExampleController()
90   {
91     // Nothing to do here;
92   }
93
94   /**
95    * Invoked upon creation of application
96    * @param[in] application The application instance
97    */
98   void Create(Application& application)
99   {
100     // The Init signal is received once (only) during the Application lifetime
101
102     Window window = application.GetWindow();
103     window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
104
105     mWindowSize = window.GetSize();
106
107     Texture texture1 = DemoHelper::LoadTexture(MATERIAL_SAMPLE);
108     Texture texture2 = DemoHelper::LoadTexture(MATERIAL_SAMPLE2);
109
110     mShader      = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER);
111     mTextureSet1 = TextureSet::New();
112     mTextureSet1.SetTexture(0u, texture1);
113
114     mTextureSet2 = TextureSet::New();
115     mTextureSet2.SetTexture(0u, texture2);
116
117     mGeometry = DemoHelper::CreateTexturedQuad();
118
119     mRenderer = Renderer::New(mGeometry, mShader);
120     mRenderer.SetTextures(mTextureSet1);
121
122     mMeshActor = Actor::New();
123     mMeshActor.AddRenderer(mRenderer);
124     mMeshActor.SetProperty(Actor::Property::SIZE, Vector2(400, 400));
125
126     Property::Index fadeColorIndex = mRenderer.RegisterProperty("uFadeColor", Color::MAGENTA);
127     mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
128
129     mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
130     mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
131     window.Add(mMeshActor);
132
133     mRenderer2 = Renderer::New(mGeometry, mShader);
134     mRenderer2.SetTextures(mTextureSet2);
135
136     mMeshActor2 = Actor::New();
137     mMeshActor2.AddRenderer(mRenderer2);
138     mMeshActor2.SetProperty(Actor::Property::SIZE, Vector2(400, 400));
139
140     mMeshActor2.RegisterProperty("anotherProperty", Color::GREEN);
141
142     mRenderer2.RegisterProperty("anotherProperty", Vector3::ZERO);
143     mRenderer2.RegisterProperty("aCoefficient", 0.008f);
144     Property::Index fadeColorIndex2 = mRenderer2.RegisterProperty("uFadeColor", Color::BLUE);
145     mRenderer2.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
146
147     mMeshActor2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
148     mMeshActor2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
149     window.Add(mMeshActor2);
150
151     Animation animation = Animation::New(5);
152     KeyFrames keyFrames = KeyFrames::New();
153     keyFrames.Add(0.0f, Vector4::ZERO);
154     keyFrames.Add(1.0f, Vector4(Color::GREEN));
155
156     KeyFrames keyFrames2 = KeyFrames::New();
157     keyFrames2.Add(0.0f, Vector4::ZERO);
158     keyFrames2.Add(1.0f, Color::MAGENTA);
159
160     animation.AnimateBetween(Property(mRenderer, fadeColorIndex), keyFrames, AlphaFunction(AlphaFunction::SIN));
161     animation.AnimateBetween(Property(mRenderer2, fadeColorIndex2), keyFrames2, AlphaFunction(AlphaFunctionSineX2));
162     animation.SetLooping(true);
163     animation.Play();
164
165     window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
166   }
167
168   /**
169    * Invoked whenever the quit button is clicked
170    * @param[in] button the quit button
171    */
172   bool OnQuitButtonClicked(Toolkit::Button button)
173   {
174     // quit the application
175     mApplication.Quit();
176     return true;
177   }
178
179   void OnKeyEvent(const KeyEvent& event)
180   {
181     if(event.GetState() == KeyEvent::DOWN)
182     {
183       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
184       {
185         mApplication.Quit();
186       }
187     }
188   }
189
190 private:
191   Application& mApplication; ///< Application instance
192   Vector3      mWindowSize;  ///< The size of the window
193
194   Shader     mShader;
195   TextureSet mTextureSet1;
196   TextureSet mTextureSet2;
197   Geometry   mGeometry;
198   Renderer   mRenderer;
199   Actor      mMeshActor;
200   Renderer   mRenderer2;
201   Actor      mMeshActor2;
202   Timer      mChangeImageTimer;
203 };
204
205 int DALI_EXPORT_API main(int argc, char** argv)
206 {
207   Application       application = Application::New(&argc, &argv);
208   ExampleController test(application);
209   application.MainLoop();
210   return 0;
211 }