Merge "A simple control using visuals" into devel/master
[platform/core/uifw/dali-demo.git] / examples / rendering-triangle / rendering-triangle.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 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20
21 using namespace Dali;
22 using namespace Toolkit;
23
24 namespace
25 {
26
27 /*
28  * Vertex shader
29  */
30 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
31 attribute mediump vec2 aPosition;\n // DALi shader builtin
32 uniform   mediump mat4 uMvpMatrix;\n // DALi shader builtin
33 uniform   mediump vec3 uSize;\n // DALi shader builtin
34 \n
35 void main()\n
36 {\n
37   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
38   vertexPosition.xyz *= uSize;\n
39   gl_Position = uMvpMatrix * vertexPosition;\n
40 }\n
41 );
42
43 /*
44  * Fragment shader
45  */
46 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
47 uniform mediump vec4 uColor;\n
48 \n
49 void main()\n
50 {\n
51   gl_FragColor = uColor;\n
52 }\n
53 );
54
55 }
56
57 // This example shows how to draw a triangle in actor's color
58 //
59 class DrawTriangleController : public ConnectionTracker
60 {
61 public:
62
63   DrawTriangleController( Application& application )
64   : mApplication( application )
65   {
66     // Connect to the Application's Init signal
67     mApplication.InitSignal().Connect( this, &DrawTriangleController::Create );
68   }
69
70   ~DrawTriangleController()
71   {
72     // Nothing to do here;
73   }
74
75   // The Init signal is received once (only) during the Application lifetime
76   void Create( Application& application )
77   {
78     // Get a handle to the stage
79     Stage stage = Stage::GetCurrent();
80     stage.SetBackgroundColor( Color::WHITE );
81
82     // Step 1. Create shader
83     CreateTriangleShader();
84
85     // Step 2. Prepare geometry
86     CreateTriangleGeometry();
87
88     // Step 3. Create a renderer
89     CreateRenderer();
90
91     // Step 4. Create an Actor
92     CreateActor();
93
94     // Respond to a click anywhere on the stage
95     stage.GetRootLayer().TouchSignal().Connect( this, &DrawTriangleController::OnTouch );
96   }
97
98   bool OnTouch( Actor actor, const TouchData& touch )
99   {
100     // quit the application
101     mApplication.Quit();
102     return true;
103   }
104
105   /**
106    * This function creates a triangle geometry made of three vertices in order
107    * to draw a coloured triangle.
108    */
109   void CreateTriangleGeometry()
110   {
111     Vector2 vertices[] = {
112       Vector2( -1.0f, -1.0f ),
113       Vector2(  1.0f,  1.0f ),
114       Vector2( -1.0f,  1.0f )
115     };
116
117     PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map()
118                                                        .Add( "aPosition", Property::VECTOR2 ) );
119     vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vector2) );
120
121     mGeometry = Geometry::New();
122     mGeometry.AddVertexBuffer( vertexBuffer );
123     mGeometry.SetType( Geometry::TRIANGLES );
124   }
125
126   /**
127    * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER
128    *
129    * Shaders are very basic and all they do is transforming vertices and applying actor's colour.
130    */
131   void CreateTriangleShader()
132   {
133     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
134   }
135
136   /**
137    * Function creates renderer.
138    */
139   void CreateRenderer()
140   {
141     mRenderer = Renderer::New( mGeometry, mShader );
142   }
143
144   /**
145    * Creates new actor and attaches renderer.
146    */
147   void CreateActor()
148   {
149     Stage stage = Stage::GetCurrent();
150     Size size = stage.GetSize() * 0.25f;
151     mActor = Actor::New();
152     mActor.SetAnchorPoint( AnchorPoint::CENTER );
153     mActor.SetParentOrigin( ParentOrigin::CENTER );
154     mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) );
155     mActor.SetColor( Color::RED );
156     mActor.SetSize( Vector3( size.x, size.x, size.x ) );
157     mActor.AddRenderer( mRenderer );
158     stage.Add( mActor );
159   }
160
161 private:
162   Application&  mApplication;
163
164   Renderer mRenderer;
165   Shader mShader;
166   Geometry mGeometry;
167   Actor mActor;
168 };
169
170 void RunTest( Application& application )
171 {
172   DrawTriangleController test( application );
173
174   application.MainLoop();
175 }
176
177 // Entry point for Linux & Tizen applications
178 //
179 int DALI_EXPORT_API main( int argc, char **argv )
180 {
181   Application application = Application::New( &argc, &argv );
182
183   RunTest( application );
184
185   return 0;
186 }