Nameless Engine
Loading...
Searching...
No Matches
ne::SpatialNode Class Reference

#include <SpatialNode.h>

Inheritance diagram for ne::SpatialNode:
ne::Node ne::Serializable ne::CameraNode ne::DirectionalLightNode ne::MeshNode ne::PointLightNode ne::SpotlightNode ne::EditorCameraNode

Classes

struct  LocalSpaceInformation
 
struct  WorldMatrixInformation
 

Public Member Functions

 SpatialNode (const std::string &sNodeName)
 
void setRelativeLocation (const glm::vec3 &location)
 
void setRelativeRotation (const glm::vec3 &rotation)
 
void setRelativeScale (const glm::vec3 &scale)
 
void setWorldLocation (const glm::vec3 &location)
 
void setWorldRotation (const glm::vec3 &rotation)
 
void setWorldScale (const glm::vec3 &scale)
 
glm::vec3 getRelativeLocation () const
 
glm::vec3 getRelativeRotation () const
 
glm::vec3 getRelativeScale () const
 
glm::mat4x4 getRelativeRotationMatrix ()
 
glm::vec3 getWorldLocation ()
 
glm::vec3 getWorldRotation ()
 
glm::quat getWorldRotationQuaternion ()
 
glm::vec3 getWorldScale ()
 
glm::vec3 getWorldForwardDirection ()
 
glm::vec3 getWorldRightDirection ()
 
glm::vec3 getWorldUpDirection ()
 
glm::mat4x4 getWorldMatrix ()
 
std::pair< std::recursive_mutex, sgc::GcPtr< SpatialNode > > * getClosestSpatialParent ()
 
- Public Member Functions inherited from ne::Node
 Node ()
 
 Node (const std::string &sName)
 
 Node (const Node &)=delete
 
Nodeoperator= (const Node &)=delete
 
 Node (Node &&)=delete
 
Nodeoperator= (Node &&)=delete
 
virtual ~Node () override
 
void setNodeName (const std::string &sName)
 
void detachFromParentAndDespawn ()
 
void addChildNode (const sgc::GcPtr< Node > &pNode, AttachmentRule locationRule=AttachmentRule::KEEP_WORLD, AttachmentRule rotationRule=AttachmentRule::KEEP_WORLD, AttachmentRule scaleRule=AttachmentRule::KEEP_WORLD)
 
void setSerialize (bool bSerialize)
 
std::optional< ErrorserializeNodeTree (const std::filesystem::path &pathToFile, bool bEnableBackup)
 
std::string getNodeName () const
 
sgc::GcPtr< NodegetWorldRootNode ()
 
std::pair< std::recursive_mutex, sgc::GcPtr< Node > > * getParentNode ()
 
std::pair< std::recursive_mutex, sgc::GcVector< sgc::GcPtr< Node > > > * getChildNodes ()
 
template<typename NodeType >
requires std::derived_from<NodeType, Node>
sgc::GcPtr< NodeType > getParentNodeOfType (const std::string &sParentNodeName="")
 
template<typename NodeType >
requires std::derived_from<NodeType, Node>
sgc::GcPtr< NodeType > getChildNodeOfType (const std::string &sChildNodeName="")
 
TickGroup getTickGroup () const
 
std::optional< size_t > getNodeId () const
 
bool isCalledEveryFrame ()
 
bool isReceivingInput ()
 
bool isSpawned ()
 
bool isParentOf (Node *pNode)
 
bool isChildOf (Node *pNode)
 
bool isSerialized () const
 
- Public Member Functions inherited from ne::Serializable
std::optional< Errorserialize (std::filesystem::path pathToFile, bool bEnableBackup, const std::unordered_map< std::string, std::string > &customAttributes={})
 
std::variant< std::string, Errorserialize (toml::value &tomlData, const std::string &sEntityId="", const std::unordered_map< std::string, std::string > &customAttributes={}, const std::optional< std::filesystem::path > &optionalPathToFile={}, bool bEnableBackup=false)
 
std::variant< std::string, Errorserialize (toml::value &tomlData, Serializable *pOriginalObject, std::string sEntityId="", const std::unordered_map< std::string, std::string > &customAttributes={}, const std::optional< std::filesystem::path > &optionalPathToFile={}, bool bEnableBackup=false)
 
