Revert "Updates following rename of PropertyBuffer"
[platform/core/uifw/dali-demo.git] / examples / point-mesh / point-mesh-example.cpp
1 /*
2  * Copyright (c) 2020 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   const 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     Window window = application.GetWindow();
146     window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
147
148     mWindowSize = window.GetSize();
149
150     // The Init signal is received once (only) during the Application lifetime
151
152     Texture texture0 = DemoHelper::LoadTexture( MATERIAL_SAMPLE );
153     Texture texture1 = DemoHelper::LoadTexture( MATERIAL_SAMPLE2 );
154
155     Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
156
157     TextureSet textureSet = TextureSet::New();
158     textureSet.SetTexture( 0u, texture0 );
159     textureSet.SetTexture( 1u, texture1 );
160
161     Geometry geometry = CreateGeometry();
162
163     mRenderer = Renderer::New( geometry, shader );
164     mRenderer.SetTextures( textureSet );
165
166     mMeshActor = Actor::New();
167     mMeshActor.AddRenderer( mRenderer );
168     mMeshActor.SetProperty( Actor::Property::SIZE, Vector2(400, 400) );
169
170     mMeshActor.RegisterProperty( "uFadeColor", Color::GREEN );
171
172     mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
173     mRenderer.RegisterProperty( "uPointSize", 80.0f );
174     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
175
176     mMeshActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
177     mMeshActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
178     window.Add( mMeshActor );
179
180     Animation  animation = Animation::New(15);
181     KeyFrames keyFrames = KeyFrames::New();
182     keyFrames.Add(0.0f, Vector4::ZERO);
183     keyFrames.Add(1.0f, Vector4( 1.0f, 0.0f, 1.0f, 1.0f ));
184
185     animation.AnimateBy( Property(mMeshActor, Actor::Property::ORIENTATION), Quaternion(Degree(360), Vector3::ZAXIS) );
186
187     animation.SetLooping(true);
188     animation.Play();
189
190     window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
191   }
192
193   /**
194    * Invoked whenever the quit button is clicked
195    * @param[in] button the quit button
196    */
197   bool OnQuitButtonClicked( Toolkit::Button button )
198   {
199     // quit the application
200     mApplication.Quit();
201     return true;
202   }
203
204   void OnKeyEvent(const KeyEvent& event)
205   {
206     if(event.state == KeyEvent::Down)
207     {
208       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
209       {
210         mApplication.Quit();
211       }
212     }
213   }
214
215 private:
216
217   Application&  mApplication;                             ///< Application instance
218   Vector3 mWindowSize;                                     ///< The size of the window
219
220   Renderer mRenderer;
221   Actor    mMeshActor;
222   Renderer mRenderer2;
223   Actor    mMeshActor2;
224   Timer    mChangeImageTimer;
225 };
226
227 int DALI_EXPORT_API main( int argc, char **argv )
228 {
229   Application application = Application::New( &argc, &argv );
230   ExampleController test( application );
231   application.MainLoop();
232   return 0;
233 }