The small3d library
Model.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <string>
12 #include <vector>
13 #include "Math.hpp"
14 #include "Image.hpp"
15 #include "File.hpp"
16 #include "Material.hpp"
17 
18 namespace glm {
19  template<class Archive> void serialize(Archive& archive, small3d::Vec3& v) { archive(v.x, v.y, v.z); }
20  template<class Archive> void serialize(Archive& archive, small3d::Vec4& v) { archive(v.x, v.y, v.z, v.w); }
21  template<class Archive> void serialize(Archive& archive, small3d::Mat4& m) { archive(m[0], m[1], m[2], m[3]); }
22 }
23 
24 namespace small3d {
25 
34  class Model {
35 
36  private:
37 
38  uint32_t positionBufferObjectId = 0;
39  uint32_t indexBufferObjectId = 0;
40  uint32_t normalsBufferObjectId = 0;
41  uint32_t uvBufferObjectId = 0;
42  uint32_t jointBufferObjectId = 0;
43  uint32_t weightBufferObjectId = 0;
44 
45  uint32_t currentAnimation = 0;
46  std::vector<uint64_t> numPoses;
47 
48  // Original transformation matrix (from armature/skin),
49  // as read from a file
50  Mat4 origTransformation = Mat4(1.0f);
51 
52  // Original rotation (from armature/skin), as read from a
53  // file (in quaternion form)
54  Quat origRotation = { 0.0f, 0.0f, 0.0f, 1.0f };
55 
56  // brief Original translation, as read from a file
57  Vec3 origTranslation = Vec3(0.0f, 0.0f, 0.0f);
58 
59  // brief Original scale, as read from a file
60  Vec3 origScale = Vec3(1.0f, 1.0f, 1.0f);
61 
62 
63  public:
64 
69 
74  uint32_t input = 0;
75  std::vector<Quat> rotationAnimation;
76  std::vector<Vec3> translationAnimation;
77  std::vector<Vec3> scaleAnimation;
78  std::vector<float> times;
79  template <class Archive>
80  void serialize(Archive& archive) {
81  archive(input, rotationAnimation, translationAnimation, scaleAnimation, times);
82  }
83  };
84 
88  struct Animation {
89  std::string name;
90  std::vector<AnimationComponent> animationComponents;
91  template <class Archive>
92  void serialize(Archive& archive) {
93  archive(name, animationComponents);
94  }
95  };
96 
100  struct Joint {
101  uint32_t node = 0;
102  std::string name;
103  Mat4 inverseBindMatrix = Mat4(1.0f);
104  Quat rotation = { 1.0f, 0.0f, 0.0f, 0.0f };
105  Vec3 scale = Vec3(1.0f, 1.0f, 1.0f);
106  Vec3 translation = Vec3(0.0f, 0.0f, 0.0f);
107  Mat4 transformation = Mat4(1.0f);
108  std::vector<uint32_t> children;
109  std::vector<Animation> animations;
110 
111  template <class Archive>
112  void serialize(Archive& archive) {
113  archive(node, name, inverseBindMatrix, rotation, scale, translation,
114  transformation, children, animations);
115  }
116  };
117 
121  Vec3 scale = Vec3(1.0f, 1.0f, 1.0f);
122 
127  static const uint32_t MAX_JOINTS_SUPPORTED = 32;
128 
133  std::shared_ptr<Image> defaultTextureImage;
134 
140  std::vector<float> vertexData;
141 
145  uint32_t vertexDataByteSize = 0;
146 
152  std::vector<uint16_t> indexData;
153 
157  uint32_t indexDataByteSize = 0;
158 
165  std::vector<float> normalsData;
166 
170  uint32_t normalsDataByteSize = 0;
171 
178  std::vector<float> textureCoordsData;
179 
184 
189  std::vector<uint8_t> jointData;
190 
194  uint32_t jointDataByteSize = 0;
195 
200  std::vector<float> weightData;
201 
205  uint32_t weightDataByteSize = 0;
206 
210  std::vector<Joint> joints;
211 
215  std::vector<Animation> animations;
216 
220  bool noShadow = false;
221 
226  Model();
227 
235  Model(File& file, const std::string& meshName = "");
236 
245  Model(File&& file, const std::string& meshName = "");
246 
252  uint64_t getNumPoses();
253 
258  size_t getNumAnimations();
259 
264  void setAnimation(uint32_t animationIdx);
265 
266 
275  Mat4 getTransform(uint32_t animationIdx, uint64_t currentPose, float seconds = 0.0f);
276 
287  Mat4 getJointTransform(size_t jointIdx, uint32_t animationIdx, uint64_t currentPose, float seconds = 0.0f);
288 
294 
299  void saveBinary(const std::string& binaryFilePath);
300 
301  template <class Archive>
302  void serialize(Archive& archive) {
303  archive(currentAnimation,
304  numPoses,
305  origTransformation,
306  origRotation,
307  origTranslation,
308  origScale,
309  material,
310  scale,
312  vertexData,
314  indexData,
316  normalsData,
320  jointData,
322  weightData,
324  joints,
325  animations
326  );
327  }
328 
329  friend class GlbFile;
330  friend class Renderer;
331 
332  };
333 }
small3d::Model::normalsData
std::vector< float > normalsData
Array, to be treated as a 3 column table. Each "row" contains the x, y and z components of the vector...
Definition: Model.hpp:165
small3d::Vec4
4 component vector
Definition: Math.hpp:56
small3d::Model::material
Material material
The material of the model.
Definition: Model.hpp:68
small3d::GlbFile
.glb (glTF) file parser class. It can load meshes, textures and linear animations from samplers with ...
Definition: GlbFile.hpp:30
small3d::Model::jointData
std::vector< uint8_t > jointData
Array, to be treated as a 4 column table. Each "row" potentially contains up to 4 joints that can inf...
Definition: Model.hpp:189
Math.hpp
Vectors, matrices and other math things.
small3d::Model::indexData
std::vector< uint16_t > indexData
3 column table. Each element refers to a "row" in the vertex data table. Each "row" in the index data...
Definition: Model.hpp:152
small3d::Model::setAnimation
void setAnimation(uint32_t animationIdx)
Set the current animation.
Definition: Model.cpp:50
small3d::Model::Model
Model()
Default constructor.
Definition: Model.cpp:25
small3d::Model::animations
std::vector< Animation > animations
Animations of the model (there are also joint animations)
Definition: Model.hpp:215
small3d::Model::scale
Vec3 scale
Use this to scale the model and not origScale.
Definition: Model.hpp:121
small3d::Model::MAX_JOINTS_SUPPORTED
static const uint32_t MAX_JOINTS_SUPPORTED
Maximum number of supported joints.
Definition: Model.hpp:127
small3d::Model::Animation
animation
Definition: Model.hpp:88
small3d::Model
A 3D model. It can be loaded from a WavefrontFile or GlbFile. It can also be constructed procedurally...
Definition: Model.hpp:34
small3d::Model::getNumPoses
uint64_t getNumPoses()
Get the number of animation poses in the current animation.
Definition: Model.cpp:42
small3d::Model::weightData
std::vector< float > weightData
Array, to be treated as a 4 column table. Each "row" contains the weights by which each of the corres...
Definition: Model.hpp:200
small3d::Model::Joint
joint
Definition: Model.hpp:100
small3d::Model::textureCoordsDataByteSize
uint32_t textureCoordsDataByteSize
Size of the texture coordinates data, in bytes.
Definition: Model.hpp:183
Image.hpp
Image loading and manipulation.
small3d::Model::AnimationComponent
animation component
Definition: Model.hpp:73
small3d::Model::vertexDataByteSize
uint32_t vertexDataByteSize
Size of the vertex data, in bytes.
Definition: Model.hpp:145
small3d::Renderer
Renderer class (OpenGL 3.3 / OpenGL ES 3.0)
Definition: Renderer.hpp:38
small3d::Model::getTransform
Mat4 getTransform(uint32_t animationIdx, uint64_t currentPose, float seconds=0.0f)
Get a transform of the model.
Definition: Model.cpp:58
small3d::Model::defaultTextureImage
std::shared_ptr< Image > defaultTextureImage
Image found in the file the model was loaded from (empty image if not - check the size)....
Definition: Model.hpp:133
small3d::Model::vertexData
std::vector< float > vertexData
The vertex data. This is an array, which is to be treated as a 4 column table, holding the x,...
Definition: Model.hpp:140
File.hpp
File parser virtual class.
small3d::Quat
Definition: Math.hpp:126
small3d::Model::jointDataByteSize
uint32_t jointDataByteSize
Size of the joint data, in bytes.
Definition: Model.hpp:194
small3d
Definition: BinaryFile.hpp:15
small3d::Model::indexDataByteSize
uint32_t indexDataByteSize
Size of the index data, in bytes.
Definition: Model.hpp:157
small3d::Model::saveBinary
void saveBinary(const std::string &binaryFilePath)
Save model data in binary format.
Definition: Model.cpp:266
small3d::File
Abstract file parser class.
Definition: File.hpp:23
small3d::Model::noShadow
bool noShadow
Should the model produce a shadow?
Definition: Model.hpp:220
small3d::Model::getNumAnimations
size_t getNumAnimations()
Get the number of available animations.
Definition: Model.cpp:46
small3d::Model::joints
std::vector< Joint > joints
The model's joints.
Definition: Model.hpp:210
small3d::Vec3
3 component float vector
Definition: Math.hpp:19
small3d::Model::textureCoordsData
std::vector< float > textureCoordsData
Array, to be treated as a 2 column table. Each "row" contains the x and y components of the pixel coo...
Definition: Model.hpp:178
small3d::Model::normalsDataByteSize
uint32_t normalsDataByteSize
Size of the normals data, in bytes.
Definition: Model.hpp:170
small3d::Model::getOriginalScale
Vec3 getOriginalScale()
Get the Model's original scale (usually the one read from the file the Model was loaded from.
Definition: Model.cpp:262
small3d::Mat4
4x4 float matrix
Definition: Math.hpp:99
small3d::Material
A 3D model material.
Definition: Material.hpp:23
Material.hpp
Material structure.
small3d::Model::weightDataByteSize
uint32_t weightDataByteSize
Size of the weight data, in bytes.
Definition: Model.hpp:205
small3d::Model::getJointTransform
Mat4 getJointTransform(size_t jointIdx, uint32_t animationIdx, uint64_t currentPose, float seconds=0.0f)
Get a joint transform, also calculating the transorms of the parent joints in the same tree and the a...
Definition: Model.cpp:147