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