2 * Copyright (c) 2016 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/public-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
23 #include "shared/view.h"
24 #include "shared/utility.h"
30 const char* MATERIAL_SAMPLE( DEMO_IMAGE_DIR "gallery-small-48.jpg" );
31 const char* MATERIAL_SAMPLE2( DEMO_IMAGE_DIR "gallery-medium-19.jpg" );
33 #define MAKE_SHADER(A)#A
35 const char* VERTEX_SHADER = MAKE_SHADER(
36 attribute mediump vec2 aPosition;
37 attribute highp float aHue;
38 varying mediump vec2 vTexCoord;
39 uniform mediump mat4 uMvpMatrix;
40 uniform mediump vec3 uSize;
41 uniform mediump float uPointSize;
42 uniform lowp vec4 uFadeColor;
43 varying mediump vec3 vVertexColor;
44 varying mediump float vHue;
48 vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
49 vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
50 return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
55 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
56 vertexPosition.xyz *= (uSize-uPointSize);
57 vertexPosition = uMvpMatrix * vertexPosition;
58 vVertexColor = hsv2rgb( vec3( aHue, 0.7, 1.0 ) );
60 gl_PointSize = uPointSize;
61 gl_Position = vertexPosition;
65 const char* FRAGMENT_SHADER = MAKE_SHADER(
66 varying mediump vec3 vVertexColor;
67 varying mediump float vHue;
68 uniform lowp vec4 uColor;
69 uniform sampler2D sTexture1;
70 uniform sampler2D sTexture2;
71 uniform lowp vec4 uFadeColor;
75 mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord);
76 mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord);
77 gl_FragColor = vec4(vVertexColor, 1.0) * ((texCol1*vHue) + (texCol2*(1.0-vHue)));
81 Geometry CreateGeometry()
84 struct Vertex { Vector2 position; float hue; };
86 unsigned int numSides = 20;
87 Vertex polyhedraVertexData[numSides];
89 float sectorAngle = 2.0f * Math::PI / (float) numSides;
91 for(unsigned int i=0; i<numSides; ++i)
93 polyhedraVertexData[i].position.x = sinf(angle)*0.5f;
94 polyhedraVertexData[i].position.y = cosf(angle)*0.5f;
95 polyhedraVertexData[i].hue = angle / ( 2.0f * Math::PI);
99 Property::Map polyhedraVertexFormat;
100 polyhedraVertexFormat["aPosition"] = Property::VECTOR2;
101 polyhedraVertexFormat["aHue"] = Property::FLOAT;
102 PropertyBuffer polyhedraVertices = PropertyBuffer::New( polyhedraVertexFormat );
103 polyhedraVertices.SetData( polyhedraVertexData, numSides );
105 // Create the geometry object
106 Geometry polyhedraGeometry = Geometry::New();
107 polyhedraGeometry.AddVertexBuffer( polyhedraVertices );
108 polyhedraGeometry.SetType( Geometry::POINTS );
110 return polyhedraGeometry;
113 } // anonymous namespace
115 // This example shows how to use a simple mesh
117 class ExampleController : public ConnectionTracker
122 * The example controller constructor.
123 * @param[in] application The application instance
125 ExampleController( Application& application )
126 : mApplication( application )
128 // Connect to the Application's Init signal
129 mApplication.InitSignal().Connect( this, &ExampleController::Create );
133 * The example controller destructor
137 // Nothing to do here;
141 * Invoked upon creation of application
142 * @param[in] application The application instance
144 void Create( Application& application )
146 Stage stage = Stage::GetCurrent();
147 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
149 mStageSize = stage.GetSize();
151 // The Init signal is received once (only) during the Application lifetime
153 // Hide the indicator bar
154 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
156 Texture texture0 = DemoHelper::LoadTexture( MATERIAL_SAMPLE );
157 Texture texture1 = DemoHelper::LoadTexture( MATERIAL_SAMPLE2 );
159 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
161 TextureSet textureSet = TextureSet::New();
162 textureSet.SetTexture( 0u, texture0 );
163 textureSet.SetTexture( 1u, texture1 );
165 Geometry geometry = CreateGeometry();
167 mRenderer = Renderer::New( geometry, shader );
168 mRenderer.SetTextures( textureSet );
170 mMeshActor = Actor::New();
171 mMeshActor.AddRenderer( mRenderer );
172 mMeshActor.SetSize(400, 400);
174 mMeshActor.RegisterProperty( "uFadeColor", Color::GREEN );
176 mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
177 mRenderer.RegisterProperty( "uPointSize", 80.0f );
178 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
180 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
181 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
182 stage.Add( mMeshActor );
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 ));
189 animation.AnimateBy( Property(mMeshActor, Actor::Property::ORIENTATION), Quaternion(Degree(360), Vector3::ZAXIS) );
191 animation.SetLooping(true);
194 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
198 * Invoked whenever the quit button is clicked
199 * @param[in] button the quit button
201 bool OnQuitButtonClicked( Toolkit::Button button )
203 // quit the application
208 void OnKeyEvent(const KeyEvent& event)
210 if(event.state == KeyEvent::Down)
212 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
221 Application& mApplication; ///< Application instance
222 Vector3 mStageSize; ///< The size of the stage
228 Timer mChangeImageTimer;
231 void RunTest( Application& application )
233 ExampleController test( application );
235 application.MainLoop();
238 // Entry point for Linux & SLP applications
240 int DALI_EXPORT_API main( int argc, char **argv )
242 Application application = Application::New( &argc, &argv );
244 RunTest( application );