[dali_1.1.30] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / line-mesh / line-mesh-example.cpp
1 /*
2  * Copyright (c) 2015 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/devel-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 // INTERNAL INCLUDES
23 #include "shared/view.h"
24
25 using namespace Dali;
26
27 namespace
28 {
29
30 #define MAKE_SHADER(A)#A
31
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;
38
39 void main()
40 {
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;
46 }
47 );
48
49 const char* FRAGMENT_SHADER = MAKE_SHADER(
50 uniform lowp  vec4    uColor;
51 uniform sampler2D     sTexture;
52
53 void main()
54 {
55   gl_FragColor = uColor;
56 }
57 );
58
59 Geometry CreateGeometry()
60 {
61   // Create vertices
62   struct Vertex { Vector2 position; };
63   Vertex pentagonVertexData[5] =
64     {
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
70     };
71
72   Vertex pentacleVertexData[5] =
73     {
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) }, //
79     };
80
81   Property::Map pentagonVertexFormat;
82   pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
83   PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat );
84   pentagonVertices.SetData(pentagonVertexData, 5);
85
86   Property::Map pentacleVertexFormat;
87   pentacleVertexFormat["aPosition2"] = Property::VECTOR2;
88   PropertyBuffer pentacleVertices = PropertyBuffer::New( pentacleVertexFormat );
89   pentacleVertices.SetData( pentacleVertexData, 5 );
90
91   // Create indices
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]) );
97
98   // Create the geometry object
99   Geometry pentagonGeometry = Geometry::New();
100   pentagonGeometry.AddVertexBuffer( pentagonVertices );
101   pentagonGeometry.AddVertexBuffer( pentacleVertices );
102   pentagonGeometry.SetIndexBuffer( indices );
103
104   pentagonGeometry.SetGeometryType( Geometry::LINES );
105
106   return pentagonGeometry;
107 }
108
109 } // anonymous namespace
110
111 // This example shows how to morph between 2 meshes with the same number of
112 // vertices.
113 class ExampleController : public ConnectionTracker
114 {
115 public:
116
117   /**
118    * The example controller constructor.
119    * @param[in] application The application instance
120    */
121   ExampleController( Application& application )
122   : mApplication( application )
123   {
124     // Connect to the Application's Init signal
125     mApplication.InitSignal().Connect( this, &ExampleController::Create );
126   }
127
128   /**
129    * The example controller destructor
130    */
131   ~ExampleController()
132   {
133     // Nothing to do here;
134   }
135
136   /**
137    * Invoked upon creation of application
138    * @param[in] application The application instance
139    */
140   void Create( Application& application )
141   {
142     Stage stage = Stage::GetCurrent();
143     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
144
145     mStageSize = stage.GetSize();
146
147     // The Init signal is received once (only) during the Application lifetime
148
149     // Hide the indicator bar
150     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
151
152     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
153
154     mMaterial = Material::New( mShader );
155
156     mGeometry = CreateGeometry();
157
158     mRenderer = Renderer::New( mGeometry, mMaterial );
159
160     mMeshActor = Actor::New();
161     mMeshActor.AddRenderer( mRenderer );
162     mMeshActor.SetSize(200, 200);
163
164     Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
165
166     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
167
168     mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
169     mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
170     stage.Add( mMeshActor );
171
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);
176
177     animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
178     animation.SetLooping(true);
179     animation.Play();
180
181     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
182   }
183
184   /**
185    * Invoked whenever the quit button is clicked
186    * @param[in] button the quit button
187    */
188   bool OnQuitButtonClicked( Toolkit::Button button )
189   {
190     // quit the application
191     mApplication.Quit();
192     return true;
193   }
194
195   void OnKeyEvent(const KeyEvent& event)
196   {
197     if(event.state == KeyEvent::Down)
198     {
199       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
200       {
201         mApplication.Quit();
202       }
203     }
204   }
205
206 private:
207
208   Application&  mApplication;                             ///< Application instance
209   Vector3 mStageSize;                                     ///< The size of the stage
210
211   Shader   mShader;
212   Material mMaterial;
213   Geometry mGeometry;
214   Renderer mRenderer;
215   Actor    mMeshActor;
216 };
217
218 void RunTest( Application& application )
219 {
220   ExampleController test( application );
221
222   application.MainLoop();
223 }
224
225 // Entry point for Linux & SLP applications
226 //
227 int main( int argc, char **argv )
228 {
229   Application application = Application::New( &argc, &argv );
230
231   RunTest( application );
232
233   return 0;
234 }