std::optional< std::pair< std::string, std::string > > getPathDeserializedFromRelativeToRes () const
 

Protected Member Functions

virtual void onAfterDeserialized () override
 
virtual void onSpawning () override
 
virtual void onAfterAttachedToNewParent (bool bThisNodeBeingAttached) override
 
virtual void onWorldLocationRotationScaleChanged ()
 
- Protected Member Functions inherited from ne::Node
void setIsCalledEveryFrame (bool bEnable)
 
void setTickGroup (TickGroup tickGroup)
 
void setIsReceivingInput (bool bEnable)
 
TimercreateTimer (const std::string &sTimerName)
 
template<typename FunctionType >
NodeNotificationBroadcaster< FunctionType > * createNotificationBroadcaster ()
 
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, bool)> > > * getActionEventBindings ()
 
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, float)> > > * getAxisEventBindings ()
 
std::recursive_mutex * getSpawnDespawnMutex ()
 
virtual void onMouseMove (double xOffset, double yOffset)
 
virtual void onMouseScrollMove (int iOffset)
 
virtual void onBeforeNewFrame (float timeSincePrevFrameInSec)
 
virtual void onSpawning ()
 
virtual void onChildNodesSpawned ()
 
virtual void onDespawning ()
 
virtual void onBeforeDetachedFromParent (bool bThisNodeBeingDetached)
 
virtual void onAfterAttachedToNewParent (bool bThisNodeBeingAttached)
 
virtual void onAfterDeserialized ()
 

Private Member Functions

void applyAttachmentRule (Node::AttachmentRule locationRule, const glm::vec3 &worldLocationBeforeAttachment, Node::AttachmentRule rotationRule, const glm::vec3 &worldRotationBeforeAttachment, Node::AttachmentRule scaleRule, const glm::vec3 &worldScaleBeforeAttachment)
 
void recalculateWorldMatrix (bool bNotifyChildren=true)
 
void recalculateLocalMatrix ()
 
void recalculateWorldMatrixForNodeAndNotifyChildren (Node *pNode)
 

Private Attributes

glm::vec3 relativeLocation = glm::vec3(0.0F, 0.0F, 0.0F)
 
glm::vec3 relativeRotation = glm::vec3(0.0F, 0.0F, 0.0F)
 
glm::vec3 relativeScale = glm::vec3(1.0F, 1.0F, 1.0F)
 
std::pair< std::recursive_mutex, sgc::GcPtr< SpatialNode > > mtxSpatialParent {}
 
std::pair< std::recursive_mutex, LocalSpaceInformationmtxLocalSpace {}
 
std::pair< std::recursive_mutex, WorldMatrixInformationmtxWorldMatrix {}
 

Friends

class Node
 

Additional Inherited Members

- Public Types inherited from ne::Node
enum class  AttachmentRule { RESET_RELATIVE , KEEP_RELATIVE , KEEP_WORLD }
 
- Static Public Member Functions inherited from ne::Node
static size_t getAliveNodeCount ()
 
static std::variant< sgc::GcPtr< Node >, ErrordeserializeNodeTree (const std::filesystem::path &pathToFile)
 
static GameInstancegetGameInstance ()
 
- Static Public Member Functions inherited from ne::Serializable
static std::variant< std::pair< std::set< std::string >, toml::value >, ErrorgetIdsFromFile (std::filesystem::path pathToFile)
 
static std::optional< ErrorserializeMultiple (std::filesystem::path pathToFile, std::vector< SerializableObjectInformation > vObjects, bool bEnableBackup)
 
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type>
requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>)
static std::variant< SmartPointer, Errordeserialize (const std::filesystem::path &pathToFile)
 
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type>
requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>)
static std::variant< SmartPointer, Errordeserialize (const std::filesystem::path &pathToFile, std::unordered_map< std::string, std::string > &customAttributes)
 
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type>
requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>)
static std::variant< SmartPointer, Errordeserialize (std::filesystem::path pathToFile, std::unordered_map< std::string, std::string > &customAttributes, const std::string &sEntityId)
 
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type>
requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>)
static std::variant< SmartPointer, Errordeserialize (const std::filesystem::path &pathToFile, const std::string &sEntityId)
 
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type>
requires std::same_as<SmartPointer, sgc::GcPtr<Serializable>> || std::same_as<SmartPointer, std::unique_ptr<Serializable>>
static std::variant< std::vector< DeserializedObjectInformation< SmartPointer > >, ErrordeserializeMultiple (std::filesystem::path pathToFile)
 
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type>
requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>)
static std::variant< SmartPointer, Errordeserialize (const toml::value &tomlData, std::unordered_map< std::string, std::string > &customAttributes, std::string sEntityId="", const std::optional< std::filesystem::path > &optionalPathToFile={})
 

