Commit aac2553a49977835209e4d768825ce38307c52b0

Authored by Adeel Kazmi
Committed by Gerrit Code Review
2 parents e430fe11 6b994496

Merge "Mesh demo improvements." into devel/master

examples/mesh-renderer/mesh-renderer-example.cpp
... ... @@ -44,25 +44,26 @@ namespace
44 44  
45 45 const float X_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
46 46 const float Y_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
47   - const float MODEL_SCALE = 0.45f;
  47 + const float MODEL_SCALE = 0.75f;
  48 + const int NUM_MESHES = 3;
48 49  
49 50 } //End namespace
50 51  
51   -class SharedMeshRendererController : public ConnectionTracker
  52 +class MeshRendererController : public ConnectionTracker
52 53 {
53 54 public:
54 55  
55   - SharedMeshRendererController( Application& application )
  56 + MeshRendererController( Application& application )
56 57 : mApplication( application ), //Store handle to the application.
57 58 mModelIndex( 1 ), //Start with metal robot.
58 59 mShaderIndex( 0 ), //Start with all textures.
59 60 mSelectedModelIndex( 0 ) //Non-valid default, which will get set to a correct value when used.
60 61 {
61 62 // Connect to the Application's Init signal
62   - mApplication.InitSignal().Connect( this, &SharedMeshRendererController::Create );
  63 + mApplication.InitSignal().Connect( this, &MeshRendererController::Create );
63 64 }
64 65  
65   - ~SharedMeshRendererController()
  66 + ~MeshRendererController()
66 67 {
67 68 }
68 69  
... ... @@ -79,6 +80,9 @@ public:
79 80  
80 81 //Setup and load the 3D models and buttons
81 82 LoadScene();
  83 +
  84 + //Allow for exiting of the application via key presses.
  85 + stage.KeyEventSignal().Connect( this, &MeshRendererController::OnKeyEvent );
82 86 }
83 87  
84 88 //Sets up the on-screen elements.
... ... @@ -91,79 +95,81 @@ public:
91 95 layer.SetParentOrigin( ParentOrigin::CENTER );
92 96 layer.SetAnchorPoint( AnchorPoint::CENTER );
93 97 layer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
94   - layer.SetBehavior( Layer::LAYER_3D );
  98 + layer.SetBehavior( Layer::LAYER_2D ); //We use a 2D layer as this is closer to UI work than full 3D scene creation.
  99 + layer.SetDepthTestDisabled( false ); //Enable depth testing, as otherwise the 2D layer would not do so.
