2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali/devel-api/actors/actor-devel.h>
23 #include "shared/view.h"
29 #define MAKE_SHADER(A) #A
31 const char* VERTEX_SHADER = MAKE_SHADER(
32 attribute mediump vec2 aInitPos;
33 attribute mediump vec2 aFinalPos;
34 attribute mediump vec3 aColor;
35 uniform mediump mat4 uMvpMatrix;
36 uniform mediump vec3 uSize;
37 uniform mediump float uDelta;
38 uniform lowp vec4 uColor;
39 varying lowp vec4 vColor;
42 mediump vec4 vertexPosition = vec4(mix(aInitPos, aFinalPos, uDelta), 0.0, 1.0);
43 vertexPosition.xyz *= uSize;
44 vertexPosition = uMvpMatrix * vertexPosition;
45 gl_Position = vertexPosition;
46 vColor = vec4(aColor, 0.) * uColor;
49 const char* FRAGMENT_SHADER = MAKE_SHADER(
50 varying lowp vec4 vColor;
53 gl_FragColor = vColor;
56 Geometry CreateGeometry()
68 VertexPosition quad[] = {
103 {Vector2(-.25, .25)},
111 float bigSide = 0.707106781;
112 float side = bigSide * .5f;
113 // float smallSide = side * .5f;
115 Vector2 pA = Vector2(side, .25);
116 Vector2 pB = pA + Vector2(0., bigSide);
117 Vector2 pC = pB + Vector2(-bigSide, 0.);
118 Vector2 pD = pA + Vector2(-.5, -.5);
119 Vector2 pE = pD + Vector2(.0, 1.);
120 Vector2 pF = pD + Vector2(-side, side);
121 Vector2 pF2 = pD + Vector2(0., bigSide);
122 Vector2 pG = pD + Vector2(-.25, .25);
123 Vector2 pH = pD + Vector2(-.5, .0);
124 Vector2 pI = pD + Vector2(-.25, -.25);
125 Vector2 pJ = pD + Vector2(0., -.5);
126 Vector2 pK = pD + Vector2(-.5, -.5);
127 Vector2 pL = pB + Vector2(0, -side);
128 Vector2 pM = pL + Vector2(side, -side);
129 Vector2 pN = pB + Vector2(side, -side);
131 VertexPosition cat[] = {
174 VertexColor colors[] = {
176 {Vector3(1., 1., 0.)},
177 {Vector3(1., 1., 0.)},
178 {Vector3(1., 1., 0.)},
181 {Vector3(0., 1., 0.)},
182 {Vector3(0., 1., 0.)},
183 {Vector3(0., 1., 0.)},
186 {Vector3(0., 0., 1.)},
187 {Vector3(0., 0., 1.)},
188 {Vector3(0., 0., 1.)},
191 {Vector3(1., 0., 0.)},
192 {Vector3(1., 0., 0.)},
193 {Vector3(1., 0., 0.)},
194 {Vector3(1., 0., 0.)},
195 {Vector3(1., 0., 0.)},
196 {Vector3(1., 0., 0.)},
199 {Vector3(0., 1., 1.)},
200 {Vector3(0., 1., 1.)},
201 {Vector3(0., 1., 1.)},
204 {Vector3(1., 0., 1.)},
205 {Vector3(1., 0., 1.)},
206 {Vector3(1., 0., 1.)},
207 {Vector3(1., 0., 1.)},
208 {Vector3(1., 0., 1.)},
209 {Vector3(1., 0., 1.)},
212 {Vector3(1., 0.5, 0.)},
213 {Vector3(1., 0.5, 0.)},
214 {Vector3(1., 0.5, 0.)},
218 unsigned int numberOfVertices = sizeof(quad) / sizeof(VertexPosition);
220 Property::Map initialPositionVertexFormat;
221 initialPositionVertexFormat["aInitPos"] = Property::VECTOR2;
222 VertexBuffer initialPositionVertices = VertexBuffer::New(initialPositionVertexFormat);
223 initialPositionVertices.SetData(quad, numberOfVertices);
225 Property::Map finalPositionVertexFormat;
226 finalPositionVertexFormat["aFinalPos"] = Property::VECTOR2;
227 VertexBuffer finalPositionVertices = VertexBuffer::New(finalPositionVertexFormat);
228 finalPositionVertices.SetData(cat, numberOfVertices);
230 Property::Map colorVertexFormat;
231 colorVertexFormat["aColor"] = Property::VECTOR3;
232 VertexBuffer colorVertices = VertexBuffer::New(colorVertexFormat);
233 colorVertices.SetData(colors, numberOfVertices);
235 // Create the geometry object
236 Geometry texturedQuadGeometry = Geometry::New();
237 texturedQuadGeometry.AddVertexBuffer(initialPositionVertices);
238 texturedQuadGeometry.AddVertexBuffer(finalPositionVertices);
239 texturedQuadGeometry.AddVertexBuffer(colorVertices);
241 return texturedQuadGeometry;
244 inline float StationarySin(float progress) ///< Single revolution
246 float val = cosf(progress * 2.0f * Math::PI) + .5f;
247 val = val > 1.f ? 1.f : val;
248 val = val < 0.f ? 0.f : val;
252 } // anonymous namespace
254 // This example shows how to use a simple mesh
256 class ExampleController : public ConnectionTracker
260 * The example controller constructor.
261 * @param[in] application The application instance
263 ExampleController(Application& application)
264 : mApplication(application)
266 // Connect to the Application's Init signal
267 mApplication.InitSignal().Connect(this, &ExampleController::Create);
271 * The example controller destructor
275 // Nothing to do here;
279 * Invoked upon creation of application
280 * @param[in] application The application instance
282 void Create(Application& application)
284 Window window = application.GetWindow();
285 window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
287 mWindowSize = window.GetSize();
289 // The Init signal is received once (only) during the Application lifetime
291 mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER);
292 mGeometry = CreateGeometry();
293 mRenderer = Renderer::New(mGeometry, mShader);
295 mMeshActor = Actor::New();
296 mMeshActor.AddRenderer(mRenderer);
297 mMeshActor.SetProperty(Actor::Property::SIZE, Vector2(400, 400));
298 mMeshActor.SetProperty(DevelActor::Property::UPDATE_SIZE_HINT, Vector2(480, 700));
300 Property::Index morphDeltaIndex = mMeshActor.RegisterProperty("uDelta", 0.f);
302 mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
304 mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
305 mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
306 window.Add(mMeshActor);
308 Animation animation = Animation::New(10);
309 animation.AnimateTo(Property(mMeshActor, morphDeltaIndex), 1.f, StationarySin);
310 animation.SetLooping(true);
313 window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
317 * Invoked whenever the quit button is clicked
318 * @param[in] button the quit button
320 bool OnQuitButtonClicked(Toolkit::Button button)
322 // quit the application
327 void OnKeyEvent(const KeyEvent& event)
329 if(event.GetState() == KeyEvent::DOWN)
331 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
339 Application& mApplication; ///< Application instance
340 Vector3 mWindowSize; ///< The size of the window
349 int DALI_EXPORT_API main(int argc, char** argv)
351 Application application = Application::New(&argc, &argv);
352 ExampleController test(application);
353 application.MainLoop();