Updated demos to use DALi clang-format
[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
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 float  aHue;
37   varying mediump vec2   vTexCoord;
38   uniform mediump mat4   uMvpMatrix;
39   uniform mediump vec3   uSize;
40   uniform mediump float  uPointSize;
41   uniform lowp vec4      uFadeColor;
42   varying mediump vec3   vVertexColor;
43   varying mediump float  vHue;
44
45   vec3 hsv2rgb(vec3 c) {
46     vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
47     vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
48     return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
49   }
50
51   void main() {
52     mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
53     vertexPosition.xyz *= (uSize - uPointSize);
54     vertexPosition = uMvpMatrix * vertexPosition;
55     vVertexColor   = hsv2rgb(vec3(aHue, 0.7, 1.0));
56     vHue           = aHue;
57     gl_PointSize   = uPointSize;
58     gl_Position    = vertexPosition;
59   });
60
61 const char* FRAGMENT_SHADER = MAKE_SHADER(
62   varying mediump vec3  vVertexColor;
63   varying mediump float vHue;
64   uniform lowp vec4     uColor;
65   uniform sampler2D     sTexture1;
66   uniform sampler2D     sTexture2;
67   uniform lowp vec4     uFadeColor;
68
69   void main() {
70     mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord);
71     mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord);
72     gl_FragColor         = vec4(vVertexColor, 1.0) * ((texCol1 * vHue) + (texCol2 * (1.0 - vHue)));
73   });
74
75 Geometry CreateGeometry()
76 {
77   // Create vertices
78   struct Vertex
79   {
80     Vector2 position;
81     float   hue;
82   };
83
84   const unsigned int numSides = 20;
85   Vertex             polyhedraVertexData[numSides];
86   float              angle       = 0;
87   float              sectorAngle = 2.0f * Math::PI / (float)numSides;
88
89   for(unsigned int i = 0; i < numSides; ++i)
90   {
91     polyhedraVertexData[i].position.x = sinf(angle) * 0.5f;
92     polyhedraVertexData[i].position.y = cosf(angle) * 0.5f;
93     polyhedraVertexData[i].hue        = angle / (2.0f * Math::PI);
94     angle += sectorAngle;
95   }
96
97   Property::Map polyhedraVertexFormat;
98   polyhedraVertexFormat["aPosition"] = Property::VECTOR2;
99   polyhedraVertexFormat["aHue"]      = Property::FLOAT;
100   VertexBuffer polyhedraVertices     = VertexBuffer::New(polyhedraVertexFormat);
101   polyhedraVertices.SetData(polyhedraVertexData, numSides);
102
103   // Create the geometry object
104   Geometry polyhedraGeometry = Geometry::New();
105   polyhedraGeometry.AddVertexBuffer(polyhedraVertices);
106   polyhedraGeometry.SetType(Geometry::POINTS);
107
108   return polyhedraGeometry;
109 }
110
111 } // anonymous namespace
112
113 // This example shows how to use a simple mesh
114 //
115 class ExampleController : public ConnectionTracker
116 {
117 public:
118   /**
119    * The example controller constructor.
120    * @param[in] application The application instance
121    */
122   ExampleController(Application& application)
123   : mApplication(application)
124   {
125     // Connect to the Application's Init signal
126     mApplication.InitSignal().Connect(this, &ExampleController::Create);
127   }
128
129   /**
130    * The example controller destructor
131    */
132   ~ExampleController()
133   {
134     // Nothing to do here;
135   }
136
137   /**
138    * Invoked upon creation of application
139    * @param[in] application The application instance
140    */
141   void Create(Application& application)
142   {
143     Window window = application.GetWindow();
144     window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
145
146     mWindowSize = window.GetSize();
147
148     // The Init signal is received once (only) during the Application lifetime
149
150     Texture texture0 = DemoHelper::LoadTexture(MATERIAL_SAMPLE);
151     Texture texture1 = DemoHelper::LoadTexture(MATERIAL_SAMPLE2);
152
153     Shader shader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER);
154
155     TextureSet textureSet = TextureSet::New();
156     textureSet.SetTexture(0u, texture0);
157     textureSet.SetTexture(1u, texture1);
158
159     Geometry geometry = CreateGeometry();
160
161     mRenderer = Renderer::New(geometry, shader);
162     mRenderer.SetTextures(textureSet);
163
164     mMeshActor = Actor::New();
165     mMeshActor.AddRenderer(mRenderer);
166     mMeshActor.SetProperty(Actor::Property::SIZE, Vector2(400, 400));
167
168     mMeshActor.RegisterProperty("uFadeColor", Color::GREEN);
169
170     mRenderer.RegisterProperty("uFadeColor", Color::MAGENTA);
171     mRenderer.RegisterProperty("uPointSize", 80.0f);
172     mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
173
174     mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
175     mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
176     window.Add(mMeshActor);
177
178     Animation animation = Animation::New(15);
179     KeyFrames keyFrames = KeyFrames::New();
180     keyFrames.Add(0.0f, Vector4::ZERO);
181     keyFrames.Add(1.0f, Vector4(1.0f, 0.0f, 1.0f, 1.0f));
182
183     animation.AnimateBy(Property(mMeshActor, Actor::Property::ORIENTATION), Quaternion(Degree(360), Vector3::ZAXIS));
184
185     animation.SetLooping(true);
186     animation.Play();
187
188     window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
189   }
190
191   /**
192    * Invoked whenever the quit button is clicked
193    * @param[in] button the quit button
194    */
195   bool OnQuitButtonClicked(Toolkit::Button button)
196   {
197     // quit the application
198     mApplication.Quit();
199     return true;
200   }
201
202   void OnKeyEvent(const KeyEvent& event)
203   {
204     if(event.GetState() == KeyEvent::DOWN)
205     {
206       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
207       {
208         mApplication.Quit();
209       }
210     }
211   }
212
213 private:
214   Application& mApplication; ///< Application instance
215   Vector3      mWindowSize;  ///< The size of the window
216
217   Renderer mRenderer;
218   Actor    mMeshActor;
219   Renderer mRenderer2;
220   Actor    mMeshActor2;
221   Timer    mChangeImageTimer;
222 };
223
224 int DALI_EXPORT_API main(int argc, char** argv)
225 {
226   Application       application = Application::New(&argc, &argv);
227   ExampleController test(application);
228   application.MainLoop();
229   return 0;
230 }