[dali_1.1.31] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / textured-mesh / textured-mesh-example.cpp
1 /*
2  * Copyright (c) 2014 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 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 Geometry CreateGeometry()
65 {
66   // Create vertices
67   const float halfQuadSize = .5f;
68   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
69   TexturedQuadVertex texturedQuadVertexData[4] = {
70     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
71     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
72     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
73     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
74
75   Property::Map texturedQuadVertexFormat;
76   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
77   texturedQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
78   PropertyBuffer texturedQuadVertices = PropertyBuffer::New( texturedQuadVertexFormat );
79   texturedQuadVertices.SetData( texturedQuadVertexData, 4 );
80
81   // Create indices
82   unsigned int indexData[6] = { 0, 3, 1, 0, 2, 3 };
83   Property::Map indexFormat;
84   indexFormat["indices"] = Property::INTEGER;
85   PropertyBuffer indices = PropertyBuffer::New( indexFormat );
86   indices.SetData( indexData, sizeof(indexData)/sizeof(indexData[0]) );
87
88   // Create the geometry object
89   Geometry texturedQuadGeometry = Geometry::New();
90   texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
91   texturedQuadGeometry.SetIndexBuffer( indices );
92
93   return texturedQuadGeometry;
94 }
95
96 /**
97  * Sinusoidal curve starting at zero with 2 cycles
98  */
99 float AlphaFunctionSineX2(float progress)
100 {
101   return 0.5f - cosf(progress * 4.0f * Math::PI) * 0.5f;
102 }
103
104 } // anonymous namespace
105
106 // This example shows how to use a simple mesh
107 //
108 class ExampleController : public ConnectionTracker
109 {
110 public:
111
112   /**
113    * The example controller constructor.
114    * @param[in] application The application instance
115    */
116   ExampleController( Application& application )
117   : mApplication( application )
118   {
119     // Connect to the Application's Init signal
120     mApplication.InitSignal().Connect( this, &ExampleController::Create );
121   }
122
123   /**
124    * The example controller destructor
125    */
126   ~ExampleController()
127   {
128     // Nothing to do here;
129   }
130
131   /**
132    * Invoked upon creation of application
133    * @param[in] application The application instance
134    */
135   void Create( Application& application )
136   {
137     // The Init signal is received once (only) during the Application lifetime
138
139     Stage stage = Stage::GetCurrent();
140     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
141
142     mStageSize = stage.GetSize();
143
144     // Hide the indicator bar
145     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
146
147     mImage = ResourceImage::New( MATERIAL_SAMPLE );
148     Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
149
150     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
151     mTextureSet1 = TextureSet::New();
152     mTextureSet1.SetImage( 0u, mImage );
153
154     mTextureSet2 = TextureSet::New();
155     mTextureSet2.SetImage( 0u, image );
156
157     mGeometry = CreateGeometry();
158
159     mRenderer = Renderer::New( mGeometry, mShader );
160     mRenderer.SetTextures( mTextureSet1 );
161
162     mMeshActor = Actor::New();
163     mMeshActor.AddRenderer( mRenderer );
164     mMeshActor.SetSize(400, 400);
165
166     Property::Index fadeColorIndex = mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
167     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
168
169     mMeshActor.SetParentOrigin( ParentOrigin::TOP_CENTER );
170     mMeshActor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
171     stage.Add( mMeshActor );
172
173     mRenderer2 = Renderer::New( mGeometry, mShader );
174     mRenderer2.SetTextures( mTextureSet2 );
175
176     mMeshActor2 = Actor::New();
177     mMeshActor2.AddRenderer( mRenderer2 );
178     mMeshActor2.SetSize(400, 400);
179
180     mMeshActor2.RegisterProperty( "anotherProperty",    Color::GREEN );
181
182     mRenderer2.RegisterProperty( "anotherProperty",    Vector3::ZERO );
183     mRenderer2.RegisterProperty( "aCoefficient",  0.008f );
184     Property::Index fadeColorIndex2 = mRenderer2.RegisterProperty( "uFadeColor", Color::BLUE );
185     mRenderer2.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
186
187     mMeshActor2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
188     mMeshActor2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
189     stage.Add( mMeshActor2 );
190
191     Animation  animation = Animation::New(5);
192     KeyFrames keyFrames = KeyFrames::New();
193     keyFrames.Add(0.0f, Vector4::ZERO);
194     keyFrames.Add(1.0f, Vector4( Color::GREEN ));
195
196     KeyFrames keyFrames2 = KeyFrames::New();
197     keyFrames2.Add(0.0f, Vector4::ZERO);
198     keyFrames2.Add(1.0f, Color::MAGENTA);
199
200     animation.AnimateBetween( Property( mRenderer, fadeColorIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
201     animation.AnimateBetween( Property( mRenderer2, fadeColorIndex2 ), keyFrames2, AlphaFunction(AlphaFunctionSineX2) );
202     animation.SetLooping(true);
203     animation.Play();
204
205     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
206   }
207
208   BufferImage CreateBufferImage()
209   {
210     BufferImage image = BufferImage::New( 200, 200, Pixel::RGB888 );
211     PixelBuffer* pixelBuffer = image.GetBuffer();
212     unsigned int stride = image.GetBufferStride();
213     for( unsigned int x=0; x<200; x++ )
214     {
215       for( unsigned int y=0; y<200; y++ )
216       {
217         PixelBuffer* pixel = pixelBuffer + y*stride + x*3;
218         if( ((int)(x/20.0f))%2 + ((int)(y/20.0f)%2) == 1 )
219         {
220           pixel[0]=255;
221           pixel[1]=0;
222           pixel[2]=0;
223           pixel[3]=255;
224         }
225         else
226         {
227           pixel[0]=0;
228           pixel[1]=0;
229           pixel[2]=255;
230           pixel[3]=255;
231         }
232       }
233     }
234     image.Update();
235     return image;
236   }
237
238   /**
239    * Invoked whenever the quit button is clicked
240    * @param[in] button the quit button
241    */
242   bool OnQuitButtonClicked( Toolkit::Button button )
243   {
244     // quit the application
245     mApplication.Quit();
246     return true;
247   }
248
249   void OnKeyEvent(const KeyEvent& event)
250   {
251     if(event.state == KeyEvent::Down)
252     {
253       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
254       {
255         mApplication.Quit();
256       }
257     }
258   }
259
260 private:
261
262   Application&  mApplication;                             ///< Application instance
263   Vector3 mStageSize;                                     ///< The size of the stage
264
265   Image    mImage;
266   Shader   mShader;
267   TextureSet mTextureSet1;
268   TextureSet mTextureSet2;
269   Geometry mGeometry;
270   Renderer mRenderer;
271   Actor    mMeshActor;
272   Renderer mRenderer2;
273   Actor    mMeshActor2;
274   Timer    mChangeImageTimer;
275 };
276
277 void RunTest( Application& application )
278 {
279   ExampleController test( application );
280
281   application.MainLoop();
282 }
283
284 // Entry point for Linux & SLP applications
285 //
286 int DALI_EXPORT_API main( int argc, char **argv )
287 {
288   Application application = Application::New( &argc, &argv );
289
290   RunTest( application );
291
292   return 0;
293 }