Merge "Update refraction-effect & radial-menu to use new mesh" into devel/new_mesh
[platform/core/uifw/dali-demo.git] / examples / point-mesh / point-mesh-example.cpp
1 /*
2  * Copyright (c) 2015 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   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( PropertyBuffer::STATIC, polyhedraVertexFormat, numSides );
102   polyhedraVertices.SetData(polyhedraVertexData);
103
104   // Create the geometry object
105   Geometry polyhedraGeometry = Geometry::New();
106   polyhedraGeometry.AddVertexBuffer( polyhedraVertices );
107   polyhedraGeometry.SetGeometryType( 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     mImage = ResourceImage::New( MATERIAL_SAMPLE );
156     Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
157     mSampler1 = Sampler::New(mImage, "sTexture1");
158     mSampler2 = Sampler::New(image, "sTexture2");
159
160     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
161
162     mMaterial = Material::New( mShader );
163     mMaterial.AddSampler( mSampler1 );
164     mMaterial.AddSampler( mSampler2 );
165
166     mGeometry = CreateGeometry();
167
168     mRenderer = Renderer::New( mGeometry, mMaterial );
169
170     mMeshActor = Actor::New();
171     mMeshActor.AddRenderer( mRenderer );
172     mMeshActor.SetSize(400, 400);
173
174     Property::Index fadeColorIndex = mMeshActor.RegisterProperty( "fade-color", Color::GREEN );
175     mMeshActor.AddUniformMapping( fadeColorIndex, std::string("uFadeColor") );
176
177     fadeColorIndex = mRenderer.RegisterProperty( "fade-color", Color::MAGENTA );
178     Property::Index pointSizeIndex = mRenderer.RegisterProperty( "point-size", 80.0f );
179     mRenderer.AddUniformMapping( fadeColorIndex, std::string("uFadeColor" ) );
180     mRenderer.AddUniformMapping( pointSizeIndex, std::string("uPointSize" ) );
181     mRenderer.SetDepthIndex(0);
182
183     mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
184     mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
185     stage.Add( mMeshActor );
186
187     Animation  animation = Animation::New(15);
188     KeyFrames keyFrames = KeyFrames::New();
189     keyFrames.Add(0.0f, Vector4::ZERO);
190     keyFrames.Add(1.0f, Vector4( 1.0f, 0.0f, 1.0f, 1.0f ));
191
192     animation.AnimateBy( Property(mMeshActor, Actor::Property::ORIENTATION), Quaternion(Degree(360), Vector3::ZAXIS) );
193
194     animation.SetLooping(true);
195     animation.Play();
196
197     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));;
198   }
199
200   /**
201    * Invoked whenever the quit button is clicked
202    * @param[in] button the quit button
203    */
204   bool OnQuitButtonClicked( Toolkit::Button button )
205   {
206     // quit the application
207     mApplication.Quit();
208     return true;
209   }
210
211   void OnKeyEvent(const KeyEvent& event)
212   {
213     if(event.state == KeyEvent::Down)
214     {
215       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
216       {
217         mApplication.Quit();
218       }
219     }
220   }
221
222 private:
223
224   Application&  mApplication;                             ///< Application instance
225   Vector3 mStageSize;                                     ///< The size of the stage
226
227   Image    mImage;
228   Sampler  mSampler1;
229   Sampler  mSampler2;
230   Shader   mShader;
231   Material mMaterial;
232   Geometry mGeometry;
233   Renderer mRenderer;
234   Actor    mMeshActor;
235   Renderer mRenderer2;
236   Actor    mMeshActor2;
237   Timer    mChangeImageTimer;
238 };
239
240 void RunTest( Application& application )
241 {
242   ExampleController test( application );
243
244   application.MainLoop();
245 }
246
247 // Entry point for Linux & SLP applications
248 //
249 int main( int argc, char **argv )
250 {
251   Application application = Application::New( &argc, &argv );
252
253   RunTest( application );
254
255   return 0;
256 }