Detailed Description

Represents a node that can have a location, rotation and a scale in a 3D space.

Constructor & Destructor Documentation

◆ SpatialNode()

ne::SpatialNode::SpatialNode ( const std::string &  sNodeName)

Creates a new node with the specified name.

Parameters
sNodeNameName of this node.

Member Function Documentation

◆ applyAttachmentRule()

void ne::SpatialNode::applyAttachmentRule ( Node::AttachmentRule  locationRule,
const glm::vec3 &  worldLocationBeforeAttachment,
Node::AttachmentRule  rotationRule,
const glm::vec3 &  worldRotationBeforeAttachment,
Node::AttachmentRule  scaleRule,
const glm::vec3 &  worldScaleBeforeAttachment 
)
private

Called by Node class after we have attached to a new parent node and now need to apply attachment rules based on this new parent node.

Parameters
locationRuleDefines how location should change.
worldLocationBeforeAttachmentWorld location of this node before being attached.
rotationRuleDefines how rotation should change.
worldRotationBeforeAttachmentWorld rotation of this node before being attached.
scaleRuleDefines how scale should change.
worldScaleBeforeAttachmentWorld scale of this node before being attached.

◆ getClosestSpatialParent()

std::pair< std::recursive_mutex, sgc::GcPtr< SpatialNode > > * ne::SpatialNode::getClosestSpatialParent ( )

Returns the first (most closer to this node) spatial node in the parent node chain (i.e. cached result of getParentNodeOfType<SpatialNode> that can be used without any search operations).

Warning
Avoid saving returned raw pointer as it points to the node's field and does not guarantee that the node will always live while you hold this pointer. Returning raw pointer in order to avoid creating GC pointers (if you for example only want to check closes parent node), but you can always save GC pointer if you need.
Returns
nullptr as a gc pointer (second value in the pair) if there is no SpatialNode in the parent node chain, otherwise closest SpatialNode in the parent node chain.

◆ getRelativeLocation()

glm::vec3 ne::SpatialNode::getRelativeLocation ( ) const
inline

Returns node's relative location (see setRelativeLocation).

Returns
Relative location.

◆ getRelativeRotation()

glm::vec3 ne::SpatialNode::getRelativeRotation ( ) const
inline

Returns node's relative rotation in degrees (see setRelativeRotation). Also see getRelativeRotationMatrix.

Returns
Relative rotation.

◆ getRelativeRotationMatrix()

glm::mat4x4 ne::SpatialNode::getRelativeRotationMatrix ( )

Returns a rotation matrix that applies node's relative rotation.

Returns
Rotation matrix.

◆ getRelativeScale()

glm::vec3 ne::SpatialNode::getRelativeScale ( ) const
inline

Returns node's relative scale (see setRelativeScale).

Returns
Relative scale.

◆ getWorldForwardDirection()

glm::vec3 ne::SpatialNode::getWorldForwardDirection ( )

Returns node's forward direction in world space.

Returns
Unit vector that points in the node's world forward direction.

◆ getWorldLocation()

glm::vec3 ne::SpatialNode::getWorldLocation ( )

Returns node's world location (see setWorldLocation).

Remarks
If the node is not spawned and has no parent, returns getRelativeLocation. If the node is not spawned but has a parent, returns its location in the hierarchy.
Returns
Location of the node in the world.

◆ getWorldMatrix()

glm::mat4x4 ne::SpatialNode::getWorldMatrix ( )

Returns node's world matrix (matrix that transforms node's data (for example vertices) to world space).

Returns
World matrix.

