Skip to content

Commit a8dd156

Browse files
committed
Optimize: Avoid unnecessary quaternion-euler conversions
1 parent c193bd3 commit a8dd156

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

irr/include/BoneSceneNode.h

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ namespace scene
1818
/** Used with SkinnedMesh and AnimatedMeshSceneNode. */
1919
class BoneSceneNode : public ISceneNode
2020
{
21+
private:
22+
23+
//! For performance reasons, we prefer to store the rotation as quaternion
24+
//! to avoid expensive conversions back and forth between Euler angles and quaternions.
25+
core::quaternion Rotation;
26+
27+
const u32 BoneIndex;
28+
2129
public:
30+
31+
//! Some file formats alternatively let bones specify a transformation matrix.
32+
//! If this is set, it overrides the TRS properties.
33+
std::optional<core::matrix4> Matrix;
34+
2235
BoneSceneNode(ISceneNode *parent,
2336
ISceneManager *mgr,
2437
s32 id = -1,
@@ -27,8 +40,8 @@ class BoneSceneNode : public ISceneNode
2740
const core::Transform &transform = {},
2841
const std::optional<core::matrix4> &matrix = std::nullopt) :
2942
ISceneNode(parent, mgr, id),
30-
Matrix(matrix),
31-
BoneIndex(boneIndex)
43+
BoneIndex(boneIndex),
44+
Matrix(matrix)
3245
{
3346
setName(boneName);
3447
setTransform(transform);
@@ -52,25 +65,32 @@ class BoneSceneNode : public ISceneNode
5265
/** Does nothing as bones are not visible. */
5366
void render() override {}
5467

68+
void setRotation(const core::vector3df &rotation) override
69+
{
70+
Rotation = core::quaternion(rotation * core::DEGTORAD).makeInverse();
71+
}
72+
73+
core::vector3df getRotation() const override
74+
{
75+
auto rot = Rotation;
76+
rot.makeInverse();
77+
core::vector3df euler;
78+
rot.toEuler(euler);
79+
return euler * core::RADTODEG;
80+
}
81+
5582
void setTransform(const core::Transform &transform)
5683
{
5784
setPosition(transform.translation);
58-
{
59-
core::vector3df euler;
60-
auto rot = transform.rotation;
61-
// Invert to be consistent with setRotationDegrees
62-
rot.makeInverse();
63-
rot.toEuler(euler);
64-
setRotation(euler * core::RADTODEG);
65-
}
85+
Rotation = transform.rotation;
6686
setScale(transform.scale);
6787
}
6888

6989
core::Transform getTransform() const
7090
{
7191
return {
7292
getPosition(),
73-
core::quaternion(getRotation() * core::DEGTORAD).makeInverse(),
93+
Rotation,
7494
getScale()
7595
};
7696
}
@@ -79,16 +99,8 @@ class BoneSceneNode : public ISceneNode
7999
{
80100
if (Matrix)
81101
return *Matrix;
82-
return ISceneNode::getRelativeTransformation();
102+
return getTransform().buildMatrix();
83103
}
84-
85-
//! Some file formats alternatively let bones specify a transformation matrix.
86-
//! If this is set, it overrides the TRS properties.
87-
std::optional<core::matrix4> Matrix;
88-
89-
private:
90-
91-
const u32 BoneIndex;
92104
};
93105

94106
} // end namespace scene

irr/include/ISceneNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ class ISceneNode : virtual public IReferenceCounted
352352
If you want the absolute rotation, use
353353
getAbsoluteTransformation().getRotation()
354354
\return Current relative rotation of the scene node. */
355-
virtual const core::vector3df &getRotation() const
355+
virtual core::vector3df getRotation() const
356356
{
357357
return RelativeRotation;
358358
}

irr/src/CDummyTransformationSceneNode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void CDummyTransformationSceneNode::setScale(const core::vector3df &scale)
6868
RelativeScale = scale;
6969
}
7070

71-
const core::vector3df &CDummyTransformationSceneNode::getRotation() const
71+
core::vector3df CDummyTransformationSceneNode::getRotation() const
7272
{
7373
os::Printer::log("CDummyTransformationSceneNode::getRotation() does not contain the relative transformation.", ELL_DEBUG);
7474
return RelativeRotation;

irr/src/CDummyTransformationSceneNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class CDummyTransformationSceneNode : public IDummyTransformationSceneNode
4040
// fixed bug id 2318691.
4141
const core::vector3df &getScale() const override;
4242
void setScale(const core::vector3df &scale) override;
43-
const core::vector3df &getRotation() const override;
43+
core::vector3df getRotation() const override;
4444
void setRotation(const core::vector3df &rotation) override;
4545
const core::vector3df &getPosition() const override;
4646
void setPosition(const core::vector3df &newpos) override;

0 commit comments

Comments
 (0)