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

#include <mesh.h>

Public Member Functions

 MeshInstance (RenderService &renderService)
 
virtual ~MeshInstance ()
 
bool init (utility::ErrorState &errorState)
 
void copyMeshProperties (RTTIMeshProperties &meshProperties)
 
GPUMeshgetGPUMesh () const
 
template<typename T >
VertexAttribute< T > * findAttribute (const std::string &id)
 
template<typename T >
const VertexAttribute< T > * findAttribute (const std::string &id) const
 
template<typename T >
VertexAttribute< T > & getAttribute (const std::string &id)
 
template<typename T >
const VertexAttribute< T > & getAttribute (const std::string &id) const
 
template<typename T >
VertexAttribute< T > & getOrCreateAttribute (const std::string &id)
 
void reserveVertices (size_t numVertices)
 
void setNumVertices (int numVertices)
 
int getNumVertices () const
 
int getNumShapes () const
 
void setDrawMode (EDrawMode mode)
 
EDrawMode getDrawMode () const
 
void setCullMode (ECullMode mode)
 
ECullMode getCullMode () const
 
void setPolygonMode (EPolygonMode mode)
 
EPolygonMode getPolygonMode () const
 
MeshShapegetShape (int index)
 
const MeshShapegetShape (int index) const
 
MeshShapecreateShape ()
 
void setUsage (EMemoryUsage inUsage)
 
EMemoryUsage getUsage () const
 
bool update (utility::ErrorState &errorState)
 
bool update (nap::BaseVertexAttribute &attribute, utility::ErrorState &errorState)
 
template<typename T >
nap::VertexAttribute< T > * findAttribute (const std::string &id)
 
template<typename T >
nap::VertexAttribute< T > & getAttribute (const std::string &id)
 
template<typename T >
nap::VertexAttribute< T > & getOrCreateAttribute (const std::string &id)
 

Protected Member Functions

bool initGPUData (utility::ErrorState &errorState)
 

Description

Runtime version of a mesh. MeshInstance holds CPU data and can convert this data into a GPUMesh.

A MeshInstance can be created in two ways: 1) Manually allocate a MeshInstance object, add attributes to it, and call the regular init() on it. This is useful for creation of procedural meshes.

2) Initialize a MeshInstance object through the init() function that takes an RTTIMeshProperties struct. This is intended for situations where the MeshInstance is created from an RTTI based object (either rtti-json or rtti-binary).

After the initial init() call, the GPU attributes are created and the mesh is updated. This means that the contents of the vertex attributes and the index buffer is uploaded to the GPU. If the contents of the vertex attributes are filled in before calling init(), the mesh is fully up to date.

It is also possible to update the mesh dynamically. The vertex attribute contents should be updated, followed by an update() call.

Constructor & Destructor Documentation

◆ MeshInstance()

MeshInstance ( RenderService renderService)

◆ ~MeshInstance()

virtual ~MeshInstance ( )
virtual

Member Function Documentation

◆ copyMeshProperties()

void copyMeshProperties ( RTTIMeshProperties meshProperties)

Clones the RTTI based data but does not build a GPU mesh from it. Call init to upload the cloned data to the GPU. The cloned data is owned by MeshInstance

Parameters
meshPropertiesThe RTTI mesh properties to clone into the mesh instance.

◆ createShape()

MeshShape& createShape ( )

Create and add a new shape to this mesh. The returned shape is uninitialized; it is up to the client to initialize as needed

Returns
The new shape

◆ findAttribute() [1/3]

VertexAttribute<T>* findAttribute ( const std::string &  id)

Finds vertex attribute.

Parameters
idThe name of the vertex attribute. For predefined vertex attributions like position, color etc, use the various MeshInstance::VertexAttributeIDs.
Returns
Type safe vertex attribute if found, nullptr if not found or if there was a type mismatch.

◆ findAttribute() [2/3]

nap::VertexAttribute<T>* findAttribute ( const std::string &  id)

◆ findAttribute() [3/3]

const VertexAttribute< T > * findAttribute ( const std::string &  id) const

Finds vertex attribute.

Parameters
idThe name of the vertex attribute. For predefined vertex attributions like position, color etc, use the various MeshInstance::VertexAttributeIDs.
Returns
Type safe vertex attribute if found, nullptr if not found or if there was a type mismatch.

◆ getAttribute() [1/3]

VertexAttribute<T>& getAttribute ( const std::string &  id)

Gets vertex attribute.

Parameters
idThe name of the vertex attribute. For predefined vertex attributions like position, color etc, use the various MeshInstance::VertexAttributeIDs.
Returns
Type safe vertex attribute. If not found or in case there is a type mismatch, the function asserts.

◆ getAttribute() [2/3]

nap::VertexAttribute<T>& getAttribute ( const std::string &  id)

