2 * Copyright (c) 2017 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.
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
22 using namespace Toolkit;
30 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
31 attribute mediump vec3 aPosition;\n // DALi shader builtin
32 attribute mediump vec3 aColor;\n // DALi shader builtin
33 uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin
34 uniform mediump vec3 uSize;\n // DALi shader builtin
36 varying mediump vec4 vColor;\n
40 mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n
41 vertexPosition.xyz *= uSize;\n
42 vColor = vec4( aColor, 1.0 );\n
43 gl_Position = uMvpMatrix * vertexPosition;\n
50 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
51 varying mediump vec4 vColor;\n
55 gl_FragColor = vColor;\n
61 // This example shows how to create a cube with colors on each side
63 class DrawCubeController : public ConnectionTracker
67 DrawCubeController( Application& application )
68 : mApplication( application )
70 // Connect to the Application's Init signal
71 mApplication.InitSignal().Connect( this, &DrawCubeController::Create );
76 // Nothing to do here;
79 // The Init signal is received once (only) during the Application lifetime
80 void Create( Application& application )
82 // Get a handle to the stage
83 Stage stage = Stage::GetCurrent();
84 stage.SetBackgroundColor( Color::WHITE );
86 // Step 1. Create shader
89 // Step 2. Prepare geometry
92 // Step 3. Create a renderer
95 // Step 4. Create an Actor
98 // Step 5. Play animation to rotate the cube
101 // Respond to a click anywhere on the stage
102 stage.GetRootLayer().TouchSignal().Connect( this, &DrawCubeController::OnTouch );
105 bool OnTouch( Actor actor, const TouchData& touch )
107 // quit the application
113 * This function creates a cube geometry including texture coordinates.
114 * Also it demonstrates using the indexed draw feature by setting an index array.
116 void CreateCubeGeometry()
124 const Vector3 COLOR0( 1.0f, 1.0f, 0.0f );
125 const Vector3 COLOR1( 0.0f, 1.0f, 1.0f );
126 const Vector3 COLOR2( 1.0f, 0.0f, 1.0f );
127 const Vector3 COLOR3( 0.0f, 1.0f, 0.0f );
128 const Vector3 COLOR4( 0.0f, 0.0f, 1.0f );
129 const Vector3 COLOR5( 1.0f, 0.0f, 0.0f );
131 Vertex vertices[] = {
132 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR5 },
133 { Vector3( -1.0f, 1.0f,-1.0f ), COLOR5 },
134 { Vector3( 1.0f, 1.0f,-1.0f ), COLOR5 },
135 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR3 },
136 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR3 },
137 { Vector3( 1.0f, 1.0f, 1.0f ), COLOR3 },
138 { Vector3( 1.0f, 1.0f, 1.0f ), COLOR4 },
139 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR4 },
140 { Vector3( 1.0f, 1.0f,-1.0f ), COLOR4 },
141 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR1 },
142 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR1 },
143 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR1 },
144 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR0 },
145 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR0 },
146 { Vector3( -1.0f, 1.0f,-1.0f ), COLOR0 },
147 { Vector3( 1.0f, 1.0f,-1.0f ), COLOR2 },
148 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR2 },
149 { Vector3( 1.0f, 1.0f, 1.0f ), COLOR2 },
150 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR5 },
151 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR5 },
152 { Vector3( -1.0f, 1.0f,-1.0f ), COLOR5 },
153 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR3 },
154 { Vector3( -1.0f,-1.0f, 1.0f ), COLOR3 },
155 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR3 },
156 { Vector3( 1.0f, 1.0f, 1.0f ), COLOR4 },
157 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR4 },
158 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR4 },
159 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR1 },
160 { Vector3( -1.0f,-1.0f, 1.0f ), COLOR1 },
161 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR1 },
162 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR0 },
163 { Vector3( -1.0f,-1.0f, 1.0f ), COLOR0 },
164 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR0 },
165 { Vector3( 1.0f, 1.0f,-1.0f ), COLOR2 },
166 { Vector3( -1.0f, 1.0f,-1.0f ), COLOR2 },
167 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR2 },
171 map[ "aPosition" ] = Property::VECTOR3;
172 map[ "aColor" ] = Property::VECTOR3;
173 PropertyBuffer vertexBuffer = PropertyBuffer::New( map );
175 vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vertex) );
178 const unsigned short INDEX_CUBE[] = {
192 mGeometry = Geometry::New();
193 mGeometry.AddVertexBuffer( vertexBuffer );
194 mGeometry.SetIndexBuffer( INDEX_CUBE,
195 sizeof(INDEX_CUBE)/sizeof(INDEX_CUBE[0])
197 mGeometry.SetType( Geometry::TRIANGLES );
201 * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER
203 * Shaders are very basic and all they do is transforming vertices and interpolating
204 * input per-vertex color.
206 void CreateCubeShader()
208 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
212 * Function creates renderer. It turns on depth test and depth write.
214 void CreateRenderer()
216 mRenderer = Renderer::New( mGeometry, mShader );
218 // Face culling is enabled to hide the backwards facing sides of the cube
219 // This is sufficient to render a single object; for more complex scenes depth-testing might be required
220 mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
224 * Creates new actor and attaches renderer.
228 Stage stage = Stage::GetCurrent();
230 float quarterStageWidth = stage.GetSize().x * 0.25f;
231 mActor = Actor::New();
232 mActor.SetAnchorPoint( AnchorPoint::CENTER );
233 mActor.SetParentOrigin( ParentOrigin::CENTER );
234 mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) );
235 mActor.SetSize( Vector3( quarterStageWidth, quarterStageWidth, quarterStageWidth ) );
236 mActor.AddRenderer( mRenderer );
245 mAnimation = Animation::New( 5.0f );
246 mAnimation.SetLooping( true );
247 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::ZAXIS ) );
248 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::YAXIS ) );
249 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::XAXIS ) );
254 Application& mApplication;
260 Animation mAnimation;
263 void RunTest( Application& application )
265 DrawCubeController test( application );
267 application.MainLoop();
270 // Entry point for Linux & Tizen applications
272 int DALI_EXPORT_API main( int argc, char **argv )
274 Application application = Application::New( &argc, &argv );
276 RunTest( application );