Skip to content

Commit

Permalink
Move MatrixTransform up to IEntityNode interface
Browse files Browse the repository at this point in the history
Avoid a new dynamic_cast in EntityNode's attachment handling by migrating the
MatrixTransform interface up to IEntityNode, so that localToParent() is
accessible as soon as the IEntityNodePtr has been created by the entity module.
  • Loading branch information
Matthew Mott committed Feb 9, 2021
1 parent affe686 commit 42cb0d2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
17 changes: 12 additions & 5 deletions include/ientity.h
Expand Up @@ -6,6 +6,7 @@
#include "irender.h"
#include "inameobserver.h"
#include "iscenegraph.h"
#include "itransformnode.h"
#include <functional>

#include "string/predicate.h"
Expand Down Expand Up @@ -263,11 +264,17 @@ class Entity
virtual void forEachAttachment(AttachmentFunc func) const = 0;
};

/// Interface for a INode subclass that contains an Entity
class IEntityNode :
public IRenderEntity,
public virtual scene::INode,
public scene::Cloneable
/**
* \brief Interface for a node which represents an entity.
*
* As well as providing access to the entity data with getEntity(), every
* IEntityNode can clone itself and apply a transformation matrix to its
* children (which might be brushes, patches or other entities).
*/
class IEntityNode : public IRenderEntity,
public virtual scene::INode,
public scene::Cloneable,
public IMatrixTransform
{
public:
virtual ~IEntityNode() {}
Expand Down
13 changes: 11 additions & 2 deletions include/itransformnode.h
Expand Up @@ -4,18 +4,27 @@

class Matrix4;

/// \brief A transform node.
/// A node which can transform the coordinate space of its children
class ITransformNode
{
public:
virtual ~ITransformNode() {}

/// \brief Returns the transform which maps the node's local-space into the local-space of its parent node.
virtual const Matrix4& localToParent() const = 0;
virtual const Matrix4& localToParent() const = 0;
};
typedef std::shared_ptr<ITransformNode> ITransformNodePtr;

inline ITransformNodePtr Node_getTransformNode(const scene::INodePtr& node)
{
return std::dynamic_pointer_cast<ITransformNode>(node);
}

/// An ITransformNode which can provide non-const access to its transform matrix
class IMatrixTransform: public ITransformNode
{
public:

/// Return a modifiable reference to a contained transformation matrix
virtual Matrix4& localToParent() = 0;
};
22 changes: 0 additions & 22 deletions libs/transformlib.h
Expand Up @@ -17,28 +17,6 @@ class IdentityTransform :
}
};

/// \brief A transform node which stores a generic transformation matrix.
class MatrixTransform :
public ITransformNode
{
Matrix4 _localToParent;
public:
MatrixTransform() :
_localToParent(Matrix4::getIdentity())
{}

Matrix4& localToParent()
{
return _localToParent;
}

/// \brief Returns the stored local->parent transform.
const Matrix4& localToParent() const
{
return _localToParent;
}
};

namespace scene
{

Expand Down
5 changes: 2 additions & 3 deletions radiantcore/entity/EntityNode.cpp
Expand Up @@ -32,9 +32,9 @@ EntityNode::EntityNode(const EntityNode& other) :
Namespaced(other),
TargetableNode(_spawnArgs, *this),
Transformable(other),
MatrixTransform(other),
_eclass(other._eclass),
_spawnArgs(other._spawnArgs),
_localToParent(other._localToParent),
_namespaceManager(_spawnArgs),
_nameKey(_spawnArgs),
_renderableName(_nameKey),
Expand Down Expand Up @@ -143,8 +143,7 @@ void EntityNode::createAttachedEntities()

// Set the attached entity's transform matrix according to the
// required offset
MatrixTransform& mt = dynamic_cast<MatrixTransform&>(*attachedEnt);
mt.localToParent() = Matrix4::getTranslation(a.offset);
attachedEnt->localToParent() = Matrix4::getTranslation(a.offset);
}
);
}
Expand Down
10 changes: 8 additions & 2 deletions radiantcore/entity/EntityNode.h
Expand Up @@ -32,8 +32,7 @@ class EntityNode :
public SelectionTestable,
public Namespaced,
public TargetableNode,
public Transformable,
public MatrixTransform // influences local2world of child nodes
public Transformable
{
protected:
// The entity class
Expand All @@ -42,6 +41,9 @@ class EntityNode :
// The actual entity (which contains the key/value pairs)
SpawnArgs _spawnArgs;

// Transformation applied to this node and its children
Matrix4 _localToParent = Matrix4::getIdentity();

// The class taking care of all the namespace-relevant stuff
NamespaceManager _namespaceManager;

Expand Down Expand Up @@ -106,6 +108,10 @@ class EntityNode :
virtual float getShaderParm(int parmNum) const override;
virtual const Vector3& getDirection() const override;

// IMatrixTransform implementation
const Matrix4& localToParent() const override { return _localToParent; }
Matrix4& localToParent() override { return _localToParent; }

// SelectionTestable implementation
virtual void testSelect(Selector& selector, SelectionTest& test) override;

Expand Down

0 comments on commit 42cb0d2

Please sign in to comment.