Commit 19369b28e77acb7d31e72bae8a35a6a1d8456729

Authored by Andrew Poor
1 parent a3d5c29a

Mesh demo improvements for lighting.

Change-Id: I6fb94272f1fc562b210c765d79f217d40a611263
examples/mesh-visual/mesh-visual-example.cpp
... ... @@ -15,14 +15,14 @@ namespace
15 15 };
16 16  
17 17 //Files for meshes
18   - const char * const MODEL_FILE[] =
  18 + const char * const MODEL_FILE_TABLE[] =
19 19 {
20 20 DEMO_MODEL_DIR "Dino.obj",
21 21 DEMO_MODEL_DIR "ToyRobot-Metal.obj",
22 22 DEMO_MODEL_DIR "Toyrobot-Plastic.obj"
23 23 };
24 24  
25   - const char * const MATERIAL_FILE[] =
  25 + const char * const MATERIAL_FILE_TABLE[] =
26 26 {
27 27 DEMO_MODEL_DIR "Dino.mtl",
28 28 DEMO_MODEL_DIR "ToyRobot-Metal.mtl",
... ... @@ -39,13 +39,12 @@ namespace
39 39 MeshVisual::ShadingMode::TEXTURELESS_WITH_DIFFUSE_LIGHTING
40 40 };
41 41  
42   - //Files for background and toolbar
43   - const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-1.jpg");
44   -
45 42 const float X_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
46 43 const float Y_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
47   - const float MODEL_SCALE = 0.75f;
48   - const int NUM_MESHES = 3;
  44 + const float MODEL_SCALE = 0.75f;
  45 + const float LIGHT_SCALE = 0.15f;
  46 + const float BUTTONS_OFFSET_BOTTOM = 0.9f;
  47 + const int NUM_MESHES = 2;
49 48  
50 49 //Used to identify actors.
51 50 const int MODEL_TAG = 0;
... ... @@ -64,7 +63,8 @@ public:
64 63 mShadingModeIndex( 0 ), //Start with textured with detailed specular lighting.
65 64 mTag( -1 ), //Non-valid default, which will get set to a correct value when used.
66 65 mSelectedModelIndex( -1 ), //Non-valid default, which will get set to a correct value when used.
67   - mPaused( false ) //Animations play by default.
  66 + mPaused( false ), //Animations play by default.
  67 + mLightFixed( true ) //The light is fixed by default.
