20 #include "fplbase/config.h"
22 #include "fplbase/asset.h"
24 #include "fplbase/handles.h"
26 #include "fplbase/render_state.h"
28 #include "mathfu/constants.h"
66 typedef std::function<Material *(const char *filename)> MaterialLoaderFn;
74 MaterialLoaderFn material_loader_fn =
nullptr,
75 Primitive primitive = kTriangles);
78 Mesh(
const void *vertex_data,
size_t count,
size_t vertex_size,
80 mathfu::vec3 *
min_position =
nullptr, Primitive primitive = kTriangles);
86 size_t vertex_size,
const Attribute *format,
110 bool is_32_bit =
false);
129 void SetBones(
const mathfu::AffineTransform *bone_transforms,
149 mathfu::AffineTransform *shader_transforms)
const;
161 size_t instances = 1);
175 const Viewport *viewport,
const mathfu::mat4 *mvp,
176 const mathfu::vec3 *camera_position,
177 bool ignore_material =
false,
size_t instances = 1);
206 static void RenderArray(Primitive primitive,
int index_count,
207 const Attribute *format,
int vertex_size,
208 const void *vertices,
const unsigned short *indices);
222 static void RenderArray(Primitive primitive,
int vertex_count,
223 const Attribute *format,
int vertex_size,
224 const void *vertices);
236 const mathfu::vec3 &bottom_left,
const mathfu::vec3 &top_right,
237 const mathfu::vec2 &tex_bottom_left = mathfu::vec2(0, 0),
238 const mathfu::vec2 &tex_top_right = mathfu::vec2(1, 1));
252 const mathfu::vec3 &top_right,
253 const mathfu::vec2i &texture_size,
254 const mathfu::vec4 &patch_info);
268 template <
typename T>
270 int numverts,
int numindices) {
271 std::unique_ptr<mathfu::vec3[]> binormals(
new mathfu::vec3[numverts]);
274 for (
int i = 0; i < numverts; i++) {
275 vertices[i].norm = mathfu::kZeros3f;
276 vertices[i].tangent = mathfu::kZeros4f;
277 binormals[i] = mathfu::kZeros3f;
283 for (
int i = 0; i < numindices; i += 3) {
284 auto &v0 = vertices[indices[i + 0]];
285 auto &v1 = vertices[indices[i + 1]];
286 auto &v2 = vertices[indices[i + 2]];
289 auto q1 = mathfu::vec3(v1.pos) - mathfu::vec3(v0.pos);
290 auto q2 = mathfu::vec3(v2.pos) - mathfu::vec3(v0.pos);
291 auto norm = normalize(cross(q1, q2));
293 v0.norm = mathfu::vec3(v0.norm) + norm;
294 v1.norm = mathfu::vec3(v1.norm) + norm;
295 v2.norm = mathfu::vec3(v2.norm) + norm;
297 auto uv1 = mathfu::vec2(v1.tc) - mathfu::vec2(v0.tc);
298 auto uv2 = mathfu::vec2(v2.tc) - mathfu::vec2(v0.tc);
299 float m = 1 / (uv1.x * uv2.y - uv2.x * uv1.y);
300 auto tangent = mathfu::vec4((uv2.y * q1 - uv1.y * q2) * m, 0);
301 auto binorm = (uv1.x * q2 - uv2.x * q1) * m;
302 v0.tangent = mathfu::vec4(v0.tangent) + tangent;
303 v1.tangent = mathfu::vec4(v1.tangent) + tangent;
304 v2.tangent = mathfu::vec4(v2.tangent) + tangent;
305 binormals[indices[i + 0]] = binorm;
306 binormals[indices[i + 1]] = binorm;
307 binormals[indices[i + 2]] = binorm;
311 for (
int i = 0; i < numverts; i++) {
312 auto norm = mathfu::vec3(vertices[i].norm);
313 auto tangent = mathfu::vec4(vertices[i].tangent);
315 norm = normalize(norm);
316 tangent = mathfu::vec4(normalize(tangent.xyz()), 0);
317 binormals[i] = normalize(binormals[i]);
318 tangent = mathfu::vec4(
320 normalize(tangent.xyz() - norm * dot(norm, tangent.xyz())),
324 dot(cross(norm, tangent.xyz()), binormals[i]));
325 vertices[i].norm = norm;
326 vertices[i].tangent = tangent;
335 kAttributeTexCoordAlt,
337 kAttributeBoneIndices,
338 kAttributeBoneWeights,
363 const std::string *
bone_names()
const {
return bone_names_.data(); }
368 return default_bone_transform_inverses_;
373 size_t num_bones()
const {
return bone_parents_.size(); }
378 return &shader_bone_indices_[0];
397 const void *vertex_data;
398 std::vector<uint8_t> owned_vertex_data;
401 std::vector<Attribute> format;
405 : vertex_data(
nullptr), count(0), vertex_size(0), has_skinning(
false) {}
412 MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE
427 void ClearPlatformDependent();
430 bool InitFromMeshDef(
const void *meshdef_buffer);
434 static MeshImpl *CreateMeshImpl();
435 static void DestroyMeshImpl(MeshImpl *impl);
438 static uint32_t GetPrimitiveTypeFlags(Mesh::Primitive primitive);
440 static const int kMaxAttributes = 9;
445 ibo(InvalidBufferHandle()),
448 indexBufferMem(InvalidDeviceMemoryHandle()) {}
457 std::vector<Indices> indices_;
460 size_t num_vertices_;
462 mathfu::vec3 min_position_;
463 mathfu::vec3 max_position_;
485 mathfu::AffineTransform *default_bone_transform_inverses_;
486 std::vector<uint8_t> bone_parents_;
487 std::vector<std::string> bone_names_;
488 std::vector<uint8_t> shader_bone_indices_;
491 MaterialLoaderFn material_loader_fn_;
const mathfu::vec3 & min_position() const
Get the minimum position of an AABB about the mesh.
Definition: mesh.h:351
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.
size_t num_bones() const
The number of bones in the mesh.
Definition: mesh.h:373
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.
const std::string * bone_names() const
Array of names for each bone.
Definition: mesh.h:363
The array must always be terminated by one of these.
Definition: mesh.h:41
void GatherShaderTransforms(const mathfu::AffineTransform *bone_transforms, mathfu::AffineTransform *shader_transforms) const
Convert bone transforms for consumption by a skinning shader.
Attribute
An array of these enums defines the format of vertex data.
Definition: mesh.h:40
size_t CalculateTotalNumberOfIndices() const
The total number of indices in all IBOs.
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.
const mathfu::AffineTransform * default_bone_transform_inverses() const
The array of default bone transform inverses.
Definition: mesh.h:367
size_t num_shader_bones() const
The number of bones used by the shader.
Definition: mesh.h:383
void set_format(const Attribute *format)
Define the vertex buffer format.
Abstraction for a set of indices, used for rendering.
Definition: mesh.h:56
Represents a shader consisting of a vertex and pixel shader.
Definition: shader.h:42
const std::string & filename() const
The name of the file associated with the resource.
Definition: async_loader.h:107
Mesh(const char *filename=nullptr, MaterialLoaderFn material_loader_fn=nullptr, Primitive primitive=kTriangles)
Initialize a Mesh from a file asynchronously.
const mathfu::vec3 & max_position() const
Get the maximum position of an AABB about the mesh.
Definition: mesh.h:355
Collections of textures used for rendering multi-texture models.
Definition: material.h:37
Second set of UVs for use with e.g. lightmaps.
Definition: mesh.h:46
virtual void Load()
Loads and unpacks the Mesh from 'filename_' and 'data_'.
size_t num_vertices() const
The number of vertices in the VBO.
Definition: mesh.h:388
static void ComputeNormalsTangents(T *vertices, const unsigned short *indices, int numverts, int numindices)
Compute normals and tangents given position and texcoords.
Definition: mesh.h:269
void Render(Renderer &renderer, bool ignore_material=false, size_t instances=1)
Render the mesh.
Material * GetMaterial(int i)
Get the material associated with the IBO at the given index.
Definition: mesh.h:183
void ParseInterleavedVertexData(const void *meshdef_buffer, InterleavedVertexData *ivd)
: Load vertex data from a FlatBuffer into CPU memory first.
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.
const uint8_t * shader_bone_indices() const
The indices of bones used by the shader.
Definition: mesh.h:377
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.
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.
mathfu::recti Viewport
Specifies the region of the surface to be used for rendering.
Definition: renderer_common.h:23
Holder for data that can be turned into a mesh.
Definition: mesh.h:396
const uint8_t * bone_parents() const
The defines parents of each bone.
Definition: mesh.h:359
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.
Renderer is the main API class for rendering commands.
Definition: renderer.h:310
bool IsValid()
Whether this object loaded and finalized correctly. Call after Finalize has been called (by AssetMana...
static size_t VertexSize(const Attribute *attributes, Attribute end=kEND)
Compute the byte size for a vertex from given attributes.
Definition: async_loader.h:48
virtual bool Finalize()
Creates a mesh from 'data_'.