2 * Copyright (c) 2014 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/devel-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.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 vec2 aTexCoord;
37 varying mediump vec2 vTexCoord;
38 uniform mediump mat4 uMvpMatrix;
39 uniform mediump vec3 uSize;
40 uniform lowp vec4 uFadeColor;
44 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
45 vertexPosition.xyz *= uSize;
46 vertexPosition = uMvpMatrix * vertexPosition;
47 vTexCoord = aTexCoord;
48 gl_Position = vertexPosition;
52 const char* FRAGMENT_SHADER = MAKE_SHADER(
53 varying mediump vec2 vTexCoord;
54 uniform lowp vec4 uColor;
55 uniform sampler2D sTexture;
56 uniform lowp vec4 uFadeColor;
60 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * uFadeColor;
64 Geometry CreateGeometry()
67 const float halfQuadSize = .5f;
68 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
69 TexturedQuadVertex texturedQuadVertexData[4] = {
70 { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
71 { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
72 { Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f) },
73 { Vector2( halfQuadSize, halfQuadSize), Vector2(1.f, 1.f) } };
75 Property::Map texturedQuadVertexFormat;
76 texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
77 texturedQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
78 PropertyBuffer texturedQuadVertices = PropertyBuffer::New( texturedQuadVertexFormat );
79 texturedQuadVertices.SetData( texturedQuadVertexData, 4 );
82 unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
84 // Create the geometry object
85 Geometry texturedQuadGeometry = Geometry::New();
86 texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
87 texturedQuadGeometry.SetIndexBuffer( &indexData[0], sizeof(indexData)/sizeof(indexData[0]) );
89 return texturedQuadGeometry;
93 * Sinusoidal curve starting at zero with 2 cycles
95 float AlphaFunctionSineX2(float progress)
97 return 0.5f - cosf(progress * 4.0f * Math::PI) * 0.5f;
100 } // anonymous namespace
102 // This example shows how to use a simple mesh
104 class ExampleController : public ConnectionTracker
109 * The example controller constructor.
110 * @param[in] application The application instance
112 ExampleController( Application& application )
113 : mApplication( application )
115 // Connect to the Application's Init signal
116 mApplication.InitSignal().Connect( this, &ExampleController::Create );
120 * The example controller destructor
124 // Nothing to do here;
128 * Invoked upon creation of application
129 * @param[in] application The application instance
131 void Create( Application& application )
133 // The Init signal is received once (only) during the Application lifetime
135 Stage stage = Stage::GetCurrent();
136 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
138 mStageSize = stage.GetSize();
140 // Hide the indicator bar
141 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
143 mImage = ResourceImage::New( MATERIAL_SAMPLE );
144 Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
146 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
147 mTextureSet1 = TextureSet::New();
148 mTextureSet1.SetImage( 0u, mImage );
150 mTextureSet2 = TextureSet::New();
151 mTextureSet2.SetImage( 0u, image );
153 mGeometry = CreateGeometry();
155 mRenderer = Renderer::New( mGeometry, mShader );
156 mRenderer.SetTextures( mTextureSet1 );
158 mMeshActor = Actor::New();
159 mMeshActor.AddRenderer( mRenderer );
160 mMeshActor.SetSize(400, 400);
162 Property::Index fadeColorIndex = mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
163 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
165 mMeshActor.SetParentOrigin( ParentOrigin::TOP_CENTER );
166 mMeshActor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
167 stage.Add( mMeshActor );
169 mRenderer2 = Renderer::New( mGeometry, mShader );
170 mRenderer2.SetTextures( mTextureSet2 );
172 mMeshActor2 = Actor::New();
173 mMeshActor2.AddRenderer( mRenderer2 );
174 mMeshActor2.SetSize(400, 400);
176 mMeshActor2.RegisterProperty( "anotherProperty", Color::GREEN );
178 mRenderer2.RegisterProperty( "anotherProperty", Vector3::ZERO );
179 mRenderer2.RegisterProperty( "aCoefficient", 0.008f );
180 Property::Index fadeColorIndex2 = mRenderer2.RegisterProperty( "uFadeColor", Color::BLUE );
181 mRenderer2.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
183 mMeshActor2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
184 mMeshActor2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
185 stage.Add( mMeshActor2 );
187 Animation animation = Animation::New(5);
188 KeyFrames keyFrames = KeyFrames::New();
189 keyFrames.Add(0.0f, Vector4::ZERO);
190 keyFrames.Add(1.0f, Vector4( Color::GREEN ));
192 KeyFrames keyFrames2 = KeyFrames::New();
193 keyFrames2.Add(0.0f, Vector4::ZERO);
194 keyFrames2.Add(1.0f, Color::MAGENTA);
196 animation.AnimateBetween( Property( mRenderer, fadeColorIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
197 animation.AnimateBetween( Property( mRenderer2, fadeColorIndex2 ), keyFrames2, AlphaFunction(AlphaFunctionSineX2) );
198 animation.SetLooping(true);
201 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
204 BufferImage CreateBufferImage()
206 BufferImage image = BufferImage::New( 200, 200, Pixel::RGB888 );
207 PixelBuffer* pixelBuffer = image.GetBuffer();
208 unsigned int stride = image.GetBufferStride();
209 for( unsigned int x=0; x<200; x++ )
211 for( unsigned int y=0; y<200; y++ )
213 PixelBuffer* pixel = pixelBuffer + y*stride + x*3;
214 if( ((int)(x/20.0f))%2 + ((int)(y/20.0f)%2) == 1 )
235 * Invoked whenever the quit button is clicked
236 * @param[in] button the quit button
238 bool OnQuitButtonClicked( Toolkit::Button button )
240 // quit the application
245 void OnKeyEvent(const KeyEvent& event)
247 if(event.state == KeyEvent::Down)
249 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
258 Application& mApplication; ///< Application instance
259 Vector3 mStageSize; ///< The size of the stage
263 TextureSet mTextureSet1;
264 TextureSet mTextureSet2;
270 Timer mChangeImageTimer;
273 void RunTest( Application& application )
275 ExampleController test( application );
277 application.MainLoop();
280 // Entry point for Linux & SLP applications
282 int DALI_EXPORT_API main( int argc, char **argv )
284 Application application = Application::New( &argc, &argv );
286 RunTest( application );