New point and line demos
[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
20 // INTERNAL INCLUDES
21 #include "shared/view.h"
22
23 #include <dali-toolkit/dali-toolkit.h>
24
25 using namespace Dali;
26
27 namespace
28 {
29 const char* MATERIAL_SAMPLE( DALI_IMAGE_DIR "gallery-small-48.jpg" );
30 const char* MATERIAL_SAMPLE2( DALI_IMAGE_DIR "gallery-medium-19.jpg" );
31
32 #define MAKE_SHADER(A)#A
33
34 const char* VERTEX_SHADER = MAKE_SHADER(
35 attribute mediump vec2    aPosition1;
36 attribute mediump vec2    aPosition2;
37 uniform   mediump mat4    uMvpMatrix;
38 uniform   mediump vec3    uSize;
39 uniform   mediump float   uMorphAmount;
40
41 void main()
42 {
43   mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
44   mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
45   vertexPosition.xyz *= uSize;
46   vertexPosition = uMvpMatrix * vertexPosition;
47   gl_Position = vertexPosition;
48 }
49 );
50
51 const char* FRAGMENT_SHADER = MAKE_SHADER(
52 uniform lowp  vec4    uColor;
53 uniform sampler2D     sTexture;
54
55 void main()
56 {
57   gl_FragColor = uColor;
58 }
59 );
60
61 Geometry CreateGeometry()
62 {
63   // Create vertices
64   struct Vertex { Vector2 position; };
65   Vertex pentagonVertexData[5] =
66     {
67       { Vector2(  0.0f,   1.00f) }, // 0
68       { Vector2( -0.95f,  0.31f) }, // 1
69       { Vector2( -0.59f, -0.81f) }, // 2
70       { Vector2(  0.59f, -0.81f) }, // 3
71       { Vector2(  0.95f,  0.31f) }, // 4
72     };
73
74   Vertex pentacleVertexData[5] =
75     {
76       { Vector2(  0.0f,  -1.00f) }, //
77       { Vector2(  0.59f,  0.81f) }, //
78       { Vector2( -0.95f, -0.31f) }, //
79       { Vector2(  0.95f, -0.31f) }, //
80       { Vector2( -0.59f,  0.81f) }, //
81     };
82
83   Property::Map pentagonVertexFormat;
84   pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
85   PropertyBuffer pentagonVertices = PropertyBuffer::New( PropertyBuffer::STATIC, pentagonVertexFormat, 5 );
86   pentagonVertices.SetData(pentagonVertexData);
87
88   Property::Map pentacleVertexFormat;
89   pentacleVertexFormat["aPosition2"] = Property::VECTOR2;
90   PropertyBuffer pentacleVertices = PropertyBuffer::New( PropertyBuffer::STATIC, pentacleVertexFormat, 5 );
91   pentacleVertices.SetData(pentacleVertexData);
92
93   // Create indices
94   unsigned short indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
95   Property::Map indexFormat;
96   indexFormat["indices"] = Property::UNSIGNED_INTEGER;
97   PropertyBuffer indices = PropertyBuffer::New( PropertyBuffer::STATIC, indexFormat, 5 );
98   indices.SetData(indexData);
99
100   // Create the geometry object
101   Geometry pentagonGeometry = Geometry::New();
102   pentagonGeometry.AddVertexBuffer( pentagonVertices );
103   pentagonGeometry.AddVertexBuffer( pentacleVertices );
104   pentagonGeometry.SetIndexBuffer( indices );
105
106   pentagonGeometry.SetGeometryType( Geometry::LINES );
107
108   return pentagonGeometry;
109 }
110
111 } // anonymous namespace
112
113 // This example shows how to morph between 2 meshes with the same number of
114 // vertices.
115 class ExampleController : public ConnectionTracker
116 {
117 public:
118
119   /**
120    * The example controller constructor.
121    * @param[in] application The application instance
122    */
123   ExampleController( Application& application )
124   : mApplication( application )
125   {
126     // Connect to the Application's Init signal
127     mApplication.InitSignal().Connect( this, &ExampleController::Create );
128   }
129
130   /**
131    * The example controller destructor
132    */
133   ~ExampleController()
134   {
135     // Nothing to do here;
136   }
137
138   /**
139    * Invoked upon creation of application
140    * @param[in] application The application instance
141    */
142   void Create( Application& application )
143   {
144     Stage stage = Stage::GetCurrent();
145     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
146
147     mStageSize = stage.GetSize();
148
149     // The Init signal is received once (only) during the Application lifetime
150
151     // Hide the indicator bar
152     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
153
154     mImage = ResourceImage::New( MATERIAL_SAMPLE );
155     mSampler = Sampler::New(mImage, "sTexture");
156     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
157
158     mMaterial = Material::New( mShader );
159     mMaterial.AddSampler( mSampler );
160
161     mGeometry = CreateGeometry();
162
163     mRenderer = Renderer::New( mGeometry, mMaterial );
164
165     mMeshActor = Actor::New();
166     mMeshActor.AddRenderer( mRenderer );
167     mMeshActor.SetSize(200, 200);
168
169     Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "morph-amount", 0.0f );
170     mMeshActor.AddUniformMapping( morphAmountIndex, std::string("uMorphAmount") );
171
172     mRenderer.SetDepthIndex(0);
173
174     mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
175     mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
176     stage.Add( mMeshActor );
177
178     mChangeImageTimer = Timer::New( 5000 );
179     mChangeImageTimer.TickSignal().Connect( this, &ExampleController::OnTimer );
180     mChangeImageTimer.Start();
181
182     Animation  animation = Animation::New(5);
183     KeyFrames keyFrames = KeyFrames::New();
184     keyFrames.Add(0.0f, 0.0f);
185     keyFrames.Add(1.0f, 1.0f);
186
187     animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunctions::Sin );
188     animation.SetLooping(true);
189     animation.Play();
190
191     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));;
192   }
193
194   /**
195    * Invoked whenever the quit button is clicked
196    * @param[in] button the quit button
197    */
198   bool OnQuitButtonClicked( Toolkit::Button button )
199   {
200     // quit the application
201     mApplication.Quit();
202     return true;
203   }
204
205   bool OnTimer()
206   {
207     Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
208     mSampler.SetImage( image );
209     return false;
210   }
211
212   void OnKeyEvent(const KeyEvent& event)
213   {
214     if(event.state == KeyEvent::Down)
215     {
216       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
217       {
218         mApplication.Quit();
219       }
220     }
221   }
222
223 private:
224
225   Application&  mApplication;                             ///< Application instance
226   Vector3 mStageSize;                                     ///< The size of the stage
227
228   Image    mImage;
229   Sampler  mSampler;
230   Shader   mShader;
231   Material mMaterial;
232   Geometry mGeometry;
233   Renderer mRenderer;
234   Actor    mMeshActor;
235   Renderer mRenderer2;
236   Actor    mMeshActor2;
237   Timer    mChangeImageTimer;
238 };
239
240 void RunTest( Application& application )
241 {
242   ExampleController test( application );
243
244   application.MainLoop();
245 }
246
247 // Entry point for Linux & SLP applications
248 //
249 int main( int argc, char **argv )
250 {
251   Application application = Application::New( &argc, &argv );
252
253   RunTest( application );
254
255   return 0;
256 }