Convert shaders in dali-demo to use shader compilation tool
[platform/core/uifw/dali-demo.git] / examples / point-mesh / point-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/point-mesh-vert.h"
25 #include "generated/point-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 Geometry CreateGeometry()
35 {
36   // Create vertices
37   struct Vertex
38   {
39     Vector2 position;
40     float   hue;
41   };
42
43   const unsigned int numSides = 20;
44   Vertex             polyhedraVertexData[numSides];
45   float              angle       = 0;
46   float              sectorAngle = 2.0f * Math::PI / (float)numSides;
47
48   for(unsigned int i = 0; i < numSides; ++i)
49   {
50     polyhedraVertexData[i].position.x = sinf(angle) * 0.5f;
51     polyhedraVertexData[i].position.y = cosf(angle) * 0.5f;
52     polyhedraVertexData[i].hue        = angle / (2.0f * Math::PI);
53     angle += sectorAngle;
54   }
55
56   Property::Map polyhedraVertexFormat;
57   polyhedraVertexFormat["aPosition"] = Property::VECTOR2;
58   polyhedraVertexFormat["aHue"]      = Property::FLOAT;
59   VertexBuffer polyhedraVertices     = VertexBuffer::New(polyhedraVertexFormat);
60   polyhedraVertices.SetData(polyhedraVertexData, numSides);
61
62   // Create the geometry object
63   Geometry polyhedraGeometry = Geometry::New();
64   polyhedraGeometry.AddVertexBuffer(polyhedraVertices);
65   polyhedraGeometry.SetType(Geometry::POINTS);
66
67   return polyhedraGeometry;
68 }
69
70 } // anonymous namespace
71
72 // This example shows how to use a simple mesh
73 //
74 class ExampleController : public ConnectionTracker
75 {
76 public:
77   /**
78    * The example controller constructor.
79    * @param[in] application The application instance
80    */
81   ExampleController(Application& application)
82   : mApplication(application)
83   {
84     // Connect to the Application's Init signal
85     mApplication.InitSignal().Connect(this, &ExampleController::Create);
86   }
87
88   /**
89    * The example controller destructor
90    */
91   ~ExampleController()
92   {
93     // Nothing to do here;
94   }
95
96   /**
97    * Invoked upon creation of application
98    * @param[in] application The application instance
99    */
100   void Create(Application& application)
101   {
102     Window window = application.GetWindow();
103     window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
104
105     mWindowSize = window.GetSize();
106
107     // The Init signal is received once (only) during the Application lifetime
108
109     Texture texture0 = DemoHelper::LoadTexture(MATERIAL_SAMPLE);
110     Texture texture1 = DemoHelper::LoadTexture(MATERIAL_SAMPLE2);
111
112     Shader shader = Shader::New(SHADER_POINT_MESH_VERT, SHADER_POINT_MESH_FRAG);
113
114     TextureSet textureSet = TextureSet::New();
115     textureSet.SetTexture(0u, texture0);
116     textureSet.SetTexture(1u, texture1);
117
118     Geometry geometry = CreateGeometry();
119
120     mRenderer = Renderer::New(geometry, shader);
121     mRenderer.SetTextures(textureSet);
122
123     mMeshActor = Actor::New();
124     mMeshActor.AddRenderer(mRenderer);
125     mMeshActor.SetProperty(Actor::Property::SIZE, Vector2(400, 400));
126
127     mMeshActor.RegisterProperty("uFadeColor", Color::GREEN);
128
129     mRenderer.RegisterProperty("uFadeColor", Color::MAGENTA);
130     mRenderer.RegisterProperty("uPointSize", 80.0f);
131     mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
132
133     mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
134     mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
135     window.Add(mMeshActor);
136
137     Animation animation = Animation::New(15);
138     KeyFrames keyFrames = KeyFrames::New();
139     keyFrames.Add(0.0f, Vector4::ZERO);
140     keyFrames.Add(1.0f, Vector4(1.0f, 0.0f, 1.0f, 1.0f));
141
142     animation.AnimateBy(Property(mMeshActor, Actor::Property::ORIENTATION), Quaternion(Degree(360), Vector3::ZAXIS));
143
144     animation.SetLooping(true);
145     animation.Play();
146
147     window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
148   }
149
150   /**
151    * Invoked whenever the quit button is clicked
152    * @param[in] button the quit button
153    */
154   bool OnQuitButtonClicked(Toolkit::Button button)
155   {
156     // quit the application
157     mApplication.Quit();
158     return true;
159   }
160
161   void OnKeyEvent(const KeyEvent& event)
162   {
163     if(event.GetState() == KeyEvent::DOWN)
164     {
165       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
166       {
167         mApplication.Quit();
168       }
169     }
170   }
171
172 private:
173   Application& mApplication; ///< Application instance
174   Vector3      mWindowSize;  ///< The size of the window
175
176   Renderer mRenderer;
177   Actor    mMeshActor;
178   Renderer mRenderer2;
179   Actor    mMeshActor2;
180   Timer    mChangeImageTimer;
181 };
182
183 int DALI_EXPORT_API main(int argc, char** argv)
184 {
185   Application       application = Application::New(&argc, &argv);
186   ExampleController test(application);
187   application.MainLoop();
188   return 0;
189 }