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 vec2 aTexCoord;
38 varying mediump vec2 vTexCoord;
39 uniform mediump mat4 uMvpMatrix;
40 uniform mediump vec3 uSize;
41 uniform lowp vec4 uFadeColor;
45 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
46 vertexPosition.xyz *= uSize;
47 vertexPosition = uMvpMatrix * vertexPosition;
48 vTexCoord = aTexCoord;
49 gl_Position = vertexPosition;
53 const char* FRAGMENT_SHADER = MAKE_SHADER(
54 varying mediump vec2 vTexCoord;
55 uniform lowp vec4 uColor;
56 uniform sampler2D sTexture;
57 uniform lowp vec4 uFadeColor;
61 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * uFadeColor;
66 * Sinusoidal curve starting at zero with 2 cycles
68 float AlphaFunctionSineX2(float progress)
70 return 0.5f - cosf(progress * 4.0f * Math::PI) * 0.5f;
73 } // anonymous namespace
75 // This example shows how to use a simple mesh
77 class ExampleController : public ConnectionTracker
82 * The example controller constructor.
83 * @param[in] application The application instance
85 ExampleController( Application& application )
86 : mApplication( application )
88 // Connect to the Application's Init signal
89 mApplication.InitSignal().Connect( this, &ExampleController::Create );
93 * The example controller destructor
97 // Nothing to do here;
101 * Invoked upon creation of application
102 * @param[in] application The application instance
104 void Create( Application& application )
106 // The Init signal is received once (only) during the Application lifetime
108 Stage stage = Stage::GetCurrent();
109 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
111 mStageSize = stage.GetSize();
113 // Hide the indicator bar
114 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
116 Texture texture1 = DemoHelper::LoadTexture( MATERIAL_SAMPLE );
117 Texture texture2 = DemoHelper::LoadTexture( MATERIAL_SAMPLE2 );
119 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
120 mTextureSet1 = TextureSet::New();
121 mTextureSet1.SetTexture( 0u, texture1 );
123 mTextureSet2 = TextureSet::New();
124 mTextureSet2.SetTexture( 0u, texture2 );
126 mGeometry = DemoHelper::CreateTexturedQuad();
128 mRenderer = Renderer::New( mGeometry, mShader );
129 mRenderer.SetTextures( mTextureSet1 );
131 mMeshActor = Actor::New();
132 mMeshActor.AddRenderer( mRenderer );
133 mMeshActor.SetSize(400, 400);
135 Property::Index fadeColorIndex = mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
136 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
138 mMeshActor.SetParentOrigin( ParentOrigin::TOP_CENTER );
139 mMeshActor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
140 stage.Add( mMeshActor );
142 mRenderer2 = Renderer::New( mGeometry, mShader );
143 mRenderer2.SetTextures( mTextureSet2 );
145 mMeshActor2 = Actor::New();
146 mMeshActor2.AddRenderer( mRenderer2 );
147 mMeshActor2.SetSize(400, 400);
149 mMeshActor2.RegisterProperty( "anotherProperty", Color::GREEN );
151 mRenderer2.RegisterProperty( "anotherProperty", Vector3::ZERO );
152 mRenderer2.RegisterProperty( "aCoefficient", 0.008f );
153 Property::Index fadeColorIndex2 = mRenderer2.RegisterProperty( "uFadeColor", Color::BLUE );
154 mRenderer2.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
156 mMeshActor2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
157 mMeshActor2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
158 stage.Add( mMeshActor2 );
160 Animation animation = Animation::New(5);
161 KeyFrames keyFrames = KeyFrames::New();
162 keyFrames.Add(0.0f, Vector4::ZERO);
163 keyFrames.Add(1.0f, Vector4( Color::GREEN ));
165 KeyFrames keyFrames2 = KeyFrames::New();
166 keyFrames2.Add(0.0f, Vector4::ZERO);
167 keyFrames2.Add(1.0f, Color::MAGENTA);
169 animation.AnimateBetween( Property( mRenderer, fadeColorIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
170 animation.AnimateBetween( Property( mRenderer2, fadeColorIndex2 ), keyFrames2, AlphaFunction(AlphaFunctionSineX2) );
171 animation.SetLooping(true);
174 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
177 BufferImage CreateBufferImage()
179 BufferImage image = BufferImage::New( 200, 200, Pixel::RGB888 );
180 PixelBuffer* pixelBuffer = image.GetBuffer();
181 unsigned int stride = image.GetBufferStride();
182 for( unsigned int x=0; x<200; x++ )
184 for( unsigned int y=0; y<200; y++ )
186 PixelBuffer* pixel = pixelBuffer + y*stride + x*3;
187 if( ((int)(x/20.0f))%2 + ((int)(y/20.0f)%2) == 1 )
208 * Invoked whenever the quit button is clicked
209 * @param[in] button the quit button
211 bool OnQuitButtonClicked( Toolkit::Button button )
213 // quit the application
218 void OnKeyEvent(const KeyEvent& event)
220 if(event.state == KeyEvent::Down)
222 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
231 Application& mApplication; ///< Application instance
232 Vector3 mStageSize; ///< The size of the stage
235 TextureSet mTextureSet1;
236 TextureSet mTextureSet2;
242 Timer mChangeImageTimer;
245 void RunTest( Application& application )
247 ExampleController test( application );
249 application.MainLoop();
252 // Entry point for Linux & SLP applications
254 int DALI_EXPORT_API main( int argc, char **argv )
256 Application application = Application::New( &argc, &argv );
258 RunTest( application );