/
ogre_application.cpp
508 lines (390 loc) · 16.3 KB
/
ogre_application.cpp
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include "ogre_application.h"
#include "bin/path_config.h"
namespace ogre_application {
/* Some configuration constants */
/* They are written here as global variables, but ideally they should be loaded from a configuration file */
/* Initialization */
const Ogre::String config_filename_g = "";
const Ogre::String plugins_filename_g = "";
const Ogre::String log_filename_g = "Ogre.log";
/* Main window settings */
const Ogre::String window_title_g = "Demo";
const Ogre::String custom_window_capacities_g = "";
const unsigned int window_width_g = 800;
const unsigned int window_height_g = 600;
const bool window_full_screen_g = false;
/* Viewport and camera settings */
float viewport_width_g = 0.95f;
float viewport_height_g = 0.95f;
float viewport_left_g = (1.0f - viewport_width_g) * 0.5f;
float viewport_top_g = (1.0f - viewport_height_g) * 0.5f;
unsigned short viewport_z_order_g = 100;
const Ogre::ColourValue viewport_background_color_g(0.0, 0.0, 0.0);
float camera_near_clip_distance_g = 0.01;
float camera_far_clip_distance_g = 100.0;
Ogre::Vector3 camera_position_g(0.5, 0.5, 1.5);
Ogre::Vector3 camera_look_at_g(0.0, 0.0, 0.0);
Ogre::Vector3 camera_up_g(0.0, 1.0, 0.0);
/* Materials */
const Ogre::String material_directory_g = MATERIAL_DIRECTORY;
OgreApplication::OgreApplication(void){
/* Don't do work in the constructor, leave it for the Init() function */
}
void OgreApplication::Init(void){
/* Set default values for the variables */
animating_ = false;
space_down_ = false;
animation_state_ = NULL;
input_manager_ = NULL;
keyboard_ = NULL;
mouse_ = NULL;
/* Run all initialization steps */
InitRootNode();
InitPlugins();
InitRenderSystem();
InitWindow();
InitViewport();
InitEvents();
InitOIS();
LoadMaterials();
}
void OgreApplication::InitRootNode(void){
try {
/* We need to have an Ogre root to be able to access all Ogre functions */
ogre_root_ = std::auto_ptr<Ogre::Root>(new Ogre::Root(config_filename_g, plugins_filename_g, log_filename_g));
//ogre_root_->showConfigDialog();
}
catch (Ogre::Exception &e){
throw(OgreAppException(std::string("Ogre::Exception: ") + std::string(e.what())));
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::InitPlugins(void){
try {
/* Load plugin responsible for OpenGL render system */
Strings plugin_names;
plugin_names.push_back("RenderSystem_GL");
Strings::iterator iter = plugin_names.begin();
Strings::iterator iter_end = plugin_names.end();
for (; iter != iter_end; iter++){
Ogre::String& plugin_name = (*iter);
if (OGRE_DEBUG_MODE){
plugin_name.append("_d");
}
ogre_root_->loadPlugin(plugin_name);
}
}
catch (Ogre::Exception &e){
throw(OgreAppException(std::string("Ogre::Exception: ") + std::string(e.what())));
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::InitRenderSystem(void){
try {
const Ogre::RenderSystemList& render_system_list = ogre_root_->getAvailableRenderers();
if (render_system_list.size() == 0)
{
throw(OgreAppException(std::string("OgreApp::Exception: Sorry, no rendersystem was found.")));
}
Ogre::RenderSystem *render_system = render_system_list.at(0);
ogre_root_->setRenderSystem(render_system);
}
catch (Ogre::Exception &e){
throw(OgreAppException(std::string("Ogre::Exception: ") + std::string(e.what())));
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::InitWindow(void){
try {
/* Create main window for the application */
bool create_window_automatically = false;
ogre_root_->initialise(create_window_automatically, window_title_g, custom_window_capacities_g);
Ogre::NameValuePairList params;
params["FSAA"] = "0";
params["vsync"] = "true";
ogre_window_ = ogre_root_->createRenderWindow(window_title_g, window_width_g, window_height_g, window_full_screen_g, ¶ms);
ogre_window_->setActive(true);
ogre_window_->setAutoUpdated(false);
}
catch (Ogre::Exception &e){
throw(OgreAppException(std::string("Ogre::Exception: ") + std::string(e.what())));
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::InitViewport(void){
try {
/* Retrieve scene manager and root scene node */
Ogre::SceneManager* scene_manager = ogre_root_->createSceneManager(Ogre::ST_GENERIC, "MySceneManager");
Ogre::SceneNode* root_scene_node = scene_manager->getRootSceneNode();
/* Create camera object */
Ogre::Camera* camera = scene_manager->createCamera("MyCamera");
Ogre::SceneNode* camera_scene_node = root_scene_node->createChildSceneNode("MyCameraNode");
camera_scene_node->attachObject(camera);
camera->setNearClipDistance(camera_near_clip_distance_g);
camera->setFarClipDistance(camera_far_clip_distance_g);
camera->setPosition(camera_position_g);
camera->lookAt(camera_look_at_g);
camera->setFixedYawAxis(true, camera_up_g);
/* Create viewport */
Ogre::Viewport *viewport = ogre_window_->addViewport(camera, viewport_z_order_g, viewport_left_g, viewport_top_g, viewport_width_g, viewport_height_g);
viewport->setAutoUpdated(true);
viewport->setBackgroundColour(viewport_background_color_g);
/* Set aspect ratio */
float ratio = float(viewport->getActualWidth()) / float(viewport->getActualHeight());
camera->setAspectRatio(ratio);
}
catch (Ogre::Exception &e){
throw(OgreAppException(std::string("Ogre::Exception: ") + std::string(e.what())));
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::InitEvents(void){
try {
/* Add this object as a FrameListener (see frameRenderingQueued event) */
ogre_root_->addFrameListener(this);
/* Add this object as a WindowEventListener to handle the window resize event */
Ogre::WindowEventUtilities::addWindowEventListener(ogre_window_, this);
}
catch (Ogre::Exception &e){
throw(OgreAppException(std::string("Ogre::Exception: ") + std::string(e.what())));
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::InitOIS(void){
/* Initialize the Object Oriented Input System (OIS) */
try {
/* Initialize input manager */
OIS::ParamList pl; // Parameter list passed to the input manager initialization
size_t windowHnd = 0;
std::ostringstream windowHndStr;
ogre_window_->getCustomAttribute("WINDOW", &windowHnd);
windowHndStr << windowHnd;
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
pl.insert(std::make_pair(std::string("w32_mouse"),
std::string("DISCL_FOREGROUND" )));
pl.insert(std::make_pair(std::string("w32_mouse"),
std::string("DISCL_NONEXCLUSIVE")));
input_manager_ = OIS::InputManager::createInputSystem(pl);
/*size_t hWnd = 0;
ogre_window_->getCustomAttribute("WINDOW", &hWnd);
input_manager_ = OIS::InputManager::createInputSystem(hWnd);*/
/* Initialize keyboard and mouse */
keyboard_ = static_cast<OIS::Keyboard*>(input_manager_->createInputObject(OIS::OISKeyboard, false));
mouse_ = static_cast<OIS::Mouse*>(input_manager_->createInputObject(OIS::OISMouse, false));
unsigned int width, height, depth;
int top, left;
ogre_window_->getMetrics(width, height, depth, left, top);
const OIS::MouseState &ms = mouse_->getMouseState();
ms.width = width;
ms.height = height;
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::LoadMaterials(void){
try {
/* Load materials that can then be assigned to objects in the scene */
Ogre::String resource_group_name = "MyGame";
Ogre::ResourceGroupManager& resource_group_manager = Ogre::ResourceGroupManager::getSingleton();
resource_group_manager.createResourceGroup(resource_group_name);
bool is_recursive = false;
resource_group_manager.addResourceLocation(material_directory_g, "FileSystem", resource_group_name, is_recursive);
resource_group_manager.initialiseResourceGroup(resource_group_name);
resource_group_manager.loadResourceGroup(resource_group_name);
}
catch (Ogre::Exception &e){
throw(OgreAppException(std::string("Ogre::Exception: ") + std::string(e.what())));
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::CreateWall(Ogre::String material_name){
try {
/* Create two rectangles */
/* Retrieve scene manager and root scene node */
Ogre::SceneManager* scene_manager = ogre_root_->getSceneManager("MySceneManager");
Ogre::SceneNode* root_scene_node = scene_manager->getRootSceneNode();
/* Create the 3D object */
Ogre::ManualObject* object = NULL;
Ogre::String object_name = "Wall";
object = scene_manager->createManualObject(object_name);
object->setDynamic(false);
/* Create triangle list for the object */
object->begin(material_name, Ogre::RenderOperation::OT_TRIANGLE_LIST);
/* Add vertices */
/* One side of the "wall" */
object->position(-1.0,-1.0,0.0);
object->normal(0.0, 0.0, -1.0);
object->tangent(-1.0, 0.0, 0.0);
object->textureCoord(1.0, 0.0);
object->position(-1.0,1.0,0.0);
object->normal(0.0, 0.0, -1.0);
object->tangent(-1.0, 0.0, 0.0);
object->textureCoord(1.0, 1.0);
object->position(1.0,1.0,0.0);
object->normal(0.0, 0.0, -1.0);
object->tangent(-1.0, 0.0, 0.0);
object->textureCoord(0.0, 1.0);
object->position(1.0,-1.0,0.0);
object->normal(0.0, 0.0, -1.0);
object->tangent(-1.0, 0.0, 0.0);
object->textureCoord(0.0, 0.0);
/* The other side of the "wall" */
object->position(-1.0,-1.0,0.0);
object->normal(0.0, 0.0, 1.0);
object->tangent(1.0, 0.0, 0.0);
object->textureCoord(0.0, 0.0);
object->position(-1.0,1.0,0.0);
object->normal(0.0, 0.0, 1.0);
object->tangent(1.0, 0.0, 0.0);
object->textureCoord(0.0, 1.0);
object->position(1.0,1.0,0.0);
object->normal(0.0, 0.0, 1.0);
object->tangent(1.0, 0.0, 0.0);
object->textureCoord(1.0, 1.0);
object->position(1.0,-1.0,0.0);
object->normal(0.0, 0.0, 1.0);
object->tangent(1.0, 0.0, 0.0);
object->textureCoord(1.0, 0.0);
/* Add triangles */
/* One side */
object->triangle(0, 1, 2);
object->triangle(0, 2, 3);
/* The other side */
object->triangle(4, 7, 6);
object->triangle(4, 6, 5);
/* We finished the object */
object->end();
/* Convert triangle list to a mesh */
Ogre::String mesh_name = "Wall";
object->convertToMesh(mesh_name);
/* Create one instance of the sphere (one entity) */
/* The same object can have multiple instances or entities */
Ogre::Entity* entity = scene_manager->createEntity(mesh_name);
/* Apply a material to the entity to give it color */
/* We already did that above, so we comment it out here */
/* entity->setMaterialName(material_name); */
/* But, this call is useful if we have multiple entities with different materials */
/* Create a scene node for the entity */
/* The scene node keeps track of the entity's position */
Ogre::SceneNode* scene_node = root_scene_node->createChildSceneNode(mesh_name);
scene_node->attachObject(entity);
/* Position and rotate the entity with the scene node */
//scene_node->rotate(Ogre::Vector3(0, 1, 0), Ogre::Degree(60));
//scene_node->rotate(Ogre::Vector3(1, 0, 0), Ogre::Degree(30));
//scene_node->rotate(Ogre::Vector3(1, 0, 0), Ogre::Degree(-90));
//scene_node->translate(0.0, 0.0, 0.0);
scene_node->scale(0.5, 0.5, 0.5);
}
catch (Ogre::Exception &e){
throw(OgreAppException(std::string("Ogre::Exception: ") + std::string(e.what())));
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::MainLoop(void){
try {
/* Main loop to keep the application going */
ogre_root_->clearEventTimes();
while(!ogre_window_->isClosed()){
ogre_window_->update(false);
ogre_window_->swapBuffers();
ogre_root_->renderOneFrame();
Ogre::WindowEventUtilities::messagePump();
}
}
catch (Ogre::Exception &e){
throw(OgreAppException(std::string("Ogre::Exception: ") + std::string(e.what())));
}
catch(std::exception &e){
throw(OgreAppException(std::string("std::Exception: ") + std::string(e.what())));
}
}
void OgreApplication::SetupAnimation(Ogre::String object_name){
/* Retrieve scene manager and root scene node */
Ogre::SceneManager* scene_manager = ogre_root_->getSceneManager("MySceneManager");
Ogre::SceneNode* root_scene_node = scene_manager->getRootSceneNode();
/* Set up animation */
Ogre::Real duration = Ogre::Math::TWO_PI;
Ogre::Real num_steps = 36;
Ogre::Real step = duration/num_steps;
Ogre::Animation* animation = scene_manager->createAnimation("Animation", duration);
animation->setInterpolationMode(Ogre::Animation::IM_LINEAR);
Ogre::Node *object_scene_node = root_scene_node->getChild(object_name);
Ogre::NodeAnimationTrack* track = animation->createNodeTrack(0, object_scene_node);
/* Set up frames for animation */
Ogre::TransformKeyFrame* key;
Ogre::Quaternion quat;
for (int i = 0; i < num_steps; i++){
Ogre::Real current = ((float) i) * step;
key = track->createNodeKeyFrame(current);
quat.FromAngleAxis(Ogre::Radian(-current), Ogre::Vector3(0, 1, 0));
key->setRotation(quat);
key->setScale(Ogre::Vector3(0.5, 0.5, 0.5));
}
/* Create animation state */
animation_state_ = scene_manager->createAnimationState("Animation");
animation_state_->setEnabled(true);
animation_state_->setLoop(true);
/* Turn on animating flag */
animating_ = true;
}
bool OgreApplication::frameRenderingQueued(const Ogre::FrameEvent& fe){
/* This event is called after a frame is queued for rendering */
/* Do stuff in this event since the GPU is rendering and the CPU is idle */
/* Keep animating if flag is on */
if (animating_){
animation_state_->addTime(fe.timeSinceLastFrame);
}
/* Capture input */
keyboard_->capture();
mouse_->capture();
/* Handle specific key events */
if (keyboard_->isKeyDown(OIS::KC_SPACE)){
space_down_ = true;
}
if ((!keyboard_->isKeyDown(OIS::KC_SPACE)) && space_down_){
animating_ = !animating_;
space_down_ = false;
}
if (keyboard_->isKeyDown(OIS::KC_ESCAPE)){
animation_state_->setTimePosition(0);
}
if (keyboard_->isKeyDown(OIS::KC_S)){
ogre_window_->writeContentsToFile("normal_map.png");
}
return true;
}
void OgreApplication::windowResized(Ogre::RenderWindow* rw){
/* Update the window and aspect ratio when the window is resized */
int width = rw->getWidth();
int height = rw->getHeight();
Ogre::SceneManager* scene_manager = ogre_root_->getSceneManager("MySceneManager");
Ogre::Camera* camera = scene_manager->getCamera("MyCamera");
if (camera != NULL){
//std::cout << "1 " << (double)width/height << std::endl;
camera->setAspectRatio((double)width/height);
}
const OIS::MouseState &ms = mouse_->getMouseState();
ms.width = width;
ms.height = height;
ogre_window_->resize(width, height);
ogre_window_->windowMovedOrResized();
ogre_window_->update();
}
} // namespace ogre_application;