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

#include <MeshNode.h>

Inheritance diagram for ne::MeshNode:
ne::SpatialNode ne::Node ne::Serializable

Classes

struct  GpuResources
 
struct  MeshShaderConstants
 

Public Member Functions

 MeshNode (const std::string &sNodeName)
 
void setMaterial (std::unique_ptr< Material > pMaterial, size_t iMaterialSlot=0)
 
void setMeshData (const MeshData &meshData)
 
void setMeshData (MeshData &&meshData)
 
void onMeshDataChanged ()
 
void setIsVisible (bool bVisible)
 
MaterialgetMaterial (size_t iMaterialSlot=0)
 
size_t getAvailableMaterialSlotCount ()
 
std::pair< std::recursive_mutex *, MeshData * > getMeshData ()
 
std::pair< std::recursive_mutex, GpuResources > * getMeshGpuResources ()
 
std::pair< std::recursive_mutex, MeshShaderConstants > * getMeshShaderConstants ()
 
AABBgetAABB ()
 
bool isVisible () const
 
- Public Member Functions inherited from ne::SpatialNode
 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
 

Static Public Member Functions

static const char * getMeshShaderConstantBufferName ()
 
- 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::optional< ErrorserializeMultiple (std::filesystem::path pathToFile, std::vector< SerializableObjectInformation > vObjects, bool bEnableBackup)
 
static std::variant< std::set< std::string >, ErrorgetIdsFromFile (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)
 
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, const std::set< std::string > &ids)
 
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="", std::optional< std::filesystem::path > optionalPathToFile={})
 

Protected Member Functions

virtual void onAfterDeserialized () override
 
virtual void onSpawning () override
 
virtual void onDespawning () override
 
virtual void onWorldLocationRotationScaleChanged () override
 
void setShaderCpuWriteResourceBindingData (const std::string &sShaderResourceName, size_t iResourceSizeInBytes, const std::function< void *()> &onStartedUpdatingResource, const std::function< void()> &onFinishedUpdatingResource)
 
void setShaderTextureResourceBindingData (const std::string &sShaderResourceName, const std::string &sPathToTextureResourceRelativeRes)
 
void markShaderCpuWriteResourceToBeCopiedToGpu (const std::string &sShaderResourceName)
 
- Protected Member Functions inherited from ne::SpatialNode
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 allocateShaderResources ()
 
void allocateGeometryBuffers ()
 
void deallocateShaderResources ()
 
void deallocateGeometryBuffers ()
 
void * onStartedUpdatingShaderMeshConstants ()
 
void onFinishedUpdatingShaderMeshConstants ()
 
void updateShaderResourcesToUseChangedMaterialPipelines ()
 
std::pair< GpuResource *, unsigned int > getIndexBufferInfoForMaterialSlot (size_t iMaterialSlot)
 

Static Private Member Functions

static std::unique_ptr< MaterialgetDefaultMaterial ()
 

Private Attributes

std::vector< std::unique_ptr< Material > > vMaterials
 
MeshData meshData
 
AABB aabb
 
std::recursive_mutex mtxMeshData
 
std::pair< std::recursive_mutex, GpuResourcesmtxGpuResources
 
std::pair< std::recursive_mutex, MeshShaderConstantsmtxShaderMeshDataConstants
 
bool bIsVisible = true
 

Static Private Attributes

static const auto sMeshShaderConstantBufferName = "meshData"
 

Friends

class Material
 

Additional Inherited Members

- Public Types inherited from ne::Node
enum class  AttachmentRule { RESET_RELATIVE , KEEP_RELATIVE , KEEP_WORLD }
 

Detailed Description

Represents a node that can have 3D geometry to display (mesh).

Remarks
Used for GPU optimized geometry - geometry that rarely (if ever) changes from the CPU side.

Constructor & Destructor Documentation

◆ MeshNode()

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

Creates a new node with the specified name.

Parameters
sNodeNameName of this node.

Member Function Documentation

◆ allocateGeometryBuffers()

void ne::MeshNode::allocateGeometryBuffers ( )
private

Allocates geometry resources (see mtxGpuResources).

◆ allocateShaderResources()

void ne::MeshNode::allocateShaderResources ( )
private

Allocates shader resources (see mtxGpuResources).

Warning
Expects that vMaterials to have initialized PSOs.

◆ deallocateGeometryBuffers()

void ne::MeshNode::deallocateGeometryBuffers ( )
private

Deallocates geometry resources (see mtxGpuResources).

◆ deallocateShaderResources()

void ne::MeshNode::deallocateShaderResources ( )
private

Deallocates shader resources (see mtxGpuResources).

◆ getAABB()

AABB * ne::MeshNode::getAABB ( )
inline

Returns pointer to axis-aligned bounding box of this mesh.

Returns
Axis-aligned bounding box.

◆ getAvailableMaterialSlotCount()

size_t ne::MeshNode::getAvailableMaterialSlotCount ( )

Returns the total number of available material slots that the current mesh data (see setMeshData) provides.

Returns
The number of available material slots.

◆ getDefaultMaterial()

