The small3d library
Tiny C++ 3D game development library for Win/MacOS/Linux/FreeBSD
Loading...
Searching...
No Matches
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
18namespace 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
24namespace 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
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 explicit Model(File& file, const std::string& meshName = "") ;
236
245 explicit 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,
314 indexData,
320 jointData,
324 joints,
326 );
327 }
328
329 friend class GlbFile;
330 friend class Renderer;
331
332 };
333}
File parser virtual class.
Image loading and manipulation.
Material structure.
Vectors, matrices and other math things.
Abstract file parser class.
Definition File.hpp:23
.glb (glTF) file parser class. It can load meshes, textures and linear animations from samplers with ...
Definition GlbFile.hpp:30
A 3D model material.
Definition Material.hpp:23
A 3D model. It can be loaded from a WavefrontFile or GlbFile. It can also be constructed procedurally...
Definition Model.hpp:34
size_t getNumAnimations()
Get the number of available animations.
Definition Model.cpp:46
uint32_t vertexDataByteSize
Size of the vertex data, in bytes.
Definition Model.hpp:145
uint32_t weightDataByteSize
Size of the weight data, in bytes.
Definition Model.hpp:205
Vec3 scale
Use this to scale the model and not origScale.
Definition Model.hpp:121
uint32_t textureCoordsDataByteSize
Size of the texture coordinates data, in bytes.
Definition Model.hpp:183
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
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
Mat4 getTransform(uint32_t animationIdx, uint64_t currentPose, float seconds=0.0f)
Get a transform of the model.
Definition Model.cpp:58
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
uint32_t jointDataByteSize
Size of the joint data, in bytes.
Definition Model.hpp:194
void setAnimation(uint32_t animationIdx)
Set the current animation.
Definition Model.cpp:50
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
Material material
The material of the model.
Definition Model.hpp:68
uint32_t indexDataByteSize
Size of the index data, in bytes.
Definition Model.hpp:157
Vec3 getOriginalScale()
Get the Model's original scale (usually the one read from the file the Model was loaded from.
Definition Model.cpp:262
Model()
Default constructor.
Definition Model.cpp:25
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
void saveBinary(const std::string &binaryFilePath)
Save model data in binary format.
Definition Model.cpp:266
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
uint64_t getNumPoses()
Get the number of animation poses in the current animation.
Definition Model.cpp:42
uint32_t normalsDataByteSize
Size of the normals data, in bytes.
Definition Model.hpp:170
std::vector< Animation > animations
Animations of the model (there are also joint animations)
Definition Model.hpp:215
std::vector< Joint > joints
The model's joints.
Definition Model.hpp:210
bool noShadow
Should the model produce a shadow?
Definition Model.hpp:220
static const uint32_t MAX_JOINTS_SUPPORTED
Maximum number of supported joints.
Definition Model.hpp:127
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
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
Renderer class (OpenGL 3.3 / OpenGL ES 3.0)
Definition Renderer.hpp:34
Definition BinaryFile.hpp:15
4x4 float matrix
Definition Math.hpp:112
animation component
Definition Model.hpp:73
animation
Definition Model.hpp:88
joint
Definition Model.hpp:100
Quaternion.
Definition Math.hpp:141
3 component float vector
Definition Math.hpp:21
4 component vector
Definition Math.hpp:68