-
Notifications
You must be signed in to change notification settings - Fork 0
/
ogre4d.cpp
471 lines (409 loc) · 13.5 KB
/
ogre4d.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
// ogre4d.cpp : Defines the entry point for the application.
//
//#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#include "stdafx.h"
//#endif
#include "ogre4d.h"
#include <limits>
Ogre4dApplication::Ogre4dApplication(void) :
mRotatePlane(false),
mAngle(0),
mShiftPlane(0),
mShiftPlaneSpeed(10),
mGoingForward(0),
mGoingRight(0),
mGoingUp(0),
mTopSpeed(10),
mFastMove(false),
mVelocity(Ogre::Vector3::ZERO)
{
}
Ogre4dApplication::~Ogre4dApplication(void)
{
}
//-------------------------------------------------------------------------------------
void Ogre4dApplication::createScene(void)
{
mCameraPosition = Vector4f(0, 0, 60, 0);
mCameraYawNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mCamera->setPosition(0, 0, 0);
mCameraYawNode->attachObject(mCamera);
mCameraYawNode->setPosition(0, 60, 0);
// Set ambient light
mSceneMgr->setAmbientLight(Ogre::ColourValue(0.9f, 0.9f, 0.9f));
// Create a light
//Ogre::Light* l = mSceneMgr->createLight("MainLight");
//l->setPosition(20,80,50);
Ogre::ResourceGroupManager::getSingleton().createResourceGroup("solid colour materials");
Ogre::MaterialManager &materialMgr = Ogre::MaterialManager::getSingleton();
Ogre::MaterialPtr material1 = materialMgr.create("solid black", "solid colour materials");
Ogre::Pass *pass1 = material1->getTechnique(0)->getPass(0);
pass1->setDiffuse(Ogre::ColourValue(0.0f, 0.0f, 0.0f));
pass1->setAmbient(Ogre::ColourValue(0.1f, 0.1f, 0.1f));
material1 = materialMgr.create("solid white", "solid colour materials");
pass1 = material1->getTechnique(0)->getPass(0);
pass1->setDiffuse(Ogre::ColourValue(1.0f, 1.0f, 1.0f));
pass1->setAmbient(Ogre::ColourValue(1.0f, 1.0f, 1.0f));
material1 = materialMgr.create("solid gray", "solid colour materials");
pass1 = material1->getTechnique(0)->getPass(0);
pass1->setDiffuse(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
pass1->setAmbient(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
material1 = materialMgr.create("solid red", "solid colour materials");
pass1 = material1->getTechnique(0)->getPass(0);
pass1->setDiffuse(Ogre::ColourValue(1.0f, 0.0f, 0.0f));
pass1->setAmbient(Ogre::ColourValue(1.0f, 0.0f, 0.0f));
material1 = materialMgr.create("solid blue", "solid colour materials");
pass1 = material1->getTechnique(0)->getPass(0);
pass1->setDiffuse(Ogre::ColourValue(0.0f, 0.0f, 1.0f));
pass1->setAmbient(Ogre::ColourValue(0.0f, 0.0f, 1.0f));
material1 = materialMgr.create("solid green", "solid colour materials");
pass1 = material1->getTechnique(0)->getPass(0);
pass1->setDiffuse(Ogre::ColourValue(0.0f, 1.0f, 0.0f));
pass1->setAmbient(Ogre::ColourValue(0.0f, 1.0f, 0.0f));
material1 = materialMgr.create("solid yellow", "solid colour materials");
pass1 = material1->getTechnique(0)->getPass(0);
pass1->setDiffuse(Ogre::ColourValue(1.0f, 1.0f, 0.0f));
pass1->setAmbient(Ogre::ColourValue(1.0f, 1.0f, 0.0f));
MakeFloor();
MakeWall();
m4dScene.AddHyperCube(Vector4f(0, 0, 25, 0), 50, 50, 50, 50);
m4dScene.AddHyperCube(Vector4f(60, 0, 25, 0), 50, 50, 50, 50);
m4dScene.AddHyperCube(Vector4f(0, 60, 25, 0), 50, 50, 50, 50);
m4dScene.AddHyperCube(Vector4f(0, 0, 25, 60), 50, 50, 50, 50);
for (int i = 0; i < 4; i++)
mNodes.push_back(mSceneMgr->getRootSceneNode()->createChildSceneNode());
for (int i = 0; i < 4; i++)
mManualObjects.push_back(nullptr);
mMaterials.push_back("solid red");
mMaterials.push_back("solid blue");
mMaterials.push_back("solid green");
mMaterials.push_back("solid yellow");
mAngle = 0.1f;
mRotatePlane = false;
renderSlice();
}
Ogre::ManualObject *render3dObject(
ThreeDimensionalObject *obj3d, const Ogre::String &name, const Ogre::String &materialName)
{
if (obj3d == nullptr || obj3d->IsEmpty())
return nullptr;
Ogre::ManualObject* obj = new Ogre::ManualObject(name);
obj->estimateVertexCount(obj3d->GetVerticesCount());
obj->estimateIndexCount(obj3d->GetTrianglesCount() * 3 * 2);
obj->begin(materialName);
ThreeDimensionalObject::ConstVertexIterator it = obj3d->VerticesConstBegin();
for (; it != obj3d->VerticesConstEnd(); ++it)
obj->position(*it);
ThreeDimensionalObject::ConstTriangleIterator tri = obj3d->TrianglesConstBegin();
for (; tri != obj3d->TrianglesConstEnd(); ++tri)
{
obj->triangle(tri->vertices[0], tri->vertices[1], tri->vertices[2]);
obj->triangle(tri->vertices[0], tri->vertices[2], tri->vertices[1]);
}
obj->end();
return obj;
}
void Ogre4dApplication::renderSlice()
{
static unsigned long int num = 0;
m4dScene.MakeSlice(mCameraPlane);
FourDimensionalScene::SliceIterator sit = m4dScene.SliceConstBegin();
for (int i = 0; sit != m4dScene.SliceConstEnd(); ++sit, ++i)
{
mManualObjects[i] = render3dObject(*sit, Ogre::StringConverter::toString(num), mMaterials[i]);
if (mManualObjects[i] != nullptr)
mNodes[i]->attachObject(mManualObjects[i]);
}
}
void Ogre4dApplication::clearSlice()
{
for (size_t i = 0; i < mManualObjects.size(); ++i)
{
if (mManualObjects[i] != nullptr)
{
mNodes[i]->detachObject(mManualObjects[i]);
mSceneMgr->destroyManualObject(mManualObjects[i]);
mManualObjects[i] = nullptr;
}
}
}
void Ogre4dApplication::MakeFloor()
{
float cubeSize = 400;
bool white = true;
for (int i = -2; i < 2; i++)
{
for (int j = -2; j < 2; j++)
{
for (int k = -2; k < 2; k++)
{
Vector4f center((i + 0.5) * cubeSize, (j + 0.5) * cubeSize, 0.0f, (k + 0.5) * cubeSize);
m4dScene.AddCube(center, cubeSize, cubeSize, cubeSize, 1.0f);
mNodes.push_back(mSceneMgr->getRootSceneNode()->createChildSceneNode());
mManualObjects.push_back(nullptr);
mMaterials.push_back(white ? "solid white" : "solid black");
white = !white;
}
white = !white;
}
white = !white;
}
}
void Ogre4dApplication::MakeWall()
{
float height = 100;
float width = 10;
float floorSize = 1600;
for (size_t coord = 0; coord < 3; coord++)
for (int i = -1; i <= 1; i+=2)
{
Vector4f center(0.0f, 0.0f, 0.0f, 0.5*height);
center[coord] = i*0.5*(floorSize+width);
std::swap(center[2], center[3]);
Vector4f size(floorSize, floorSize, floorSize, height);
size[coord] = width;
std::swap(size[2], size[3]);
m4dScene.AddHyperCube(center, size[0], size[1], size[2], size[3]);
mNodes.push_back(mSceneMgr->getRootSceneNode()->createChildSceneNode());
mManualObjects.push_back(nullptr);
mMaterials.push_back("solid gray");
}
}
bool Ogre4dApplication::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
bool ret = BaseApplication::frameRenderingQueued(evt);
if (!ret)
return false;
bool updateSlice = false;
if (mAngle != 0.0f)
{
mCameraPosition = mCameraPlane.GetGlobalCoordinates(mCameraYawNode->getPosition());
mCameraPlane.RotateAroundXY(mAngle, mCameraPosition);
mCameraYawNode->setPosition(mCameraPlane.GetInnerCoordinates(mCameraPosition));
updateSlice = true;
mAngle = 0.0f;
}
if (mShiftPlane != 0)
{
updateSlice = true;
float shift = (mFastMove ? 20 : 1) * mShiftPlane * mShiftPlaneSpeed * evt.timeSinceLastFrame;
mCameraPlane.Shift(shift);
}
if (updateSlice)
{
clearSlice();
renderSlice();
}
{
// build our acceleration vector based on keyboard input composite
Ogre::Vector3 accel = Ogre::Vector3::ZERO;
accel.z -= mGoingForward;
accel.x += mGoingRight;
accel.y += mGoingUp;
//accel += mGoingForward * mCamera->getDirection();
//accel += mGoingRight * mCamera->getRight();
//accel += mGoingUp * mCamera->getUp();
// if accelerating, try to reach top speed in a certain time
Ogre::Real topSpeed = mFastMove ? mTopSpeed * 20 : mTopSpeed;
if (accel.squaredLength() != 0)
{
accel.normalise();
mVelocity += accel * topSpeed * evt.timeSinceLastFrame * 10;
}
// if not accelerating, try to stop in a certain time
else mVelocity -= mVelocity * evt.timeSinceLastFrame * 10;
Ogre::Real tooSmall = std::numeric_limits<Ogre::Real>::epsilon();
// keep camera velocity below top speed and above epsilon
if (mVelocity.squaredLength() > topSpeed * topSpeed)
{
mVelocity.normalise();
mVelocity *= topSpeed;
}
else if (mVelocity.squaredLength() < tooSmall * tooSmall)
mVelocity = Ogre::Vector3::ZERO;
if (mVelocity != Ogre::Vector3::ZERO)
//mCamera->move(mVelocity * evt.timeSinceLastFrame);
mCameraYawNode->translate(mVelocity, Ogre::Node::TS_LOCAL);
}
mCameraPosition = mCameraPlane.GetGlobalCoordinates(mCameraYawNode->getPosition());
return true;
}
bool Ogre4dApplication::keyPressed( const OIS::KeyEvent &arg )
{
switch (arg.key)
{
case OIS::KC_Q:
mShiftPlane = -1;
return true;
case OIS::KC_E:
mShiftPlane = 1;
return true;
case OIS::KC_S:
case OIS::KC_DOWN:
mGoingForward = -1;
return true;
case OIS::KC_W:
case OIS::KC_UP:
mGoingForward = 1;
return true;
case OIS::KC_A:
case OIS::KC_LEFT:
mGoingRight = -1;
return true;
case OIS::KC_D:
case OIS::KC_RIGHT:
mGoingRight = 1;
return true;
case OIS::KC_PGUP:
mGoingUp = 1;
return true;
case OIS::KC_PGDOWN:
mGoingUp = -1;
return true;
case OIS::KC_LSHIFT:
mFastMove = true;
return true;
default:
break;
}
return BaseApplication::keyPressed(arg);
}
bool Ogre4dApplication::keyReleased( const OIS::KeyEvent &arg )
{
switch (arg.key)
{
case OIS::KC_Q:
mShiftPlane = 0;
return true;
case OIS::KC_E:
mShiftPlane = 0;
return true;
case OIS::KC_S:
case OIS::KC_DOWN:
case OIS::KC_W:
case OIS::KC_UP:
mGoingForward = 0;
return true;
case OIS::KC_A:
case OIS::KC_LEFT:
case OIS::KC_D:
case OIS::KC_RIGHT:
mGoingRight = 0;
return true;
case OIS::KC_PGUP:
case OIS::KC_PGDOWN:
mGoingUp = 0;
return true;
case OIS::KC_LSHIFT:
mFastMove = false;
return true;
default:
break;
}
return BaseApplication::keyReleased(arg);
}
bool Ogre4dApplication::mouseMoved( const OIS::MouseEvent &arg )
{
if (mRotatePlane)
{
mAngle = Ogre::Degree(-arg.state.X.rel * 0.15f).valueRadians();
return true;
}
mCameraYawNode->yaw(Ogre::Degree(-arg.state.X.rel * 0.15f), Ogre::Node::TS_LOCAL);
mCamera->pitch(Ogre::Degree(-arg.state.Y.rel * 0.15f));
{
// Angle of rotation around the X-axis.
float pitchAngle = 2 * Ogre::Degree(Ogre::Math::ACos(mCamera->getOrientation().w)).valueDegrees();
// Just to determine the sign of the angle we pick up above, the
// value itself does not interest us.
float pitchAngleSign = mCamera->getOrientation().x;
// Limit the pitch between -90 degress and +90 degrees, Quake3-style.
if (pitchAngle > 90.0f)
{
if (pitchAngleSign > 0)
// Set orientation to 90 degrees on X-axis.
mCamera->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
Ogre::Math::Sqrt(0.5f), 0, 0));
else if (pitchAngleSign < 0)
// Sets orientation to -90 degrees on X-axis.
mCamera->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
-Ogre::Math::Sqrt(0.5f), 0, 0));
}
}
return BaseApplication::mouseMoved(arg);
}
bool Ogre4dApplication::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
if (id == OIS::MB_Left)
{
mRotatePlane = true;
return true;
}
return BaseApplication::mousePressed(arg, id);
}
bool Ogre4dApplication::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
if (id == OIS::MB_Left)
{
mRotatePlane = false;
return true;
}
return BaseApplication::mouseReleased(arg, id);
}
Ogre::StringVector Ogre4dApplication::getDetailsPanelFields()
{
Ogre::StringVector items;
items.push_back("cam.pW");
items.push_back("cam.pX");
items.push_back("cam.pY");
items.push_back("cam.pZ");
items.push_back("");
items.push_back("cam.oW");
items.push_back("cam.oX");
items.push_back("cam.oY");
items.push_back("cam.oZ");
items.push_back("");
items.push_back("Filtering");
items.push_back("Poly Mode");
return items;
}
void Ogre4dApplication::updateDetailsPanelContent()
{
mDetailsPanel->setParamValue(0, Ogre::StringConverter::toString(mCameraPosition[0]));
mDetailsPanel->setParamValue(1, Ogre::StringConverter::toString(mCameraPosition[1]));
mDetailsPanel->setParamValue(2, Ogre::StringConverter::toString(mCameraPosition[2]));
mDetailsPanel->setParamValue(3, Ogre::StringConverter::toString(mCameraPosition[3]));
mDetailsPanel->setParamValue(5, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().w));
mDetailsPanel->setParamValue(6, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().x));
mDetailsPanel->setParamValue(7, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().y));
mDetailsPanel->setParamValue(8, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().z));
Ogre::String filtering;
switch (mFiltering)
{
case Ogre::TFO_BILINEAR:
filtering = "Bilinear";
break;
case Ogre::TFO_TRILINEAR:
filtering = "Trilinear";
break;
case Ogre::TFO_ANISOTROPIC:
filtering = "Anisotropic";
break;
default:
filtering = "None";
}
mDetailsPanel->setParamValue(10, filtering);
Ogre::String polyMode;
switch (mCamera->getPolygonMode())
{
case Ogre::PM_SOLID:
polyMode = "Solid";
break;
case Ogre::PM_WIREFRAME:
polyMode = "Wireframe";
break;
default:
polyMode = "Points";
}
mDetailsPanel->setParamValue(11, polyMode);
}