68 68 {
69 69 // Connect to the Application's Init signal
70 70 mApplication.InitSignal().Connect( this, &MeshVisualController::Create );
... ... @@ -79,23 +79,7 @@ public:
79 79 {
80 80 // Get a handle to the stage
81 81 Stage stage = Stage::GetCurrent();
82   -
83   - //Add background
84   - ImageView backView = ImageView::New( BACKGROUND_IMAGE );
85   - backView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
86   - stage.Add( backView );
87   -
88   - //Setup and load the 3D models and buttons
89   - LoadScene();
90   -
91   - //Allow for exiting of the application via key presses.
92   - stage.KeyEventSignal().Connect( this, &MeshVisualController::OnKeyEvent );
93   - }
94   -
95   - //Sets up the on-screen elements.
96   - void LoadScene()
97   - {
98   - Stage stage = Stage::GetCurrent();
  82 + stage.SetBackgroundColor( Vector4( 0.0, 0.5, 1.0, 1.0 ) );
99 83  
100 84 //Set up layer to place objects on.
101 85 Layer baseLayer = Layer::New();
... ... @@ -108,41 +92,45 @@ public:
108 92 baseLayer.TouchedSignal().Connect( this, &MeshVisualController::OnTouch );
109 93 stage.Add( baseLayer );
110 94  
111   - //Add containers to house each visual-holding-actor.
  95 + //Place models on the scene.
  96 + SetupModels( baseLayer );
  97 +
  98 + //Place buttons on the scene.
  99 + SetupButtons( baseLayer );
  100 +
  101 + //Add a light to the scene.
  102 + SetupLight( baseLayer );
  103 +
  104 + //Allow for exiting of the application via key presses.
  105 + stage.KeyEventSignal().Connect( this, &MeshVisualController::OnKeyEvent );
  106 + }
  107 +
  108 + //Loads and adds the models to the scene, inside containers for hit detection.
  109 + void SetupModels( Layer layer )
  110 + {
  111 + //Add containers to house each renderer-holding-actor.
112 112 for( int i = 0; i < NUM_MESHES; i++ )
113 113 {
114 114 mContainers[i] = Actor::New();
115 115 mContainers[i].SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
116 116 mContainers[i].RegisterProperty( "Tag", MODEL_TAG ); //Used to differentiate between different kinds of actor.
117 117 mContainers[i].RegisterProperty( "Model", Property::Value( i ) ); //Used to index into the model.
118   -
119   - //Position each container on screen
120   - if( i == 0 )
121   - {
122   - //Main, central model
123   - mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
124   - mContainers[i].SetParentOrigin( ParentOrigin::CENTER );
125   - mContainers[i].SetAnchorPoint( AnchorPoint::CENTER );
126   - }
127   - else if( i == 1 )
128   - {
129   - //Top left model
130   - mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
131   - mContainers[i].SetParentOrigin( Vector3( 0.05, 0.03, 0.5 ) ); //Offset from top left
132   - mContainers[i].SetAnchorPoint( AnchorPoint::TOP_LEFT );
133   - }
134   - else if( i == 2 )
135   - {
136   - //Top right model
137   - mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
138   - mContainers[i].SetParentOrigin( Vector3( 0.95, 0.03, 0.5 ) ); //Offset from top right
139   - mContainers[i].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
140   - }
141   -
142 118 mContainers[i].TouchedSignal().Connect( this, &MeshVisualController::OnTouch );
143   - baseLayer.Add( mContainers[i] );
  119 + layer.Add( mContainers[i] );
144 120 }
145 121  
  122 + //Position each container individually on screen
  123 +
  124 + //Main, central model
  125 + mContainers[0].SetSizeModeFactor( Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
  126 + mContainers[0].SetParentOrigin( ParentOrigin::CENTER );
  127 + mContainers[0].SetAnchorPoint( AnchorPoint::CENTER );
  128 +
  129 + //Top left model
  130 + mContainers[1].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
  131 + mContainers[1].SetParentOrigin( Vector3( 0.05, 0.03, 0.5 ) ); //Offset from top left
  132 + mContainers[1].SetAnchorPoint( AnchorPoint::TOP_LEFT );
  133 +
146 134 //Set up models
147 135 for( int i = 0; i < NUM_MESHES; i++ )
148 136 {
... ... @@ -170,70 +158,120 @@ public:
170 158  
171 159 //Calling this sets the model in the controls.
172 160 ReloadModel();
  161 + }
  162 +
  163 + //Place the various buttons on the bottom of the screen, with title labels where necessary.
  164 + void SetupButtons( Layer layer )
  165 + {
  166 + //Text label title for changing model or shader.
  167 + TextLabel changeTitleLabel = TextLabel::New( "Switch" );
  168 + changeTitleLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
  169 + changeTitleLabel.SetProperty( TextLabel::Property::UNDERLINE, "{\"thickness\":\"2.0\"}" );
  170 + changeTitleLabel.SetParentOrigin( Vector3( 0.2, BUTTONS_OFFSET_BOTTOM, 0.5 ) );
  171 + changeTitleLabel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
  172 + layer.Add( changeTitleLabel );
173 173  
174 174 //Create button for model changing
175   - Toolkit::PushButton modelButton = Toolkit::PushButton::New();
  175 + PushButton modelButton = Toolkit::PushButton::New();
176 176 modelButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
177 177 modelButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeModelClicked );
178   - modelButton.SetParentOrigin( Vector3( 0.05, 0.95, 0.5 ) ); //Offset from bottom left
179   - modelButton.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
180   - modelButton.SetLabelText( "Change Model" );
181   - baseLayer.Add( modelButton );
  178 + modelButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
  179 + modelButton.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
  180 + modelButton.SetLabelText( "Model" );
  181 + changeTitleLabel.Add( modelButton );
182 182  
183 183 //Create button for shader changing
184   - Toolkit::PushButton shaderButton = Toolkit::PushButton::New();
  184 + PushButton shaderButton = Toolkit::PushButton::New();
185 185 shaderButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
186 186 shaderButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeShaderClicked );
187   - shaderButton.SetParentOrigin( Vector3( 0.95, 0.95, 0.5 ) ); //Offset from bottom right
188   - shaderButton.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
189   - shaderButton.SetLabelText( "Change Shader" );
190   - baseLayer.Add( shaderButton );
  187 + shaderButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
  188 + shaderButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
  189 + shaderButton.SetLabelText( "Shader" );
  190 + changeTitleLabel.Add( shaderButton );
191 191  
192 192 //Create button for pausing animations
193   - Toolkit::PushButton pauseButton = Toolkit::PushButton::New();
  193 + PushButton pauseButton = Toolkit::PushButton::New();
194 194 pauseButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
195 195 pauseButton.ClickedSignal().Connect( this, &MeshVisualController::OnPauseClicked );
196   - pauseButton.SetParentOrigin( Vector3( 0.5, 0.95, 0.5 ) ); //Offset from bottom center
197   - pauseButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
  196 + pauseButton.SetParentOrigin( Vector3( 0.5, BUTTONS_OFFSET_BOTTOM, 0.5 ) );
  197 + pauseButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
198 198 pauseButton.SetLabelText( " || " );
199   - baseLayer.Add( pauseButton );
  199 + layer.Add( pauseButton );
  200 +
  201 + //Text label title for light position mode.
  202 + TextLabel lightTitleLabel = TextLabel::New( "Light Position" );
  203 + lightTitleLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
  204 + lightTitleLabel.SetProperty( TextLabel::Property::UNDERLINE, "{\"thickness\":\"2.0\"}" );
  205 + lightTitleLabel.SetParentOrigin( Vector3( 0.8, BUTTONS_OFFSET_BOTTOM, 0.5 ) );
  206 + lightTitleLabel.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
  207 + layer.Add( lightTitleLabel );
  208 +
  209 + //Create button for switching between manual and fixed light position.
  210 + PushButton lightButton = Toolkit::PushButton::New();
  211 + lightButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
  212 + lightButton.ClickedSignal().Connect( this, &MeshVisualController::OnChangeLightModeClicked );
  213 + lightButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
  214 + lightButton.SetAnchorPoint( AnchorPoint::TOP_CENTER );
  215 + lightButton.SetLabelText( "FIXED" );
  216 + lightTitleLabel.Add( lightButton );
  217 + }
200 218  
  219 + //Add a point light source the the scene, on a layer above the first.
  220 + void SetupLight( Layer baseLayer )
  221 + {
201 222 //Create control to act as light source of scene.
202 223 mLightSource = Control::New();
203   - mLightSource.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::WIDTH );
204   - mLightSource.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
205 224 mLightSource.RegisterProperty( "Tag", LIGHT_TAG );
206 225  
  226 + //Set size of control based on screen dimensions.
  227 + Stage stage = Stage::GetCurrent();
  228 + if( stage.GetSize().width < stage.GetSize().height )
  229 + {
  230 + //Scale to width.
  231 + mLightSource.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::WIDTH );
  232 + mLightSource.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
  233 + mLightSource.SetSizeModeFactor( Vector3( LIGHT_SCALE, 0.0f, 0.0f ) );
  234 + }
  235 + else
  236 + {
  237 + //Scale to height.
  238 + mLightSource.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
  239 + mLightSource.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::WIDTH );
  240 + mLightSource.SetSizeModeFactor( Vector3( 0.0f, LIGHT_SCALE, 0.0f ) );
  241 + }
  242 +