std::unique_ptr< Material > ne::MeshNode::getDefaultMaterial ( )
staticprivate

Returns default mesh node material.

Returns
Default material for mesh node.

◆ getIndexBufferInfoForMaterialSlot()

std::pair< GpuResource *, unsigned int > ne::MeshNode::getIndexBufferInfoForMaterialSlot ( size_t  iMaterialSlot)
private

Returns information about an index buffer for the specified material slot.

Warning
Shows an error and throws an exception if the index buffer at the specified index cannon be found.
Parameters
iMaterialSlotMaterial slot that corresponds with an index buffer to look for.
Returns
Information about an index buffer at the specified material slot.

◆ getMaterial()

Material * ne::MeshNode::getMaterial ( size_t  iMaterialSlot = 0)

Returns material used by this name.

Warning
Do not delete (free) returned pointer.
Parameters
iMaterialSlotSlot from which requested material is taken from.
Returns
nullptr if the specified slot does not exist (see getAvailableMaterialSlotCount), otherwise used material.

◆ getMeshData()

std::pair< std::recursive_mutex *, MeshData * > ne::MeshNode::getMeshData ( )
inline

Returns mesh geometry for read/write operations.

Warning
Must be used with mutex.
If you're changing mesh data you must call onMeshDataChanged after you finished modifying the mesh data to update internal CPU/GPU resources and see updated geometry on the screen, otherwise this might cause the object to be displayed incorrectly.
Remarks
Note that changing mesh data using onMeshDataChanged will have the same performance as if you used setMeshData because they both do the same work. The main reason why this getter exists is to avoid copying MeshData when somebody wants to read/modify existing mesh data.
Returns
Mesh data.

◆ getMeshGpuResources()

std::pair< std::recursive_mutex, GpuResources > * ne::MeshNode::getMeshGpuResources ( )
inline

Returns GPU resources that this mesh node uses.

Returns
GPU resources.

◆ getMeshShaderConstantBufferName()

static const char * ne::MeshNode::getMeshShaderConstantBufferName ( )
inlinestatic

Returns name of the constant buffer that stores mesh constants.

Returns
Name of shader resource.

◆ getMeshShaderConstants()

std::pair< std::recursive_mutex, MeshShaderConstants > * ne::MeshNode::getMeshShaderConstants ( )
inline

Returns shader constants (copied to GPU).

Remarks
Changes made this returned object will not be copied to the GPU, this getter only exists read access.
Returns
Mesh constants.

◆ isVisible()

bool ne::MeshNode::isVisible ( ) const

Tells whether this mesh is currently visible or not.

Returns
Whether the mesh is visible or not.

◆ markShaderCpuWriteResourceToBeCopiedToGpu()

void ne::MeshNode::markShaderCpuWriteResourceToBeCopiedToGpu ( const std::string &  sShaderResourceName)
protected

Looks for binding created using setShaderCpuWriteResourceBindingData and notifies the engine that there is new (updated) data for shader CPU write resource to copy to the GPU to be used by shaders.

Remarks
You don't need to check if the node is spawned or not before calling this function, if the binding does not exist or some other condition is not met this call will be silently ignored without any errors.
Note that the callbacks that you have specified in setShaderCpuWriteResourceBindingData will not be called inside of this function, moreover they are most likely to be called in the next frame(s) (most likely multiple times) when the engine is ready to copy the data to the GPU, so if the resource's data is used by multiple threads in your code, make sure to use mutex or other synchronization primitive in your callbacks.
Parameters
sShaderResourceNameName of the shader CPU write resource (should be exactly the same as the resource name written in the shader file we are referencing).

◆ onAfterDeserialized()

void ne::MeshNode::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::SpatialNode.

◆ onDespawning()

void ne::MeshNode::onDespawning ( )
overrideprotectedvirtual

Called before this node is despawned from the world to execute custom despawn logic.

Remarks
This node will be marked as despawned after this function is called.
This function is called after all child nodes were despawned.
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.

◆ onFinishedUpdatingShaderMeshConstants()

void ne::MeshNode::onFinishedUpdatingShaderMeshConstants ( )
private

Called after finished copying data from mtxShaderMeshDataConstants.

◆ onMeshDataChanged()

void ne::MeshNode::onMeshDataChanged ( )

Must be called after mesh data was modified to (re)create internal CPU/GPU resources.

◆ onSpawning()

void ne::MeshNode::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::SpatialNode.

◆ onStartedUpdatingShaderMeshConstants()

void * ne::MeshNode::onStartedUpdatingShaderMeshConstants ( )
private

Called to copy data from mtxShaderMeshDataConstants.

Returns
Pointer to data in mtxShaderMeshDataConstants.

◆ onWorldLocationRotationScaleChanged()

void ne::MeshNode::onWorldLocationRotationScaleChanged ( )
overrideprotectedvirtual

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.

Reimplemented from ne::SpatialNode.

◆ setIsVisible()

void ne::MeshNode::setIsVisible ( bool  bVisible)

Sets whether this mesh is visible or not.

Parameters
bVisibleWhether this mesh is visible or not.

◆ setMaterial()