◆ getWorldRightDirection()

glm::vec3 ne::SpatialNode::getWorldRightDirection ( )

Returns node's right direction in world space.

Returns
Unit vector that points in the node's right direction.

◆ getWorldRotation()

glm::vec3 ne::SpatialNode::getWorldRotation ( )

Returns node's world rotation in degrees (see setWorldRotation). Also see getWorldRotationQuaternion.

Remarks
If the node is not spawned and has no parent, returns getRelativeRotation. If the node is not spawned but has a parent, returns its rotation in the hierarchy.
Returns
Rotation of the node in the world.

◆ getWorldRotationQuaternion()

glm::quat ne::SpatialNode::getWorldRotationQuaternion ( )

Returns node's world rotation in the quaternion form (see getWorldRotation).

Returns
Rotation of the node in the world.

◆ getWorldScale()

glm::vec3 ne::SpatialNode::getWorldScale ( )

Returns node's world scale (see setWorldScale).

Remarks
If the node is not spawned and has no parent, returns getRelativeScale. If the node is not spawned but has a parent, returns its scale in the hierarchy.
Returns
Scale of the node in the world.

◆ getWorldUpDirection()

glm::vec3 ne::SpatialNode::getWorldUpDirection ( )

Returns node's up direction in world space.

Returns
Unit vector that points in the node's up direction.

◆ onAfterAttachedToNewParent()

void ne::SpatialNode::onAfterAttachedToNewParent ( bool  bThisNodeBeingAttached)
overrideprotectedvirtual

Called after this node or one of the node's parents (in the parent hierarchy) was attached to a new parent node.

Warning
If overriding you must call the parent's version of this function first (before executing your login) to execute parent's logic.
Remarks
This function will also be called on all child nodes after this function is finished.
Parameters
bThisNodeBeingAttachedtrue if this node was attached to a parent, false if some node in the parent hierarchy was attached to a parent.

Reimplemented from ne::Node.

Reimplemented in ne::EditorCameraNode.

◆ onAfterDeserialized()

void ne::SpatialNode::onAfterDeserialized ( )
overrideprotectedvirtual

Called after the object was successfully deserialized. Used to execute post-deserialization logic.

Warning
If overriding you must call the parent's version of this function first (before executing your login) to execute parent's logic.

Reimplemented from ne::Serializable.

Reimplemented in ne::DirectionalLightNode, ne::PointLightNode, ne::SpotlightNode, and ne::MeshNode.

◆ onSpawning()

void ne::SpatialNode::onSpawning ( )
overrideprotectedvirtual

Called when this node was not spawned previously and it was either attached to a parent node that is spawned or set as world's root node to execute custom spawn logic.

Remarks
This node will be marked as spawned before this function is called.
This function is called before any of the child nodes are spawned.
Warning
If overriding you must call the parent's version of this function first (before executing your login) to execute parent's logic.

Reimplemented from ne::Node.

Reimplemented in ne::DirectionalLightNode, ne::PointLightNode, ne::SpotlightNode, and ne::MeshNode.

◆ onWorldLocationRotationScaleChanged()

virtual void ne::SpatialNode::onWorldLocationRotationScaleChanged ( )
inlineprotectedvirtual

Called after node's world location/rotation/scale was changed.

Warning
If overriding you must call the parent's version of this function first (before executing your login) to execute parent's logic.
Remarks
If you change location/rotation/scale inside of this function, this function will not be called again (no recursion will occur).

Reimplemented in ne::CameraNode, ne::DirectionalLightNode, ne::PointLightNode, ne::SpotlightNode, and ne::MeshNode.

◆ recalculateLocalMatrix()

void ne::SpatialNode::recalculateLocalMatrix ( )
private

Recalculates node's local matrix based on local location/rotation/scale.

◆ recalculateWorldMatrix()

void ne::SpatialNode::recalculateWorldMatrix ( bool  bNotifyChildren = true)
private

Recalculates node's world matrix based on the parent world matrix (can be identity if there's node parent) and optionally notifies spatial child nodes.

Warning
Expects mtxLocalSpace to be up to date (see recalculateLocalMatrix).
Parameters
bNotifyChildrenWhether to notify spatial child nodes so that could recalculate their world matrix or not.

