2 * Copyright (c) 2015 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/devel-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 aPosition1;
34 attribute mediump vec2 aPosition2;
35 uniform mediump mat4 uMvpMatrix;
36 uniform mediump vec3 uSize;
37 uniform mediump float uMorphAmount;
41 mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
42 mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
43 vertexPosition.xyz *= uSize;
44 vertexPosition = uMvpMatrix * vertexPosition;
45 gl_Position = vertexPosition;
49 const char* FRAGMENT_SHADER = MAKE_SHADER(
50 uniform lowp vec4 uColor;
51 uniform sampler2D sTexture;
55 gl_FragColor = uColor;
59 Geometry CreateGeometry()
62 struct Vertex { Vector2 position; };
63 Vertex pentagonVertexData[5] =
65 { Vector2( 0.0f, 1.00f) }, // 0
66 { Vector2( -0.95f, 0.31f) }, // 1
67 { Vector2( -0.59f, -0.81f) }, // 2
68 { Vector2( 0.59f, -0.81f) }, // 3
69 { Vector2( 0.95f, 0.31f) }, // 4
72 Vertex pentacleVertexData[5] =
74 { Vector2( 0.0f, -1.00f) }, //
75 { Vector2( 0.59f, 0.81f) }, //
76 { Vector2( -0.95f, -0.31f) }, //
77 { Vector2( 0.95f, -0.31f) }, //
78 { Vector2( -0.59f, 0.81f) }, //
81 Property::Map pentagonVertexFormat;
82 pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
83 PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat );
84 pentagonVertices.SetData(pentagonVertexData, 5);
86 Property::Map pentacleVertexFormat;
87 pentacleVertexFormat["aPosition2"] = Property::VECTOR2;
88 PropertyBuffer pentacleVertices = PropertyBuffer::New( pentacleVertexFormat );
89 pentacleVertices.SetData( pentacleVertexData, 5 );
92 unsigned int indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
93 Property::Map indexFormat;
94 indexFormat["indices"] = Property::INTEGER;
95 PropertyBuffer indices = PropertyBuffer::New( indexFormat );
96 indices.SetData( indexData, sizeof(indexData)/sizeof(indexData[0]) );
98 // Create the geometry object
99 Geometry pentagonGeometry = Geometry::New();
100 pentagonGeometry.AddVertexBuffer( pentagonVertices );
101 pentagonGeometry.AddVertexBuffer( pentacleVertices );
102 pentagonGeometry.SetIndexBuffer( indices );
104 pentagonGeometry.SetGeometryType( Geometry::LINES );
106 return pentagonGeometry;
109 } // anonymous namespace
111 // This example shows how to morph between 2 meshes with the same number of
113 class ExampleController : public ConnectionTracker
118 * The example controller constructor.
119 * @param[in] application The application instance
121 ExampleController( Application& application )
122 : mApplication( application )
124 // Connect to the Application's Init signal
125 mApplication.InitSignal().Connect( this, &ExampleController::Create );
129 * The example controller destructor
133 // Nothing to do here;
137 * Invoked upon creation of application
138 * @param[in] application The application instance
140 void Create( Application& application )
142 Stage stage = Stage::GetCurrent();
143 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
145 mStageSize = stage.GetSize();
147 // The Init signal is received once (only) during the Application lifetime
149 // Hide the indicator bar
150 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
152 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
154 mMaterial = Material::New( mShader );
156 mGeometry = CreateGeometry();
158 mRenderer = Renderer::New( mGeometry, mMaterial );
160 mMeshActor = Actor::New();
161 mMeshActor.AddRenderer( mRenderer );
162 mMeshActor.SetSize(200, 200);
164 Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
166 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
168 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
169 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
170 stage.Add( mMeshActor );
172 Animation animation = Animation::New(5);
173 KeyFrames keyFrames = KeyFrames::New();
174 keyFrames.Add(0.0f, 0.0f);
175 keyFrames.Add(1.0f, 1.0f);
177 animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
178 animation.SetLooping(true);
181 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
185 * Invoked whenever the quit button is clicked
186 * @param[in] button the quit button
188 bool OnQuitButtonClicked( Toolkit::Button button )
190 // quit the application
195 void OnKeyEvent(const KeyEvent& event)
197 if(event.state == KeyEvent::Down)
199 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
208 Application& mApplication; ///< Application instance
209 Vector3 mStageSize; ///< The size of the stage
218 void RunTest( Application& application )
220 ExampleController test( application );
222 application.MainLoop();
225 // Entry point for Linux & SLP applications
227 int main( int argc, char **argv )
229 Application application = Application::New( &argc, &argv );
231 RunTest( application );