New point and line demos
[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   lowp    vec4    uFadeColor;
41 varying   mediump vec3    vVertexColor;
42 varying   mediump float   vHue;
43
44 vec3 hsv2rgb(vec3 c)
45 {
46   vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
47   vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
48   return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
49 }
50
51 void main()
52 {
53   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
54   vertexPosition.xyz *= uSize;
55   vertexPosition = uMvpMatrix * vertexPosition;
56   vVertexColor = hsv2rgb( vec3( aHue, 0.6, 0.7 ) );
57   vHue = aHue;
58   gl_PointSize = 80.0;
59   gl_Position = vertexPosition;
60 }
61 );
62
63 const char* FRAGMENT_SHADER = MAKE_SHADER(
64 varying mediump vec3  vVertexColor;
65 varying mediump float vHue;
66 uniform lowp  vec4    uColor;
67 uniform sampler2D     sTexture1;
68 uniform sampler2D     sTexture2;
69 uniform lowp vec4     uFadeColor;
70
71 void main()
72 {
73   mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord);
74   mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord);
75   gl_FragColor = vec4(vVertexColor, 1.0) * ((texCol1*vHue) + (texCol2*(1.0-vHue)));
76 }
77 );
78
79 Geometry CreateGeometry()
80 {
81   // Create vertices
82   struct Vertex { Vector2 position; float hue; };
83
84   unsigned int numSides = 20;
85   Vertex polyhedraVertexData[numSides];
86   float angle=0;
87   float sectorAngle = 2.0f * Math::PI / (float) numSides;
88   for(unsigned int i=0; i<numSides; ++i)
89   {
90     polyhedraVertexData[i].position.x = sinf(angle);
91     polyhedraVertexData[i].position.y = cosf(angle);
92     polyhedraVertexData[i].hue = angle / ( 2.0f * Math::PI);
93     angle += sectorAngle;
94   }
95
96   Property::Map polyhedraVertexFormat;
97   polyhedraVertexFormat["aPosition"] = Property::VECTOR2;
98   polyhedraVertexFormat["aHue"] = Property::FLOAT;
99   PropertyBuffer polyhedraVertices = PropertyBuffer::New( PropertyBuffer::STATIC, polyhedraVertexFormat, numSides );
100   polyhedraVertices.SetData(polyhedraVertexData);
101
102   // Create the geometry object
103   Geometry polyhedraGeometry = Geometry::New();
104   polyhedraGeometry.AddVertexBuffer( polyhedraVertices );
105   polyhedraGeometry.SetGeometryType( Geometry::POINTS );
106
107   return polyhedraGeometry;
108 }
109
110 } // anonymous namespace
111
112 // This example shows how to use a simple mesh
113 //
114 class ExampleController : public ConnectionTracker
115 {
116 public:
117
118   /**
119    * The example controller constructor.
120    * @param[in] application The application instance
121    */
122   ExampleController( Application& application )
123   : mApplication( application )
124   {
125     // Connect to the Application's Init signal
126     mApplication.InitSignal().Connect( this, &ExampleController::Create );
127   }
128
129   /**
130    * The example controller destructor
131    */
132   ~ExampleController()
133   {
134     // Nothing to do here;
135   }
136
137   /**
138    * Invoked upon creation of application
139    * @param[in] application The application instance
140    */
141   void Create( Application& application )
142   {
143     Stage stage = Stage::GetCurrent();
144     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
145
146     mStageSize = stage.GetSize();
147
148     // The Init signal is received once (only) during the Application lifetime
149
150     // Hide the indicator bar
151     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
152
153     mImage = ResourceImage::New( MATERIAL_SAMPLE );
154     Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
155     mSampler1 = Sampler::New(mImage, "sTexture1");
156     mSampler2 = Sampler::New(image, "sTexture2");
157
158     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
159
160     mMaterial = Material::New( mShader );
161     mMaterial.AddSampler( mSampler1 );
162     mMaterial.AddSampler( mSampler2 );
163
164     mGeometry = CreateGeometry();
165
166     mRenderer = Renderer::New( mGeometry, mMaterial );
167
168     mMeshActor = Actor::New();
169     mMeshActor.AddRenderer( mRenderer );
170     mMeshActor.SetSize(200, 200);
171
172     Property::Index fadeColorIndex = mMeshActor.RegisterProperty( "fade-color", Color::GREEN );
173     mMeshActor.AddUniformMapping( fadeColorIndex, std::string("uFadeColor") );
174
175     fadeColorIndex = mRenderer.RegisterProperty( "fade-color", Color::MAGENTA );
176     mRenderer.AddUniformMapping( fadeColorIndex, std::string("uFadeColor" ) );
177     mRenderer.SetDepthIndex(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.RotateBy( mMeshActor, 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   Image    mImage;
224   Sampler  mSampler1;
225   Sampler  mSampler2;
226   Shader   mShader;
227   Material mMaterial;
228   Geometry mGeometry;
229   Renderer mRenderer;
230   Actor    mMeshActor;
231   Renderer mRenderer2;
232   Actor    mMeshActor2;
233   Timer    mChangeImageTimer;
234 };
235
236 void RunTest( Application& application )
237 {
238   ExampleController test( application );
239
240   application.MainLoop();
241 }
242
243 // Entry point for Linux & SLP applications
244 //
245 int main( int argc, char **argv )
246 {
247   Application application = Application::New( &argc, &argv );
248
249   RunTest( application );
250
251   return 0;
252 }