mesh-renderer-example.cpp
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <dali-toolkit/dali-toolkit.h>
#include <dali/public-api/object/property-map.h>
using namespace Dali;
using namespace Dali::Toolkit;
namespace
{
//Keeps information about each model for access.
struct Model
{
Control control; // Control housing the mesh renderer of the model.
Vector2 rotation; // Keeps track of rotation about x and y axis for manual rotation.
Animation rotationAnimation; // Automatically rotates when left alone.
};
//Files for meshes
const char * const MODEL_FILE[] =
{
DEMO_MODEL_DIR "Dino.obj",
DEMO_MODEL_DIR "ToyRobot-Metal.obj",
DEMO_MODEL_DIR "Toyrobot-Plastic.obj"
};
const char * const MATERIAL_FILE[] =
{
DEMO_MODEL_DIR "Dino.mtl",
DEMO_MODEL_DIR "ToyRobot-Metal.mtl",
DEMO_MODEL_DIR "Toyrobot-Plastic.mtl"
};
const char * const TEXTURES_PATH( DEMO_IMAGE_DIR "" );
//Possible shader options.
const char * const SHADER_TYPE[] =
{
"ALL_TEXTURES",
"DIFFUSE_TEXTURE",
"TEXTURELESS"
};
//Files for background and toolbar
const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-1.jpg");
const float X_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
const float Y_ROTATION_DISPLACEMENT_FACTOR = 60.0f;
const float MODEL_SCALE = 0.75f;
const int NUM_MESHES = 3;
} //End namespace
class MeshRendererController : public ConnectionTracker
{
public:
MeshRendererController( Application& application )
: mApplication( application ), //Store handle to the application.
mModelIndex( 1 ), //Start with metal robot.
mShaderIndex( 0 ), //Start with all textures.
mSelectedModelIndex( 0 ) //Non-valid default, which will get set to a correct value when used.
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect( this, &MeshRendererController::Create );
}
~MeshRendererController()
{
}
// 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();
//Add background
ImageView backView = ImageView::New( BACKGROUND_IMAGE );
backView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
stage.Add( backView );
//Setup and load the 3D models and buttons
LoadScene();
//Allow for exiting of the application via key presses.
stage.KeyEventSignal().Connect( this, &MeshRendererController::OnKeyEvent );
}
//Sets up the on-screen elements.
void LoadScene()
{
Stage stage = Stage::GetCurrent();
//Set up 3D layer to place objects on.
Layer layer = Layer::New();
layer.SetParentOrigin( ParentOrigin::CENTER );
layer.SetAnchorPoint( AnchorPoint::CENTER );
layer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
layer.SetBehavior( Layer::LAYER_2D ); //We use a 2D layer as this is closer to UI work than full 3D scene creation.
layer.SetDepthTestDisabled( false ); //Enable depth testing, as otherwise the 2D layer would not do so.
stage.Add( layer );
//Create gesture detector for panning of models.
mPanGestureDetector = PanGestureDetector::New();
mPanGestureDetector.DetectedSignal().Connect( this, &MeshRendererController::OnPan );
//Add containers to house each renderer-holding-actor.
for( int i = 0; i < NUM_MESHES; i++ )
{
mContainers[i] = Actor::New();
mContainers[i].SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
mContainers[i].RegisterProperty( "Tag", Property::Value( i ) ); //Used to identify the actor and index into the model.
//Position each container on screen
if( i == 0 )
{
//Main, central model
mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE, MODEL_SCALE, 0.0f ) );
mContainers[i].SetParentOrigin( ParentOrigin::CENTER );
mContainers[i].SetAnchorPoint( AnchorPoint::CENTER );
}
else if( i == 1 )
{
//Top left model
mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
mContainers[i].SetParentOrigin( Vector3( 0.05, 0.03, 0.5 ) ); //Offset from top left
mContainers[i].SetAnchorPoint( AnchorPoint::TOP_LEFT );
}
else if( i == 2 )
{
//Top right model
mContainers[i].SetSizeModeFactor( Vector3( MODEL_SCALE / 3.0f, MODEL_SCALE / 3.0f, 0.0f ) );
mContainers[i].SetParentOrigin( Vector3( 0.95, 0.03, 0.5 ) ); //Offset from top right
mContainers[i].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
}
mPanGestureDetector.Attach( mContainers[i] );
layer.Add( mContainers[i] );
}
//Set up models
for( int i = 0; i < NUM_MESHES; i++ )
{
//Create control to display model
Control control = Control::New();
control.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
control.SetParentOrigin( ParentOrigin::CENTER );
control.SetAnchorPoint( AnchorPoint::CENTER );
mContainers[i].Add( control );
//Make model spin to demonstrate 3D
Animation rotationAnimation = Animation::New( 15.0f );
float spin = i % 2 == 0 ? 1.0f : -1.0f; //Make actors spin in different directions to better show independence.
rotationAnimation.AnimateBy( Property( control, Actor::Property::ORIENTATION ),
Quaternion( Degree( 0.0f ), Degree( spin * 360.0f ), Degree( 0.0f ) ) );
rotationAnimation.SetLooping( true );
rotationAnimation.Play();
//Store model information in corresponding structs.
mModels[i].control = control;
mModels[i].rotation.x = 0.0f;
mModels[i].rotation.y = 0.0f;
mModels[i].rotationAnimation = rotationAnimation;
}
//Calling this sets the model in the controls.
ReloadModel();
//Create button for model changing
Toolkit::PushButton modelButton = Toolkit::PushButton::New();
modelButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
modelButton.ClickedSignal().Connect( this, &MeshRendererController::OnChangeModelClicked );
modelButton.SetParentOrigin( Vector3( 0.1, 0.95, 0.5 ) ); //Offset from bottom left
modelButton.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
modelButton.SetLabelText( "Change Model" );
layer.Add( modelButton );
//Create button for shader changing
Toolkit::PushButton shaderButton = Toolkit::PushButton::New();
shaderButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
shaderButton.ClickedSignal().Connect( this, &MeshRendererController::OnChangeShaderClicked );
shaderButton.SetParentOrigin( Vector3( 0.9, 0.95, 0.5 ) ); //Offset from bottom right
shaderButton.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
shaderButton.SetLabelText( "Change Shader" );
layer.Add( shaderButton );
}
//Updates the displayed models to account for parameter changes.
void ReloadModel()
{
//Create mesh property map
Property::Map map;
map.Insert( "rendererType", "mesh" );
map.Insert( "objectUrl", MODEL_FILE[mModelIndex] );
map.Insert( "materialUrl", MATERIAL_FILE[mModelIndex] );
map.Insert( "texturesPath", TEXTURES_PATH );
map.Insert( "shaderType", SHADER_TYPE[mShaderIndex] );
//Set the two controls to use the mesh
for( int i = 0; i < NUM_MESHES; i++ )
{
mModels[i].control.SetProperty( Control::Property::BACKGROUND, Property::Value( map ) );
}
}
//Rotates the panned model based on the gesture.
void OnPan( Actor actor, const PanGesture& gesture )
{
switch( gesture.state )
{
case Gesture::Started:
{
//Find out which model has been selected
actor.GetProperty( actor.GetPropertyIndex( "Tag" ) ).Get( mSelectedModelIndex );
//Pause current animation, as the gesture will be used to manually rotate the model
mModels[mSelectedModelIndex].rotationAnimation.Pause();
break;
}
case Gesture::Continuing:
{
//Rotate based off the gesture.
mModels[mSelectedModelIndex].rotation.x -= gesture.displacement.y / X_ROTATION_DISPLACEMENT_FACTOR; // Y displacement rotates around X axis
mModels[mSelectedModelIndex].rotation.y += gesture.displacement.x / Y_ROTATION_DISPLACEMENT_FACTOR; // X displacement rotates around Y axis
Quaternion rotation = Quaternion( Radian( mModels[mSelectedModelIndex].rotation.x ), Vector3::XAXIS) *
Quaternion( Radian( mModels[mSelectedModelIndex].rotation.y ), Vector3::YAXIS);
mModels[mSelectedModelIndex].control.SetOrientation( rotation );
break;
}
case Gesture::Finished:
{
//Return to automatic animation
mModels[mSelectedModelIndex].rotationAnimation.Play();
break;
}
case Gesture::Cancelled:
{
//Return to automatic animation
mModels[mSelectedModelIndex].rotationAnimation.Play();
break;
}
default:
{
//We can ignore other gestures and gesture states.
break;
}
}
}
//Cycle through the list of models.
bool OnChangeModelClicked( Toolkit::Button button )
{
++mModelIndex %= 3;
ReloadModel();
return true;
}
//Cycle through the list of shaders.
bool OnChangeShaderClicked( Toolkit::Button button )
{
++mShaderIndex %= 3;
ReloadModel();
return true;
}
//If escape or the back button is pressed, quit the application (and return to the launcher)
void OnKeyEvent( const KeyEvent& event )
{
if( event.state == KeyEvent::Down )
{
if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
{
mApplication.Quit();
}
}
}
private:
Application& mApplication;
//The models displayed on screen, including information about rotation.
Model mModels[NUM_MESHES];
Actor mContainers[NUM_MESHES];
//Used to detect panning to rotate the selected model.
PanGestureDetector mPanGestureDetector;
int mModelIndex; //Index of model to load.
int mShaderIndex; //Index of shader type to use.
int mSelectedModelIndex; //Index of model selected on screen.
};
void RunTest( Application& application )
{
MeshRendererController test( application );
application.MainLoop();
}
// Entry point for Linux & Tizen applications
//
int main( int argc, char **argv )
{
Application application = Application::New( &argc, &argv );
RunTest( application );
return 0;
}