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