From a0e18d232dc3a8a590c0bfd302f4e5787a6159c6 Mon Sep 17 00:00:00 2001 From: "adam.b" Date: Wed, 15 Feb 2017 18:00:24 +0000 Subject: [PATCH] Added Rendering tutorials - Drawing line - Drawing triangle - Drawing colored cube - Drawing textured cube Change-Id: Ief3c47871b0f06ba764fa86a260ad8561c78acd3 --- examples-reel/dali-examples-reel.cpp | 6 +- examples/rendering-cube/rendering-cube.cpp | 277 +++++++++++++++++++ examples/rendering-line/rendering-line.cpp | 185 +++++++++++++ .../rendering-textured-cube.cpp | 296 +++++++++++++++++++++ examples/rendering-triangle/rendering-triangle.cpp | 186 +++++++++++++ resources/po/as.po | 12 + resources/po/de.po | 12 + resources/po/en_GB.po | 12 + resources/po/en_US.po | 12 + resources/po/es.po | 12 + resources/po/fi.po | 12 + resources/po/ko.po | 12 + resources/po/ml.po | 12 + resources/po/ur.po | 12 + resources/po/zn_CH.po | 12 + shared/dali-demo-strings.h | 8 + 16 files changed, 1077 insertions(+), 1 deletion(-) create mode 100644 examples/rendering-cube/rendering-cube.cpp create mode 100644 examples/rendering-line/rendering-line.cpp create mode 100644 examples/rendering-textured-cube/rendering-textured-cube.cpp create mode 100644 examples/rendering-triangle/rendering-triangle.cpp diff --git a/examples-reel/dali-examples-reel.cpp b/examples-reel/dali-examples-reel.cpp index 9f29f97..6dc8f5b 100644 --- a/examples-reel/dali-examples-reel.cpp +++ b/examples-reel/dali-examples-reel.cpp @@ -61,6 +61,10 @@ int DALI_EXPORT_API main(int argc, char **argv) demo.AddExample(Example("popup.example", DALI_DEMO_STR_TITLE_POPUP)); demo.AddExample(Example("primitive-shapes.example", DALI_DEMO_STR_TITLE_PRIMITIVE_SHAPES)); demo.AddExample(Example("progress-bar.example", DALI_DEMO_STR_TITLE_PROGRESS_BAR)); + demo.AddExample(Example("rendering-line.example", DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE)); + demo.AddExample(Example("rendering-triangle.example", DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE)); + demo.AddExample(Example("rendering-cube.example", DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE)); + demo.AddExample(Example("rendering-textured-cube.example", DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE)); demo.AddExample(Example("scroll-view.example", DALI_DEMO_STR_TITLE_SCROLL_VIEW)); demo.AddExample(Example("size-negotiation.example", DALI_DEMO_STR_TITLE_NEGOTIATE_SIZE)); demo.AddExample(Example("styling.example", DALI_DEMO_STR_TITLE_STYLING)); @@ -70,7 +74,7 @@ int DALI_EXPORT_API main(int argc, char **argv) demo.AddExample(Example("text-label-multi-language.example", DALI_DEMO_STR_TITLE_TEXT_LABEL_MULTI_LANGUAGE)); demo.AddExample(Example("text-label-emojis.example", DALI_DEMO_STR_TITLE_EMOJI_TEXT)); demo.AddExample(Example("text-scrolling.example", DALI_DEMO_STR_TITLE_TEXT_SCROLLING)); - demo.AddExample(Example("textured-mesh.example", DALI_DEMO_STR_TITLE_TEXTURED_MESH)); + demo.AddExample(Example("texturedss-mesh.example", DALI_DEMO_STR_TITLE_TEXTURED_MESH)); demo.AddExample(Example("tilt.example", DALI_DEMO_STR_TITLE_TILT_SENSOR)); demo.AddExample(Example("tooltip.example", DALI_DEMO_STR_TITLE_TOOLTIP)); demo.AddExample(Example("transitions.example", DALI_DEMO_STR_TITLE_VISUAL_TRANSITIONS)); diff --git a/examples/rendering-cube/rendering-cube.cpp b/examples/rendering-cube/rendering-cube.cpp new file mode 100644 index 0000000..a42e832 --- /dev/null +++ b/examples/rendering-cube/rendering-cube.cpp @@ -0,0 +1,277 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +using namespace Dali; +using namespace Toolkit; + +namespace +{ + +/* + * Vertex shader + */ +const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( +attribute mediump vec3 aPosition;\n // DALi shader builtin +attribute mediump vec3 aColor;\n // DALi shader builtin +uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin +uniform mediump vec3 uSize;\n // DALi shader builtin +\n +varying mediump vec4 vColor;\n +\n +void main()\n +{\n + mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n + vertexPosition.xyz *= uSize;\n + vColor = vec4( aColor, 1.0 );\n + gl_Position = uMvpMatrix * vertexPosition;\n +}\n +); + +/* + * Fragment shader + */ +const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( +varying mediump vec4 vColor;\n +\n +void main()\n +{\n + gl_FragColor = vColor;\n +}\n +); + +} + +// This example shows how to create a cube with colors on each side +// +class DrawCubeController : public ConnectionTracker +{ +public: + + DrawCubeController( Application& application ) + : mApplication( application ) + { + // Connect to the Application's Init signal + mApplication.InitSignal().Connect( this, &DrawCubeController::Create ); + } + + ~DrawCubeController() + { + // Nothing to do here; + } + + // The Init signal is received once (only) during the Application lifetime + void Create( Application& application ) + { + // Get a handle to the stage + Stage stage = Stage::GetCurrent(); + stage.SetBackgroundColor( Color::WHITE ); + + // Step 1. Create shader + CreateCubeShader(); + + // Step 2. Prepare geometry + CreateCubeGeometry(); + + // Step 3. Create a renderer + CreateRenderer(); + + // Step 4. Create an Actor + CreateActor(); + + // Step 5. Play animation to rotate the cube + PlayAnimation(); + + // Respond to a click anywhere on the stage + stage.GetRootLayer().TouchSignal().Connect( this, &DrawCubeController::OnTouch ); + } + + bool OnTouch( Actor actor, const TouchData& touch ) + { + // quit the application + mApplication.Quit(); + return true; + } + + /** + * This function creates a cube geometry including texture coordinates. + * Also it demonstrates using the indexed draw feature by setting an index array. + */ + void CreateCubeGeometry() + { + struct Vertex + { + Vector3 aPosition; + Vector3 aColor; + }; + + const Vector3 COLOR0( 1.0f, 1.0f, 0.0f ); + const Vector3 COLOR1( 0.0f, 1.0f, 1.0f ); + const Vector3 COLOR2( 1.0f, 0.0f, 1.0f ); + const Vector3 COLOR3( 0.0f, 1.0f, 0.0f ); + const Vector3 COLOR4( 0.0f, 0.0f, 1.0f ); + const Vector3 COLOR5( 1.0f, 0.0f, 0.0f ); + + Vertex vertices[] = { + { Vector3( 1.0f,-1.0f,-1.0f ), COLOR5 }, + { Vector3( -1.0f, 1.0f,-1.0f ), COLOR5 }, + { Vector3( 1.0f, 1.0f,-1.0f ), COLOR5 }, + { Vector3( -1.0f, 1.0f, 1.0f ), COLOR3 }, + { Vector3( 1.0f,-1.0f, 1.0f ), COLOR3 }, + { Vector3( 1.0f, 1.0f, 1.0f ), COLOR3 }, + { Vector3( 1.0f, 1.0f, 1.0f ), COLOR4 }, + { Vector3( 1.0f,-1.0f,-1.0f ), COLOR4 }, + { Vector3( 1.0f, 1.0f,-1.0f ), COLOR4 }, + { Vector3( 1.0f,-1.0f, 1.0f ), COLOR1 }, + { Vector3( -1.0f,-1.0f,-1.0f ), COLOR1 }, + { Vector3( 1.0f,-1.0f,-1.0f ), COLOR1 }, + { Vector3( -1.0f,-1.0f,-1.0f ), COLOR0 }, + { Vector3( -1.0f, 1.0f, 1.0f ), COLOR0 }, + { Vector3( -1.0f, 1.0f,-1.0f ), COLOR0 }, + { Vector3( 1.0f, 1.0f,-1.0f ), COLOR2 }, + { Vector3( -1.0f, 1.0f, 1.0f ), COLOR2 }, + { Vector3( 1.0f, 1.0f, 1.0f ), COLOR2 }, + { Vector3( 1.0f,-1.0f,-1.0f ), COLOR5 }, + { Vector3( -1.0f,-1.0f,-1.0f ), COLOR5 }, + { Vector3( -1.0f, 1.0f,-1.0f ), COLOR5 }, + { Vector3( -1.0f, 1.0f, 1.0f ), COLOR3 }, + { Vector3( -1.0f,-1.0f, 1.0f ), COLOR3 }, + { Vector3( 1.0f,-1.0f, 1.0f ), COLOR3 }, + { Vector3( 1.0f, 1.0f, 1.0f ), COLOR4 }, + { Vector3( 1.0f,-1.0f, 1.0f ), COLOR4 }, + { Vector3( 1.0f,-1.0f,-1.0f ), COLOR4 }, + { Vector3( 1.0f,-1.0f, 1.0f ), COLOR1 }, + { Vector3( -1.0f,-1.0f, 1.0f ), COLOR1 }, + { Vector3( -1.0f,-1.0f,-1.0f ), COLOR1 }, + { Vector3( -1.0f,-1.0f,-1.0f ), COLOR0 }, + { Vector3( -1.0f,-1.0f, 1.0f ), COLOR0 }, + { Vector3( -1.0f, 1.0f, 1.0f ), COLOR0 }, + { Vector3( 1.0f, 1.0f,-1.0f ), COLOR2 }, + { Vector3( -1.0f, 1.0f,-1.0f ), COLOR2 }, + { Vector3( -1.0f, 1.0f, 1.0f ), COLOR2 }, + }; + + PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map() + .Add( "aPosition", Property::VECTOR3 ) + .Add( "aColor", Property::VECTOR3 ) ); + vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vertex) ); + + // create indices + const unsigned short INDEX_CUBE[] = { + 2, 1, 0, + 5, 4, 3, + 8, 7, 6, + 11, 10, 9, + 14, 13, 12, + 17, 16, 15, + 20, 19, 18, + 23, 22, 21, + 26, 25, 24, + 29, 28, 27, + 32, 31, 30, + 35, 34, 33 + }; + mGeometry = Geometry::New(); + mGeometry.AddVertexBuffer( vertexBuffer ); + mGeometry.SetIndexBuffer( INDEX_CUBE, + sizeof(INDEX_CUBE)/sizeof(INDEX_CUBE[0]) + ); + mGeometry.SetType( Geometry::TRIANGLES ); + } + + /** + * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER + * + * Shaders are very basic and all they do is transforming vertices and interpolating + * input per-vertex color. + */ + void CreateCubeShader() + { + mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ); + } + + /** + * Function creates renderer. It turns on depth test and depth write. + */ + void CreateRenderer() + { + mRenderer = Renderer::New( mGeometry, mShader ); + + // Face culling is enabled to hide the backwards facing sides of the cube + // This is sufficient to render a single object; for more complex scenes depth-testing might be required + mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK ); + } + + /** + * Creates new actor and attaches renderer. + */ + void CreateActor() + { + Stage stage = Stage::GetCurrent(); + + float quarterStageWidth = stage.GetSize().x * 0.25f; + mActor = Actor::New(); + mActor.SetAnchorPoint( AnchorPoint::CENTER ); + mActor.SetParentOrigin( ParentOrigin::CENTER ); + mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) ); + mActor.SetSize( Vector3( quarterStageWidth, quarterStageWidth, quarterStageWidth ) ); + mActor.AddRenderer( mRenderer ); + stage.Add( mActor ); + } + + /** + * Plays animation + */ + void PlayAnimation() + { + mAnimation = Animation::New( 5.0f ); + mAnimation.SetLooping( true ); + mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::ZAXIS ) ); + mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::YAXIS ) ); + mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::XAXIS ) ); + mAnimation.Play(); + } + +private: + Application& mApplication; + + Renderer mRenderer; + Shader mShader; + Geometry mGeometry; + Actor mActor; + Animation mAnimation; +}; + +void RunTest( Application& application ) +{ + DrawCubeController test( application ); + + application.MainLoop(); +} + +// Entry point for Linux & Tizen applications +// +int DALI_EXPORT_API main( int argc, char **argv ) +{ + Application application = Application::New( &argc, &argv ); + + RunTest( application ); + + return 0; +} diff --git a/examples/rendering-line/rendering-line.cpp b/examples/rendering-line/rendering-line.cpp new file mode 100644 index 0000000..70d55cc --- /dev/null +++ b/examples/rendering-line/rendering-line.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +using namespace Dali; +using namespace Toolkit; + +namespace +{ + +/* + * Vertex shader + */ +const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( +attribute mediump vec2 aPosition;\n // DALi shader builtin +uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin +uniform mediump vec3 uSize;\n // DALi shader builtin +\n +void main()\n +{\n + mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n + vertexPosition.xyz *= uSize;\n + gl_Position = uMvpMatrix * vertexPosition;\n +}\n +); + +/* + * Fragment shader + */ +const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( +uniform mediump vec4 uColor;\n +\n +void main()\n +{\n + gl_FragColor = uColor;\n +}\n +); + +} + +// This example shows how to draw a line in actor's color +// +class DrawLineController : public ConnectionTracker +{ +public: + + DrawLineController( Application& application ) + : mApplication( application ) + { + // Connect to the Application's Init signal + mApplication.InitSignal().Connect( this, &DrawLineController::Create ); + } + + ~DrawLineController() + { + // Nothing to do here; + } + + // The Init signal is received once (only) during the Application lifetime + void Create( Application& application ) + { + // Get a handle to the stage + Stage stage = Stage::GetCurrent(); + stage.SetBackgroundColor( Color::WHITE ); + + // Step 1. Create shader + CreateLineShader(); + + // Step 2. Prepare geometry + CreateLineGeometry(); + + // Step 3. Create a renderer + CreateRenderer(); + + // Step 4. Create an Actor + CreateActor(); + + // Respond to a click anywhere on the stage + stage.GetRootLayer().TouchSignal().Connect( this, &DrawLineController::OnTouch ); + } + + bool OnTouch( Actor actor, const TouchData& touch ) + { + // quit the application + mApplication.Quit(); + return true; + } + + /** + * This function creates a line geometry made of two vertices in order + * to draw a diagonal line. + */ + void CreateLineGeometry() + { + Vector2 vertices[] = { + Vector2( -1.0f, -1.0f ), + Vector2( 1.0f, 1.0f ) + }; + + PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map() + .Add( "aPosition", Property::VECTOR2 ) ); + vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vector2) ); + + mGeometry = Geometry::New(); + mGeometry.AddVertexBuffer( vertexBuffer ); + mGeometry.SetType( Geometry::LINES ); + } + + /** + * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER + * + * Shaders are very basic and all they do is transforming vertices and applying actor's colour. + */ + void CreateLineShader() + { + mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ); + } + + /** + * Function creates renderer. + */ + void CreateRenderer() + { + mRenderer = Renderer::New( mGeometry, mShader ); + } + + /** + * Creates new actor and attaches renderer. + */ + void CreateActor() + { + Stage stage = Stage::GetCurrent(); + Size size = stage.GetSize() * 0.25f; + mActor = Actor::New(); + mActor.SetAnchorPoint( AnchorPoint::CENTER ); + mActor.SetParentOrigin( ParentOrigin::CENTER ); + mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) ); + mActor.SetColor( Color::BLACK ); + mActor.SetSize( Vector3( size.x, size.x, size.x ) ); + mActor.AddRenderer( mRenderer ); + stage.Add( mActor ); + } + +private: + Application& mApplication; + + Renderer mRenderer; + Shader mShader; + Geometry mGeometry; + Actor mActor; +}; + +void RunTest( Application& application ) +{ + DrawLineController test( application ); + + application.MainLoop(); +} + +// Entry point for Linux & Tizen applications +// +int DALI_EXPORT_API main( int argc, char **argv ) +{ + Application application = Application::New( &argc, &argv ); + + RunTest( application ); + + return 0; +} diff --git a/examples/rendering-textured-cube/rendering-textured-cube.cpp b/examples/rendering-textured-cube/rendering-textured-cube.cpp new file mode 100644 index 0000000..eeda48b --- /dev/null +++ b/examples/rendering-textured-cube/rendering-textured-cube.cpp @@ -0,0 +1,296 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +using namespace Dali; +using namespace Toolkit; + +namespace +{ + +/* + * Vertex shader + */ +const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( +attribute mediump vec3 aPosition;\n // DALi shader builtin +attribute mediump vec2 aTexCoord;\n // DALi shader builtin +uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin +uniform mediump vec3 uSize;\n // DALi shader builtin +\n +varying mediump vec2 vTexCoord;\n +void main()\n +{\n + mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n + vertexPosition.xyz *= uSize;\n + vTexCoord = aTexCoord;\n + gl_Position = uMvpMatrix * vertexPosition;\n +}\n +); + +/* + * Fragment shader + */ +const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( +uniform sampler2D uTexture;\n +\n +varying mediump vec2 vTexCoord;\n +void main()\n +{\n + mediump vec4 texColor = texture2D( uTexture, vTexCoord );\n + gl_FragColor = texColor;\n +}\n +); + +const char* TEXTURE_URL = DEMO_IMAGE_DIR "wood.png"; + +} + +// This example shows how to create textured cube +// +class TexturedCubeController : public ConnectionTracker +{ +public: + + TexturedCubeController( Application& application ) + : mApplication( application ) + { + // Connect to the Application's Init signal + mApplication.InitSignal().Connect( this, &TexturedCubeController::Create ); + } + + ~TexturedCubeController() + { + // Nothing to do here; + } + + // The Init signal is received once (only) during the Application lifetime + void Create( Application& application ) + { + // Get a handle to the stage + Stage stage = Stage::GetCurrent(); + stage.SetBackgroundColor( Color::WHITE ); + + // Step 1. Create shader + CreateCubeShader(); + + // Step 2. Load a texture + CreateTexture(); + + // Step 3. Prepare geometry + CreateCubeGeometry(); + + // Step 4. Create a renderer + CreateRenderer(); + + // Step 5. Create an Actor + CreateActor(); + + // Step 6. Play animation to rotate the cube + PlayAnimation(); + + // Respond to a click anywhere on the stage + stage.GetRootLayer().TouchSignal().Connect( this, &TexturedCubeController::OnTouch ); + } + + bool OnTouch( Actor actor, const TouchData& touch ) + { + // quit the application + mApplication.Quit(); + return true; + } + + /** + * @brief CreateCubeGeometry + * This function creates a cube geometry including texture coordinates. + * Also it demonstrates using the indexed draw feature by setting an index array. + */ + void CreateCubeGeometry() + { + struct Vertex + { + Vector3 aPosition; + Vector2 aTexCoord; + }; + + Vertex vertices[] = { + { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( -1.0f, 1.0f,-1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( 1.0f, 1.0f,-1.0f ), Vector2( 0.0, 1.0 ) }, + { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( 1.0f, 1.0f, 1.0f ), Vector2( 0.0, 1.0 ) }, + { Vector3( 1.0f, 1.0f, 1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( 1.0f, 1.0f,-1.0f ), Vector2( 0.0, 1.0 ) }, + { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 0.0, 1.0 ) }, + { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( -1.0f, 1.0f,-1.0f ), Vector2( 0.0, 1.0 ) }, + { Vector3( 1.0f, 1.0f,-1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( 1.0f, 1.0f, 1.0f ), Vector2( 0.0, 1.0 ) }, + { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 1.0, 0.0 ) }, + { Vector3( -1.0f, 1.0f,-1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( -1.0f,-1.0f, 1.0f ), Vector2( 1.0, 0.0 ) }, + { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( 1.0f, 1.0f, 1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 1.0, 0.0 ) }, + { Vector3( 1.0f,-1.0f,-1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( 1.0f,-1.0f, 1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( -1.0f,-1.0f, 1.0f ), Vector2( 1.0, 0.0 ) }, + { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( -1.0f,-1.0f,-1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( -1.0f,-1.0f, 1.0f ), Vector2( 1.0, 0.0 ) }, + { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 0.0, 0.0 ) }, + { Vector3( 1.0f, 1.0f,-1.0f ), Vector2( 1.0, 1.0 ) }, + { Vector3( -1.0f, 1.0f,-1.0f ), Vector2( 1.0, 0.0 ) }, + { Vector3( -1.0f, 1.0f, 1.0f ), Vector2( 0.0, 0.0 ) }, + }; + + PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map() + .Add( "aPosition", Property::VECTOR3 ) + .Add( "aTexCoord", Property::VECTOR2 ) ); + vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vertex) ); + + // create indices + const unsigned short INDEX_CUBE[] = { + 2, 1, 0, + 5, 4, 3, + 8, 7, 6, + 11, 10, 9, + 14, 13, 12, + 17, 16, 15, + 20, 19, 18, + 23, 22, 21, + 26, 25, 24, + 29, 28, 27, + 32, 31, 30, + 35, 34, 33 + }; + mGeometry = Geometry::New(); + mGeometry.AddVertexBuffer( vertexBuffer ); + mGeometry.SetIndexBuffer( INDEX_CUBE, + sizeof(INDEX_CUBE)/sizeof(INDEX_CUBE[0]) ); + mGeometry.SetType( Geometry::TRIANGLES ); + } + + /** + * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER + * + * Shaders are very basic and all they do is transforming vertices and sampling + * a texture. + */ + void CreateCubeShader() + { + mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ); + } + + /** + * This function loads a pixel data from a file. In order to load it we use SyncImageLoader utility. + * If loading succeeds returned PixelData object can be used to create a texture. + * Texture must be uploaded. In the end the texture must be set on the TextureSet object. + */ + void CreateTexture() + { + // Load image from file + PixelData pixels = SyncImageLoader::Load( TEXTURE_URL ); + + Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() ); + texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() ); + + // create TextureSet + mTextureSet = TextureSet::New(); + mTextureSet.SetTexture( 0, texture ); + } + + /** + * Function creates renderer. It turns on depth test and depth write. + */ + void CreateRenderer() + { + mRenderer = Renderer::New( mGeometry, mShader ); + mRenderer.SetTextures( mTextureSet ); + + // Face culling is enabled to hide the backwards facing sides of the cube + // This is sufficient to render a single object; for more complex scenes depth-testing might be required + mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK ); + } + + /** + * Creates new actor and attaches renderer. + */ + void CreateActor() + { + Stage stage = Stage::GetCurrent(); + + float quarterStageWidth = stage.GetSize().x * 0.25f; + mActor = Actor::New(); + mActor.SetAnchorPoint( AnchorPoint::CENTER ); + mActor.SetParentOrigin( ParentOrigin::CENTER ); + mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) ); + mActor.SetSize( Vector3( quarterStageWidth, quarterStageWidth, quarterStageWidth ) ); + mActor.AddRenderer( mRenderer ); + stage.Add( mActor ); + } + + /** + * Plays animation + */ + void PlayAnimation() + { + mAnimation = Animation::New( 5.0f ); + mAnimation.SetLooping( true ); + mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::ZAXIS ) ); + mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::YAXIS ) ); + mAnimation.AnimateBy( Property( mActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( 360 )), Vector3::XAXIS ) ); + mAnimation.Play(); + } + +private: + Application& mApplication; + + Renderer mRenderer; + Shader mShader; + Geometry mGeometry; + TextureSet mTextureSet; + Actor mActor; + Animation mAnimation; +}; + +void RunTest( Application& application ) +{ + TexturedCubeController test( application ); + + application.MainLoop(); +} + +// Entry point for Linux & Tizen applications +// +int DALI_EXPORT_API main( int argc, char **argv ) +{ + Application application = Application::New( &argc, &argv ); + + RunTest( application ); + + return 0; +} diff --git a/examples/rendering-triangle/rendering-triangle.cpp b/examples/rendering-triangle/rendering-triangle.cpp new file mode 100644 index 0000000..57233f8 --- /dev/null +++ b/examples/rendering-triangle/rendering-triangle.cpp @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +using namespace Dali; +using namespace Toolkit; + +namespace +{ + +/* + * Vertex shader + */ +const char* VERTEX_SHADER = DALI_COMPOSE_SHADER( +attribute mediump vec2 aPosition;\n // DALi shader builtin +uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin +uniform mediump vec3 uSize;\n // DALi shader builtin +\n +void main()\n +{\n + mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n + vertexPosition.xyz *= uSize;\n + gl_Position = uMvpMatrix * vertexPosition;\n +}\n +); + +/* + * Fragment shader + */ +const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( +uniform mediump vec4 uColor;\n +\n +void main()\n +{\n + gl_FragColor = uColor;\n +}\n +); + +} + +// This example shows how to draw a triangle in actor's color +// +class DrawTriangleController : public ConnectionTracker +{ +public: + + DrawTriangleController( Application& application ) + : mApplication( application ) + { + // Connect to the Application's Init signal + mApplication.InitSignal().Connect( this, &DrawTriangleController::Create ); + } + + ~DrawTriangleController() + { + // Nothing to do here; + } + + // The Init signal is received once (only) during the Application lifetime + void Create( Application& application ) + { + // Get a handle to the stage + Stage stage = Stage::GetCurrent(); + stage.SetBackgroundColor( Color::WHITE ); + + // Step 1. Create shader + CreateTriangleShader(); + + // Step 2. Prepare geometry + CreateTriangleGeometry(); + + // Step 3. Create a renderer + CreateRenderer(); + + // Step 4. Create an Actor + CreateActor(); + + // Respond to a click anywhere on the stage + stage.GetRootLayer().TouchSignal().Connect( this, &DrawTriangleController::OnTouch ); + } + + bool OnTouch( Actor actor, const TouchData& touch ) + { + // quit the application + mApplication.Quit(); + return true; + } + + /** + * This function creates a triangle geometry made of three vertices in order + * to draw a coloured triangle. + */ + void CreateTriangleGeometry() + { + Vector2 vertices[] = { + Vector2( -1.0f, -1.0f ), + Vector2( 1.0f, 1.0f ), + Vector2( -1.0f, 1.0f ) + }; + + PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map() + .Add( "aPosition", Property::VECTOR2 ) ); + vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vector2) ); + + mGeometry = Geometry::New(); + mGeometry.AddVertexBuffer( vertexBuffer ); + mGeometry.SetType( Geometry::TRIANGLES ); + } + + /** + * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER + * + * Shaders are very basic and all they do is transforming vertices and applying actor's colour. + */ + void CreateTriangleShader() + { + mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ); + } + + /** + * Function creates renderer. + */ + void CreateRenderer() + { + mRenderer = Renderer::New( mGeometry, mShader ); + } + + /** + * Creates new actor and attaches renderer. + */ + void CreateActor() + { + Stage stage = Stage::GetCurrent(); + Size size = stage.GetSize() * 0.25f; + mActor = Actor::New(); + mActor.SetAnchorPoint( AnchorPoint::CENTER ); + mActor.SetParentOrigin( ParentOrigin::CENTER ); + mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) ); + mActor.SetColor( Color::RED ); + mActor.SetSize( Vector3( size.x, size.x, size.x ) ); + mActor.AddRenderer( mRenderer ); + stage.Add( mActor ); + } + +private: + Application& mApplication; + + Renderer mRenderer; + Shader mShader; + Geometry mGeometry; + Actor mActor; +}; + +void RunTest( Application& application ) +{ + DrawTriangleController test( application ); + + application.MainLoop(); +} + +// Entry point for Linux & Tizen applications +// +int DALI_EXPORT_API main( int argc, char **argv ) +{ + Application application = Application::New( &argc, &argv ); + + RunTest( application ); + + return 0; +} diff --git a/resources/po/as.po b/resources/po/as.po index cc27807..62e9c3f 100755 --- a/resources/po/as.po +++ b/resources/po/as.po @@ -147,3 +147,15 @@ msgstr "Tooltip" msgid "DALI_DEMO_STR_TITLE_FPP_GAME" msgstr "FPP খেলা" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "ৰেণ্ডাৰিং গাঁথনি" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "ৰেণ্ডাৰিং ঘনক" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "ৰেণ্ডাৰিং ত্ৰিকোণমিতি" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "ৰেণ্ডাৰিং শাৰী" diff --git a/resources/po/de.po b/resources/po/de.po index ece6e56..fb5a408 100755 --- a/resources/po/de.po +++ b/resources/po/de.po @@ -147,3 +147,15 @@ msgstr "Kurzinfo" msgid "DALI_DEMO_STR_TITLE_FPP_GAME" msgstr "FPP Spiel" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "Texturierter Würfel" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "Würfel zeichnen" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "Dreieck zeichnen" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "Zeichnen" diff --git a/resources/po/en_GB.po b/resources/po/en_GB.po index 5054071..b6f2750 100755 --- a/resources/po/en_GB.po +++ b/resources/po/en_GB.po @@ -150,3 +150,15 @@ msgstr "FPP Game" msgid "DALI_DEMO_STR_TITLE_VISUAL_TRANSITIONS" msgstr "Visual Transitions" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "Textured cube" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "Draw cube" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "Draw triangle" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "Draw line" diff --git a/resources/po/en_US.po b/resources/po/en_US.po index 28ed480..dddbf79 100755 --- a/resources/po/en_US.po +++ b/resources/po/en_US.po @@ -150,3 +150,15 @@ msgstr "FPP Game" msgid "DALI_DEMO_STR_TITLE_VISUAL_TRANSITIONS" msgstr "Visual Transitions" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "Textured cube" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "Draw cube" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "Draw triangle" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "Draw line" diff --git a/resources/po/es.po b/resources/po/es.po index 6426481..6f89f16 100755 --- a/resources/po/es.po +++ b/resources/po/es.po @@ -147,3 +147,15 @@ msgstr "Tooltip" msgid "DALI_DEMO_STR_TITLE_FPP_GAME" msgstr "Juego FPP" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "Cubo con textura" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "Dibujar cubo" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "Dibujar triángulo" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "Dibujar linea" diff --git a/resources/po/fi.po b/resources/po/fi.po index 57f5600..d27c376 100755 --- a/resources/po/fi.po +++ b/resources/po/fi.po @@ -147,3 +147,15 @@ msgstr "Tooltip" msgid "DALI_DEMO_STR_TITLE_FPP_GAME" msgstr "FPP peli" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "kuvioitu kuutio" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "piirtää kuutio" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "Piirrä kolmio" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "Draw linja" diff --git a/resources/po/ko.po b/resources/po/ko.po index 0b375c6..73c953d 100755 --- a/resources/po/ko.po +++ b/resources/po/ko.po @@ -147,3 +147,15 @@ msgstr "툴팁" msgid "DALI_DEMO_STR_TITLE_FPP_GAME" msgstr "FPP Game" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "질감 입방체" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "큐브 그리기" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "삼각형 그리기" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "선 그리기" diff --git a/resources/po/ml.po b/resources/po/ml.po index 414c7bd..2d6be55 100755 --- a/resources/po/ml.po +++ b/resources/po/ml.po @@ -147,3 +147,15 @@ msgstr "കൂടുതൽ വിവരങ്ങൾ" msgid "DALI_DEMO_STR_TITLE_FPP_GAME" msgstr "FPP Game" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "ടെക്സ്ചർ ക്യൂബ്" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "ക്യൂബ് വരയ്ക്കുക" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "ത്രികോണം വരയ്ക്കുക" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "സമനില ലൈൻ" diff --git a/resources/po/ur.po b/resources/po/ur.po index d7b6cc6..212d7ad 100755 --- a/resources/po/ur.po +++ b/resources/po/ur.po @@ -147,3 +147,15 @@ msgstr "مزید معلومات" msgid "DALI_DEMO_STR_TITLE_FPP_GAME" msgstr "FPP گیم" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "بویک ٹوانب " + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "ارڈ بویک " + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "ارڈ ثلثم " + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "انچنیھک ریکل " diff --git a/resources/po/zn_CH.po b/resources/po/zn_CH.po index 55e7377..5664c1d 100755 --- a/resources/po/zn_CH.po +++ b/resources/po/zn_CH.po @@ -147,3 +147,15 @@ msgstr "更多信息" msgid "DALI_DEMO_STR_TITLE_FPP_GAME" msgstr "FPP游戏" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE" +msgstr "纹理的多维数据集" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE" +msgstr "绘制多维数据集" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE" +msgstr "绘制三角形" + +msgid "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE" +msgstr "画线" diff --git a/shared/dali-demo-strings.h b/shared/dali-demo-strings.h index 6c6d171..0647335 100644 --- a/shared/dali-demo-strings.h +++ b/shared/dali-demo-strings.h @@ -67,6 +67,10 @@ extern "C" #define DALI_DEMO_STR_TITLE_POPUP dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_POPUP") #define DALI_DEMO_STR_TITLE_PRIMITIVE_SHAPES dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_PRIMITIVE_SHAPES") #define DALI_DEMO_STR_TITLE_PROGRESS_BAR dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_PROGRESS_BAR") +#define DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE") +#define DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE") +#define DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE") +#define DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE") #define DALI_DEMO_STR_TITLE_REFRACTION dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_REFRACTION") #define DALI_DEMO_STR_TITLE_RENDERER_STENCIL dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_RENDERER_STENCIL") #define DALI_DEMO_STR_TITLE_SCRIPT_BASED_UI dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_SCRIPT_BASED_UI") @@ -120,6 +124,10 @@ extern "C" #define DALI_DEMO_STR_TITLE_POPUP "Popup" #define DALI_DEMO_STR_TITLE_PRIMITIVE_SHAPES "Primitive Shapes" #define DALI_DEMO_STR_TITLE_PROGRESS_BAR "Progress Bar" +#define DALI_DEMO_STR_TITLE_RENDERING_DRAW_LINE "Draw Line" +#define DALI_DEMO_STR_TITLE_RENDERING_DRAW_TRIANGLE "Draw Triangle" +#define DALI_DEMO_STR_TITLE_RENDERING_DRAW_CUBE "Draw Cube" +#define DALI_DEMO_STR_TITLE_RENDERING_TEXTURED_CUBE "Textured Cube" #define DALI_DEMO_STR_TITLE_REFRACTION "Refract Effect" #define DALI_DEMO_STR_TITLE_RENDERER_STENCIL "Renderer Stencils" #define DALI_DEMO_STR_TITLE_SCRIPT_BASED_UI "Script Based UI" -- 2.7.4