-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBareOgre.cpp
509 lines (405 loc) · 13.8 KB
/
BareOgre.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
509
#include <iostream>
#include "BareOgre.h"
#include "OgreCamera.h"
#include "OgreOptionsFile.h"
#include "OgreEntity.h"
#include "OgreException.h"
#include "OgreSceneManager.h"
#include "OgreRenderWindow.h"
#include "OgreViewport.h"
#include "OgreWindowEventUtilities.h"
#include "OgreSceneNode.h"
#include "OgreMaterialManager.h"
#include "OgreTextureManager.h"
BareOgre::BareOgre()
: mSlowMove(false)
, mMovingForward(false)
, mMovingBack(false)
, mMovingRight(false)
, mMovingLeft(false)
, mMovingUp(false)
, mMovingDown(false)
, mPluginsCfg("")
, mResourcesCfg("")
, mRoot(0)
, mShutdown(false)
, mVelocityDirection(Ogre::Vector3::ZERO)
{
// Default camera control parameters.
mRotationSpeed = 0.13;
mMaxSpeed = 1000;
mSlowMaxSpeed = mMaxSpeed / 15;
mAccelerationRate = 100;
}
BareOgre::~BareOgre()
{
// Remove ourself as a window listener.
// Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
// windowClosed(mWindow);
SDL_Quit();
delete mRoot;
}
bool BareOgre::go()
{
mLog = new Ogre::LogManager;
//TODO: Come up with a better log name.
mLog->createLog("ogre.log");
#ifdef _DEBUG
mResourcesCfg = "resources_d.cfg";
mPluginsCfg = "plugins_d.cfg";
#else
mResourcesCfg = "resources.cfg";
mPluginsCfg = "plugins.cfg";
#endif
mRoot = new Ogre::Root(mPluginsCfg);
// Load resource paths from resources config file.
setupResources();
// Configure dialog.
if (!(mRoot->restoreOptions() || mRoot->showOptionsDialog())) {
return false;
}
SDL_Init(0);
// Create RenderWindow.
mWindow = mRoot->initialise(true, "BareOgre Render Window");
// initVideo();
// mWindow = mRoot->initialise(false);
// struct SDL_SysWMinfo wmInfo;
// SDL_VERSION(&wmInfo.version);
// if (SDL_GetWindowWMInfo(mSDLWindow, &wmInfo) == SDL_FALSE)
// throw std::runtime_error("Couldn't get WM Info!");
// Ogre::String winHandle;
// switch (wmInfo.subsystem)
// {
// case SDL_SYSWM_X11:
// winHandle = Ogre::StringConverter::toString((unsigned long)wmInfo.info.x11.window);
// break;
// default:
// throw std::runtime_error("Unexpected WM!");
// break;
// }
// Ogre::NameValuePairList params;
// params.insert(std::make_pair("title", "OGRE Window"));
// params.insert(std::make_pair("FSAA", "0"));
// params.insert(std::make_pair("vsync", "false"));
// // params.insert(std::make_pair("parentWindowHandle", winHandle));
// params.insert(std::make_pair("externalWindowHandle", winHandle));
// mWindow = Ogre::Root::getSingleton().createRenderWindow("OGRE Window", 640, 480, false, ¶ms);
// mWindow->setVisible(true);
// mWindow->setActive(true);
// mWindow->setAutoUpdated(false);
// Initialize input. (SDL2)
initInput();
// Set default mipmap level.
Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
// Initialize all resource groups.
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
// Create scene.
createScene();
// Set initial mouse clipping size.
// windowResized(mWindow);
// Register as a Window listener.
// Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
// Start render loop.
mLog->logMessage("*** Begin Rendering ***");
mRoot->addFrameListener(this);
mRoot->startRendering();
return true;
}
void BareOgre::createScene()
{
// Create SceneManager.
mSceneMgr = mRoot->createSceneManager("DefaultSceneManager");
Ogre::SceneNode rootNode = SceneMgr->getRootSceneNode();
// Create a camera.
mCamera = mSceneMgr->createCamera("PlayerCam");
mCamera->setPosition(Ogre::Vector3(0, 0, 80));
mCamera->lookAt(Ogre::Vector3(0, 0, -3));
mCamera->setNearClipDistance(5);
mCamNode = rootNode->createChildSceneNode("CamNode");
mCamNode->attachObject(mCamera);
// Create one viewport, entire window.
Ogre::Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(Ogre::ColourValue(0, 0, 0));
// Set camera aspect ratio to match viewport.
mCamera->setAspectRatio(
Ogre::Real(vp->getActualWidth() / Ogre::Real(vp->getActualHeight())));
// Add lighting.
mSceneMgr->setAmbientLight(Ogre::ColourValue(1.0, 1.0, 1.0));
Ogre::Light* l = mSceneMgr->createLight("MainLight");
l->setPosition((Ogre::Real)20.0, (Ogre::Real)80.0, (Ogre::Real)50.0);
// Add meshes.
Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh");
Ogre::SceneNode* headNode = rootNode->createChildSceneNode();
headNode->attachObject(ogreHead);
}
// void BareOgre::initInput()
// {
// mLog->logMessage("*** Initializing OIS ***");
// OIS::ParamList pl;
// size_t windowHnd = 0;
// std::ostringstream windowHndStr;
// mWindow->getCustomAttribute("WINDOW", &windowHnd);
// windowHndStr << windowHnd;
// pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
// //TODO: Remove me or integrate further.
// bool nograb = true;
// if (nograb)
// {
// pl.insert(std::make_pair("x11_keyboard_grab", "false"));
// pl.insert(std::make_pair("x11_mouse_grab", "false"));
// pl.insert(std::make_pair("w32_mouse", "DISCL_FOREGROUND"));
// pl.insert(std::make_pair("w32_mouse", "DISCL_NONEXCLUSIVE"));
// pl.insert(std::make_pair("w32_keyboard", "DISCL_FOREGROUND"));
// pl.insert(std::make_pair("w32_keyboard", "DISCL_NONEXCLUSIVE"));
// }
// mInputManager = OIS::InputManager::createInputSystem(pl);
// //TODO switch to buffered input
// mKeyboard = static_cast<OIS::Keyboard*>(
// mInputManager->createInputObject(OIS::OISKeyboard, true));
// mMouse = static_cast<OIS::Mouse*>(
// mInputManager->createInputObject(OIS::OISMouse, true));
// mKeyboard->setEventCallback(this);
// mMouse->setEventCallback(this);
// }
// void BareOgre::initVideo()
// {
// mLog->logMessage("*** Initializing SDL2 Video ***");
// // if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0)
// // {
// // mLog->logMessage("error: SDL2 input failed to initialize!");
// // //TODO: error handling.
// // }
// // Create an application window with the following settings:
// mSDLWindow = SDL_CreateWindow(
// "An SDL2 window", // window title
// SDL_WINDOWPOS_UNDEFINED, // initial x position
// SDL_WINDOWPOS_UNDEFINED, // initial y position
// 640, // width, in pixels
// 480, // height, in pixels
// SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_GRABBED // flags - see below
// );
// // Check that the window was successfully made
// if (mSDLWindow == NULL) {
// // In the event that the window could not be made...
// std::cout << "Could not create window: " << SDL_GetError() << std::endl;
// }
// }
void BareOgre::initInput()
{
mLog->logMessage("*** Initializing SDL2 Input ***");
if (SDL_InitSubSystem(SDL_INIT_EVENTS) != 0)
{
mLog->logMessage("error: SDL2 input failed to initialize!");
//TODO: error handling.
}
}
void BareOgre::setupResources()
{
Ogre::OptionsFile cf;
cf.load(mResourcesCfg);
//TODO convert to C++11
Ogre::OptionsFile::SectionIterator seci = cf.getSectionIterator();
Ogre::String secName, typeName, archName;
while (seci.hasMoreElements()) {
secName = seci.peekNextKey();
Ogre::OptionsFile::OptionsMultiMap *settings = seci.getNext();
Ogre::OptionsFile::OptionsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); i++) {
typeName = i->first;
archName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName);
}
}
}
bool BareOgre::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
if (mWindow->isClosed()) return false;
if (mShutdown) return false;
// Need to capture/update each device.
processInput();
// Build our acceleration vector based on keyboard input composite.
mAcceleration = Ogre::Vector3::ZERO;
if (mMovingForward) mAcceleration += mCamera->getDirection();
if (mMovingBack) mAcceleration -= mCamera->getDirection();
if (mMovingRight) mAcceleration += mCamera->getRight();
if (mMovingLeft) mAcceleration -= mCamera->getRight();
if (mMovingUp) mAcceleration += mCamera->getUp();
if (mMovingDown) mAcceleration -= mCamera->getUp();
// If accelerating, try to reach max speed in a certain time.
if (mAcceleration != Ogre::Vector3::ZERO)
{
mAcceleration.normalise();
mVelocity += mAccelerationRate * mAcceleration * evt.timeSinceLastFrame;
}
// If not accelerating, but still moving, come to a gradual stop.
else if (mVelocity.squaredLength() > 1E-10)
{
mVelocity /= mAccelerationRate * evt.timeSinceLastFrame;
}
// If moving extremely slowly, stop.
else
{
mVelocity = Ogre::Vector3::ZERO;
}
if (mVelocity != Ogre::Vector3::ZERO)
{
// Keep camera velocity below max speed and above epsilon.
Ogre::Real maxSpeed = mSlowMove ? mSlowMaxSpeed : mMaxSpeed;
if (mVelocity.squaredLength() > maxSpeed * maxSpeed)
{
mVelocity.normalise();
mVelocity *= maxSpeed;
}
mCamera->move(mVelocity * evt.timeSinceLastFrame);
}
return true;
}
// Adjust mouse clipping area.
// void BareOgre::windowResized(Ogre::RenderWindow* rw)
// {
// unsigned int width, height, depth;
// int left, top;
// rw->getMetrics(width, height, depth, left, top);
// // const OIS::MouseState &ms = mMouse->getMouseState();
// // ms.width = width;
// // ms.height = height;
// }
// // Unattach OIS before window shutdown (very important under Linux).
// void BareOgre::windowClosed(Ogre::RenderWindow* rw)
// {
// SDL_Quit();
// }
//==============================================================================
// User Input (SDL2)
//==============================================================================
void BareOgre::processInput()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
mShutdown = true;
break;
case SDL_KEYDOWN:
if (event.key.repeat) break;
std::cout << "Key down!" << std::endl;
keyPressed(event.key);
break;
case SDL_KEYUP:
std::cout << "Key up!" << std::endl;
keyReleased(event.key);
break;
case SDL_MOUSEBUTTONDOWN:
std::cout << "Mouse button down!" << std::endl;
mousePressed(event.button);
break;
case SDL_MOUSEBUTTONUP:
mouseReleased(event.button);
break;
case SDL_MOUSEMOTION:
SDL_ShowCursor(SDL_DISABLE);
SDL_SetRelativeMouseMode(SDL_TRUE);
mouseMoved(event.motion);
break;
case SDL_MOUSEWHEEL:
mouseWheelRotated(event.wheel);
break;
// default:
// std::cout << "Got somethin!" << std::endl;
}
}
}
void BareOgre::keyPressed(const SDL_KeyboardEvent& event) {
SDL_Keysym key = event.keysym;
switch (key.scancode)
{
case SDL_SCANCODE_UP:
case SDL_SCANCODE_W:
mMovingForward = true;
break;
case SDL_SCANCODE_DOWN:
case SDL_SCANCODE_S:
mMovingBack = true;
break;
case SDL_SCANCODE_RIGHT:
case SDL_SCANCODE_D:
mMovingRight = true;
break;
case SDL_SCANCODE_LEFT:
case SDL_SCANCODE_A:
mMovingLeft = true;
break;
case SDL_SCANCODE_PAGEUP:
case SDL_SCANCODE_Q:
mMovingUp = true;
break;
case SDL_SCANCODE_PAGEDOWN:
case SDL_SCANCODE_E:
mMovingDown = true;
break;
case SDL_SCANCODE_LSHIFT:
mSlowMove = true;
break;
case SDL_SCANCODE_ESCAPE:
mShutdown = true;
break;
case SDL_SCANCODE_PRINTSCREEN:
mWindow->writeContentsToTimestampedFile("screenshot_", ".jpg");
break;
case SDL_SCANCODE_F5:
Ogre::TextureManager::getSingleton().reloadAll();
break;
}
}
void BareOgre::keyReleased(const SDL_KeyboardEvent& event) {
SDL_Keysym key = event.keysym;
switch (key.scancode) {
case SDL_SCANCODE_UP:
case SDL_SCANCODE_W:
mMovingForward = false;
break;
case SDL_SCANCODE_DOWN:
case SDL_SCANCODE_S:
mMovingBack = false;
break;
case SDL_SCANCODE_RIGHT:
case SDL_SCANCODE_D:
mMovingRight = false;
break;
case SDL_SCANCODE_LEFT:
case SDL_SCANCODE_A:
mMovingLeft = false;
break;
case SDL_SCANCODE_PAGEUP:
case SDL_SCANCODE_Q:
mMovingUp = false;
break;
case SDL_SCANCODE_PAGEDOWN:
case SDL_SCANCODE_E:
mMovingDown = false;
break;
case SDL_SCANCODE_LSHIFT:
mSlowMove = false;
break;
}
}
void BareOgre::mouseMoved(const SDL_MouseMotionEvent& event)
{
mCamera->yaw(Ogre::Degree(-event.xrel * mRotationSpeed));
mCamera->pitch(Ogre::Degree(-event.yrel * mRotationSpeed));
}
void BareOgre::mousePressed(const SDL_MouseButtonEvent& event)
{
// Nothing to do (for now).
}
void BareOgre::mouseReleased(const SDL_MouseButtonEvent& event)
{
// Nothing to do (for now).
}
void BareOgre::mouseWheelRotated(const SDL_MouseWheelEvent& event)
{
// Nothing to do (for now).
}