207 243 //Set position relative to top left, as the light source property is also relative to the top left.
208 244 mLightSource.SetParentOrigin( ParentOrigin::TOP_LEFT );
209 245 mLightSource.SetAnchorPoint( AnchorPoint::CENTER );
210   - mLightSource.SetPosition( Stage::GetCurrent().GetSize().x * 0.5f, Stage::GetCurrent().GetSize().y * 0.1f );
  246 + mLightSource.SetPosition( Stage::GetCurrent().GetSize().x * 0.85f, Stage::GetCurrent().GetSize().y * 0.125 );
211 247  
212   - //Make white background.
  248 + //Supply an image to represent the light.
213 249 Property::Map lightMap;
214   - lightMap.Insert( Visual::Property::TYPE, Visual::COLOR );
215   - lightMap.Insert( ColorVisual::Property::MIX_COLOR, Color::WHITE );
  250 + lightMap.Insert( Visual::Property::TYPE, Visual::IMAGE );
  251 + lightMap.Insert( ImageVisual::Property::URL, DEMO_IMAGE_DIR "light-icon.png" );
216 252 mLightSource.SetProperty( Control::Property::BACKGROUND, Property::Value( lightMap ) );
217 253  
218   - //Label to show what this actor is for the user.
219   - TextLabel lightLabel = TextLabel::New( "Light" );
220   - lightLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
221   - lightLabel.SetParentOrigin( ParentOrigin::CENTER );
222   - lightLabel.SetAnchorPoint( AnchorPoint::CENTER );
223   - float padding = 5.0f;
224   - lightLabel.SetPadding( Padding( padding, padding, padding, padding ) );
225   - mLightSource.Add( lightLabel );
226   -
227 254 //Connect to touch signal for dragging.
228 255 mLightSource.TouchedSignal().Connect( this, &MeshVisualController::OnTouch );
229 256  
230 257 //Place the light source on a layer above the base, so that it is rendered above everything else.
231 258 Layer upperLayer = Layer::New();
  259 + upperLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
  260 + upperLayer.SetParentOrigin( ParentOrigin::CENTER );
  261 + upperLayer.SetAnchorPoint( AnchorPoint::CENTER );
  262 +
