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 );
104 // Respond to key events
105 stage.KeyEventSignal().Connect( this, &DrawCubeController::OnKeyEvent );
108 bool OnTouch( Actor actor, const TouchData& touch )
110 // quit the application
116 * @brief Called when any key event is received
118 * Will use this to quit the application if Back or the Escape key is received
119 * @param[in] event The key event information
121 void OnKeyEvent( const KeyEvent& event )
123 if( event.state == KeyEvent::Down )
125 if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
133 * This function creates a cube geometry including texture coordinates.
134 * Also it demonstrates using the indexed draw feature by setting an index array.
136 void CreateCubeGeometry()
144 const Vector3 COLOR0( 1.0f, 1.0f, 0.0f );
145 const Vector3 COLOR1( 0.0f, 1.0f, 1.0f );
146 const Vector3 COLOR2( 1.0f, 0.0f, 1.0f );
147 const Vector3 COLOR3( 0.0f, 1.0f, 0.0f );
148 const Vector3 COLOR4( 0.0f, 0.0f, 1.0f );
149 const Vector3 COLOR5( 1.0f, 0.0f, 0.0f );
151 Vertex vertices[] = {
152 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR5 },
153 { Vector3( -1.0f, 1.0f,-1.0f ), COLOR5 },
154 { Vector3( 1.0f, 1.0f,-1.0f ), COLOR5 },
155 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR3 },
156 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR3 },
157 { Vector3( 1.0f, 1.0f, 1.0f ), COLOR3 },
158 { Vector3( 1.0f, 1.0f, 1.0f ), COLOR4 },
159 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR4 },
160 { Vector3( 1.0f, 1.0f,-1.0f ), COLOR4 },
161 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR1 },
162 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR1 },
163 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR1 },
164 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR0 },
165 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR0 },
166 { Vector3( -1.0f, 1.0f,-1.0f ), COLOR0 },
167 { Vector3( 1.0f, 1.0f,-1.0f ), COLOR2 },
168 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR2 },
169 { Vector3( 1.0f, 1.0f, 1.0f ), COLOR2 },
170 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR5 },
171 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR5 },
172 { Vector3( -1.0f, 1.0f,-1.0f ), COLOR5 },
173 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR3 },
174 { Vector3( -1.0f,-1.0f, 1.0f ), COLOR3 },
175 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR3 },
176 { Vector3( 1.0f, 1.0f, 1.0f ), COLOR4 },
177 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR4 },
178 { Vector3( 1.0f,-1.0f,-1.0f ), COLOR4 },
179 { Vector3( 1.0f,-1.0f, 1.0f ), COLOR1 },
180 { Vector3( -1.0f,-1.0f, 1.0f ), COLOR1 },
181 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR1 },
182 { Vector3( -1.0f,-1.0f,-1.0f ), COLOR0 },
183 { Vector3( -1.0f,-1.0f, 1.0f ), COLOR0 },
184 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR0 },
185 { Vector3( 1.0f, 1.0f,-1.0f ), COLOR2 },
186 { Vector3( -1.0f, 1.0f,-1.0f ), COLOR2 },
187 { Vector3( -1.0f, 1.0f, 1.0f ), COLOR2 },
190 PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map()
191 .Add( "aPosition", Property::VECTOR3 )
192 .Add( "aColor", Property::VECTOR3 ) );
193 vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vertex) );
196 const unsigned short INDEX_CUBE[] = {
210 mGeometry = Geometry::New();
211 mGeometry.AddVertexBuffer( vertexBuffer );
212 mGeometry.SetIndexBuffer( INDEX_CUBE,
213 sizeof(INDEX_CUBE)/sizeof(INDEX_CUBE[0])
215 mGeometry.SetType( Geometry::TRIANGLES );
219 * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER
221 * Shaders are very basic and all they do is transforming vertices and interpolating
222 * input per-vertex color.
224 void CreateCubeShader()
226 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
230 * Function creates renderer. It turns on depth test and depth write.
232 void CreateRenderer()
234 mRenderer = Renderer::New( mGeometry, mShader );
236 // Face culling is enabled to hide the backwards facing sides of the cube
237 // This is sufficient to render a single object; for more complex scenes depth-testing might be required
238 mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
242 * Creates new actor and attaches renderer.
246 Stage stage = Stage::GetCurrent();
248 float quarterStageWidth = stage.GetSize().x * 0.25f;
249 mActor = Actor::New();
250 mActor.SetAnchorPoint( AnchorPoint::CENTER );
251 mActor.SetParentOrigin( ParentOrigin::CENTER );
252 mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) );
253 mActor.SetSize( Vector3( quarterStageWidth, quarterStageWidth, quarterStageWidth ) );
254 mActor.AddRenderer( mRenderer );
263 mAnimation = Animation::New( 5.0f );
264 mAnimation.SetLooping( true );
265 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::ZAXIS ) );
266 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::YAXIS ) );
267 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::XAXIS ) );
272 Application& mApplication;
278 Animation mAnimation;
281 int DALI_EXPORT_API main( int argc, char **argv )
283 Application application = Application::New( &argc, &argv );
284 DrawCubeController test( application );
285 application.MainLoop();