86e2d7172b0b21939a2237e009da72333c536533
[platform/core/uifw/dali-demo.git] / examples / point-mesh / point-mesh-example.cpp
1 /*
2  * Copyright (c) 2016 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/public-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   float   aHue;
38 varying   mediump vec2    vTexCoord;
39 uniform   mediump mat4    uMvpMatrix;
40 uniform   mediump vec3    uSize;
41 uniform   mediump float   uPointSize;
42 uniform   lowp    vec4    uFadeColor;
43 varying   mediump vec3    vVertexColor;
44 varying   mediump float   vHue;
45
46 vec3 hsv2rgb(vec3 c)
47 {
48   vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
49   vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
50   return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
51 }
52
53 void main()
54 {
55   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
56   vertexPosition.xyz *= (uSize-uPointSize);
57   vertexPosition = uMvpMatrix * vertexPosition;
58   vVertexColor = hsv2rgb( vec3( aHue, 0.7, 1.0 ) );
59   vHue = aHue;
60   gl_PointSize = uPointSize;
61   gl_Position = vertexPosition;
62 }
63 );
64
65 const char* FRAGMENT_SHADER = MAKE_SHADER(
66 varying mediump vec3  vVertexColor;
67 varying mediump float vHue;
68 uniform lowp  vec4    uColor;
69 uniform sampler2D     sTexture1;
70 uniform sampler2D     sTexture2;
71 uniform lowp vec4     uFadeColor;
72
73 void main()
74 {
75   mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord);
76   mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord);
77   gl_FragColor = vec4(vVertexColor, 1.0) * ((texCol1*vHue) + (texCol2*(1.0-vHue)));
78 }
79 );
80
81 Geometry CreateGeometry()
82 {
83   // Create vertices
84   struct Vertex { Vector2 position; float hue; };
85
86   unsigned int numSides = 20;
87   Vertex polyhedraVertexData[numSides];
88   float angle=0;
89   float sectorAngle = 2.0f * Math::PI / (float) numSides;
90
91   for(unsigned int i=0; i<numSides; ++i)
92   {
93     polyhedraVertexData[i].position.x = sinf(angle)*0.5f;
94     polyhedraVertexData[i].position.y = cosf(angle)*0.5f;
95     polyhedraVertexData[i].hue = angle / ( 2.0f * Math::PI);
96     angle += sectorAngle;
97   }
98
99   Property::Map polyhedraVertexFormat;
100   polyhedraVertexFormat["aPosition"] = Property::VECTOR2;
101   polyhedraVertexFormat["aHue"] = Property::FLOAT;
102   PropertyBuffer polyhedraVertices = PropertyBuffer::New( polyhedraVertexFormat );
103   polyhedraVertices.SetData( polyhedraVertexData, numSides );
104
105   // Create the geometry object
106   Geometry polyhedraGeometry = Geometry::New();
107   polyhedraGeometry.AddVertexBuffer( polyhedraVertices );
108   polyhedraGeometry.SetType( Geometry::POINTS );
109
110   return polyhedraGeometry;
111 }
112
113 } // anonymous namespace
114
115 // This example shows how to use a simple mesh
116 //
117 class ExampleController : public ConnectionTracker
118 {
119 public:
120
121   /**
122    * The example controller constructor.
123    * @param[in] application The application instance
124    */
125   ExampleController( Application& application )
126   : mApplication( application )
127   {
128     // Connect to the Application's Init signal
129     mApplication.InitSignal().Connect( this, &ExampleController::Create );
130   }
131
132   /**
133    * The example controller destructor
134    */
135   ~ExampleController()
136   {
137     // Nothing to do here;
138   }
139
140   /**
141    * Invoked upon creation of application
142    * @param[in] application The application instance
143    */
144   void Create( Application& application )
145   {
146     Stage stage = Stage::GetCurrent();
147     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
148
149     mStageSize = stage.GetSize();
150
151     // The Init signal is received once (only) during the Application lifetime
152
153     // Hide the indicator bar
154     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
155
156     Texture texture0 = DemoHelper::LoadTexture( MATERIAL_SAMPLE );
157     Texture texture1 = DemoHelper::LoadTexture( MATERIAL_SAMPLE2 );
158
159     Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
160
161     TextureSet textureSet = TextureSet::New();
162     textureSet.SetTexture( 0u, texture0 );
163     textureSet.SetTexture( 1u, texture1 );
164
165     Geometry geometry = CreateGeometry();
166
167     mRenderer = Renderer::New( geometry, shader );
168     mRenderer.SetTextures( textureSet );
169
170     mMeshActor = Actor::New();
171     mMeshActor.AddRenderer( mRenderer );
172     mMeshActor.SetSize(400, 400);
173
174     mMeshActor.RegisterProperty( "uFadeColor", Color::GREEN );
175
176     mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
177     mRenderer.RegisterProperty( "uPointSize", 80.0f );
178     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
179
180     mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
181     mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
182     stage.Add( mMeshActor );
183
184     Animation  animation = Animation::New(15);
185     KeyFrames keyFrames = KeyFrames::New();
186     keyFrames.Add(0.0f, Vector4::ZERO);
187     keyFrames.Add(1.0f, Vector4( 1.0f, 0.0f, 1.0f, 1.0f ));
188
189     animation.AnimateBy( Property(mMeshActor, Actor::Property::ORIENTATION), Quaternion(Degree(360), Vector3::ZAXIS) );
190
191     animation.SetLooping(true);
192     animation.Play();
193
194     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
195   }
196
197   /**
198    * Invoked whenever the quit button is clicked
199    * @param[in] button the quit button
200    */
201   bool OnQuitButtonClicked( Toolkit::Button button )
202   {
203     // quit the application
204     mApplication.Quit();
205     return true;
206   }
207
208   void OnKeyEvent(const KeyEvent& event)
209   {
210     if(event.state == KeyEvent::Down)
211     {
212       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
213       {
214         mApplication.Quit();
215       }
216     }
217   }
218
219 private:
220
221   Application&  mApplication;                             ///< Application instance
222   Vector3 mStageSize;                                     ///< The size of the stage
223
224   Renderer mRenderer;
225   Actor    mMeshActor;
226   Renderer mRenderer2;
227   Actor    mMeshActor2;
228   Timer    mChangeImageTimer;
229 };
230
231 void RunTest( Application& application )
232 {
233   ExampleController test( application );
234
235   application.MainLoop();
236 }
237
238 // Entry point for Linux & SLP applications
239 //
240 int DALI_EXPORT_API main( int argc, char **argv )
241 {
242   Application application = Application::New( &argc, &argv );
243
244   RunTest( application );
245
246   return 0;
247 }