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