New point and line 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
20 // INTERNAL INCLUDES
21 #include "shared/view.h"
22
23 #include <dali-toolkit/dali-toolkit.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( PropertyBuffer::STATIC, texturedQuadVertexFormat, 4 );
79   texturedQuadVertices.SetData(texturedQuadVertexData);
80
81   // Create indices
82   unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
83   Property::Map indexFormat;
84   indexFormat["indices"] = Property::UNSIGNED_INTEGER;
85   PropertyBuffer indices = PropertyBuffer::New( PropertyBuffer::STATIC, indexFormat, 3 );
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 } // anonymous namespace
97
98 // This example shows how to use a simple mesh
99 //
100 class ExampleController : public ConnectionTracker
101 {
102 public:
103
104   /**
105    * The example controller constructor.
106    * @param[in] application The application instance
107    */
108   ExampleController( Application& application )
109   : mApplication( application )
110   {
111     // Connect to the Application's Init signal
112     mApplication.InitSignal().Connect( this, &ExampleController::Create );
113   }
114
115   /**
116    * The example controller destructor
117    */
118   ~ExampleController()
119   {
120     // Nothing to do here;
121   }
122
123   /**
124    * Invoked upon creation of application
125    * @param[in] application The application instance
126    */
127   void Create( Application& application )
128   {
129     Stage stage = Stage::GetCurrent();
130     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
131
132     mStageSize = stage.GetSize();
133
134     // The Init signal is received once (only) during the Application lifetime
135
136     // Hide the indicator bar
137     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
138
139     mImage = ResourceImage::New( MATERIAL_SAMPLE );
140     mSampler1 = Sampler::New(mImage, "sTexture");
141
142     Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
143     mSampler2 = Sampler::New(image, "sTexture");
144
145     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
146
147     mMaterial1 = Material::New( mShader );
148     mMaterial1.AddSampler( mSampler1 );
149
150     mMaterial2 = Material::New( mShader );
151     mMaterial2.AddSampler( mSampler2);
152
153     mGeometry = CreateGeometry();
154
155     mRenderer = Renderer::New( mGeometry, mMaterial1 );
156
157     mMeshActor = Actor::New();
158     mMeshActor.AddRenderer( mRenderer );
159     mMeshActor.SetSize(400, 400);
160
161     Property::Index fadeColorIndex = mMeshActor.RegisterProperty( "fade-color", Color::GREEN );
162     mMeshActor.AddUniformMapping( fadeColorIndex, std::string("uFadeColor") );
163
164     fadeColorIndex = mRenderer.RegisterProperty( "fade-color", Color::MAGENTA );
165     mRenderer.AddUniformMapping( fadeColorIndex, std::string("uFadeColor" ) );
166     mRenderer.SetDepthIndex(0);
167
168     mMeshActor.SetParentOrigin( ParentOrigin::TOP_CENTER );
169     mMeshActor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
170     stage.Add( mMeshActor );
171
172     mRenderer2 = Renderer::New( mGeometry, mMaterial2 );
173
174     mMeshActor2 = Actor::New();
175     mMeshActor2.AddRenderer( mRenderer2 );
176     mMeshActor2.SetSize(400, 400);
177
178     mMeshActor2.RegisterProperty( "a-n-other-property", Color::GREEN );
179     Property::Index fadeColorIndex2 = mMeshActor2.RegisterProperty( "another-fade-color", Color::GREEN );
180     mMeshActor2.AddUniformMapping( fadeColorIndex2, std::string("uFadeColor") );
181
182     mRenderer2.RegisterProperty( "a-n-other-property", Vector3::ZERO );
183     mRenderer2.RegisterProperty( "a-coefficient", 0.008f );
184     fadeColorIndex2 = mRenderer2.RegisterProperty( "another-fade-color", Color::GREEN );
185     mRenderer2.AddUniformMapping( fadeColorIndex2, std::string("uFadeColor" ) );
186     mRenderer2.SetDepthIndex(0);
187
188     mMeshActor2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
189     mMeshActor2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
190     stage.Add( mMeshActor2 );
191
192     Animation  animation = Animation::New(5);
193     KeyFrames keyFrames = KeyFrames::New();
194     keyFrames.Add(0.0f, Vector4::ZERO);
195     keyFrames.Add(1.0f, Vector4( 1.0f, 0.0f, 1.0f, 1.0f ));
196
197     KeyFrames keyFrames2 = KeyFrames::New();
198     keyFrames2.Add(0.0f, Vector4::ZERO);
199     keyFrames2.Add(1.0f, Color::GREEN);
200
201     animation.AnimateBetween( Property( mRenderer, fadeColorIndex ), keyFrames, AlphaFunctions::Sin );
202     animation.AnimateBetween( Property( mRenderer2, fadeColorIndex2 ), keyFrames2, AlphaFunctions::Sin2x );
203     animation.SetLooping(true);
204     animation.Play();
205
206     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));;
207   }
208
209   /**
210    * Invoked whenever the quit button is clicked
211    * @param[in] button the quit button
212    */
213   bool OnQuitButtonClicked( Toolkit::Button button )
214   {
215     // quit the application
216     mApplication.Quit();
217     return true;
218   }
219
220   void OnKeyEvent(const KeyEvent& event)
221   {
222     if(event.state == KeyEvent::Down)
223     {
224       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
225       {
226         mApplication.Quit();
227       }
228     }
229   }
230
231 private:
232
233   Application&  mApplication;                             ///< Application instance
234   Vector3 mStageSize;                                     ///< The size of the stage
235
236   Image    mImage;
237   Sampler  mSampler1;
238   Sampler  mSampler2;
239   Shader   mShader;
240   Material mMaterial1;
241   Material mMaterial2;
242   Geometry mGeometry;
243   Renderer mRenderer;
244   Actor    mMeshActor;
245   Renderer mRenderer2;
246   Actor    mMeshActor2;
247   Timer    mChangeImageTimer;
248 };
249
250 void RunTest( Application& application )
251 {
252   ExampleController test( application );
253
254   application.MainLoop();
255 }
256
257 // Entry point for Linux & SLP applications
258 //
259 int main( int argc, char **argv )
260 {
261   Application application = Application::New( &argc, &argv );
262
263   RunTest( application );
264
265   return 0;
266 }