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 vec2 aTexCoord;\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 vec2 vTexCoord;\n
39 mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n
40 vertexPosition.xyz *= uSize;\n
41 vTexCoord = aTexCoord;\n
42 gl_Position = uMvpMatrix * vertexPosition;\n
49 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
50 uniform sampler2D uTexture;\n
52 varying mediump vec2 vTexCoord;\n
55 mediump vec4 texColor = texture2D( uTexture, vTexCoord );\n
56 gl_FragColor = texColor;\n
60 const char* TEXTURE_URL = DEMO_IMAGE_DIR "wood.png";
64 // This example shows how to create textured cube
66 class TexturedCubeController : public ConnectionTracker
70 TexturedCubeController( Application& application )
71 : mApplication( application )
73 // Connect to the Application's Init signal
74 mApplication.InitSignal().Connect( this, &TexturedCubeController::Create );
77 ~TexturedCubeController()
79 // Nothing to do here;
82 // The Init signal is received once (only) during the Application lifetime
83 void Create( Application& application )
85 // Get a handle to the stage
86 Stage stage = Stage::GetCurrent();
87 stage.SetBackgroundColor( Color::WHITE );
89 // Step 1. Create shader
92 // Step 2. Load a texture
95 // Step 3. Prepare geometry
98 // Step 4. Create a renderer
101 // Step 5. Create an Actor
104 // Step 6. Play animation to rotate the cube
107 // Respond to a click anywhere on the stage
108 stage.GetRootLayer().TouchSignal().Connect( this, &TexturedCubeController::OnTouch );
111 bool OnTouch( Actor actor, const TouchData& touch )
113 // quit the application
119 * @brief CreateCubeGeometry
120 * This function creates a cube geometry including texture coordinates.
121 * Also it demonstrates using the indexed draw feature by setting an index array.
123 void CreateCubeGeometry()
131 Vertex vertices[] = {
132 { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 1.0, 1.0 ) },
133 { Vector3( -1.0f, 1.0f,-1.0f ), Vector2( 0.0, 0.0 ) },
134 { Vector3( 1.0f, 1.0f,-1.0f ), Vector2( 0.0, 1.0 ) },
135 { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 1.0, 1.0 ) },
136 { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 0.0, 0.0 ) },
137 { Vector3( 1.0f, 1.0f, 1.0f ), Vector2( 0.0, 1.0 ) },
138 { Vector3( 1.0f, 1.0f, 1.0f ), Vector2( 1.0, 1.0 ) },
139 { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 0.0, 0.0 ) },
140 { Vector3( 1.0f, 1.0f,-1.0f ), Vector2( 0.0, 1.0 ) },
141 { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 1.0, 1.0 ) },
142 { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 0.0, 0.0 ) },
143 { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 0.0, 1.0 ) },
144 { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 1.0, 1.0 ) },
145 { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 0.0, 0.0 ) },
146 { Vector3( -1.0f, 1.0f,-1.0f ), Vector2( 0.0, 1.0 ) },
147 { Vector3( 1.0f, 1.0f,-1.0f ), Vector2( 1.0, 1.0 ) },
148 { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 0.0, 0.0 ) },
149 { Vector3( 1.0f, 1.0f, 1.0f ), Vector2( 0.0, 1.0 ) },
150 { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 1.0, 1.0 ) },
151 { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 1.0, 0.0 ) },
152 { Vector3( -1.0f, 1.0f,-1.0f ), Vector2( 0.0, 0.0 ) },
153 { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 1.0, 1.0 ) },
154 { Vector3( -1.0f,-1.0f, 1.0f ), Vector2( 1.0, 0.0 ) },
155 { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 0.0, 0.0 ) },
156 { Vector3( 1.0f, 1.0f, 1.0f ), Vector2( 1.0, 1.0 ) },
157 { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 1.0, 0.0 ) },
158 { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 0.0, 0.0 ) },
159 { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 1.0, 1.0 ) },
160 { Vector3( -1.0f,-1.0f, 1.0f ), Vector2( 1.0, 0.0 ) },
161 { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 0.0, 0.0 ) },
162 { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 1.0, 1.0 ) },
163 { Vector3( -1.0f,-1.0f, 1.0f ), Vector2( 1.0, 0.0 ) },
164 { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 0.0, 0.0 ) },
165 { Vector3( 1.0f, 1.0f,-1.0f ), Vector2( 1.0, 1.0 ) },
166 { Vector3( -1.0f, 1.0f,-1.0f ), Vector2( 1.0, 0.0 ) },
167 { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 0.0, 0.0 ) },
170 PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map()
171 .Add( "aPosition", Property::VECTOR3 )
172 .Add( "aTexCoord", Property::VECTOR2 ) );
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]) );
194 mGeometry.SetType( Geometry::TRIANGLES );
198 * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER
200 * Shaders are very basic and all they do is transforming vertices and sampling
203 void CreateCubeShader()
205 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
209 * This function loads a pixel data from a file. In order to load it we use SyncImageLoader utility.
210 * If loading succeeds returned PixelData object can be used to create a texture.
211 * Texture must be uploaded. In the end the texture must be set on the TextureSet object.
215 // Load image from file
216 PixelData pixels = SyncImageLoader::Load( TEXTURE_URL );
218 Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
219 texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() );
222 mTextureSet = TextureSet::New();
223 mTextureSet.SetTexture( 0, texture );
227 * Function creates renderer. It turns on depth test and depth write.
229 void CreateRenderer()
231 mRenderer = Renderer::New( mGeometry, mShader );
232 mRenderer.SetTextures( mTextureSet );
234 // Face culling is enabled to hide the backwards facing sides of the cube
235 // This is sufficient to render a single object; for more complex scenes depth-testing might be required
236 mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
240 * Creates new actor and attaches renderer.
244 Stage stage = Stage::GetCurrent();
246 float quarterStageWidth = stage.GetSize().x * 0.25f;
247 mActor = Actor::New();
248 mActor.SetAnchorPoint( AnchorPoint::CENTER );
249 mActor.SetParentOrigin( ParentOrigin::CENTER );
250 mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) );
251 mActor.SetSize( Vector3( quarterStageWidth, quarterStageWidth, quarterStageWidth ) );
252 mActor.AddRenderer( mRenderer );
261 mAnimation = Animation::New( 5.0f );
262 mAnimation.SetLooping( true );
263 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::ZAXIS ) );
264 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::YAXIS ) );
265 mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::XAXIS ) );
270 Application& mApplication;
275 TextureSet mTextureSet;
277 Animation mAnimation;
280 void RunTest( Application& application )
282 TexturedCubeController test( application );
284 application.MainLoop();
287 // Entry point for Linux & Tizen applications
289 int DALI_EXPORT_API main( int argc, char **argv )
291 Application application = Application::New( &argc, &argv );
293 RunTest( application );