FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
fplbase::Mesh Class Reference

Abstraction for a set of indices, used for rendering. More...

#include <mesh.h>

Inheritance diagram for fplbase::Mesh:
fplbase::AsyncAsset fplbase::Asset

Detailed Description

Abstraction for a set of indices, used for rendering.

A mesh instance contains a VBO and one or more IBO's.

Classes

struct  InterleavedVertexData
 Holder for data that can be turned into a mesh. More...
 

Public Types

enum  Primitive {
  kTriangles, kTriangleStrip, kTriangleFan, kLines,
  kPoints
}
 
enum  {
  kAttributePosition, kAttributeNormal, kAttributeTangent, kAttributeTexCoord,
  kAttributeTexCoordAlt, kAttributeColor, kAttributeBoneIndices, kAttributeBoneWeights
}
 
typedef std::function
< Material *(const char
*filename)> 
MaterialLoaderFn
 
- Public Types inherited from fplbase::AsyncAsset
typedef std::function< void()> AssetFinalizedCallback
 A function pointer to an asset loaded callback function.
 

Public Member Functions

 Mesh (const char *filename=nullptr, MaterialLoaderFn material_loader_fn=nullptr, Primitive primitive=kTriangles)
 Initialize a Mesh from a file asynchronously. More...
 
 Mesh (const void *vertex_data, size_t count, size_t vertex_size, const Attribute *format, mathfu::vec3 *max_position=nullptr, mathfu::vec3 *min_position=nullptr, Primitive primitive=kTriangles)
 Initialize a Mesh by creating one VBO, and no IBO's.
 
virtual void LoadFromMemory (const void *vertex_data, size_t count, size_t vertex_size, const Attribute *format, mathfu::vec3 *max_position=nullptr, mathfu::vec3 *min_position=nullptr)
 Initialize a Mesh by creating one VBO, and no IBO's.
 
virtual void Load ()
 Loads and unpacks the Mesh from 'filename_' and 'data_'.
 
virtual bool Finalize ()
 Creates a mesh from 'data_'.
 
bool IsValid ()
 Whether this object loaded and finalized correctly. Call after Finalize has been called (by AssetManager::TryFinalize).
 
void AddIndices (const void *indices, int count, Material *mat, bool is_32_bit=false)
 Add an index buffer object to be part of this mesh. More...
 
void SetBones (const mathfu::AffineTransform *bone_transforms, const uint8_t *bone_parents, const char **bone_names, size_t num_bones, const uint8_t *shader_bone_indices, size_t num_shader_bones)
 Set the bones used by an animated mesh. More...
 
void GatherShaderTransforms (const mathfu::AffineTransform *bone_transforms, mathfu::AffineTransform *shader_transforms) const
 Convert bone transforms for consumption by a skinning shader. More...
 
void Render (Renderer &renderer, bool ignore_material=false, size_t instances=1)
 Render the mesh. More...
 
void RenderStereo (Renderer &renderer, const Shader *shader, const Viewport *viewport, const mathfu::mat4 *mvp, const mathfu::vec3 *camera_position, bool ignore_material=false, size_t instances=1)
 Render the mesh, itself, into stereoscopic viewports. More...
 
MaterialGetMaterial (int i)
 Get the material associated with the IBO at the given index. More...
 
void set_format (const Attribute *format)
 Define the vertex buffer format. More...
 
const mathfu::vec3 & min_position () const
 Get the minimum position of an AABB about the mesh. More...
 
const mathfu::vec3 & max_position () const
 Get the maximum position of an AABB about the mesh. More...
 
const uint8_t * bone_parents () const
 The defines parents of each bone. More...
 
const std::string * bone_names () const
 Array of names for each bone. More...
 
const mathfu::AffineTransform * default_bone_transform_inverses () const
 The array of default bone transform inverses. More...
 
size_t num_bones () const
 The number of bones in the mesh. More...
 
const uint8_t * shader_bone_indices () const
 The indices of bones used by the shader. More...
 
size_t num_shader_bones () const
 The number of bones used by the shader. More...
 
size_t num_vertices () const
 The number of vertices in the VBO. More...
 
