NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
smodel.hpp
Go to the documentation of this file.
1
13#pragma once
14
15#include <functional>
16#include <glm/glm.hpp>
17#include <string>
18#include <vector>
19
24
25namespace nnl {
26
33
38constexpr std::size_t kMaxNumBoneWeight = 8;
39
58struct SVertex {
59 glm::vec3 position{0.0f};
60
61 glm::vec3 normal{0.0f, 1.0f, 0.0f};
62
63 glm::vec2 uv{0.0f};
64
65 glm::vec4 color{1.0f, 1.0f, 1.0f, 1.0f};
66
67 std::array<u16, kMaxNumBoneWeight> bones{0};
68
69 std::array<float, kMaxNumBoneWeight> weights{1.0f};
71
80 void Transform(const glm::mat4& transform);
81
86 bool HasAlpha() const;
87
93 bool IsApproxEqual(const SVertex& rhs) const;
94
99
104 void QuantWeights(unsigned int steps = 128);
105
110
117 void LimitWeights(unsigned int max_weights = 3);
118
123
128
132 void ResetUVs();
133
138
150 friend SVertex operator*(const glm::mat4& transform, SVertex vertex);
151
152 friend SVertex operator*(const SVertex& vertex, float scalar);
153};
154
162struct STriangle {
163 std::array<SVertex, 3> vertices;
164
171 const SVertex& operator[](std::size_t index) const;
172
179 SVertex& operator[](std::size_t index);
180
190 bool IsDegenerate() const;
191
197};
198
212struct SMesh {
213 std::string name;
214
215 std::vector<u32> indices;
216
217 std::vector<SVertex> vertices;
218
219 std::size_t material_id = 0;
220
221 std::map<std::size_t, std::vector<std::size_t>> material_variants;
225
226 std::size_t mesh_group = 0;
228
229 bool uses_uv = false;
230 bool uses_normal = false;
231 bool uses_color = false;
232
234
241 [[nodiscard]] static SMesh FromTriangles(const std::vector<STriangle>& triangles);
242
251 void Join(const SMesh& smesh);
252
261 glm::vec3 CalculateCenter() const;
262
270
279 void Transform(const glm::mat4& transform);
289 void TransformUV(const glm::mat3& transform);
290
299
309 static std::pair<std::vector<u32>, std::vector<SVertex>> RemoveDuplicateVertices(
310 const std::vector<u32>& indices, const std::vector<SVertex>& vertices);
311
319
327
337 std::pair<glm::vec3, glm::vec3> FindMinMax() const;
338
348 std::pair<glm::vec2, glm::vec2> FindMinMaxUV() const;
349
356
362 bool HasAlphaVertex() const;
363
370
377
388 void QuantWeights(unsigned int steps = 128);
389
394
405 void LimitWeightsPerVertex(unsigned int max_weights = 3);
406
417 void LimitWeightsPerTriangle(unsigned int max_weights = 8);
418
423
428
432 void ResetUVs();
433
438};
439
449struct SBone {
450 std::string name;
451
452 glm::vec3 scale{1.0f, 1.0f, 1.0f};
453
454 glm::quat rotation{1.0f, 0.0f, 0.0f, 0.0f};
455
456 glm::vec3 translation{0.0f, 0.0f, 0.0f};
457
458 glm::mat4 inverse{1.0f};
459
460 std::vector<SBone> children;
467 glm::mat4 GetTransform() const;
477 void SetTransform(const glm::mat4& transform);
478};
479
485struct SSkeleton {
486 std::vector<SBone> roots;
488
496 std::vector<glm::mat4> GetGlobalMatrices() const;
497
509 std::vector<glm::mat4> GetSkinningMatrices() const;
510
518
527 std::vector<std::reference_wrapper<const SBone>> GetBoneRefs() const;
528
532 std::vector<std::reference_wrapper<SBone>> GetBoneRefs();
533};
534
542 kOpaque = 0b00000,
543 kAlpha = 0b00001,
548 kClip = 0b00010,
550 kAdd = 0b00100,
551 kSub = 0b01000
553};
554
556
567
578struct SMaterial {
579 std::string name;
580
581 glm::vec3 specular{0.0f};
582 glm::vec3 diffuse{1.0f};
583 glm::vec3 ambient{0.5f};
584 glm::vec3 emissive{0.0f};
585
586 float opacity = 1.0f;
587
588 float specular_power = 50.0f;
591
592 int texture_id = -1;
594
595 bool lit = false;
598
600
603
606
609
616 bool operator==(const SMaterial& rhs) const;
617};
618
625 std::string name = "";
626
627 std::size_t id = 0;
628 std::size_t bone = 0;
629
630 glm::vec3 scale{1.0f, 1.0f, 1.0f};
631 glm::quat rotation{1.0f, 0.0f, 0.0f, 0.0f};
632 glm::vec3 translation{0.0f, 0.0f, 0.0f};
633
634 glm::mat4 GetTransform() const;
635
636 void SetTransform(const glm::mat4& transform);
637};
638
653
654struct SModel {
657
658 std::vector<SAttachment> attachments;
659
660 std::vector<SMesh> meshes;
661
662 std::vector<SMaterial> materials;
663
664 std::vector<std::string> material_variants;
670
671 std::vector<SUVAnimation> uv_animations;
673
681 glm::vec3 CalculateCenter() const;
682
701
710 void Transform(const glm::mat4& matrix);
711
721 void TransformUV(const glm::mat3& matrix);
722
730
737
745
755 std::pair<glm::vec3, glm::vec3> FindMinMax() const;
756
768 glm::mat4 Normalize();
769
783 std::vector<std::pair<glm::vec2, glm::vec2>> NormalizeUV();
784
791
798
809 void QuantWeights(unsigned int steps = 128);
810
815
826 void LimitWeightsPerVertex(unsigned int max_weights = 3);
827
838 void LimitWeightsPerTriangle(unsigned int max_weights = 8);
839
844
849
853 void ResetUVs();
854
859};
860
861} // namespace nnl
Contains macros for defining flag enums.
Contains macros and definitions for fixed-width types.
std::uint16_t u16
16-bit unsigned integer
Definition fixed_type.hpp:62
std::uint8_t u8
8-bit unsigned integer
Definition fixed_type.hpp:64
#define NNL_DEFINE_ENUM_OPERATORS(ENUMTYPE)
Macro to define bitwise operators for enum types.
Definition enum_flag.hpp:36
#define NNL_FLAG_ENUM
An attribute for defining flag enums.
Definition enum_flag.hpp:28
std::array< float, kMaxNumBoneWeight > weights
Definition smodel.hpp:69
void GenerateSmoothNormals()
Generates smooth normals for the meshes.
std::pair< glm::vec3, glm::vec3 > FindMinMax() const
Finds the minimum and maximum points of the model.
std::vector< std::string > material_variants
Definition smodel.hpp:664
void TransformUV(const glm::mat3 &transform)
Transforms the UV coordinates of the mesh using a transformation matrix.
float specular_power
Definition smodel.hpp:588
std::vector< SVertex > vertices
The vertex data for the mesh.
Definition smodel.hpp:217
glm::mat4 GetTransform() const
Calculates the local transformation matrix of the bone from the SRT properties.
std::vector< std::reference_wrapper< SBone > > GetBoneRefs()
Retrieves a flat array of references to the bones of the skeleton.
void GenerateSmoothNormals()
Generates smooth normals for the mesh.
void QuantWeights(unsigned int steps=128)
Quantizes the vertex weights to a specified number of steps.
void NormalizeWeights()
Normalizes the weights of the vertices.
void SortWeights()
Sorts vertex weights by influence, placing the highest values first.
void ResetColors()
Resets all vertex colors to the default value.
void RemoveDuplicateMaterials()
Removes duplicate materials from the model.
glm::vec3 specular
The specular color of the material.
Definition smodel.hpp:581
std::array< u16, kMaxNumBoneWeight > bones
Bone indices.
Definition smodel.hpp:67
STextureProjection projection_mode
Definition smodel.hpp:604
void ReverseWindingOrder()
Changes the winding order of the triangle.
glm::vec3 normal
A vertex normal.
Definition smodel.hpp:61
void NormalizeNormals()
Normalizes the normals of the vertices.
void ResetColors()
Resets the vertex color to its default value.
bool IsAffectedByFewBones() const
Checks if the mesh is influenced by at least 2 bones.
bool uses_color
Indicates whether the mesh uses vertex colors.
Definition smodel.hpp:231
void NormalizeWeights()
Normalizes the weights of the vertices.
std::size_t mesh_group
Definition smodel.hpp:226
std::vector< std::reference_wrapper< const SBone > > GetBoneRefs() const
Retrieves a flat array of references to the bones of the skeleton.
std::string name
An optional name for the material.
Definition smodel.hpp:579
glm::vec4 color
Vertex color (RGBA)
Definition smodel.hpp:65
bool uses_normal
Indicates whether the mesh uses vertex normals.
Definition smodel.hpp:230
void ResetNormals()
Resets the vertex normal to its default value.
std::vector< SBone > children
The children of this bone.
Definition smodel.hpp:460
void UpdateInverseMatrices()
Updates the inverse matrices from local bone transformations.
bool TryBakeBindShape()
Bakes bind shape transforms from the inverse matrices into the model.
bool operator==(const SMaterial &rhs) const
Compares two materials for equality.
glm::vec3 scale
The local scale of the bone.
Definition smodel.hpp:452
void TransformUV(const glm::mat3 &matrix)
Transforms the UV coordinates of the model using a transformation matrix.
std::string name
An optional name for the mesh.
Definition smodel.hpp:213
SVertex & operator[](std::size_t index)
Accesses a vertex of the triangle by index.
void ResetColors()
Resets all vertex colors to the default value.
void LimitWeightsPerVertex(unsigned int max_weights=3)
Limits the number of weights per vertex.
glm::vec3 diffuse
The diffuse color of the material.
Definition smodel.hpp:582
void GenerateFlatNormals()
Generates flat normals for the meshes.
void Join(const SMesh &smesh)
Joins another mesh into this mesh.
SValue extras
Definition smodel.hpp:607
glm::vec3 ambient
The ambient color of the material.
Definition smodel.hpp:583
const SVertex & operator[](std::size_t index) const
Accesses a vertex of the triangle by index.
glm::vec3 translation
Translation transformation.
Definition smodel.hpp:632
std::vector< SBone > roots
Definition smodel.hpp:486
SValue extras
Any additional data for custom use.
Definition smodel.hpp:233
glm::vec3 position
The position of the vertex in 3D space.
Definition smodel.hpp:59
SBlendMode alpha_mode
Definition smodel.hpp:601
void SetTransform(const glm::mat4 &transform)
Sets the local SRT properties of the bone.
std::vector< SMaterial > materials
A vector of materials used by the meshes.
Definition smodel.hpp:662
std::pair< glm::vec2, glm::vec2 > FindMinMaxUV() const
Finds the minimum and maximum UV coordinates of the mesh.
float opacity
The opacity of the material. [0.0, 1.0].
Definition smodel.hpp:586
void SortWeights()
Sorts vertex weights by influence, placing the highest values first.
static SMesh FromTriangles(const std::vector< STriangle > &triangles)
Creates a mesh from a triangle list.
glm::vec2 uv
Texture coordinates.
Definition smodel.hpp:63
void ResetNormals()
Resets all vertex normals to the default value.
glm::mat4 inverse
The inverse bind matrix of the bone.
Definition smodel.hpp:458
void LimitWeightsPerTriangle(unsigned int max_weights=8)
Limits the number of weights per triangle.
void SortWeights()
Sorts vertex weights by influence, placing the highest values first.
void NormalizeNormals()
Normalizes the normals of the vertices.
void LimitWeightsPerTriangle(unsigned int max_weights=8)
Limits the number of unique bone influences per triangle.
void NormalizeWeights()
Normalizes the weights of the vertex so they add up to 1.0.
void ResetNormals()
Resets all vertex normals to the default value.
void ResetUVs()
Resets all texture coordinates to the default value.
void ResetUVs()
Resets the texture coordinates to the default value.
int texture_id
Definition smodel.hpp:592
void ResetUVs()
Resets all texture coordinates to the default value.
std::size_t bone
The parent bone.
Definition smodel.hpp:628
std::array< SVertex, 3 > vertices
The vertices that define the triangle.
Definition smodel.hpp:163
glm::vec3 CalculateCenter() const
Finds the center of the model.
std::vector< glm::mat4 > GetGlobalMatrices() const
Retrieves the global matrices.
bool uses_uv
Indicates whether the mesh uses texture coordinates.
Definition smodel.hpp:229
std::vector< SAttachment > attachments
A vector of attachments associated with the model.
Definition smodel.hpp:658
std::string name
An optional name for the attachment.
Definition smodel.hpp:625
void RemoveDuplicateVertices()
Removes duplicate vertices from the mesh.
glm::vec3 CalculateCenter() const
Finds the center of the mesh.
void LimitWeightsPerVertex(unsigned int max_weights=3)
Limits the number of weights per vertex.
bool IsApproxEqual(const SVertex &rhs) const
Compares this vertex with another one for approximate equality.
glm::quat rotation
The local rotation of the bone.
Definition smodel.hpp:454
bool HasAlpha() const
Checks if the alpha component of the vertex color is less than ~1.0.
void Transform(const glm::mat4 &transform)
Transforms the mesh using a transformation matrix.
std::size_t material_id
The ID of the material associated with the mesh.
Definition smodel.hpp:219
bool HasAlphaVertex() const
Checks if the mesh has any transparent vertex colors.
glm::vec3 emissive
The emissive color of the material.
Definition smodel.hpp:584
void QuantWeights(unsigned int steps=128)
Quantizes the weights to the specified number of steps.
bool lit
Definition smodel.hpp:595
u16 vfx_group_id
The visual effects group of the material.
Definition smodel.hpp:599
void ResetWeights()
Resets the vertex bone indices and weights to their default value.
void LimitWeights(unsigned int max_weights=3)
Limits the number of bones a vertex refers to by removing the least influential weights.
glm::quat rotation
Rotation transformation.
Definition smodel.hpp:631
std::map< std::size_t, std::vector< std::size_t > > material_variants
Definition smodel.hpp:221
std::pair< glm::vec3, glm::vec3 > FindMinMax() const
Finds the minimum and maximum points of the mesh.
glm::vec3 scale
Scale transformation.
Definition smodel.hpp:630
std::vector< SMesh > meshes
A vector of meshes that make up the model.
Definition smodel.hpp:660
std::vector< u32 > indices
The vertex indices that define the triangles.
Definition smodel.hpp:215
glm::vec3 translation
The local translation of the bone.
Definition smodel.hpp:456
bool IsDegenerate() const
Checks if the triangle has no area.
std::vector< glm::mat4 > GetSkinningMatrices() const
Retrieves the skinning matrices for transforming vertices in model space.
void QuantWeights(unsigned int steps=128)
Quantizes the vertex weights to a specified number of steps.
void ResetWeights()
Removes all weights.
SSkeleton skeleton
Definition smodel.hpp:655
void ReverseWindingOrder()
Reverses the winding order of the mesh's triangles.
void GenerateFlatNormals()
Generates flat normals for the mesh.
void ResetWeights()
Removes all weights.
std::vector< SUVAnimation > uv_animations
Definition smodel.hpp:671
std::string name
An optional name for the bone.
Definition smodel.hpp:450
std::vector< std::pair< glm::vec2, glm::vec2 > > NormalizeUV()
Normalizes the UV coordinates of the model.
static std::pair< std::vector< u32 >, std::vector< SVertex > > RemoveDuplicateVertices(const std::vector< u32 > &indices, const std::vector< SVertex > &vertices)
Removes duplicate or unused vertices from the vertex data.
friend SVertex operator*(const glm::mat4 &transform, SVertex vertex)
Multiplication operator for transforming the vertex by a matrix.
void Transform(const glm::mat4 &transform)
Transforms the vertex using a transformation matrix.
glm::mat4 Normalize()
Normalizes the model to fit within the range of -1.0 to 1.0.
void Transform(const glm::mat4 &matrix)
Transforms the model using a transformation matrix.
Represents a triangle defined by three vertices.
Definition smodel.hpp:162
Represents a simple material with various properties for 3D rendering.
Definition smodel.hpp:578
Represents a bone/joint in a skeletal hierarchy of a 3D model.
Definition smodel.hpp:449
Represents a simple mesh.
Definition smodel.hpp:212
Represents a node attached to a bone.
Definition smodel.hpp:624
Represents a simple model.
Definition smodel.hpp:654
Represents a simple vertex with a fixed set of attributes.
Definition smodel.hpp:58
Represents the skeleton of a 3D model.
Definition smodel.hpp:485
STextureProjection
Enumeration of projection modes for textures.
Definition smodel.hpp:562
SBlendMode
Enumeration of blending modes for materials.
Definition smodel.hpp:541
constexpr std::size_t kMaxNumBoneWeight
Maximum number of bone weights and indices in SVertex.
Definition smodel.hpp:38
@ kMatrix
Matrix-based projection.
Definition smodel.hpp:564
@ kEnvironment
Environment mapping projection.
Definition smodel.hpp:565
@ kUV
Standard UV mapping.
Definition smodel.hpp:563
@ kAlpha
Definition smodel.hpp:543
@ kOpaque
Completely opaque.
Definition smodel.hpp:542
@ kAdd
Additive blending; colors are added to the background.
Definition smodel.hpp:550
@ kClip
Definition smodel.hpp:548
@ kSub
Definition smodel.hpp:551
A class representing JSON-like values (null, bool, numbers, strings, arrays, or objects)
Definition svalue.hpp:41
Definition exception.hpp:56
Provides data structures for representing various animation types and their essential components.
Provides a class for representing "extras" fields for custom metadata.