FPLBase
An open source project by
FPL.
|
Abstraction for a set of indices, used for rendering. More...
#include <mesh.h>
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... | |
Material * | GetMaterial (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_ |
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.
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.
indices | The indices to be included in the IBO. |
count | The number of indices. |
mat | The material associated with the IBO. |
is_32_bit | Specifies that the indices are 32bit. Default 16bit. |
primitive | How the triangles are assembled from the indices. |
|
inline |
Array of names for each bone.
|
inline |
The defines parents of each bone.
size_t fplbase::Mesh::CalculateTotalNumberOfIndices | ( | ) | const |
The total number of indices in all IBOs.
|
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;
vertices | The vertices to computes the information for. |
indices | The indices that make up the mesh. |
numverts | The number of vertices in the vertex array. |
numindices | The number of indices in the index array. |
|
inline |
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.
bone_transforms | Array of bone transforms, in object space. Length num_bones(). ith element represents the tranformation of the ith skeleton bone to its animated position. |
shader_transforms | Output array of transforms, one for each bone that has vertices weighted to it. Bones without any weighted vertices are pruned. Length num_shader_bones(). |
|
inline |
Get the material associated with the IBO at the given index.
i | The index of the IBO. |
|
inline |
Get the maximum position of an AABB about the mesh.
|
inline |
Get the minimum position of an AABB about the mesh.
|
inline |
The number of bones in the mesh.
|
inline |
The number of bones used by the shader.
|
inline |
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).
renderer | The renderer object to be used. |
ignore_material | Whether to ignore the meshes defined material. |
instances | The number of instances to be rendered. |
|
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.
bottom_left | The bottom left coordinate of the Quad. |
top_right | The bottom left coordinate of the Quad. |
tex_bottom_left | The texture coordinates at the bottom left. |
tex_top_right | The texture coordinates at the top right. |
|
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.
bottom_left | The bottom left coordinate of the Quad. |
top_right | The top right coordinate of the Quad. |
texture_size | The size of the texture used by the patches. |
patch_info | Defines how the patches are set up. |
|
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.
primitive | The type of primitive to render the data as. |
index_count | The total number of indices. |
format | The vertex buffer format, following the same rules as described in set_format(). |
vertex_size | The size of an individual vertex. |
vertices | The array of vertices. |
indices | The array of indices into the vertex array. |
|
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.
primitive | The type of primitive to render the data as. |
vertex_count | The total number of vertices. |
format | The vertex buffer format, following the same rules as described in set_format(). |
vertex_size | The size of an individual vertex. |
vertices | The 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.
renderer | The renderer object to be used. |
shader | The shader object to be used. |
viewport | An array with two elements (left and right parameters) for the viewport. |
mvp | An array with two elements (left and right parameters) for the Model View Projection (MVP) matrix. |
camera_position | An array with two elements (left and right parameters) for camera position. |
ignore_material | Whether to ignore the meshes defined material. |
instances | The 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
.
format | Array 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.
bone_transforms | Array of bones to be used. |
bone_parents | Array that contains, for each bone, the index of its parent. |
bone_names | Array containing the names of the bones. |
num_bones | The number of bones in the given array. |
shader_bone_indices | The indices of bones used by the shader. |
num_shader_bones | The number of bones in the shader bones array. |
|
inline |
The indices of bones used by the shader.
|
static |
Compute the byte size for a vertex from given attributes.
attributes | The array of attributes describing the vertex. |
end | The attribute to treat as the end of the array. |