Merge branch 'devel/new_mesh' into 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( DALI_IMAGE_DIR "gallery-small-48.jpg" );
30 const char* MATERIAL_SAMPLE2( DALI_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, 4 );
79   texturedQuadVertices.SetData(texturedQuadVertexData);
80
81   // Create indices
82   unsigned int indexData[6] = { 0, 3, 1, 0, 2, 3 };
83   Property::Map indexFormat;
84   indexFormat["indices"] = Property::UNSIGNED_INTEGER;
85   PropertyBuffer indices = PropertyBuffer::New( indexFormat, sizeof(indexData)/sizeof(indexData[0]) );
86   indices.SetData(indexData);
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, ResourceImage::ON_DEMAND, Image::NEVER );
148     mSampler1 = Sampler::New(mImage, "sTexture");
149
150     Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
151     mSampler2 = Sampler::New(image, "sTexture");
152
153     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
154     mMaterial1 = Material::New( mShader );
155     mMaterial1.AddSampler( mSampler1 );
156
157     mMaterial2 = Material::New( mShader );
158     mMaterial2.AddSampler( mSampler2 );
159
160     mGeometry = CreateGeometry();
161
162     mRenderer = Renderer::New( mGeometry, mMaterial1 );
163
164     mMeshActor = Actor::New();
165     mMeshActor.AddRenderer( mRenderer );
166     mMeshActor.SetSize(400, 400);
167
168     Property::Index fadeColorIndex = mMeshActor.RegisterProperty( "uFadeColor", Color::GREEN );
169
170     fadeColorIndex = mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
171     mRenderer.SetDepthIndex(0);
172
173     mMeshActor.SetParentOrigin( ParentOrigin::TOP_CENTER );
174     mMeshActor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
175     stage.Add( mMeshActor );
176
177     mRenderer2 = Renderer::New( mGeometry, mMaterial2 );
178
179     mMeshActor2 = Actor::New();
180     mMeshActor2.AddRenderer( mRenderer2 );
181     mMeshActor2.SetSize(400, 400);
182
183     mMeshActor2.RegisterProperty( "a-n-other-property", Color::GREEN );
184     Property::Index fadeColorIndex2 = mMeshActor2.RegisterProperty( "uFadeColor", Color::GREEN );
185
186     mRenderer2.RegisterProperty( "a-n-other-property", Vector3::ZERO );
187     mRenderer2.RegisterProperty( "a-coefficient", 0.008f );
188     fadeColorIndex2 = mRenderer2.RegisterProperty( "uFadeColor", Color::BLUE );
189     mRenderer2.SetDepthIndex(0);
190
191     mMeshActor2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
192     mMeshActor2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
193     stage.Add( mMeshActor2 );
194
195     Animation  animation = Animation::New(5);
196     KeyFrames keyFrames = KeyFrames::New();
197     keyFrames.Add(0.0f, Vector4::ZERO);
198     keyFrames.Add(1.0f, Vector4( Color::GREEN ));
199
200     KeyFrames keyFrames2 = KeyFrames::New();
201     keyFrames2.Add(0.0f, Vector4::ZERO);
202     keyFrames2.Add(1.0f, Color::MAGENTA);
203
204     animation.AnimateBetween( Property( mRenderer, fadeColorIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
205     animation.AnimateBetween( Property( mRenderer2, fadeColorIndex2 ), keyFrames2, AlphaFunction(AlphaFunctionSineX2) );
206     animation.SetLooping(true);
207     animation.Play();
208
209     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));;
210   }
211
212   BufferImage CreateBufferImage()
213   {
214     BufferImage image = BufferImage::New( 200, 200, Pixel::RGB888 );
215     PixelBuffer* pixelBuffer = image.GetBuffer();
216     unsigned int stride = image.GetBufferStride();
217     for( unsigned int x=0; x<200; x++ )
218     {
219       for( unsigned int y=0; y<200; y++ )
220       {
221         PixelBuffer* pixel = pixelBuffer + y*stride + x*3;
222         if( ((int)(x/20.0f))%2 + ((int)(y/20.0f)%2) == 1 )
223         {
224           pixel[0]=255;
225           pixel[1]=0;
226           pixel[2]=0;
227           pixel[3]=255;
228         }
229         else
230         {
231           pixel[0]=0;
232           pixel[1]=0;
233           pixel[2]=255;
234           pixel[3]=255;
235         }
236       }
237     }
238     image.Update();
239     return image;
240   }
241
242   /**
243    * Invoked whenever the quit button is clicked
244    * @param[in] button the quit button
245    */
246   bool OnQuitButtonClicked( Toolkit::Button button )
247   {
248     // quit the application
249     mApplication.Quit();
250     return true;
251   }
252
253   void OnKeyEvent(const KeyEvent& event)
254   {
255     if(event.state == KeyEvent::Down)
256     {
257       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
258       {
259         mApplication.Quit();
260       }
261     }
262   }
263
264 private:
265
266   Application&  mApplication;                             ///< Application instance
267   Vector3 mStageSize;                                     ///< The size of the stage
268
269   Image    mImage;
270   Sampler  mSampler1;
271   Sampler  mSampler2;
272   Shader   mShader;
273   Material mMaterial1;
274   Material mMaterial2;
275   Geometry mGeometry;
276   Renderer mRenderer;
277   Actor    mMeshActor;
278   Renderer mRenderer2;
279   Actor    mMeshActor2;
280   Timer    mChangeImageTimer;
281 };
282
283 void RunTest( Application& application )
284 {
285   ExampleController test( application );
286
287   application.MainLoop();
288 }
289
290 // Entry point for Linux & SLP applications
291 //
292 int main( int argc, char **argv )
293 {
294   Application application = Application::New( &argc, &argv );
295
296   RunTest( application );
297
298   return 0;
299 }