◆ getAttribute() [3/3]

const VertexAttribute< T > & getAttribute ( const std::string &  id) const

Gets vertex attribute.

Parameters
idThe name of the vertex attribute. For predefined vertex attributions like position, color etc, use the various MeshInstance::VertexAttributeIDs.
Returns
Type safe vertex attribute. If not found or in case there is a type mismatch, the function asserts.

◆ getCullMode()

ECullMode getCullMode ( ) const
Returns
the cull mode of this mesh (front, back etc.)

◆ getDrawMode()

EDrawMode getDrawMode ( ) const
Returns
The topology of this mesh (triangle list, strip, lines etc).

◆ getGPUMesh()

GPUMesh& getGPUMesh ( ) const
Returns
the mesh that can be drawn to screen or buffer

◆ getNumShapes()

int getNumShapes ( ) const
Returns
The number of shapes contained in this mesh

◆ getNumVertices()

int getNumVertices ( ) const
Returns
the total number of vertices associated with this mesh

◆ getOrCreateAttribute() [1/2]

VertexAttribute<T>& getOrCreateAttribute ( const std::string &  id)

Gets a vertex attribute or creates it if it does not exist. In case the attribute did exist, but with a different type, the function asserts.

Parameters
idThe name of the vertex attribute. For predefined vertex attributions like position, color etc, use the various MeshInstance::VertexAttributeIDs.
Returns
Type safe vertex attribute.

◆ getOrCreateAttribute() [2/2]

nap::VertexAttribute<T>& getOrCreateAttribute ( const std::string &  id)

◆ getPolygonMode()

EPolygonMode getPolygonMode ( ) const
Returns
the polygon mode of this mesh

◆ getShape() [1/2]

MeshShape& getShape ( int  index)

Get the shape at the specified index

Parameters
indexThe index of the shape to get (between 0 and getNumShapes())
Returns
The shape

◆ getShape() [2/2]

const MeshShape& getShape ( int  index) const

Get the shape at the specified index

Parameters
indexThe index of the shape to get (between 0 and getNumShapes())
Returns
The shape

◆ getUsage()

EMemoryUsage getUsage ( ) const
Returns
how this mesh is used at runtime

◆ init()

bool init ( utility::ErrorState errorState)

Builds a GPU mesh from the CPU mesh. Attribute information should be filled in prior to calling init().

Parameters
errorStateContains error information if an error occurred.
Returns
True if succeeded, false on error.

◆ initGPUData()

bool initGPUData ( utility::ErrorState errorState)
protected

◆ reserveVertices()

void reserveVertices ( size_t  numVertices)

Reserves CPU memory for the given amount of vertices. All associated vertex attributes are affected. GPU memory is reserved after update() is called.

Parameters
numVerticesamount of vertices to reserve memory for.

◆ setCullMode()

void setCullMode ( ECullMode  mode)
Parameters
modethe cull mode of this mesh (front, back etc.)

◆ setDrawMode()

void setDrawMode ( EDrawMode  mode)
Returns
Set the topology of this mesh (triangle list, strip, lines etc).

◆ setNumVertices()

void setNumVertices ( int  numVertices)

Sets number of vertices. The amount of elements for each vertex buffer should be equal to the amount of vertices in the mesh.

Parameters
numVerticesamount of vertices in the mesh.

◆ setPolygonMode()

void setPolygonMode ( EPolygonMode  mode)

Sets the polygon mode of this mesh to use (fill, line or point). Logs a warning if the requested mode is not supported. To ensure a specific mode is supported call: RenderService::getPolygonModeSupported().

Parameters
modethe polygon mode to use

◆ setUsage()

void setUsage ( EMemoryUsage  inUsage)

Set the usage for this mesh. Note that it only makes sense to change this before init is called, changing it after init will not have any effect.

◆ update() [1/2]

bool update ( nap::BaseVertexAttribute attribute,
utility::ErrorState errorState 
)

Push one specific CPU vertex buffer to the GPU. Use this when updating only specific vertex attributes at run-time. Only update the mesh when 'Usage' is set to 'DynamicWrite', an assert is triggered otherwise. If there is a mismatch between the vertex buffer an error will be returned

Parameters
attributethe attribute to synchronize.
errorStatecontains the error when synchronization fails.
Returns
if the update succeeded or not.

◆ update() [2/2]

bool update ( utility::ErrorState errorState)

Pushes all CPU vertex buffers to the GPU. Note that update() is called during init(), so this is only required if CPU data is modified after init(). Only update the mesh when 'Usage' is set to 'DynamicWrite', an assert is triggered otherwise. If there is a mismatch between vertex buffer, an error will be returned.

Parameters
errorStateContains error information if an error occurred.
Returns
True if succeeded, false on error.