232 263 baseLayer.Add( upperLayer );
233 264 upperLayer.Add( mLightSource );
234 265  
235   - //Calling this sets the light position of each model to that of the light source control.
236   - UpdateLight();
  266 + //Decide which light to use to begin with.
  267 + if( mLightFixed )
  268 + {
  269 + UseFixedLight();
  270 + }
  271 + else
  272 + {
  273 + UseManualLight();
  274 + }
237 275 }
238 276  
239 277 //Updates the displayed models to account for parameter changes.
... ... @@ -242,11 +280,10 @@ public:
242 280 //Create mesh property map
243 281 Property::Map map;
244 282 map.Insert( Visual::Property::TYPE, Visual::MESH );
245   - map.Insert( MeshVisual::Property::OBJECT_URL, MODEL_FILE[mModelIndex] );
246   - map.Insert( MeshVisual::Property::MATERIAL_URL, MATERIAL_FILE[mModelIndex] );
  283 + map.Insert( MeshVisual::Property::OBJECT_URL, MODEL_FILE_TABLE[mModelIndex] );
  284 + map.Insert( MeshVisual::Property::MATERIAL_URL, MATERIAL_FILE_TABLE[mModelIndex] );
247 285 map.Insert( MeshVisual::Property::TEXTURES_PATH, TEXTURES_PATH );
248 286 map.Insert( MeshVisual::Property::SHADING_MODE, SHADING_MODE_TABLE[mShadingModeIndex] );
249   - map.Insert( MeshVisual::Property::USE_SOFT_NORMALS, false );
250 287  
251 288 //Set the two controls to use the mesh
252 289 for( int i = 0; i < NUM_MESHES; i++ )
... ... @@ -255,12 +292,39 @@ public:
255 292 }
256 293 }
257 294  
  295 + void UseFixedLight()
  296 + {
  297 + //Hide draggable source
  298 + mLightSource.SetVisible( false );
  299 +
  300 + //Use stage dimensions to place light at center, offset outwards in z axis.
  301 + Stage stage = Stage::GetCurrent();
  302 + float width = stage.GetSize().width;
  303 + float height = stage.GetSize().height;
  304 + Vector3 lightPosition = Vector3( width / 2.0f, height / 2.0f, std::max( width, height ) * 5.0f );
  305 +
  306 + //Set global light position
  307 + for( int i = 0; i < NUM_MESHES; ++i )
  308 + {
  309 + mModels[i].control.RegisterProperty( "lightPosition", lightPosition, Property::ANIMATABLE );
  310 + }
  311 + }
  312 +
  313 + void UseManualLight()
  314 + {
  315 + //Show draggable source
  316 + mLightSource.SetVisible( true );
  317 +
  318 + //Update to switch light position to that off the source.
  319 + UpdateLight();
  320 + }
  321 +
258 322 //Updates the light position for each model to account for changes in the source on screen.
259 323 void UpdateLight()
260 324 {
261 325 //Set light position to the x and y of the light control, offset out of the screen.
262 326 Vector3 controlPosition = mLightSource.GetCurrentPosition();
263   - Vector3 lightPosition = Vector3( controlPosition.x, controlPosition.y, Stage::GetCurrent().GetSize().x * 2.0f );
  327 + Vector3 lightPosition = Vector3( controlPosition.x, controlPosition.y, Stage::GetCurrent().GetSize().x / 2.0f );
264 328  
265 329 for( int i = 0; i < NUM_MESHES; ++i )
266 330 {
... ... @@ -402,6 +466,29 @@ public:
402 466 return true;
403 467 }
404 468  
  469 +
  470 + //Switch between a fixed light source in front of the screen, and a light source the user can drag around.
  471 + bool OnChangeLightModeClicked( Toolkit::Button button )
  472 + {
  473 + //Toggle state.
  474 + mLightFixed = !mLightFixed;
  475 +
  476 + if( mLightFixed )
  477 + {
  478 + UseFixedLight();
  479 +
  480 + button.SetLabelText( "FIXED" );
  481 + }
  482 + else
  483 + {
  484 + UseManualLight();
  485 +
  486 + button.SetLabelText( "MANUAL" );
  487 + }
  488 +
  489 + return true;
  490 + }
  491 +
405 492 //If escape or the back button is pressed, quit the application (and return to the launcher)
406 493 void OnKeyEvent( const KeyEvent& event )
407 494 {
... ... @@ -433,6 +520,7 @@ private:
433 520 int mTag; //Identifies what kind of actor has been selected in OnTouch.
434 521 int mSelectedModelIndex; //Index of model selected on screen.
435 522 bool mPaused; //If true, all animations are paused and should stay so.
  523 + bool mLightFixed; //If false, the light is in manual.
436 524 };
437 525  
438 526 // Entry point for Linux & Tizen applications
... ...
resources/images/light-icon.png 0 โ†’ 100644

115 KB