NAP
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
RenderableMeshComponentInstance Class Reference

#include <renderablemeshcomponent.h>

Public Member Functions

 RenderableMeshComponentInstance (EntityInstance &entity, Component &component)
 
virtual bool init (utility::ErrorState &errorState) override
 
RenderableMesh createRenderableMesh (IMesh &mesh, MaterialInstance &materialInstance, utility::ErrorState &errorState)
 
RenderableMesh createRenderableMesh (IMesh &mesh, utility::ErrorState &errorState)
 
void setMesh (const RenderableMesh &mesh)
 
MaterialInstancegetMaterialInstance ()
 
IMeshgetMesh ()
 
MeshInstancegetMeshInstance ()
 
const RenderableMeshgetRenderableMesh () const
 
void setClipRect (const math::Rect &rect)
 
void setLineWidth (float lineWidth)
 
const math::RectgetClipRect () const
 
const TransformComponentInstancegetTransform ()
 
MaterialInstancegetOrCreateMaterial ()
 
- Public Member Functions inherited from RenderableComponentInstance
 RenderableComponentInstance (EntityInstance &entity, Component &resource)
 
void draw (IRenderTarget &renderTarget, VkCommandBuffer commandBuffer, const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix)
 
void setVisible (bool visible)
 
bool isVisible () const
 
const std::vector< ResourcePtr< RenderTag > > & getTags () const
 
RenderMask getMask () const
 
bool includesMask (RenderMask otherMask)
 
int getRank () const
 
const RenderLayergetLayer () const
 
virtual bool isSupported (nap::CameraComponentInstance &camera) const
 
- Public Member Functions inherited from ComponentInstance
 ComponentInstance (EntityInstance &entity, Component &resource)
 
virtual void update (double deltaTime)
 
nap::EntityInstancegetEntityInstance () const
 
nap::ComponentgetComponent () const
 
template<typename T >
T * getComponent () const
 
virtual bool init (utility::ErrorState &errorState)
 
- Public Member Functions inherited from Object
 Object ()
 
virtual ~Object ()
 
virtual void onDestroy ()
 
 Object (Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&)=delete
 
Objectoperator= (Object &&)=delete
 

Protected Member Functions

virtual void onDraw (IRenderTarget &renderTarget, VkCommandBuffer commandBuffer, const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix) override
 

Protected Attributes

TransformComponentInstancemTransformComponent
 Cached pointer to transform. More...
 
MaterialInstance mMaterialInstance
 The MaterialInstance as created from the resource. More...
 
math::Rect mClipRect
 Clipping rectangle for this instance, in pixel coordinates. More...
 
RenderableMesh mRenderableMesh
 The currently active renderable mesh, either set during init() or set by setMesh. More...
 
UniformMat4InstancemModelMatUniform = nullptr
 Pointer to the model matrix uniform. More...
 
UniformMat4InstancemViewMatUniform = nullptr
 Pointer to the view matrix uniform. More...
 
UniformMat4InstancemProjectMatUniform = nullptr
 Pointer to the projection matrix uniform. More...
 
UniformMat4InstancemNormalMatrixUniform = nullptr
 Pointer to the normal matrix uniform. More...
 
UniformVec3InstancemCameraWorldPosUniform = nullptr
 Pointer to the camera world position uniform. More...
 
float mLineWidth = 1.0f
 Line width, clamped to 1.0 if not supported by GPU. More...
 
- Protected Attributes inherited from RenderableComponentInstance
RenderServicemRenderService = nullptr
 

Additional Inherited Members

- Static Public Member Functions inherited from Object
static bool isIDProperty (rtti::Instance &object, const rtti::Property &property)
 
- Public Attributes inherited from Object
std::string mID
 Property: 'mID' unique name of the object. Used as an identifier by the system. More...
 

Description

Instance part of the renderable mesh component. Renders a mesh to screen or any other render target. You can set the mesh at runtime if necessary.

A mesh becomes 'renderable' when it is used in combination with a material. Such a mesh-material combination forms a 'nap::RenderableMesh'. Vertex attributes in both the shader and mesh should match. Otherwise, the RenderableMesh is invalid. The nap::RenderableMesh is created based on the given mesh and the material properties.

It is, however, possible to switch the mesh and / or material from the RenderableMeshComponent to some other mesh and / or material. To do so, other components should create their own nap::RenderableMesh by calling nap::RenderService::createRenderableMesh, and pass the returned object to setMesh(). The object calling nap::RenderService::createRenderableMesh should own the custom mesh and / or material and should validate the result on init().

The model view and projection matrices are automatically updated when the vertex shader exposes a struct with the 'uniform::mvpStruct' name.

uniform nap
{
mat4 viewMatrix;
mat4 modelMatrix;
} mvp; ///< Optional

A Transform component is required to position the mesh.

Inheritance diagram for RenderableMeshComponentInstance:
[legend]
Collaboration diagram for RenderableMeshComponentInstance:
[legend]

Constructor & Destructor Documentation

◆ RenderableMeshComponentInstance()

RenderableMeshComponentInstance ( EntityInstance entity,
Component component 
)

Member Function Documentation

◆ createRenderableMesh() [1/2]

RenderableMesh createRenderableMesh ( IMesh mesh,
MaterialInstance materialInstance,
utility::ErrorState errorState 
)