size_t CalculateTotalNumberOfIndices () const
 The total number of indices in all IBOs. More...
 
void ParseInterleavedVertexData (const void *meshdef_buffer, InterleavedVertexData *ivd)
 : Load vertex data from a FlatBuffer into CPU memory first.
 
- Public Member Functions inherited from fplbase::AsyncAsset
 AsyncAsset ()
 Default constructor for an empty AsyncAsset.
 
 AsyncAsset (const char *filename)
 Construct an AsyncAsset with a given file name. More...
 
virtual ~AsyncAsset ()
 AsyncAsset destructor.
 
bool LoadNow ()
 Performs a synchronous load by calling Load & Finalize. More...
 
void set_filename (const std::string &filename)
 Sets the filename that should be loaded. More...
 
const std::string & filename () const
 The name of the file associated with the resource. More...
 
void AddFinalizeCallback (AssetFinalizedCallback callback)
 Adds a callback to be called when the asset is finalized. More...
 
- Public Member Functions inherited from fplbase::Asset
void IncreaseRefCount ()
 indicate there is an additional owner of this asset. By default, when you call any of the UnLoad*() functions in the AssetManager, that will directly delete the asset since they all start out with a single reference count. Call this function to indicate multiple owners will call Unload*() independently, and only have the asset deleted by the last one.
 

Static Public Member Functions

static void RenderArray (Primitive primitive, int index_count, const Attribute *format, int vertex_size, const void *vertices, const unsigned short *indices)
 Renders the given vertex and index data directly. More...
 
static void RenderArray (Primitive primitive, int vertex_count, const Attribute *format, int vertex_size, const void *vertices)
 Renders the given vertex data directly. More...
 
static void RenderAAQuadAlongX (const mathfu::vec3 &bottom_left, const mathfu::vec3 &top_right, const mathfu::vec2 &tex_bottom_left=mathfu::vec2(0, 0), const mathfu::vec2 &tex_top_right=mathfu::vec2(1, 1))
 Convenience method for rendering a Quad. More...
 
static void RenderAAQuadAlongXNinePatch (const mathfu::vec3 &bottom_left, const mathfu::vec3 &top_right, const mathfu::vec2i &texture_size, const mathfu::vec4 &patch_info)
 Convenience method for rendering a Quad with nine patch settings. More...
 
template<typename T >
static void ComputeNormalsTangents (T *vertices, const unsigned short *indices, int numverts, int numindices)
 Compute normals and tangents given position and texcoords. More...
 
static size_t VertexSize (const Attribute *attributes, Attribute end=kEND)
 Compute the byte size for a vertex from given attributes. More...
 

Additional Inherited Members

- Protected Member Functions inherited from fplbase::AsyncAsset
void CallFinalizeCallback ()
 Calls app callbacks when an asset is ready to be used. More...
 
- Protected Attributes inherited from fplbase::AsyncAsset
std::string filename_
 The resource file name.
 
const uint8_t * data_
 The resource data.
 
std::vector
< AssetFinalizedCallback
finalize_callbacks_
 

Constructor & Destructor Documentation

fplbase::Mesh::Mesh ( const char *  filename = nullptr,
MaterialLoaderFn  material_loader_fn = nullptr,
Primitive  primitive = kTriangles 
)

Initialize a Mesh from a file asynchronously.

Asynchronously create mesh from a file if filename is valid. Otherwise, if filename is null, need to call LoadFromMemory to init manually.

Member Function Documentation

void fplbase::Mesh::AddIndices ( const void *  indices,
int  count,
Material mat,
bool  is_32_bit = false 
)

Add an index buffer object to be part of this mesh.

Create one IBO to be part of this mesh. May be called more than once.

Parameters
indicesThe indices to be included in the IBO.
countThe number of indices.
matThe material associated with the IBO.
is_32_bitSpecifies that the indices are 32bit. Default 16bit.
primitiveHow the triangles are assembled from the indices.
const std::string* fplbase::Mesh::bone_names ( ) const
inline

Array of names for each bone.

Returns
Returns the array of names for each bone, of length num_bones().
const uint8_t* fplbase::Mesh::bone_parents ( ) const
inline

