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