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.
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( 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 mediump float uPointSize;
41 uniform lowp vec4 uFadeColor;
42 varying mediump vec3 vVertexColor;
43 varying mediump float vHue;
47 vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
48 vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
49 return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
54 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
55 vertexPosition.xyz *= (uSize-uPointSize);
56 vertexPosition = uMvpMatrix * vertexPosition;
57 vVertexColor = hsv2rgb( vec3( aHue, 0.7, 1.0 ) );
59 gl_PointSize = uPointSize;
60 gl_Position = vertexPosition;
64 const char* FRAGMENT_SHADER = MAKE_SHADER(
65 varying mediump vec3 vVertexColor;
66 varying mediump float vHue;
67 uniform lowp vec4 uColor;
68 uniform sampler2D sTexture1;
69 uniform sampler2D sTexture2;
70 uniform lowp vec4 uFadeColor;
74 mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord);
75 mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord);
76 gl_FragColor = vec4(vVertexColor, 1.0) * ((texCol1*vHue) + (texCol2*(1.0-vHue)));
80 Geometry CreateGeometry()
83 struct Vertex { Vector2 position; float hue; };
85 unsigned int numSides = 20;
86 Vertex polyhedraVertexData[numSides];
88 float sectorAngle = 2.0f * Math::PI / (float) numSides;
90 for(unsigned int i=0; i<numSides; ++i)
92 polyhedraVertexData[i].position.x = sinf(angle)*0.5f;
93 polyhedraVertexData[i].position.y = cosf(angle)*0.5f;
94 polyhedraVertexData[i].hue = angle / ( 2.0f * Math::PI);
98 Property::Map polyhedraVertexFormat;
99 polyhedraVertexFormat["aPosition"] = Property::VECTOR2;
100 polyhedraVertexFormat["aHue"] = Property::FLOAT;
101 PropertyBuffer polyhedraVertices = PropertyBuffer::New( polyhedraVertexFormat, numSides );
102 polyhedraVertices.SetData(polyhedraVertexData);
104 // Create the geometry object
105 Geometry polyhedraGeometry = Geometry::New();
106 polyhedraGeometry.AddVertexBuffer( polyhedraVertices );
107 polyhedraGeometry.SetGeometryType( Geometry::POINTS );
109 return polyhedraGeometry;
112 } // anonymous namespace
114 // This example shows how to use a simple mesh
116 class ExampleController : public ConnectionTracker
121 * The example controller constructor.
122 * @param[in] application The application instance
124 ExampleController( Application& application )
125 : mApplication( application )
127 // Connect to the Application's Init signal
128 mApplication.InitSignal().Connect( this, &ExampleController::Create );
132 * The example controller destructor
136 // Nothing to do here;
140 * Invoked upon creation of application
141 * @param[in] application The application instance
143 void Create( Application& application )
145 Stage stage = Stage::GetCurrent();
146 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
148 mStageSize = stage.GetSize();
150 // The Init signal is received once (only) during the Application lifetime
152 // Hide the indicator bar
153 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
155 mImage = ResourceImage::New( MATERIAL_SAMPLE );
156 Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
157 mSampler1 = Sampler::New(mImage, "sTexture1");
158 mSampler2 = Sampler::New(image, "sTexture2");
160 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
162 mMaterial = Material::New( mShader );
163 mMaterial.AddSampler( mSampler1 );
164 mMaterial.AddSampler( mSampler2 );
166 mGeometry = CreateGeometry();
168 mRenderer = Renderer::New( mGeometry, mMaterial );
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.SetDepthIndex(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
234 Timer mChangeImageTimer;
237 void RunTest( Application& application )
239 ExampleController test( application );
241 application.MainLoop();
244 // Entry point for Linux & SLP applications
246 int main( int argc, char **argv )
248 Application application = Application::New( &argc, &argv );
250 RunTest( application );