2 * Copyright (c) 2015 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>
21 using Dali::Toolkit::Model3dView;
25 const int MODEL_NUMBER(3);
27 const char * const MODEL_FILE[] = {
28 DALI_MODEL_DIR "Dino.obj",
29 DALI_MODEL_DIR "ToyRobot-Metal.obj",
30 DALI_MODEL_DIR "Toyrobot-Plastic.obj"
33 const char * const MATERIAL_FILE[] = {
34 DALI_MODEL_DIR "Dino.mtl",
35 DALI_MODEL_DIR "ToyRobot-Metal.mtl",
36 DALI_MODEL_DIR "Toyrobot-Plastic.mtl"
39 const char * const IMAGE_PATH( DALI_IMAGE_DIR "" );
41 const char * BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-1.jpg");
46 * This example shows how to create and display a model3d-view control.
47 * The application can cycle between 3 different models, and 3 different shaders.
48 * There are two animations running, one is a rotation for the model, one is a light that
49 * goes from one side of the model to the other.
50 * There are dedicated buttons for changing the models, the shaders and pausing the animations.
51 * The animations can also be paused, resumed with the space key
52 * A double tap in the model3d-view will make zoom-in/out of the zone clicked
55 class Model3dViewController : public ConnectionTracker
59 Model3dViewController( Application& application )
60 : mApplication( application )
62 // Connect to the Application's Init signal
63 mApplication.InitSignal().Connect( this, &Model3dViewController::Create );
66 ~Model3dViewController()
68 // Nothing to do here;
71 // The Init signal is received once (only) during the Application lifetime
72 void Create( Application& application )
74 // Get a handle to the stage
75 Stage stage = Stage::GetCurrent();
76 Vector2 screenSize = stage.GetSize();
79 Image imageBackground = ResourceImage::New( BACKGROUND_IMAGE );
80 Toolkit::ImageView backView = Toolkit::ImageView::New(imageBackground);
81 backView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
84 //Add 3D model control
85 m3DLayer = Layer::New();
86 stage.GetRootLayer().Add(m3DLayer);
88 //3D models require 3D based rendering method, so it can use depth buffer, etc.
89 m3DLayer.SetBehavior(Layer::LAYER_3D);
90 m3DLayer.SetParentOrigin( ParentOrigin::CENTER );
91 m3DLayer.SetAnchorPoint( AnchorPoint::CENTER );
95 mModel3dView = Model3dView::New( MODEL_FILE[0], MATERIAL_FILE[0], IMAGE_PATH );
96 mModel3dView.SetParentOrigin( ParentOrigin::CENTER );
97 mModel3dView.SetAnchorPoint( AnchorPoint::CENTER );
98 mModel3dView.SetName( "model3dViewControl" );
99 mModel3dView.SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
100 mModel3dView.SetSize(screenSize);
102 mModel3dView.SetProperty(Model3dView::Property::LIGHT_POSITION, Vector3(5,10.,0));
104 m3DLayer.Add( mModel3dView );
106 mIlluminationShader = Model3dView::IlluminationType(mModel3dView.GetProperty<int>(Model3dView::Property::ILLUMINATION_TYPE));
108 mButtonLayer = Layer::New();
109 mButtonLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
110 mButtonLayer.SetParentOrigin( ParentOrigin::CENTER );
111 mButtonLayer.SetAnchorPoint( AnchorPoint::CENTER );
112 stage.GetRootLayer().Add(mButtonLayer);
114 // Create button for model changing
115 Toolkit::PushButton editButton = Toolkit::PushButton::New();
116 editButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
117 editButton.ClickedSignal().Connect( this, &Model3dViewController::OnChangeModelClicked);
118 editButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
119 editButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
120 editButton.SetLabelText( "Change Model" );
121 mButtonLayer.Add( editButton );
123 // Create button for shader changing
124 editButton = Toolkit::PushButton::New();
125 editButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
126 editButton.ClickedSignal().Connect( this, &Model3dViewController::OnChangeLightingClicked);
127 editButton.SetParentOrigin( ParentOrigin::TOP_RIGHT );
128 editButton.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
129 editButton.SetLabelText( "Change Shader" );
130 mButtonLayer.Add( editButton );
132 // Create button for pause/resume animation
133 editButton = Toolkit::PushButton::New();
134 editButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
135 editButton.ClickedSignal().Connect( this, &Model3dViewController::OnPauseAnimationsClicked);
136 editButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
137 editButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
138 editButton.SetLabelText( "Pause Animations" );
139 mButtonLayer.Add( editButton );
142 mLightAnimation = Animation::New(6.f);
143 mLightAnimation.AnimateTo(Property(mModel3dView, Model3dView::Property::LIGHT_POSITION), Vector3(-5,10.0,0), TimePeriod( 0.0f, 3.0f ));
144 mLightAnimation.AnimateTo(Property(mModel3dView, Model3dView::Property::LIGHT_POSITION), Vector3(5,10.0,0), TimePeriod( 3.0f, 3.0f ));
145 mLightAnimation.SetLooping(true);
146 mLightAnimation.Play();
148 mRotationAnimation = Animation::New(15.f);
149 mRotationAnimation.AnimateBy(Property(mModel3dView, Actor::Property::ORIENTATION), Quaternion(Degree(0.f), Degree(360.f), Degree(0.f)) );
150 mRotationAnimation.SetLooping(true);
151 mRotationAnimation.Play();
156 // Respond to a click anywhere on the stage
157 stage.KeyEventSignal().Connect(this, &Model3dViewController::OnKeyEvent);
159 //Create a tap gesture detector for zoom
160 mTapDetector = TapGestureDetector::New( 2 );
161 mTapDetector.DetectedSignal().Connect(this, &Model3dViewController::OnTap);
163 mTapDetector.Attach( backView );
167 * Main Tap event handler
169 void OnTap( Actor actor, const TapGesture& tap )
173 mModel3dView.SetScale(1.0);
174 mModel3dView.SetPosition(0,0,0);
179 Stage stage = Stage::GetCurrent();
180 Vector2 screenSize = stage.GetSize();
183 position.x = tap.screenPoint.x - screenSize.x * 0.5;
184 position.y = tap.screenPoint.y - screenSize.y * 0.5;
188 mModel3dView.SetScale(size);
189 mModel3dView.SetPosition(-position.x * size,-position.y * size, 0);
195 * Change models button signal function
197 bool OnChangeModelClicked(Toolkit::Button button)
199 mModelCounter = (mModelCounter + 1) % MODEL_NUMBER;
200 mModel3dView.SetProperty(Model3dView::Property::GEOMETRY_URL, MODEL_FILE[mModelCounter]);
201 mModel3dView.SetProperty(Model3dView::Property::MATERIAL_URL, MATERIAL_FILE[mModelCounter]);
202 mModel3dView.SetProperty(Model3dView::Property::IMAGES_URL, IMAGE_PATH);
208 * Change shader button signal function
210 bool OnChangeLightingClicked(Toolkit::Button button)
212 if( mIlluminationShader == Model3dView::DIFFUSE_WITH_NORMAL_MAP )
214 mModel3dView.SetProperty(Model3dView::Property::ILLUMINATION_TYPE, Model3dView::DIFFUSE_WITH_TEXTURE);
215 mIlluminationShader = Model3dView::IlluminationType(mModel3dView.GetProperty<int>(Model3dView::Property::ILLUMINATION_TYPE));
217 else if( mIlluminationShader == Model3dView::DIFFUSE_WITH_TEXTURE )
219 mModel3dView.SetProperty(Model3dView::Property::ILLUMINATION_TYPE, Model3dView::DIFFUSE);
220 mIlluminationShader = Model3dView::IlluminationType(mModel3dView.GetProperty<int>(Model3dView::Property::ILLUMINATION_TYPE));
222 else if( mIlluminationShader == Model3dView::DIFFUSE )
224 mModel3dView.SetProperty(Model3dView::Property::ILLUMINATION_TYPE, Model3dView::DIFFUSE_WITH_NORMAL_MAP);
225 mIlluminationShader = Model3dView::IlluminationType(mModel3dView.GetProperty<int>(Model3dView::Property::ILLUMINATION_TYPE));
232 * Function to pause resume all animations
234 void PauseAnimations()
238 mRotationAnimation.Pause();
239 mLightAnimation.Pause();
245 mRotationAnimation.Play();
246 mLightAnimation.Play();
253 * Pause button signal function
255 bool OnPauseAnimationsClicked(Toolkit::Button button)
263 * Main key event handler
265 void OnKeyEvent(const KeyEvent& event)
267 if(event.state == KeyEvent::Down)
269 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
282 Application& mApplication;
285 Model3dView mModel3dView;
289 TapGestureDetector mTapDetector;
291 Model3dView::IlluminationType mIlluminationShader;
293 Animation mRotationAnimation;
294 Animation mLightAnimation;
300 void RunTest( Application& application )
302 Model3dViewController test( application );
304 application.MainLoop();
307 // Entry point for Linux & Tizen applications
309 int main( int argc, char **argv )
311 Application application = Application::New( &argc, &argv );
313 RunTest( application );