-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOctreeSceneNode.cpp
633 lines (561 loc) · 14.1 KB
/
OctreeSceneNode.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#include "OctreeSceneNode.h"
OctreeSceneNode::QueuedUpdates OctreeSceneNode::msQueuedUpdates;
OctreeSceneNode::OctreeSceneNode(const String& name)
:mParent(0),
mNeedParentUpdate(false),
mNeedChildUpdate(false),
mParentNotified(false),
mQueuedForUpdate(false),
mName(name),
mOrientation(Quaternion::IDENTITY),
mPosition(Vector3::ZERO),
mScale(Vector3::UNIT_SCALE),
mInheritOrientation(true),
mInheritScale(true),
mDerivedOrientation(Quaternion::IDENTITY),
mDerivedPosition(Vector3::ZERO),
mDerivedScale(Vector3::UNIT_SCALE),
mCachedTransformOutOfDate(true)
{
needUpdate();
}
OctreeSceneNode::~OctreeSceneNode()
{
removeAllChildren();
if (mParent)
mParent->removeChild(this);
if (mQueuedForUpdate)
{
//Erase from queued updates
QueuedUpdates::iterator it = std::find(msQueuedUpdates.begin(), msQueuedUpdates.end(), this);
assert(it!= msQueuedUpdates.end());
if (it != msQueuedUpdates.end())
{
//Optimised algorithm to erase an element from unordered vector.
*it = msQueuedUpdates.back();
msQueuedUpdates.pop_back();
}
}
}
void OctreeSceneNode::attachEntity(Entity * entity)
{
}
void OctreeSceneNode::setOrientation(const Quaternion & q)
{
if (q.isNaN())
{
String tip;
tip.append("Invalid orientation supplied as parameter");
MessageBoxA(NULL, tip.c_str(), "ERROR", MB_ICONERROR);
}
mOrientation = q;
mOrientation.normalise();
needUpdate();
}
void OctreeSceneNode::setOrientation(Real w, Real x, Real y, Real z)
{
setOrientation(Quaternion(w, x, y, z));
}
void OctreeSceneNode::resetOrientation()
{
mOrientation = Quaternion::IDENTITY;
needUpdate();
}
void OctreeSceneNode::setPosition(const Vector3 & pos)
{
assert(!pos.isNaN() && "Invalid vector supplied as parameter");
mPosition = pos;
needUpdate();
}
void OctreeSceneNode::setPosition(Real x, Real y, Real z)
{
Vector3 v(x,y,z);
setPosition(v);
}
void OctreeSceneNode::setScale(const Vector3 & scale)
{
assert(!scale.isNaN() && "Invalid vector supplied as parameter");
mScale = scale;
needUpdate();
}
void OctreeSceneNode::setScale(Real x, Real y, Real z)
{
setScale(Vector3(x,y,z));
}
void OctreeSceneNode::setInheritOrientation(bool inherit)
{
mInheritOrientation = inherit;
needUpdate();
}
void OctreeSceneNode::setInheritScale(bool inherit)
{
mInheritScale = inherit;
needUpdate();
}
void OctreeSceneNode::scale(const Vector3 & scale)
{
mScale = mScale * scale;
needUpdate();
}
void OctreeSceneNode::scale(Real x, Real y, Real z)
{
mScale.x *= x;
mScale.y *= y;
mScale.z *= z;
needUpdate();
}
void OctreeSceneNode::translate(const Vector3 & d, TransformSpace relativeTo)
{
switch (relativeTo)
{
case TS_LOCAL:
//position is relative to parent so transform downwards
mPosition += mOrientation *d;
break;
case TS_WORLD:
//position is relative to parent so transform upwards
if (mParent)
{
mPosition += mParent->convertWorldToLocalDirection(d, true);
}
else
{
mPosition += d;
}
break;
case TS_PARENT:
mPosition += d;
break;
}
needUpdate();
}
void OctreeSceneNode::translate(Real x, Real y, Real z, TransformSpace relativeTo)
{
Vector3 v(x,y,z);
translate(v,relativeTo);
}
void OctreeSceneNode::translate(const Matrix3 & axes, const Vector3 & move, TransformSpace relativeTo)
{
Vector3 derived = axes * move;
translate(derived,relativeTo);
}
void OctreeSceneNode::translate(const Matrix3 & axes, Real x, Real y, Real z, TransformSpace relativeTo)
{
Vector3 d(x,y,z);
translate(axes,d,relativeTo);
}
const Vector3 & OctreeSceneNode::_getDerivedScale() const
{
// TODO: ÔÚ´Ë´¦²åÈë return Óï¾ä
if (mNeedParentUpdate)
{
_updateFromParent();
}
return mDerivedScale;
}
const Matrix4& OctreeSceneNode::_getFullTransform() const
{
if (mCachedTransformOutOfDate)
{
//use derived values
mCachedTransform.makeTransform(_getDerivedPosition(),_getDerivedScale(),_getDerivedOrientation());
mCachedTransformOutOfDate = false;
}
return mCachedTransform;
}
void OctreeSceneNode::_update(bool updateChildren, bool parentHasChanged)
{
//always clear information about parent notification
mParentNotified = false;
//see if we should process everyone
if (mNeedParentUpdate || parentHasChanged)
{
//update transform from parent
_updateFromParent();
}
if (updateChildren)
{
if (mNeedChildUpdate || parentHasChanged)
{
ChildNodeMap::iterator it, itend;
itend = mChildren.end();
for (it = mChildren.begin(); it != itend; ++it)
{
OctreeSceneNode* child = it->second;
child->_update(true,true);
}
}
else
{
//Just update selected children
ChildUpdateSet::iterator it, itend;
itend = mChildrenToUpdate.end();
for (it = mChildrenToUpdate.begin(); it != itend; ++it)
{
OctreeSceneNode* child = *it;
child->_update(true,false);
}
}
mChildrenToUpdate.clear();
mNeedChildUpdate = false;
}
}
Vector3 OctreeSceneNode::convertWorldToLocalPosition(const Vector3 & worldPos)
{
if (mNeedParentUpdate)
{
_updateFromParent();
}
return mDerivedOrientation.Inverse() * (worldPos - mDerivedPosition) / mDerivedScale;
}
Vector3 OctreeSceneNode::convertLocalToWorldPosition(const Vector3 & localPos)
{
if (mNeedParentUpdate)
{
_updateFromParent();
}
return _getFullTransform().transformAffine(localPos);
}
Vector3 OctreeSceneNode::convertWorldToLocalDirection(const Vector3 & worldDir, bool useScale)
{
if (mNeedParentUpdate)
{
_updateFromParent();
}
return useScale? mDerivedOrientation.Inverse() * worldDir / mDerivedScale :
mDerivedOrientation.Inverse() * worldDir;
}
Vector3 OctreeSceneNode::convertLocalToWorldDirection(const Vector3 & localDir, bool useScale)
{
if (mNeedParentUpdate)
{
_updateFromParent();
}
return useScale ? _getFullTransform().transformDirectionAffine(localDir) : mDerivedOrientation * localDir;
}
Quaternion OctreeSceneNode::convertWorldToLocalOrientation(const Quaternion & worldOrientation)
{
if (mNeedParentUpdate)
{
_updateFromParent();
}
return mDerivedOrientation.Inverse() * worldOrientation;
}
Quaternion OctreeSceneNode::convertLocalToWorldOrientation(const Quaternion & localOrientation)
{
if (mNeedParentUpdate)
{
_updateFromParent();
}
return mDerivedOrientation * localOrientation;
}
void OctreeSceneNode::needUpdate(bool forceParentUpdate)
{
mNeedParentUpdate = true;
mNeedChildUpdate = true;
mCachedTransformOutOfDate = true;
//Make sure we're not root and parent hasn't been notified before
if (mParent && (!mParentNotified || forceParentUpdate))
{
mParent->requestUpdate(this,forceParentUpdate);
mParentNotified = true;
}
//all children will be updated
mChildrenToUpdate.clear();
}
void OctreeSceneNode::requestUpdate(OctreeSceneNode * child, bool forceParentUpdate)
{
//If we're already going to update everything this doesn't matter
if (mNeedChildUpdate)
{
return;
}
mChildrenToUpdate.insert(child);
//Request selective update of me,if we didn't do it before
if (mParent && (!mParentNotified || forceParentUpdate))
{
mParent->requestUpdate(this,forceParentUpdate);
mParentNotified = true;
}
}
void OctreeSceneNode::cancelUpdate(OctreeSceneNode * child)
{
mChildrenToUpdate.erase(child);
//Propagate this up if we are done
if (mChildrenToUpdate.empty() && mParent && !mNeedChildUpdate)
{
mParent->cancelUpdate(this);
mParentNotified = false;
}
}
void OctreeSceneNode::queueNeedUpdate(OctreeSceneNode * n)
{
//Don't queue the node more than once
if (!n->mQueuedForUpdate)
{
n->mQueuedForUpdate = true;
msQueuedUpdates.push_back(n);
}
}
void OctreeSceneNode::processQueuedUpdates()
{
for (QueuedUpdates::iterator i = msQueuedUpdates.begin(); i != msQueuedUpdates.end(); ++i)
{
//Update,and force parent update since chances are we've ended up with some mixed state in there due to re-entrancy
OctreeSceneNode* n = *i;
n->mQueuedForUpdate = false;
n->needUpdate(true);
}
msQueuedUpdates.clear();
}
void OctreeSceneNode::setParent(OctreeSceneNode * parent)
{
mParent = parent;
//Request update from parent
mParentNotified = false;
needUpdate();
}
void OctreeSceneNode::_updateFromParent(void) const
{
updateFromParentImpl();
}
void OctreeSceneNode::updateFromParentImpl(void) const
{
mCachedTransformOutOfDate = true;
if (mParent)
{
//Update orientation
const Quaternion& parentOrientation = mParent->_getDerivedOrientation();
if (mInheritOrientation)
{
//Combine orientation with that of parent
mDerivedOrientation = parentOrientation * mOrientation;
}
else
{
//No inheritance
mDerivedOrientation = mOrientation;
}
//Update scale
const Vector3& parentScale = mParent->_getDerivedScale();
if (mInheritScale)
{
mDerivedScale = parentScale * mScale;
}
else
{
mDerivedScale = mScale;
}
//Change position vector based on parent's orientation & scale
mDerivedPosition = parentOrientation * (parentScale * mPosition);
//Add altered position vector to parents
mDerivedPosition += mParent->_getDerivedPosition();
}
else
{
//Root node,no parent
mDerivedOrientation = mOrientation;
mDerivedPosition = mPosition;
mDerivedScale = mScale;
}
mNeedParentUpdate = false;
}
OctreeSceneNode * OctreeSceneNode::createChildImpl(const String & name)
{
return nullptr;
}
void OctreeSceneNode::roll(const Radian & angle, TransformSpace relativeTo)
{
rotate(Vector3::UNIT_Z,angle,relativeTo);
}
void OctreeSceneNode::pitch(const Radian & angle, TransformSpace relativeTo)
{
rotate(Vector3::UNIT_X,angle,relativeTo);
}
void OctreeSceneNode::yaw(const Radian & angle, TransformSpace relativeTo)
{
rotate(Vector3::UNIT_Y,angle,relativeTo);
}
void OctreeSceneNode::rotate(const Vector3 & axis, const Radian & angle, TransformSpace relativeTo)
{
Quaternion q;
q.FromAngleAxis(angle,axis);
rotate(q, relativeTo);
}
void OctreeSceneNode::rotate(const Quaternion & q, TransformSpace relativeTo)
{
switch (relativeTo)
{
case TS_PARENT:
//Rotations are normally relative to local axes,trnaform up
mOrientation = q * mOrientation;
break;
case TS_WORLD:
mOrientation = mOrientation * _getDerivedOrientation() * q * _getDerivedOrientation();
break;
case TS_LOCAL:
mOrientation = mOrientation * q;
break;
}
//Normalize quaternion to avoid drift
mOrientation.normalise();
needUpdate();
}
Matrix3 OctreeSceneNode::getLocalAxes() const
{
Vector3 axisX = Vector3::UNIT_X;
Vector3 axisY = Vector3::UNIT_Y;
Vector3 axisZ = Vector3::UNIT_Z;
axisX = mOrientation * axisX;
axisY = mOrientation * axisY;
axisZ = mOrientation * axisZ;
return Matrix3(axisX.x,axisY.x,axisZ.x,
axisX.y,axisY.y,axisZ.y,
axisX.z,axisY.z,axisZ.z);
}
/*
OctreeSceneNode * OctreeSceneNode::createChild(const Vector3 & translate, const Quaternion & rotate)
{
OctreeSceneNode* newNode = createChildImpl();
newNode->setPosition(translate);
newNode->setOrientation(rotate);
this->addChild(newNode);
return newNode;
}*/
OctreeSceneNode * OctreeSceneNode::createChild(const String & name, const Vector3 & translate, const Quaternion & rotate)
{
OctreeSceneNode* newNode = createChildImpl(name);
newNode->setPosition(translate);
newNode->setOrientation(rotate);
this->addChild(newNode);
return newNode;
}
void OctreeSceneNode::addChild(OctreeSceneNode * child)
{
if (child->mParent)
{
String tip;
tip.append("Node " + child->getName() + "already was a child of" + child->mParent->getName() + "OctreeSceneNode::addChild");
MessageBoxA(NULL, tip.c_str(), "ERROR", MB_ICONERROR);
return;
}
typedef std::pair<String, OctreeSceneNode*> nodepair;
mChildren.insert(nodepair(child->getName(),child));
child->setParent(this);
}
OctreeSceneNode * OctreeSceneNode::getChild(unsigned short index) const
{
if (index < mChildren.size())
{
ChildNodeMap::const_iterator i = mChildren.begin();
while (index--) ++i;
return i->second;
}
else
return nullptr;
}
OctreeSceneNode * OctreeSceneNode::getChild(const String & name) const
{
ChildNodeMap::const_iterator i = mChildren.find(name);
if (i == mChildren.end())
{
String tip;
tip.append("Child node " + name + "does not exist.OctreeSceneNode::getChild");
MessageBoxA(NULL, tip.c_str(), "ERROR", MB_ICONERROR);
return NULL;
}
return i->second;
}
OctreeSceneNode * OctreeSceneNode::removeChild(unsigned short index)
{
if (index < mChildren.size())
{
ChildNodeMap::iterator i = mChildren.begin();
while (index--) ++i;
OctreeSceneNode* ret = i->second;
//cancel any pending update
cancelUpdate(ret);
mChildren.erase(i);
ret->setParent(NULL);
return ret;
}
else
{
String tip;
tip.append("Child index out of bounds OctreeSceneNode::getChild");
MessageBoxA(NULL, tip.c_str(), "ERROR", MB_ICONERROR);
}
return nullptr;
}
OctreeSceneNode * OctreeSceneNode::removeChild(OctreeSceneNode * child)
{
if (child)
{
ChildNodeMap::iterator i = mChildren.find(child->getName());
//ensure it's our child
if (i != mChildren.end() && i->second == child)
{
//cancel any pending update
cancelUpdate(child);
mChildren.erase(i);
child->setParent(NULL);
}
}
return child;
}
OctreeSceneNode * OctreeSceneNode::removeChild(const String & name)
{
ChildNodeMap::iterator i = mChildren.find(name);
if (i == mChildren.end())
{
String tip;
tip.append("Child node named" + name + "does not exist.OctreeSceneNode::removeChild");
MessageBoxA(NULL, tip.c_str(), "ERROR", MB_ICONERROR);
return NULL;
}
OctreeSceneNode* ret = i->second;
//Cancel any pending update
cancelUpdate(ret);
mChildren.erase(i);
ret->setParent(NULL);
return ret;
}
void OctreeSceneNode::removeAllChildren()
{
ChildNodeMap::iterator i, iend;
iend = mChildren.end();
for (i = mChildren.begin(); i != iend; ++i)
{
i->second->setParent(0);
}
mChildren.clear();
mChildrenToUpdate.clear();
}
void OctreeSceneNode::_setDerivedPosition(const Vector3 & pos)
{
//find where the node would end up in parent's local space
if (mParent)
setPosition(mParent->convertWorldToLocalPosition(pos));
}
void OctreeSceneNode::_setDerivedOrientation(const Quaternion & q)
{
//find where the node would end up in parent's local space
if (mParent)
setOrientation(mParent->convertWorldToLocalOrientation(q));
}
const Quaternion & OctreeSceneNode::_getDerivedOrientation() const
{
if (mNeedParentUpdate)
{
_updateFromParent();
}
return mDerivedOrientation;
}
const Vector3 & OctreeSceneNode::_getDerivedPosition() const
{
if (mNeedParentUpdate)
{
_updateFromParent();
}
return mDerivedPosition;
}