void ne::MeshNode::setMaterial ( std::unique_ptr< Material pMaterial,
size_t  iMaterialSlot = 0 
)

Sets material to use.

Remarks
Replaces old (previously used) material.
Logs an error if the specified material slot does not exist (see getAvailableMaterialSlotCount).
Parameters
pMaterialMaterial to use.
iMaterialSlotIndex of the material slot to set this new material to. By default all meshes have 1 material at slot 0 and mesh's geometry uses only this slot.

◆ setMeshData() [1/2]

void ne::MeshNode::setMeshData ( const MeshData meshData)

Sets mesh data (geometry) to use.

Remarks
The number of available material slots will be changed according to the mesh data.
Parameters
meshDataMesh geometry.

◆ setMeshData() [2/2]

void ne::MeshNode::setMeshData ( MeshData &&  meshData)

Sets mesh data (geometry) to use.

Remarks
The number of available material slots will be changed according to the mesh data.
Parameters
meshDataMesh geometry.

◆ setShaderCpuWriteResourceBindingData()

void ne::MeshNode::setShaderCpuWriteResourceBindingData ( const std::string &  sShaderResourceName,
size_t  iResourceSizeInBytes,
const std::function< void *()> &  onStartedUpdatingResource,
const std::function< void()> &  onFinishedUpdatingResource 
)
protected

Setups callbacks for shader resource with CPU write access to copy data from CPU to the GPU to be used by shaders.

Remarks
Call this function in your onSpawning function to bind to shader resources, all bindings will be automatically removed in onDespawning. An error will be shown if this function is called when the node is not spawned.
When data of a resource that you registered was updated on the CPU side you need to call markShaderCpuWriteResourceToBeCopiedToGpu so that update callbacks will be called and updated data will be copied to the GPU to be used by shaders. Note that you don't need to call markShaderCpuWriteResourceToBeCopiedToGpu for resources you have not registered yourself. Also note that all registered resources are marked as "need update" by default so you don't have to call markShaderCpuWriteResourceToBeCopiedToGpu right after calling this function.
Parameters
sShaderResourceNameName of the resource we are referencing (should be exactly the same as the resource name written in the shader file we are referencing).
iResourceSizeInBytesSize of the data that this resource will contain. Note that the specified size will most likely be padded (changed) to be a multiple of 256 because of the hardware requirement for shader constant buffers.
onStartedUpdatingResourceFunction that will be called when the engine has started copying data to the GPU. Function returns pointer to new (updated) data of the specified resource that will be copied to the GPU.
onFinishedUpdatingResourceFunction that will be called when the engine has finished copying resource data to the GPU (usually used for unlocking mutexes).

◆ setShaderTextureResourceBindingData()

void ne::MeshNode::setShaderTextureResourceBindingData ( const std::string &  sShaderResourceName,
const std::string &  sPathToTextureResourceRelativeRes 
)
protected

Setups a shader resource that references a texture that will be used in shaders when this mesh is rendered.

Remarks
Call this function in allocateShaderResources to bind to shader resources, all bindings will be automatically removed in deallocateShaderResources.
Parameters
sShaderResourceNameName of the resource we are referencing (should be exactly the same as the resource name written in the shader file we are referencing).
sPathToTextureResourceRelativeResPath to the directory with texture resource to use.

◆ updateShaderResourcesToUseChangedMaterialPipelines()

void ne::MeshNode::updateShaderResourcesToUseChangedMaterialPipelines ( )
private

Called after vMaterials is changed (some slots are added/removed) to notify all shader resources.

Warning
Expects that the caller is using some mutex to protect this shader resource from being used in the draw function while this function is not finished (i.e. make sure the CPU will not queue a new frame while this function is not finished).
Expects that the node is spawned and all vMaterials have pipelines initialized.

Member Data Documentation

◆ aabb

AABB ne::MeshNode::aabb
private

Axis-aligned bounding box for meshData.

◆ bIsVisible

bool ne::MeshNode::bIsVisible = true
private

Whether mesh is visible or not.

◆ meshData

MeshData ne::MeshNode::meshData
private

Mesh geometry.

Warning
Use with mtxMeshData.

◆ mtxGpuResources

std::pair<std::recursive_mutex, GpuResources> ne::MeshNode::mtxGpuResources
private

Stores GPU resources used by this mesh node.

◆ mtxMeshData

std::recursive_mutex ne::MeshNode::mtxMeshData
private

Mutex for meshData.

◆ mtxShaderMeshDataConstants

std::pair<std::recursive_mutex, MeshShaderConstants> ne::MeshNode::mtxShaderMeshDataConstants
private

Stores data for constant buffer used by shaders.

◆ sMeshShaderConstantBufferName

const auto ne::MeshNode::sMeshShaderConstantBufferName = "meshData"
inlinestaticprivate

Name of the constant buffer used to store general mesh data in shaders.

◆ vMaterials

std::vector<std::unique_ptr<Material> > ne::MeshNode::vMaterials
private

Stores materials of the mesh. Material at index 0 used by index buffer 0, material at index 1 used by index buffer 1 and so on (meshData defines available slots).

Remarks
Always contains valid pointers.

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