The defines parents of each bone.

Returns
Returns an array of indices of each bone's parent.
size_t fplbase::Mesh::CalculateTotalNumberOfIndices ( ) const

The total number of indices in all IBOs.

Returns
Returns the total number of indices across all IBOs.
template<typename T >
static void fplbase::Mesh::ComputeNormalsTangents ( T *  vertices,
const unsigned short *  indices,
int  numverts,
int  numindices 
)
inlinestatic

Compute normals and tangents given position and texcoords.

The template type should be a struct with at least the following fields: mathfu::vec3_packed pos; mathfu::vec2_packed tc; mathfu::vec3_packed norm; mathfu::vec4_packed tangent;

Parameters
verticesThe vertices to computes the information for.
indicesThe indices that make up the mesh.
numvertsThe number of vertices in the vertex array.
numindicesThe number of indices in the index array.
const mathfu::AffineTransform* fplbase::Mesh::default_bone_transform_inverses ( ) const
inline

The array of default bone transform inverses.

Returns
Returns the array of default bone transform inverses.
void fplbase::Mesh::GatherShaderTransforms ( const mathfu::AffineTransform *  bone_transforms,
mathfu::AffineTransform *  shader_transforms 
) const

Convert bone transforms for consumption by a skinning shader.

Vertices are stored in object space, but we need to manipulate them in bone space, so the shader transform multiplies the inverse of the default bone transform. See default_bone_transform_inverses_ for details.

Parameters
bone_transformsArray of bone transforms, in object space. Length num_bones(). ith element represents the tranformation of the ith skeleton bone to its animated position.
shader_transformsOutput array of transforms, one for each bone that has vertices weighted to it. Bones without any weighted vertices are pruned. Length num_shader_bones().
Material* fplbase::Mesh::GetMaterial ( int  i)
inline

Get the material associated with the IBO at the given index.

Parameters
iThe index of the IBO.
Returns
Returns the material of the corresponding IBO.
const mathfu::vec3& fplbase::Mesh::max_position ( ) const
inline

Get the maximum position of an AABB about the mesh.

Returns
Returns the maximum position of the mesh.
const mathfu::vec3& fplbase::Mesh::min_position ( ) const
inline

Get the minimum position of an AABB about the mesh.

Returns
Returns the minimum position of the mesh.
size_t fplbase::Mesh::num_bones ( ) const
inline

The number of bones in the mesh.

Returns
Returns the number of bones.
size_t fplbase::Mesh::num_shader_bones ( ) const
inline

The number of bones used by the shader.

Returns
Returns the number of bones used by the shader.
size_t fplbase::Mesh::num_vertices ( ) const
inline

The number of vertices in the VBO.

Returns
Returns the number of vertices in the VBO.
void fplbase::Mesh::Render ( Renderer renderer,
bool  ignore_material = false,
size_t  instances = 1 
)

Render the mesh.

Call to have the mesh render itself. Uniforms must have been set before calling this. For instanced rendering, pass in a value >1 (needs OpenGL ES 3.0 to work).

Parameters
rendererThe renderer object to be used.
ignore_materialWhether to ignore the meshes defined material.
instancesThe number of instances to be rendered.
static void fplbase::Mesh::RenderAAQuadAlongX ( const mathfu::vec3 &  bottom_left,
const mathfu::vec3 &  top_right,
const mathfu::vec2 &  tex_bottom_left = mathfu::vec2(0, 0),
const mathfu::vec2 &  tex_top_right = mathfu::vec2(1, 1) 
)
static

Convenience method for rendering a Quad.

bottom_left and top_right must have their X coordinate be different, but either Y or Z can be the same.

Parameters
bottom_leftThe bottom left coordinate of the Quad.
top_rightThe bottom left coordinate of the Quad.
tex_bottom_leftThe texture coordinates at the bottom left.
tex_top_rightThe texture coordinates at the top right.
static void fplbase::Mesh::RenderAAQuadAlongXNinePatch ( const mathfu::vec3 &  bottom_left,
const mathfu::vec3 &  top_right,
const mathfu::vec2i &  texture_size,
const mathfu::vec4 &  patch_info 
)
static

