Merge branch 'devel/master' 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 #include <dali/devel-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 // INTERNAL INCLUDES
23 #include "shared/view.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( 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     mMeshActor.RegisterProperty( "uFadeColor", Color::GREEN );
175
176     mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
177     mRenderer.RegisterProperty( "uPointSize", 80.0f );
178     mRenderer.SetDepthIndex(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   Image    mImage;
225   Sampler  mSampler1;
226   Sampler  mSampler2;
227   Shader   mShader;
228   Material mMaterial;
229   Geometry mGeometry;
230   Renderer mRenderer;
231   Actor    mMeshActor;
232   Renderer mRenderer2;
233   Actor    mMeshActor2;
234   Timer    mChangeImageTimer;
235 };
236
237 void RunTest( Application& application )
238 {
239   ExampleController test( application );
240
241   application.MainLoop();
242 }
243
244 // Entry point for Linux & SLP applications
245 //
246 int main( int argc, char **argv )
247 {
248   Application application = Application::New( &argc, &argv );
249
250   RunTest( application );
251
252   return 0;
253 }