2 * Copyright (c) 2017 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>
22 #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;
43 mediump vec4 vertexPosition = vec4(mix(aInitPos, aFinalPos, uDelta), 0.0, 1.0);
44 vertexPosition.xyz *= uSize;
45 vertexPosition = uMvpMatrix * vertexPosition;
46 gl_Position = vertexPosition;
47 vColor = vec4(aColor, 0.) * uColor;
51 const char* FRAGMENT_SHADER = MAKE_SHADER(
52 varying lowp vec4 vColor;
56 gl_FragColor = vColor;
60 Geometry CreateGeometry()
63 struct VertexPosition { Vector2 position; };
64 struct VertexColor { Vector3 color; };
66 VertexPosition quad[] = {
68 { Vector2(-.5, -.5) },
73 { Vector2(-.5, -.5) },
74 { Vector2( .5, -.5) },
80 { Vector2(.25, -.25) },
83 { Vector2(.25, -.25) },
85 { Vector2(.25, .25) },
86 { Vector2(.25, .25) },
88 { Vector2(.25, -.25) },
92 { Vector2( .25, .25) },
93 { Vector2(-.25, .25) },
96 { Vector2(-.25, .25) },
97 { Vector2( .25, .25) },
100 { Vector2(-.5, .5) },
101 { Vector2(-.25, .25) },
104 { Vector2( .5, .0) },
105 { Vector2( .5, .5) },
106 { Vector2( .0, .5) },
109 float bigSide = 0.707106781;
110 float side = bigSide * .5f;
111 // float smallSide = side * .5f;
113 Vector2 pA = Vector2( side, .25 );
114 Vector2 pB = pA + Vector2( 0., bigSide );
115 Vector2 pC = pB + Vector2( -bigSide, 0. );
116 Vector2 pD = pA + Vector2(-.5, -.5 );
117 Vector2 pE = pD + Vector2( .0, 1. );
118 Vector2 pF = pD + Vector2(-side, side );
119 Vector2 pF2 = pD + Vector2( 0., bigSide );
120 Vector2 pG = pD + Vector2(-.25, .25 );
121 Vector2 pH = pD + Vector2( -.5, .0 );
122 Vector2 pI = pD + Vector2(-.25, -.25 );
123 Vector2 pJ = pD + Vector2( 0., -.5);
124 Vector2 pK = pD + Vector2(-.5, -.5);
125 Vector2 pL = pB + Vector2(0, -side);
126 Vector2 pM = pL + Vector2(side, -side);
127 Vector2 pN = pB + Vector2(side, -side);
129 VertexPosition cat[] = {
172 VertexColor colors[] = {
174 { Vector3( 1., 1., 0. ) },
175 { Vector3( 1., 1., 0. ) },
176 { Vector3( 1., 1., 0. ) },
179 { Vector3( 0., 1., 0. ) },
180 { Vector3( 0., 1., 0. ) },
181 { Vector3( 0., 1., 0. ) },
184 { Vector3( 0., 0., 1. ) },
185 { Vector3( 0., 0., 1. ) },
186 { Vector3( 0., 0., 1. ) },
189 { Vector3( 1., 0., 0. ) },
190 { Vector3( 1., 0., 0. ) },
191 { Vector3( 1., 0., 0. ) },
192 { Vector3( 1., 0., 0. ) },
193 { Vector3( 1., 0., 0. ) },
194 { Vector3( 1., 0., 0. ) },
197 { Vector3( 0., 1., 1. ) },
198 { Vector3( 0., 1., 1. ) },
199 { Vector3( 0., 1., 1. ) },
202 { Vector3( 1., 0., 1. ) },
203 { Vector3( 1., 0., 1. ) },
204 { Vector3( 1., 0., 1. ) },
205 { Vector3( 1., 0., 1. ) },
206 { Vector3( 1., 0., 1. ) },
207 { Vector3( 1., 0., 1. ) },
210 { Vector3( 1., 0.5, 0. ) },
211 { Vector3( 1., 0.5, 0. ) },
212 { Vector3( 1., 0.5, 0. ) },
216 unsigned int numberOfVertices = sizeof(quad)/sizeof(VertexPosition);
218 Property::Map initialPositionVertexFormat;
219 initialPositionVertexFormat["aInitPos"] = Property::VECTOR2;
220 PropertyBuffer initialPositionVertices = PropertyBuffer::New( initialPositionVertexFormat );
221 initialPositionVertices.SetData( quad, numberOfVertices );
223 Property::Map finalPositionVertexFormat;
224 finalPositionVertexFormat["aFinalPos"] = Property::VECTOR2;
225 PropertyBuffer finalPositionVertices = PropertyBuffer::New( finalPositionVertexFormat );
226 finalPositionVertices.SetData( cat, numberOfVertices );
228 Property::Map colorVertexFormat;
229 colorVertexFormat["aColor"] = Property::VECTOR3;
230 PropertyBuffer colorVertices = PropertyBuffer::New( colorVertexFormat );
231 colorVertices.SetData( colors, numberOfVertices );
233 // Create the geometry object
234 Geometry texturedQuadGeometry = Geometry::New();
235 texturedQuadGeometry.AddVertexBuffer( initialPositionVertices );
236 texturedQuadGeometry.AddVertexBuffer( finalPositionVertices );
237 texturedQuadGeometry.AddVertexBuffer( colorVertices );
239 return texturedQuadGeometry;
242 inline float StationarySin( float progress ) ///< Single revolution
244 float val = cosf(progress * 2.0f * Math::PI) + .5f;
245 val = val > 1.f ? 1.f : val;
246 val = val < 0.f ? 0.f : val;
250 } // anonymous namespace
252 // This example shows how to use a simple mesh
254 class ExampleController : public ConnectionTracker
259 * The example controller constructor.
260 * @param[in] application The application instance
262 ExampleController( Application& application )
263 : mApplication( application )
265 // Connect to the Application's Init signal
266 mApplication.InitSignal().Connect( this, &ExampleController::Create );
270 * The example controller destructor
274 // Nothing to do here;
278 * Invoked upon creation of application
279 * @param[in] application The application instance
281 void Create( Application& application )
283 Stage stage = Stage::GetCurrent();
284 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
286 mStageSize = stage.GetSize();
288 // The Init signal is received once (only) during the Application lifetime
290 // Hide the indicator bar
291 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
293 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
294 mGeometry = CreateGeometry();
295 mRenderer = Renderer::New( mGeometry, mShader );
297 mMeshActor = Actor::New();
298 mMeshActor.AddRenderer( mRenderer );
299 mMeshActor.SetSize(400, 400);
301 Property::Index morphDeltaIndex = mMeshActor.RegisterProperty( "uDelta", 0.f );
303 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
305 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
306 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
307 stage.Add( mMeshActor );
309 Animation animation = Animation::New(10);
310 animation.AnimateTo( Property( mMeshActor, morphDeltaIndex ), 1.f, StationarySin );
311 animation.SetLooping( true );
314 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
318 * Invoked whenever the quit button is clicked
319 * @param[in] button the quit button
321 bool OnQuitButtonClicked( Toolkit::Button button )
323 // quit the application
328 void OnKeyEvent(const KeyEvent& event)
330 if(event.state == KeyEvent::Down)
332 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
341 Application& mApplication; ///< Application instance
342 Vector3 mStageSize; ///< The size of the stage
351 int DALI_EXPORT_API main( int argc, char **argv )
353 Application application = Application::New( &argc, &argv );
354 ExampleController test( application );
355 application.MainLoop();