Creates a renderable mesh that can be used to switch to another mesh and/or material at runtime. This function should be called from init() functions on other components, and the result should be validated.

Parameters
meshThe mesh that is used in the mesh-material combination.
materialInstanceThe material instance that is used in the mesh-material combination.
errorStateIf this function returns an invalid renderable mesh, the error state contains error information.
Returns
A RenderableMesh object that can be used in setMesh calls. Check isValid on the object to see if creation succeeded or failed.

◆ createRenderableMesh() [2/2]

RenderableMesh createRenderableMesh ( IMesh mesh,
utility::ErrorState errorState 
)

Creates a renderable mesh that can be used to switch to another mesh at runtime. The material remains the same material as the one that was already set on the RenderableMeshComponent.. This function should be called from init() functions on other components, and the result should be validated.

Parameters
meshThe mesh that is used in the mesh-material combination.
errorStateIf this function returns an invalid renderable mesh, the error state contains error information.
Returns
A RenderableMesh object that can be used in setMesh calls. Check isValid on the object to see if creation succeeded or failed.

◆ getClipRect()

const math::Rect& getClipRect ( ) const
Returns
the clipping rectangle in pixel coordinates

◆ getMaterialInstance()

MaterialInstance& getMaterialInstance ( )
Returns
current material used when drawing the mesh.

◆ getMesh()

IMesh& getMesh ( )
Returns
Currently active mesh that is drawn.

◆ getMeshInstance()

MeshInstance& getMeshInstance ( )

Returns the runtime version of the mesh that is drawn, part of the original mesh.

Returns
the mesh instance that is drawn

◆ getOrCreateMaterial()

MaterialInstance* getOrCreateMaterial ( )

Returns the program used to render the mesh.

TODO: This should be private, but our current RTTI implementation 'mangles' class name-spaces, causing the RTTR_REGISTRATION_FRIEND macro to fail -> needs to be fixed. It is therefore not recommended to use this function at runtime, use 'getMaterialInstance' instead!

Returns
material handle

◆ getRenderableMesh()

const RenderableMesh& getRenderableMesh ( ) const

Returns the renderable mesh which can be used to extract low-level vertex buffer information

Returns
the renderable mesh

◆ getTransform()

const TransformComponentInstance& getTransform ( )
Returns
the transform component instance, used to compose the model matrix

◆ init()

virtual bool init ( utility::ErrorState errorState)
overridevirtual

Acquires VAO, copies clipping rectangle, initializes material instance.

Reimplemented from RenderableComponentInstance.

◆ onDraw()

virtual void onDraw ( IRenderTarget renderTarget,
VkCommandBuffer  commandBuffer,
const glm::mat4 &  viewMatrix,
const glm::mat4 &  projectionMatrix 
)
overrideprotectedvirtual

Renders the model from the ModelResource, using the material on the ModelResource.

Implements RenderableComponentInstance.

◆ setClipRect()

void setClipRect ( const math::Rect rect)

Sets clipping rectangle on this instance.

Parameters
rectRectangle in pixel coordinates.

◆ setLineWidth()

void setLineWidth ( float  lineWidth)

Sets line width

Parameters
lineWidthNew line width

◆ setMesh()

void setMesh ( const RenderableMesh mesh)

Switches the mesh and/or the material that is rendered. The renderable mesh should be created through createRenderableMesh, and must be created from an init() function.

Parameters
meshThe mesh that was retrieved through createRenderableMesh.

Member Data Documentation

◆ mCameraWorldPosUniform

UniformVec3Instance* mCameraWorldPosUniform = nullptr
protected

Pointer to the camera world position uniform.

◆ mClipRect

math::Rect mClipRect
protected

Clipping rectangle for this instance, in pixel coordinates.

◆ mLineWidth

float mLineWidth = 1.0f
protected

Line width, clamped to 1.0 if not supported by GPU.

◆ mMaterialInstance

MaterialInstance mMaterialInstance
protected

The MaterialInstance as created from the resource.

◆ mModelMatUniform

UniformMat4Instance* mModelMatUniform = nullptr
protected

Pointer to the model matrix uniform.

◆ mNormalMatrixUniform

UniformMat4Instance* mNormalMatrixUniform = nullptr
protected

Pointer to the normal matrix uniform.

◆ mProjectMatUniform

UniformMat4Instance* mProjectMatUniform = nullptr
protected

Pointer to the projection matrix uniform.

◆ mRenderableMesh

RenderableMesh mRenderableMesh
protected

The currently active renderable mesh, either set during init() or set by setMesh.

◆ mTransformComponent

TransformComponentInstance* mTransformComponent
protected

Cached pointer to transform.

◆ mViewMatUniform

UniformMat4Instance* mViewMatUniform = nullptr
protected

Pointer to the view matrix uniform.

nap::uniform::viewMatrix
constexpr const char * viewMatrix
uniform view matrix name
Definition: renderglobals.h:57
nap::uniform::modelMatrix
constexpr const char * modelMatrix
uniform model matrix name
Definition: renderglobals.h:56
nap
Definition: templateapp.h:17
nap::uniform::projectionMatrix
constexpr const char * projectionMatrix
uniform projection matrix name
Definition: renderglobals.h:58