◆ recalculateWorldMatrixForNodeAndNotifyChildren()

void ne::SpatialNode::recalculateWorldMatrixForNodeAndNotifyChildren ( Node pNode)
private

Checks if the specified node is a SpatialNode and calls recalculateWorldMatrix, otherwise calls this function on all child nodes.

Parameters
pNodeNode to recalculate world matrix for (if SpatialNode).

◆ setRelativeLocation()

void ne::SpatialNode::setRelativeLocation ( const glm::vec3 &  location)

Sets node's relative location, if there is another SpatialNode in the parent chain then this location is relative to the first SpatialNode in the parent chain, otherwise if there is no SpatialNode in the parent chain, this location is relative to the world.

Parameters
locationRelative location.

◆ setRelativeRotation()

void ne::SpatialNode::setRelativeRotation ( const glm::vec3 &  rotation)

Sets node's relative rotation (roll, pitch, yaw in degrees), if there is another SpatialNode in the parent chain then this rotation is relative to the first SpatialNode in the parent chain, otherwise if there is no SpatialNode in the parent chain, this rotation is relative to the world.

Parameters
rotationRelative rotation.

◆ setRelativeScale()

void ne::SpatialNode::setRelativeScale ( const glm::vec3 &  scale)

Sets node's relative scale, if there is another SpatialNode in the parent chain then this scale is relative to the first SpatialNode in the parent chain, otherwise if there is no SpatialNode in the parent chain, this scale is relative to the world.

Parameters
scaleRelative scale.

◆ setWorldLocation()

void ne::SpatialNode::setWorldLocation ( const glm::vec3 &  location)

Sets relative location in the way that the resulting node's location in the world would match the specified location.

Remarks
If the node is not spawned just sets node's relative location.
Parameters
locationLocation that the node should take in the world.

◆ setWorldRotation()

void ne::SpatialNode::setWorldRotation ( const glm::vec3 &  rotation)

Sets relative rotation (roll, pitch, yaw in degrees) in the way that the resulting node's rotation in the world would match the specified rotation.

Remarks
If the node is not spawned just sets node's relative rotation.
Parameters
rotationRotation that the node should take in the world.

◆ setWorldScale()

void ne::SpatialNode::setWorldScale ( const glm::vec3 &  scale)

Sets relative scale in the way that the resulting node's scale in the world would match the specified scale.

Remarks
If the node is not spawned just sets node's relative scale.
Parameters
scaleScale that the node should take in the world.

Member Data Documentation

◆ mtxLocalSpace

std::pair<std::recursive_mutex, LocalSpaceInformation> ne::SpatialNode::mtxLocalSpace {}
private

Matrix that describes basis vectors that define node's local space.

◆ mtxSpatialParent

std::pair<std::recursive_mutex, sgc::GcPtr<SpatialNode> > ne::SpatialNode::mtxSpatialParent {}
private

First (most closer to this node) spatial node in the parent chain.

◆ mtxWorldMatrix

std::pair<std::recursive_mutex, WorldMatrixInformation> ne::SpatialNode::mtxWorldMatrix {}
private

World related information, must be used with mutex.

◆ relativeLocation

glm::vec3 ne::SpatialNode::relativeLocation = glm::vec3(0.0F, 0.0F, 0.0F)
private

Node's location, if there is another SpatialNode in the parent chain then this location is relative to the first SpatialNode in the parent chain, otherwise if there is no SpatialNode in the parent chain, relative to the world.

◆ relativeRotation

glm::vec3 ne::SpatialNode::relativeRotation = glm::vec3(0.0F, 0.0F, 0.0F)
private

Node's rotation in degrees, if there is another SpatialNode in the parent chain then this rotation is relative to the first SpatialNode in the parent chain, otherwise if there is no SpatialNode in the parent chain, relative to the world.

◆ relativeScale

glm::vec3 ne::SpatialNode::relativeScale = glm::vec3(1.0F, 1.0F, 1.0F)
private

Node's scale, if there is another SpatialNode in the parent chain then this scale is relative to the first SpatialNode in the parent chain, otherwise if there is no SpatialNode in the parent chain, relative to the world.


The documentation for this class was generated from the following files: