2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/dali-toolkit.h>
22 #include "shared/utility.h"
23 #include "shared/view.h"
29 const char* MATERIAL_SAMPLE(DEMO_IMAGE_DIR "gallery-small-48.jpg");
30 const char* MATERIAL_SAMPLE2(DEMO_IMAGE_DIR "gallery-medium-19.jpg");
32 #define MAKE_SHADER(A) #A
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;
45 vec3 hsv2rgb(vec3 c) {
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);
52 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
53 vertexPosition.xyz *= (uSize - uPointSize);
54 vertexPosition = uMvpMatrix * vertexPosition;
55 vVertexColor = hsv2rgb(vec3(aHue, 0.7, 1.0));
57 gl_PointSize = uPointSize;
58 gl_Position = vertexPosition;
61 const char* FRAGMENT_SHADER = MAKE_SHADER(
62 varying mediump vec3 vVertexColor;
63 varying mediump float vHue;
64 uniform lowp vec4 uColor;
65 uniform sampler2D sTexture1;
66 uniform sampler2D sTexture2;
67 uniform lowp vec4 uFadeColor;
70 mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord);
71 mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord);
72 gl_FragColor = vec4(vVertexColor, 1.0) * ((texCol1 * vHue) + (texCol2 * (1.0 - vHue)));
75 Geometry CreateGeometry()
84 const unsigned int numSides = 20;
85 Vertex polyhedraVertexData[numSides];
87 float sectorAngle = 2.0f * Math::PI / (float)numSides;
89 for(unsigned int i = 0; i < numSides; ++i)
91 polyhedraVertexData[i].position.x = sinf(angle) * 0.5f;
92 polyhedraVertexData[i].position.y = cosf(angle) * 0.5f;
93 polyhedraVertexData[i].hue = angle / (2.0f * Math::PI);
97 Property::Map polyhedraVertexFormat;
98 polyhedraVertexFormat["aPosition"] = Property::VECTOR2;
99 polyhedraVertexFormat["aHue"] = Property::FLOAT;
100 VertexBuffer polyhedraVertices = VertexBuffer::New(polyhedraVertexFormat);
101 polyhedraVertices.SetData(polyhedraVertexData, numSides);
103 // Create the geometry object
104 Geometry polyhedraGeometry = Geometry::New();
105 polyhedraGeometry.AddVertexBuffer(polyhedraVertices);
106 polyhedraGeometry.SetType(Geometry::POINTS);
108 return polyhedraGeometry;
111 } // anonymous namespace
113 // This example shows how to use a simple mesh
115 class ExampleController : public ConnectionTracker
119 * The example controller constructor.
120 * @param[in] application The application instance
122 ExampleController(Application& application)
123 : mApplication(application)
125 // Connect to the Application's Init signal
126 mApplication.InitSignal().Connect(this, &ExampleController::Create);
130 * The example controller destructor
134 // Nothing to do here;
138 * Invoked upon creation of application
139 * @param[in] application The application instance
141 void Create(Application& application)
143 Window window = application.GetWindow();
144 window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
146 mWindowSize = window.GetSize();
148 // The Init signal is received once (only) during the Application lifetime
150 Texture texture0 = DemoHelper::LoadTexture(MATERIAL_SAMPLE);
151 Texture texture1 = DemoHelper::LoadTexture(MATERIAL_SAMPLE2);
153 Shader shader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER);
155 TextureSet textureSet = TextureSet::New();
156 textureSet.SetTexture(0u, texture0);
157 textureSet.SetTexture(1u, texture1);
159 Geometry geometry = CreateGeometry();
161 mRenderer = Renderer::New(geometry, shader);
162 mRenderer.SetTextures(textureSet);
164 mMeshActor = Actor::New();
165 mMeshActor.AddRenderer(mRenderer);
166 mMeshActor.SetProperty(Actor::Property::SIZE, Vector2(400, 400));
168 mMeshActor.RegisterProperty("uFadeColor", Color::GREEN);
170 mRenderer.RegisterProperty("uFadeColor", Color::MAGENTA);
171 mRenderer.RegisterProperty("uPointSize", 80.0f);
172 mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
174 mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
175 mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
176 window.Add(mMeshActor);
178 Animation animation = Animation::New(15);
179 KeyFrames keyFrames = KeyFrames::New();
180 keyFrames.Add(0.0f, Vector4::ZERO);
181 keyFrames.Add(1.0f, Vector4(1.0f, 0.0f, 1.0f, 1.0f));
183 animation.AnimateBy(Property(mMeshActor, Actor::Property::ORIENTATION), Quaternion(Degree(360), Vector3::ZAXIS));
185 animation.SetLooping(true);
188 window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
192 * Invoked whenever the quit button is clicked
193 * @param[in] button the quit button
195 bool OnQuitButtonClicked(Toolkit::Button button)
197 // quit the application
202 void OnKeyEvent(const KeyEvent& event)
204 if(event.GetState() == KeyEvent::DOWN)
206 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
214 Application& mApplication; ///< Application instance
215 Vector3 mWindowSize; ///< The size of the window
221 Timer mChangeImageTimer;
224 int DALI_EXPORT_API main(int argc, char** argv)
226 Application application = Application::New(&argc, &argv);
227 ExampleController test(application);
228 application.MainLoop();