95 100 stage.Add( layer );
96 101  
97   - //Containers to house each renderer-holding-actor, to provide a constant hitbox for pan detection.
98   - Actor container1 = Actor::New();
99   - container1.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
100   - container1.SetSizeModeFactor( Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
101   - container1.SetParentOrigin( ParentOrigin::CENTER );
102   - container1.SetAnchorPoint( AnchorPoint::CENTER );
103   - container1.SetPosition( stage.GetSize().width * 0.25, 0.0 ); //Place on right half of screen.
104   - container1.RegisterProperty( "Tag", Property::Value( 0 ) ); // Used to identify this actor and index into the model.
105   - layer.Add( container1 );
106   -
107   - Actor container2 = Actor::New();
108   - container2.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
109   - container2.SetSizeModeFactor( Vector3( MODEL_SCALE / 2, MODEL_SCALE / 2, 0.0f ) );
110   - container2.SetParentOrigin( ParentOrigin::CENTER );
111   - container2.SetAnchorPoint( AnchorPoint::CENTER );
112   - container2.SetPosition( stage.GetSize().width * -0.25, 0.0 ); //Place on left half of screen.
113   - container2.RegisterProperty( "Tag", Property::Value( 1 ) ); // Used to identify this actor and index into the model.
114   - layer.Add( container2 );
115   -
116   - //Attach gesture detector to pan models when rotated.
  102 + //Create gesture detector for panning of models.
117 103 mPanGestureDetector = PanGestureDetector::New();
118   - mPanGestureDetector.Attach( container1 );
119   - mPanGestureDetector.Attach( container2 );
120   - mPanGestureDetector.DetectedSignal().Connect( this, &SharedMeshRendererController::OnPan );
121   -
122   - //Create actors to display meshes.
123   - Control control1 = Control::New();
124   - control1.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
125   - control1.SetParentOrigin( ParentOrigin::CENTER );
126   - control1.SetAnchorPoint( AnchorPoint::CENTER );
127   - container1.Add( control1 );
128   -
129   - Control control2 = Control::New();
130   - control2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
131   - control2.SetParentOrigin( ParentOrigin::CENTER );
132   - control2.SetAnchorPoint( AnchorPoint::CENTER );
133   - container2.Add( control2 );
134   -
135   - //Make actors spin to demonstrate 3D.
136   - Animation rotationAnimation1 = Animation::New( 15.0f );
137   - rotationAnimation1.AnimateBy( Property( control1, Actor::Property::ORIENTATION ),
138   - Quaternion( Degree( 0.0f ), Degree( 360.0f ), Degree( 0.0f ) ) );
139   - rotationAnimation1.SetLooping( true );
140   - rotationAnimation1.Play();
141   -
142   - Animation rotationAnimation2 = Animation::New( 15.0f );
143   - rotationAnimation2.AnimateBy( Property( control2, Actor::Property::ORIENTATION ),
144   - Quaternion( Degree( 0.0f ), Degree( -360.0f ), Degree( 0.0f ) ) );
145   - rotationAnimation2.SetLooping( true );
146   - rotationAnimation2.Play();
147   -
148   - //Store model information in corresponding structs.
149   - mModels[0].control = control1;
150   - mModels[0].rotation.x = 0.0f;
151   - mModels[0].rotation.y = 0.0f;
152   - mModels[0].rotationAnimation = rotationAnimation1;
153   -
154   - mModels[1].control = control2;
155   - mModels[1].rotation.x = 0.0f;
156   - mModels[1].rotation.y = 0.0f;
157   - mModels[1].rotationAnimation = rotationAnimation2;
158   -
159   - //Calling this sets the model in the two actors.
  104 + mPanGestureDetector.DetectedSignal().Connect( this, &MeshRendererController::OnPan );
  105 +
  106 + //Add containers to house each renderer-holding-actor.
  107 + for( int i = 0; i < NUM_MESHES; i++ )
  108 + {
  109 + mContainers[i] = Actor::New();
  110 + mContainers[i].SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
  111 + mContainers[i].RegisterProperty( "Tag", Property::Value( i ) ); //Used to identify the actor and index into the model.
  112 +
  113 + //Position each container on screen
  114 + if( i == 0 )
  115 + {
  116 + //Main, central model
  117 + mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
  118 + mContainers[i].SetParentOrigin( ParentOrigin::CENTER );
  119 + mContainers[i].SetAnchorPoint( AnchorPoint::CENTER );
  120 + }
  121 + else if( i == 1 )
  122 + {
  123 + //Top left model
  124 + mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
  125 + mContainers[i].SetParentOrigin( Vector3( 0.05, 0.03, 0.5 ) ); //Offset from top left
  126 + mContainers[i].SetAnchorPoint( AnchorPoint::TOP_LEFT );
  127 + }
  128 + else if( i == 2 )
  129 + {
  130 + //Top right model
  131 + mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
  132 + mContainers[i].SetParentOrigin( Vector3( 0.95, 0.03, 0.5 ) ); //Offset from top right
  133 + mContainers[i].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
  134 + }
  135 +
  136 + mPanGestureDetector.Attach( mContainers[i] );
  137 + layer.Add( mContainers[i] );
  138 + }
  139 +
  140 + //Set up models
  141 + for( int i = 0; i < NUM_MESHES; i++ )
  142 + {
  143 + //Create control to display model
  144 + Control control = Control::New();
  145 + control.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
  146 + control.SetParentOrigin( ParentOrigin::CENTER );
  147 + control.SetAnchorPoint( AnchorPoint::CENTER );
  148 + mContainers[i].Add( control );
  149 +
  150 + //Make model spin to demonstrate 3D
  151 + Animation rotationAnimation = Animation::New( 15.0f );
  152 + float spin = i % 2 == 0 ? 1.0f : -1.0f; //Make actors spin in different directions to better show independence.
  153 + rotationAnimation.AnimateBy( Property( control, Actor::Property::ORIENTATION ),
  154 + Quaternion( Degree( 0.0f ), Degree( spin * 360.0f ), Degree( 0.0f ) ) );
  155 + rotationAnimation.SetLooping( true );
  156 + rotationAnimation.Play();
  157 +
  158 + //Store model information in corresponding structs.
  159 + mModels[i].control = control;
  160 + mModels[i].rotation.x = 0.0f;
  161 + mModels[i].rotation.y = 0.0f;
  162 + mModels[i].rotationAnimation = rotationAnimation;
  163 + }
  164 +
  165 + //Calling this sets the model in the controls.
160 166 ReloadModel();
161 167  
162 168 //Create button for model changing
163 169 Toolkit::PushButton modelButton = Toolkit::PushButton::New();
164 170 modelButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
165   - modelButton.ClickedSignal().Connect( this, &SharedMeshRendererController::OnChangeModelClicked );
166   - modelButton.SetParentOrigin( Vector3( 0.1, 0.9, 0.5 ) ); //Offset from bottom left
  171 + modelButton.ClickedSignal().Connect( this, &MeshRendererController::OnChangeModelClicked );
  172 + modelButton.SetParentOrigin( Vector3( 0.1, 0.95, 0.5 ) ); //Offset from bottom left
167 173 modelButton.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
168 174 modelButton.SetLabelText( "Change Model" );
169 175 layer.Add( modelButton );
... ... @@ -171,8 +177,8 @@ public:
171 177 //Create button for shader changing
172 178 Toolkit::PushButton shaderButton = Toolkit::PushButton::New();
173 179 shaderButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
174   - shaderButton.ClickedSignal().Connect( this, &SharedMeshRendererController::OnChangeShaderClicked );
175   - shaderButton.SetParentOrigin( Vector3( 0.9, 0.9, 0.5 ) ); //Offset from bottom right
  180 + shaderButton.ClickedSignal().Connect( this, &MeshRendererController::OnChangeShaderClicked );
  181 + shaderButton.SetParentOrigin( Vector3( 0.9, 0.95, 0.5 ) ); //Offset from bottom right
176 182 shaderButton.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
177 183 shaderButton.SetLabelText( "Change Shader" );
178 184 layer.Add( shaderButton );
... ... @@ -190,8 +196,10 @@ public:
190 196 map.Insert( "shaderType", SHADER_TYPE[mShaderIndex] );
191 197  
192 198 //Set the two controls to use the mesh
193   - mModels[0].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
194   - mModels[1].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
  199 + for( int i = 0; i < NUM_MESHES; i++ )
  200 + {
  201 + mModels[i].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
  202 + }
195 203 }
196 204  
197 205 //Rotates the panned model based on the gesture.
... ... @@ -263,11 +271,24 @@ public:
263 271 return true;
264 272 }
265 273  
  274 + //If escape or the back button is pressed, quit the application (and return to the launcher)
  275 + void OnKeyEvent( const KeyEvent& event )
  276 + {
  277 + if( event.state == KeyEvent::Down )
  278 + {
  279 + if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
  280 + {
  281 + mApplication.Quit();
  282 + }
  283 + }
  284 + }
  285 +
266 286 private:
267 287 Application& mApplication;
268 288  
269 289 //The models displayed on screen, including information about rotation.
270   - Model mModels[2];
  290 + Model mModels[NUM_MESHES];
  291 + Actor mContainers[NUM_MESHES];
271 292  
272 293 //Used to detect panning to rotate the selected model.
273 294 PanGestureDetector mPanGestureDetector;
... ... @@ -279,7 +300,7 @@ private:
279 300  
280 301 void RunTest( Application& application )
281 302 {
282   - SharedMeshRendererController test( application );
  303 + MeshRendererController test( application );
283 304  
284 305 application.MainLoop();
285 306 }
... ...