Using migrated Public Visual API
[platform/core/uifw/dali-demo.git] / examples / textured-mesh / textured-mesh-example.cpp
1 /*
2  * Copyright (c) 2017 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-toolkit/dali-toolkit.h>
20
21 // INTERNAL INCLUDES
22 #include "shared/view.h"
23 #include "shared/utility.h"
24
25 using namespace Dali;
26
27 namespace
28 {
29 const char* MATERIAL_SAMPLE( DEMO_IMAGE_DIR "gallery-small-48.jpg" );
30 const char* MATERIAL_SAMPLE2( DEMO_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    aPosition;
36 attribute highp   vec2    aTexCoord;
37 varying   mediump vec2    vTexCoord;
38 uniform   mediump mat4    uMvpMatrix;
39 uniform   mediump vec3    uSize;
40 uniform   lowp    vec4    uFadeColor;
41
42 void main()
43 {
44   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
45   vertexPosition.xyz *= uSize;
46   vertexPosition = uMvpMatrix * vertexPosition;
47   vTexCoord = aTexCoord;
48   gl_Position = vertexPosition;
49 }
50 );
51
52 const char* FRAGMENT_SHADER = MAKE_SHADER(
53 varying mediump vec2  vTexCoord;
54 uniform lowp    vec4  uColor;
55 uniform sampler2D     sTexture;
56 uniform lowp    vec4  uFadeColor;
57
58 void main()
59 {
60   gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * uFadeColor;
61 }
62 );
63
64 /**
65  * Sinusoidal curve starting at zero with 2 cycles
66  */
67 float AlphaFunctionSineX2(float progress)
68 {
69   return 0.5f - cosf(progress * 4.0f * Math::PI) * 0.5f;
70 }
71
72 } // anonymous namespace
73
74 // This example shows how to use a simple mesh
75 //
76 class ExampleController : public ConnectionTracker
77 {
78 public:
79
80   /**
81    * The example controller constructor.
82    * @param[in] application The application instance
83    */
84   ExampleController( Application& application )
85   : mApplication( application )
86   {
87     // Connect to the Application's Init signal
88     mApplication.InitSignal().Connect( this, &ExampleController::Create );
89   }
90
91   /**
92    * The example controller destructor
93    */
94   ~ExampleController()
95   {
96     // Nothing to do here;
97   }
98
99   /**
100    * Invoked upon creation of application
101    * @param[in] application The application instance
102    */
103   void Create( Application& application )
104   {
105     // The Init signal is received once (only) during the Application lifetime
106
107     Stage stage = Stage::GetCurrent();
108     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
109
110     mStageSize = stage.GetSize();
111
112     // Hide the indicator bar
113     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
114
115     Texture texture1 = DemoHelper::LoadTexture( MATERIAL_SAMPLE );
116     Texture texture2 = DemoHelper::LoadTexture( MATERIAL_SAMPLE2 );
117
118     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
119     mTextureSet1 = TextureSet::New();
120     mTextureSet1.SetTexture( 0u, texture1 );
121
122     mTextureSet2 = TextureSet::New();
123     mTextureSet2.SetTexture( 0u, texture2 );
124
125     mGeometry = DemoHelper::CreateTexturedQuad();
126
127     mRenderer = Renderer::New( mGeometry, mShader );
128     mRenderer.SetTextures( mTextureSet1 );
129
130     mMeshActor = Actor::New();
131     mMeshActor.AddRenderer( mRenderer );
132     mMeshActor.SetSize(400, 400);
133
134     Property::Index fadeColorIndex = mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
135     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
136
137     mMeshActor.SetParentOrigin( ParentOrigin::TOP_CENTER );
138     mMeshActor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
139     stage.Add( mMeshActor );
140
141     mRenderer2 = Renderer::New( mGeometry, mShader );
142     mRenderer2.SetTextures( mTextureSet2 );
143
144     mMeshActor2 = Actor::New();
145     mMeshActor2.AddRenderer( mRenderer2 );
146     mMeshActor2.SetSize(400, 400);
147
148     mMeshActor2.RegisterProperty( "anotherProperty",    Color::GREEN );
149
150     mRenderer2.RegisterProperty( "anotherProperty",    Vector3::ZERO );
151     mRenderer2.RegisterProperty( "aCoefficient",  0.008f );
152     Property::Index fadeColorIndex2 = mRenderer2.RegisterProperty( "uFadeColor", Color::BLUE );
153     mRenderer2.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
154
155     mMeshActor2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
156     mMeshActor2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
157     stage.Add( mMeshActor2 );
158
159     Animation  animation = Animation::New(5);
160     KeyFrames keyFrames = KeyFrames::New();
161     keyFrames.Add(0.0f, Vector4::ZERO);
162     keyFrames.Add(1.0f, Vector4( Color::GREEN ));
163
164     KeyFrames keyFrames2 = KeyFrames::New();
165     keyFrames2.Add(0.0f, Vector4::ZERO);
166     keyFrames2.Add(1.0f, Color::MAGENTA);
167
168     animation.AnimateBetween( Property( mRenderer, fadeColorIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
169     animation.AnimateBetween( Property( mRenderer2, fadeColorIndex2 ), keyFrames2, AlphaFunction(AlphaFunctionSineX2) );
170     animation.SetLooping(true);
171     animation.Play();
172
173     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
174   }
175
176   BufferImage CreateBufferImage()
177   {
178     BufferImage image = BufferImage::New( 200, 200, Pixel::RGB888 );
179     PixelBuffer* pixelBuffer = image.GetBuffer();
180     unsigned int stride = image.GetBufferStride();
181     for( unsigned int x=0; x<200; x++ )
182     {
183       for( unsigned int y=0; y<200; y++ )
184       {
185         PixelBuffer* pixel = pixelBuffer + y*stride + x*3;
186         if( ((int)(x/20.0f))%2 + ((int)(y/20.0f)%2) == 1 )
187         {
188           pixel[0]=255;
189           pixel[1]=0;
190           pixel[2]=0;
191           pixel[3]=255;
192         }
193         else
194         {
195           pixel[0]=0;
196           pixel[1]=0;
197           pixel[2]=255;
198           pixel[3]=255;
199         }
200       }
201     }
202     image.Update();
203     return image;
204   }
205
206   /**
207    * Invoked whenever the quit button is clicked
208    * @param[in] button the quit button
209    */
210   bool OnQuitButtonClicked( Toolkit::Button button )
211   {
212     // quit the application
213     mApplication.Quit();
214     return true;
215   }
216
217   void OnKeyEvent(const KeyEvent& event)
218   {
219     if(event.state == KeyEvent::Down)
220     {
221       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
222       {
223         mApplication.Quit();
224       }
225     }
226   }
227
228 private:
229
230   Application&  mApplication;                             ///< Application instance
231   Vector3 mStageSize;                                     ///< The size of the stage
232
233   Shader   mShader;
234   TextureSet mTextureSet1;
235   TextureSet mTextureSet2;
236   Geometry mGeometry;
237   Renderer mRenderer;
238   Actor    mMeshActor;
239   Renderer mRenderer2;
240   Actor    mMeshActor2;
241   Timer    mChangeImageTimer;
242 };
243
244 void RunTest( Application& application )
245 {
246   ExampleController test( application );
247
248   application.MainLoop();
249 }
250
251 // Entry point for Linux & SLP applications
252 //
253 int DALI_EXPORT_API main( int argc, char **argv )
254 {
255   Application application = Application::New( &argc, &argv );
256
257   RunTest( application );
258
259   return 0;
260 }