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-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/tool-bar/tool-bar.h>
20 #include "shared/view.h"
21 #include "shared/utility.h"
25 using Dali::Toolkit::TextLabel;
26 using Dali::Toolkit::Control;
27 using Dali::Toolkit::ToolBar;
29 const char* BACKGROUND_IMAGE( "" );
30 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
31 const char* APPLICATION_TITLE( "Ray Marching" );
32 const char* SHADER_NAME("raymarch_sphere_shaded");
35 * @brief LoadShaderCode
40 bool LoadShaderCode( const char* path, const char* filename, std::vector<char>& output )
42 std::string fullpath( path );
44 FILE* f = fopen( fullpath.c_str(), "rb" );
49 fseek( f, 0, SEEK_END );
50 size_t size = ftell( f );
51 fseek( f, 0, SEEK_SET );
52 output.resize( size + 1 );
53 std::fill( output.begin(), output.end(), 0 );
54 ssize_t result = fread( output.data(), size, 1, f );
57 return ( result >= 0 );
65 Shader LoadShaders( const std::string& shaderName )
67 std::vector<char> bufV, bufF;
68 std::string shaderVSH( shaderName );
69 std::string shaderFSH( shaderName );
72 LoadShaderCode( DEMO_SHADER_DIR, shaderVSH.c_str(), bufV );
73 LoadShaderCode( DEMO_SHADER_DIR, shaderFSH.c_str(), bufF );
74 Shader shader = Shader::New( bufV.data(), bufF.data() );
78 // This example shows how to create a Ray Marching using a shader
80 class RayMarchingExample : public ConnectionTracker
84 RayMarchingExample( Application& application )
85 : mApplication( application )
87 // Connect to the Application's Init signal
88 mApplication.InitSignal().Connect( this, &RayMarchingExample::Create );
93 // Nothing to do here;
96 // The Init signal is received once (only) during the Application lifetime
97 void Create( Application& application )
99 // Get a handle to the stage
100 Stage stage = Stage::GetCurrent();
102 stage.GetRootLayer().TouchSignal().Connect( this, &RayMarchingExample::OnTouch );
104 stage.KeyEventSignal().Connect(this, &RayMarchingExample::OnKeyEvent);
106 stage.SetBackgroundColor( Color::YELLOW );
108 // Hide the indicator bar
109 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
111 // Creates a default view with a default tool bar.
112 // The view is added to the stage.
113 mContentLayer = DemoHelper::CreateView( application,
120 // Add an extra space on the right to center the title text.
121 mToolBar.AddControl( Actor::New(), DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight );
126 bool OnTouch( Actor actor, const TouchData& touch )
128 // quit the application
134 * Main key event handler
136 void OnKeyEvent(const KeyEvent& event)
138 if(event.state == KeyEvent::Down)
140 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
148 * Creates quad renderer
150 Renderer CreateQuadRenderer()
152 // Create shader & geometry needed by Renderer
153 Shader shader = LoadShaders( SHADER_NAME );
155 Property::Map vertexFormat;
156 vertexFormat["aPosition"] = Property::VECTOR2;
157 PropertyBuffer vertexBuffer = PropertyBuffer::New( vertexFormat );
159 const float P( 0.5f );
160 const Vector2 vertices[] = {
167 vertexBuffer.SetData( vertices, 4 );
169 // Instantiate quad geometry
170 Geometry geometry = Geometry::New();
171 geometry.AddVertexBuffer( vertexBuffer );
172 geometry.SetType( Geometry::TRIANGLE_STRIP );
175 Renderer renderer = Renderer::New( geometry, shader );
177 renderer.RegisterProperty("uRadius", 0.0f );
178 renderer.RegisterProperty("uAdjuster", -4.0f );
180 // Animate the sphere radius uniform and a generic uAdjust uniform currently used to move the light around
181 Animation animation = Animation::New(8.0f);
182 animation.AnimateTo( Property(renderer,"uRadius"), 1.2f, AlphaFunction::BOUNCE);
183 animation.AnimateTo( Property(renderer,"uAdjuster"), 4.0f, AlphaFunction::BOUNCE);
184 animation.SetLooping( true );
190 void AddContentLayer()
192 Stage stage = Stage::GetCurrent();
194 //Create all the renderers
195 Renderer renderer = CreateQuadRenderer();
197 Actor actor = Actor::New();
198 actor.AddRenderer( renderer );
200 actor.SetAnchorPoint( Dali::AnchorPoint::CENTER );
201 actor.SetParentOrigin( Dali::ParentOrigin::CENTER );
202 actor.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
204 mContentLayer.Add( actor );
208 Application& mApplication;
214 void RunTest( Application& application )
216 RayMarchingExample test( application );
218 application.MainLoop();
221 // Entry point for Linux & Tizen applications
223 int DALI_EXPORT_API main( int argc, char **argv )
225 Application application = Application::New( &argc, &argv );
227 RunTest( application );