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