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 },
170 PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map()
171 .Add( "aPosition", Property::VECTOR3 )
172 .Add( "aColor", Property::VECTOR3 ) );
173 vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vertex) );
176 const unsigned short INDEX_CUBE[] = {
190 mGeometry = Geometry::New();
191 mGeometry.AddVertexBuffer( vertexBuffer );
192 mGeometry.SetIndexBuffer( INDEX_CUBE,
193 sizeof(INDEX_CUBE)/sizeof(INDEX_CUBE[0])
195 mGeometry.SetType( Geometry::TRIANGLES );
199 * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER
201 * Shaders are very basic and all they do is transforming vertices and interpolating
202 * input per-vertex color.
204 void CreateCubeShader()
206 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
210 * Function creates renderer. It turns on depth test and depth write.
212 void CreateRenderer()
214 mRenderer = Renderer::New( mGeometry, mShader );
216 // Face culling is enabled to hide the backwards facing sides of the cube
217 // This is sufficient to render a single object; for more complex scenes depth-testing might be required
218 mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
222 * Creates new actor and attaches renderer.
226 Stage stage = Stage::GetCurrent();
228 float quarterStageWidth = stage.GetSize().x * 0.25f;
229 mActor = Actor::New();
230 mActor.SetAnchorPoint( AnchorPoint::CENTER );
231 mActor.SetParentOrigin( ParentOrigin::CENTER );
232 mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) );
233 mActor.SetSize( Vector3( quarterStageWidth, quarterStageWidth, quarterStageWidth ) );
234 mActor.AddRenderer( mRenderer );
243 mAnimation = Animation::New( 5.0f );
244 mAnimation.SetLooping( true );
245 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::ZAXIS ) );
246 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::YAXIS ) );
247 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::XAXIS ) );
252 Application& mApplication;
258 Animation mAnimation;
261 void RunTest( Application& application )
263 DrawCubeController test( application );
265 application.MainLoop();
268 // Entry point for Linux & Tizen applications
270 int DALI_EXPORT_API main( int argc, char **argv )
272 Application application = Application::New( &argc, &argv );
274 RunTest( application );