Merge "Removing unused variables" into devel/master
[platform/core/uifw/dali-demo.git] / examples / textured-mesh / textured-mesh-example.cpp
1 /*
2  * Copyright (c) 2020 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     Window window = application.GetWindow();
108     window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
109
110     mWindowSize = window.GetSize();
111
112     Texture texture1 = DemoHelper::LoadTexture( MATERIAL_SAMPLE );
113     Texture texture2 = DemoHelper::LoadTexture( MATERIAL_SAMPLE2 );
114
115     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
116     mTextureSet1 = TextureSet::New();
117     mTextureSet1.SetTexture( 0u, texture1 );
118
119     mTextureSet2 = TextureSet::New();
120     mTextureSet2.SetTexture( 0u, texture2 );
121
122     mGeometry = DemoHelper::CreateTexturedQuad();
123
124     mRenderer = Renderer::New( mGeometry, mShader );
125     mRenderer.SetTextures( mTextureSet1 );
126
127     mMeshActor = Actor::New();
128     mMeshActor.AddRenderer( mRenderer );
129     mMeshActor.SetProperty( Actor::Property::SIZE, Vector2(400, 400) );
130
131     Property::Index fadeColorIndex = mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
132     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
133
134     mMeshActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
135     mMeshActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
136     window.Add( mMeshActor );
137
138     mRenderer2 = Renderer::New( mGeometry, mShader );
139     mRenderer2.SetTextures( mTextureSet2 );
140
141     mMeshActor2 = Actor::New();
142     mMeshActor2.AddRenderer( mRenderer2 );
143     mMeshActor2.SetProperty( Actor::Property::SIZE, Vector2(400, 400) );
144
145     mMeshActor2.RegisterProperty( "anotherProperty",    Color::GREEN );
146
147     mRenderer2.RegisterProperty( "anotherProperty",    Vector3::ZERO );
148     mRenderer2.RegisterProperty( "aCoefficient",  0.008f );
149     Property::Index fadeColorIndex2 = mRenderer2.RegisterProperty( "uFadeColor", Color::BLUE );
150     mRenderer2.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
151
152     mMeshActor2.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
153     mMeshActor2.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
154     window.Add( mMeshActor2 );
155
156     Animation  animation = Animation::New(5);
157     KeyFrames keyFrames = KeyFrames::New();
158     keyFrames.Add(0.0f, Vector4::ZERO);
159     keyFrames.Add(1.0f, Vector4( Color::GREEN ));
160
161     KeyFrames keyFrames2 = KeyFrames::New();
162     keyFrames2.Add(0.0f, Vector4::ZERO);
163     keyFrames2.Add(1.0f, Color::MAGENTA);
164
165     animation.AnimateBetween( Property( mRenderer, fadeColorIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
166     animation.AnimateBetween( Property( mRenderer2, fadeColorIndex2 ), keyFrames2, AlphaFunction(AlphaFunctionSineX2) );
167     animation.SetLooping(true);
168     animation.Play();
169
170     window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
171   }
172
173   /**
174    * Invoked whenever the quit button is clicked
175    * @param[in] button the quit button
176    */
177   bool OnQuitButtonClicked( Toolkit::Button button )
178   {
179     // quit the application
180     mApplication.Quit();
181     return true;
182   }
183
184   void OnKeyEvent(const KeyEvent& event)
185   {
186     if(event.GetState() == KeyEvent::DOWN)
187     {
188       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
189       {
190         mApplication.Quit();
191       }
192     }
193   }
194
195 private:
196
197   Application&  mApplication;                             ///< Application instance
198   Vector3 mWindowSize;                                     ///< The size of the window
199
200   Shader   mShader;
201   TextureSet mTextureSet1;
202   TextureSet mTextureSet2;
203   Geometry mGeometry;
204   Renderer mRenderer;
205   Actor    mMeshActor;
206   Renderer mRenderer2;
207   Actor    mMeshActor2;
208   Timer    mChangeImageTimer;
209 };
210
211 int DALI_EXPORT_API main( int argc, char **argv )
212 {
213   Application application = Application::New( &argc, &argv );
214   ExampleController test( application );
215   application.MainLoop();
216   return 0;
217 }