2 * Copyright (c) 2016 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/public-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
23 #include "shared/view.h"
30 #define MAKE_SHADER(A)#A
32 const char* VERTEX_SHADER = MAKE_SHADER(
33 attribute mediump vec2 aInitPos;
34 attribute mediump vec2 aFinalPos;
35 attribute mediump vec3 aColor;
36 uniform mediump mat4 uMvpMatrix;
37 uniform mediump vec3 uSize;
38 uniform mediump float uDelta;
39 uniform lowp vec4 uColor;
40 varying lowp vec4 vColor;
44 mediump vec4 vertexPosition = vec4(mix(aInitPos, aFinalPos, uDelta), 0.0, 1.0);
45 vertexPosition.xyz *= uSize;
46 vertexPosition = uMvpMatrix * vertexPosition;
47 gl_Position = vertexPosition;
48 vColor = vec4(aColor, 0.) * uColor;
52 const char* FRAGMENT_SHADER = MAKE_SHADER(
53 varying lowp vec4 vColor;
57 gl_FragColor = vColor;
61 Geometry CreateGeometry()
64 struct VertexPosition { Vector2 position; };
65 struct VertexColor { Vector3 color; };
67 VertexPosition quad[] = {
69 { Vector2(-.5, -.5) },
74 { Vector2(-.5, -.5) },
75 { Vector2( .5, -.5) },
81 { Vector2(.25, -.25) },
84 { Vector2(.25, -.25) },
86 { Vector2(.25, .25) },
87 { Vector2(.25, .25) },
89 { Vector2(.25, -.25) },
93 { Vector2( .25, .25) },
94 { Vector2(-.25, .25) },
97 { Vector2(-.25, .25) },
98 { Vector2( .25, .25) },
100 { Vector2( .0, .5) },
101 { Vector2(-.5, .5) },
102 { Vector2(-.25, .25) },
105 { Vector2( .5, .0) },
106 { Vector2( .5, .5) },
107 { Vector2( .0, .5) },
110 float bigSide = 0.707106781;
111 float side = bigSide * .5f;
112 // float smallSide = side * .5f;
114 Vector2 pA = Vector2( side, .25 );
115 Vector2 pB = pA + Vector2( 0., bigSide );
116 Vector2 pC = pB + Vector2( -bigSide, 0. );
117 Vector2 pD = pA + Vector2(-.5, -.5 );
118 Vector2 pE = pD + Vector2( .0, 1. );
119 Vector2 pF = pD + Vector2(-side, side );
120 Vector2 pF2 = pD + Vector2( 0., bigSide );
121 Vector2 pG = pD + Vector2(-.25, .25 );
122 Vector2 pH = pD + Vector2( -.5, .0 );
123 Vector2 pI = pD + Vector2(-.25, -.25 );
124 Vector2 pJ = pD + Vector2( 0., -.5);
125 Vector2 pK = pD + Vector2(-.5, -.5);
126 Vector2 pL = pB + Vector2(0, -side);
127 Vector2 pM = pL + Vector2(side, -side);
128 Vector2 pN = pB + Vector2(side, -side);
130 VertexPosition cat[] = {
173 VertexColor colors[] = {
175 { Vector3( 1., 1., 0. ) },
176 { Vector3( 1., 1., 0. ) },
177 { Vector3( 1., 1., 0. ) },
180 { Vector3( 0., 1., 0. ) },
181 { Vector3( 0., 1., 0. ) },
182 { Vector3( 0., 1., 0. ) },
185 { Vector3( 0., 0., 1. ) },
186 { Vector3( 0., 0., 1. ) },
187 { Vector3( 0., 0., 1. ) },
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. ) },
195 { Vector3( 1., 0., 0. ) },
198 { Vector3( 0., 1., 1. ) },
199 { Vector3( 0., 1., 1. ) },
200 { Vector3( 0., 1., 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. ) },
208 { Vector3( 1., 0., 1. ) },
211 { Vector3( 1., 0.5, 0. ) },
212 { Vector3( 1., 0.5, 0. ) },
213 { Vector3( 1., 0.5, 0. ) },
217 unsigned int numberOfVertices = sizeof(quad)/sizeof(VertexPosition);
219 Property::Map initialPositionVertexFormat;
220 initialPositionVertexFormat["aInitPos"] = Property::VECTOR2;
221 PropertyBuffer initialPositionVertices = PropertyBuffer::New( initialPositionVertexFormat );
222 initialPositionVertices.SetData( quad, numberOfVertices );
224 Property::Map finalPositionVertexFormat;
225 finalPositionVertexFormat["aFinalPos"] = Property::VECTOR2;
226 PropertyBuffer finalPositionVertices = PropertyBuffer::New( finalPositionVertexFormat );
227 finalPositionVertices.SetData( cat, numberOfVertices );
229 Property::Map colorVertexFormat;
230 colorVertexFormat["aColor"] = Property::VECTOR3;
231 PropertyBuffer colorVertices = PropertyBuffer::New( colorVertexFormat );
232 colorVertices.SetData( colors, numberOfVertices );
234 // Create the geometry object
235 Geometry texturedQuadGeometry = Geometry::New();
236 texturedQuadGeometry.AddVertexBuffer( initialPositionVertices );
237 texturedQuadGeometry.AddVertexBuffer( finalPositionVertices );
238 texturedQuadGeometry.AddVertexBuffer( colorVertices );
240 return texturedQuadGeometry;
243 inline float StationarySin( float progress ) ///< Single revolution
245 float val = cosf(progress * 2.0f * Math::PI) + .5f;
246 val = val > 1.f ? 1.f : val;
247 val = val < 0.f ? 0.f : val;
251 } // anonymous namespace
253 // This example shows how to use a simple mesh
255 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 Stage stage = Stage::GetCurrent();
285 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
287 mStageSize = stage.GetSize();
289 // The Init signal is received once (only) during the Application lifetime
291 // Hide the indicator bar
292 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
294 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
295 mGeometry = CreateGeometry();
296 mRenderer = Renderer::New( mGeometry, mShader );
298 mMeshActor = Actor::New();
299 mMeshActor.AddRenderer( mRenderer );
300 mMeshActor.SetSize(400, 400);
302 Property::Index morphDeltaIndex = mMeshActor.RegisterProperty( "uDelta", 0.f );
304 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
306 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
307 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
308 stage.Add( mMeshActor );
310 Animation animation = Animation::New(10);
311 animation.AnimateTo( Property( mMeshActor, morphDeltaIndex ), 1.f, StationarySin );
312 animation.SetLooping( true );
315 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
319 * Invoked whenever the quit button is clicked
320 * @param[in] button the quit button
322 bool OnQuitButtonClicked( Toolkit::Button button )
324 // quit the application
329 void OnKeyEvent(const KeyEvent& event)
331 if(event.state == KeyEvent::Down)
333 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
342 Application& mApplication; ///< Application instance
343 Vector3 mStageSize; ///< The size of the stage
352 void RunTest( Application& application )
354 ExampleController test( application );
356 application.MainLoop();
359 // Entry point for Linux & SLP applications
361 int DALI_EXPORT_API main( int argc, char **argv )
363 Application application = Application::New( &argc, &argv );
365 RunTest( application );