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, 4 );
79 texturedQuadVertices.SetData(texturedQuadVertexData);
82 unsigned int indexData[6] = { 0, 3, 1, 0, 2, 3 };
83 Property::Map indexFormat;
84 indexFormat["indices"] = Property::INTEGER;
85 PropertyBuffer indices = PropertyBuffer::New( indexFormat, sizeof(indexData)/sizeof(indexData[0]) );
86 indices.SetData(indexData);
88 // Create the geometry object
89 Geometry texturedQuadGeometry = Geometry::New();
90 texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
91 texturedQuadGeometry.SetIndexBuffer( indices );
93 return texturedQuadGeometry;
97 * Sinusoidal curve starting at zero with 2 cycles
99 float AlphaFunctionSineX2(float progress)
101 return 0.5f - cosf(progress * 4.0f * Math::PI) * 0.5f;
104 } // anonymous namespace
106 // This example shows how to use a simple mesh
108 class ExampleController : public ConnectionTracker
113 * The example controller constructor.
114 * @param[in] application The application instance
116 ExampleController( Application& application )
117 : mApplication( application )
119 // Connect to the Application's Init signal
120 mApplication.InitSignal().Connect( this, &ExampleController::Create );
124 * The example controller destructor
128 // Nothing to do here;
132 * Invoked upon creation of application
133 * @param[in] application The application instance
135 void Create( Application& application )
137 // The Init signal is received once (only) during the Application lifetime
139 Stage stage = Stage::GetCurrent();
140 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
142 mStageSize = stage.GetSize();
144 // Hide the indicator bar
145 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
147 mImage = ResourceImage::New( MATERIAL_SAMPLE );
148 Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
150 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
151 mMaterial1 = Material::New( mShader );
152 mMaterial1.AddTexture(mImage, "sTexture");
154 mMaterial2 = Material::New( mShader );
155 mMaterial2.AddTexture(image, "sTexture");
157 mGeometry = CreateGeometry();
159 mRenderer = Renderer::New( mGeometry, mMaterial1 );
161 mMeshActor = Actor::New();
162 mMeshActor.AddRenderer( mRenderer );
163 mMeshActor.SetSize(400, 400);
165 Property::Index fadeColorIndex = mRenderer.RegisterProperty( "uFadeColor", Color::MAGENTA );
166 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
168 mMeshActor.SetParentOrigin( ParentOrigin::TOP_CENTER );
169 mMeshActor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
170 stage.Add( mMeshActor );
172 mRenderer2 = Renderer::New( mGeometry, mMaterial2 );
174 mMeshActor2 = Actor::New();
175 mMeshActor2.AddRenderer( mRenderer2 );
176 mMeshActor2.SetSize(400, 400);
178 mMeshActor2.RegisterProperty( "anotherProperty", Color::GREEN );
180 mRenderer2.RegisterProperty( "anotherProperty", Vector3::ZERO );
181 mRenderer2.RegisterProperty( "aCoefficient", 0.008f );
182 Property::Index fadeColorIndex2 = mRenderer2.RegisterProperty( "uFadeColor", Color::BLUE );
183 mRenderer2.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
185 mMeshActor2.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
186 mMeshActor2.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
187 stage.Add( mMeshActor2 );
189 Animation animation = Animation::New(5);
190 KeyFrames keyFrames = KeyFrames::New();
191 keyFrames.Add(0.0f, Vector4::ZERO);
192 keyFrames.Add(1.0f, Vector4( Color::GREEN ));
194 KeyFrames keyFrames2 = KeyFrames::New();
195 keyFrames2.Add(0.0f, Vector4::ZERO);
196 keyFrames2.Add(1.0f, Color::MAGENTA);
198 animation.AnimateBetween( Property( mRenderer, fadeColorIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
199 animation.AnimateBetween( Property( mRenderer2, fadeColorIndex2 ), keyFrames2, AlphaFunction(AlphaFunctionSineX2) );
200 animation.SetLooping(true);
203 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
206 BufferImage CreateBufferImage()
208 BufferImage image = BufferImage::New( 200, 200, Pixel::RGB888 );
209 PixelBuffer* pixelBuffer = image.GetBuffer();
210 unsigned int stride = image.GetBufferStride();
211 for( unsigned int x=0; x<200; x++ )
213 for( unsigned int y=0; y<200; y++ )
215 PixelBuffer* pixel = pixelBuffer + y*stride + x*3;
216 if( ((int)(x/20.0f))%2 + ((int)(y/20.0f)%2) == 1 )
237 * Invoked whenever the quit button is clicked
238 * @param[in] button the quit button
240 bool OnQuitButtonClicked( Toolkit::Button button )
242 // quit the application
247 void OnKeyEvent(const KeyEvent& event)
249 if(event.state == KeyEvent::Down)
251 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
260 Application& mApplication; ///< Application instance
261 Vector3 mStageSize; ///< The size of the stage
272 Timer mChangeImageTimer;
275 void RunTest( Application& application )
277 ExampleController test( application );
279 application.MainLoop();
282 // Entry point for Linux & SLP applications
284 int main( int argc, char **argv )
286 Application application = Application::New( &argc, &argv );
288 RunTest( application );