2 * Copyright (c) 2015 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.
21 #include "shared/view.h"
23 #include <dali-toolkit/dali-toolkit.h>
29 const char* MATERIAL_SAMPLE( DALI_IMAGE_DIR "gallery-small-48.jpg" );
30 const char* MATERIAL_SAMPLE2( DALI_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 lowp vec4 uFadeColor;
41 varying mediump vec3 vVertexColor;
42 varying mediump float vHue;
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);
53 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
54 vertexPosition.xyz *= uSize;
55 vertexPosition = uMvpMatrix * vertexPosition;
56 vVertexColor = hsv2rgb( vec3( aHue, 0.6, 0.7 ) );
59 gl_Position = vertexPosition;
63 const char* FRAGMENT_SHADER = MAKE_SHADER(
64 varying mediump vec3 vVertexColor;
65 varying mediump float vHue;
66 uniform lowp vec4 uColor;
67 uniform sampler2D sTexture1;
68 uniform sampler2D sTexture2;
69 uniform lowp vec4 uFadeColor;
73 mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord);
74 mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord);
75 gl_FragColor = vec4(vVertexColor, 1.0) * ((texCol1*vHue) + (texCol2*(1.0-vHue)));
79 Geometry CreateGeometry()
82 struct Vertex { Vector2 position; float hue; };
84 unsigned int numSides = 20;
85 Vertex polyhedraVertexData[numSides];
87 float sectorAngle = 2.0f * Math::PI / (float) numSides;
88 for(unsigned int i=0; i<numSides; ++i)
90 polyhedraVertexData[i].position.x = sinf(angle);
91 polyhedraVertexData[i].position.y = cosf(angle);
92 polyhedraVertexData[i].hue = angle / ( 2.0f * Math::PI);
96 Property::Map polyhedraVertexFormat;
97 polyhedraVertexFormat["aPosition"] = Property::VECTOR2;
98 polyhedraVertexFormat["aHue"] = Property::FLOAT;
99 PropertyBuffer polyhedraVertices = PropertyBuffer::New( PropertyBuffer::STATIC, polyhedraVertexFormat, numSides );
100 polyhedraVertices.SetData(polyhedraVertexData);
102 // Create the geometry object
103 Geometry polyhedraGeometry = Geometry::New();
104 polyhedraGeometry.AddVertexBuffer( polyhedraVertices );
105 polyhedraGeometry.SetGeometryType( Geometry::POINTS );
107 return polyhedraGeometry;
110 } // anonymous namespace
112 // This example shows how to use a simple mesh
114 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 Stage stage = Stage::GetCurrent();
144 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
146 mStageSize = stage.GetSize();
148 // The Init signal is received once (only) during the Application lifetime
150 // Hide the indicator bar
151 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
153 mImage = ResourceImage::New( MATERIAL_SAMPLE );
154 Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
155 mSampler1 = Sampler::New(mImage, "sTexture1");
156 mSampler2 = Sampler::New(image, "sTexture2");
158 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
160 mMaterial = Material::New( mShader );
161 mMaterial.AddSampler( mSampler1 );
162 mMaterial.AddSampler( mSampler2 );
164 mGeometry = CreateGeometry();
166 mRenderer = Renderer::New( mGeometry, mMaterial );
168 mMeshActor = Actor::New();
169 mMeshActor.AddRenderer( mRenderer );
170 mMeshActor.SetSize(200, 200);
172 Property::Index fadeColorIndex = mMeshActor.RegisterProperty( "fade-color", Color::GREEN );
173 mMeshActor.AddUniformMapping( fadeColorIndex, std::string("uFadeColor") );
175 fadeColorIndex = mRenderer.RegisterProperty( "fade-color", Color::MAGENTA );
176 mRenderer.AddUniformMapping( fadeColorIndex, std::string("uFadeColor" ) );
177 mRenderer.SetDepthIndex(0);
179 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
180 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
181 stage.Add( mMeshActor );
183 Animation animation = Animation::New(15);
184 KeyFrames keyFrames = KeyFrames::New();
185 keyFrames.Add(0.0f, Vector4::ZERO);
186 keyFrames.Add(1.0f, Vector4( 1.0f, 0.0f, 1.0f, 1.0f ));
188 animation.RotateBy( mMeshActor, Degree(360), Vector3::ZAXIS );
190 animation.SetLooping(true);
193 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));;
197 * Invoked whenever the quit button is clicked
198 * @param[in] button the quit button
200 bool OnQuitButtonClicked( Toolkit::Button button )
202 // quit the application
207 void OnKeyEvent(const KeyEvent& event)
209 if(event.state == KeyEvent::Down)
211 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
220 Application& mApplication; ///< Application instance
221 Vector3 mStageSize; ///< The size of the stage
233 Timer mChangeImageTimer;
236 void RunTest( Application& application )
238 ExampleController test( application );
240 application.MainLoop();
243 // Entry point for Linux & SLP applications
245 int main( int argc, char **argv )
247 Application application = Application::New( &argc, &argv );
249 RunTest( application );