Convenience method for rendering a Quad with nine patch settings.

In the patch_info, the user can define nine patch settings as vec4(x0, y0, x1, y1) where (x0,y0): top-left corner of stretchable area in UV coordinate. (x1,y1): bottom-right corner of stretchable area in UV coordinate.

Parameters
bottom_leftThe bottom left coordinate of the Quad.
top_rightThe top right coordinate of the Quad.
texture_sizeThe size of the texture used by the patches.
patch_infoDefines how the patches are set up.
static void fplbase::Mesh::RenderArray ( Primitive  primitive,
int  index_count,
const Attribute format,
int  vertex_size,
const void *  vertices,
const unsigned short *  indices 
)
static

Renders the given vertex and index data directly.

Renders primitives using vertex and index data directly in local memory. This is a convenient alternative to creating a Mesh instance for small amounts of data, or dynamic data.

Parameters
primitiveThe type of primitive to render the data as.
index_countThe total number of indices.
formatThe vertex buffer format, following the same rules as described in set_format().
vertex_sizeThe size of an individual vertex.
verticesThe array of vertices.
indicesThe array of indices into the vertex array.
static void fplbase::Mesh::RenderArray ( Primitive  primitive,
int  vertex_count,
const Attribute format,
int  vertex_size,
const void *  vertices 
)
static

Renders the given vertex data directly.

Renders primitives using vertex data directly in local memory. This is a convenient alternative to creating a Mesh instance for small amounts of data, or dynamic data.

Parameters
primitiveThe type of primitive to render the data as.
vertex_countThe total number of vertices.
formatThe vertex buffer format, following the same rules as described in set_format().
vertex_sizeThe size of an individual vertex.
verticesThe array of vertices.
void fplbase::Mesh::RenderStereo ( Renderer renderer,
const Shader shader,
const Viewport viewport,
const mathfu::mat4 *  mvp,
const mathfu::vec3 *  camera_position,
bool  ignore_material = false,
size_t  instances = 1 
)

Render the mesh, itself, into stereoscopic viewports.

Parameters
rendererThe renderer object to be used.
shaderThe shader object to be used.
viewportAn array with two elements (left and right parameters) for the viewport.
mvpAn array with two elements (left and right parameters) for the Model View Projection (MVP) matrix.
camera_positionAn array with two elements (left and right parameters) for camera position.
ignore_materialWhether to ignore the meshes defined material.
instancesThe number of instances to be rendered.
void fplbase::Mesh::set_format ( const Attribute format)

Define the vertex buffer format.

format must have length <= kMaxAttributes, including kEND.

Parameters
formatArray of attributes to set the format to, delimitted with kEND.
void fplbase::Mesh::SetBones ( const mathfu::AffineTransform *  bone_transforms,
const uint8_t *  bone_parents,
const char **  bone_names,
size_t  num_bones,
const uint8_t *  shader_bone_indices,
size_t  num_shader_bones 
)

Set the bones used by an animated mesh.

If mesh is animated set the transform from a bone's parent space into the bone's local space. Optionally record the bone names, too, for debugging. The shader only accesses a bone if at least one vertex is weighted to it. So, we don't have to pass every bone transform up to the shader. Instead, we compact the bone transforms by passing only those in shader_bone_indices.

Parameters
bone_transformsArray of bones to be used.
bone_parentsArray that contains, for each bone, the index of its parent.
bone_namesArray containing the names of the bones.
num_bonesThe number of bones in the given array.
shader_bone_indicesThe indices of bones used by the shader.
num_shader_bonesThe number of bones in the shader bones array.
const uint8_t* fplbase::Mesh::shader_bone_indices ( ) const
inline

The indices of bones used by the shader.

Returns
Returns an array of indices of bones used by the shader.
static size_t fplbase::Mesh::VertexSize ( const Attribute attributes,
Attribute  end = kEND 
)
static

Compute the byte size for a vertex from given attributes.

Parameters
attributesThe array of attributes describing the vertex.
endThe attribute to treat as the end of the array.
Returns
Returns the byte size based on the given attributes.

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