-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathModelUnmanaged.hpp
More file actions
207 lines (180 loc) · 5.87 KB
/
ModelUnmanaged.hpp
File metadata and controls
207 lines (180 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef RAYLIB_CPP_INCLUDE_MODELUNMANAGED_HPP_
#define RAYLIB_CPP_INCLUDE_MODELUNMANAGED_HPP_
#include <string>
#include "./BoundingBox.hpp"
#include "./RaylibException.hpp"
#include "./raylib-cpp-utils.hpp"
#include "./raylib.hpp"
namespace raylib {
/**
* A Model that is not managed by C++ RAII.
*
* Make sure to Unload() this if needed, otherwise use raylib::Model.
*
* @see raylib::Model
*/
class ModelUnmanaged : public ::Model {
public:
/**
* Default constructor.
*/
ModelUnmanaged() {}
/**
* Creates a ModelUnmanaged from an existing Model struct.
*/
ModelUnmanaged(const ::Model& model) : ::Model(model) {}
/**
* Loads a Model from the given file.
*
* @throws raylib::RaylibException Throws if failed to load the Model.
*/
ModelUnmanaged(const std::string& fileName) { Load(fileName); }
/**
* Loads a Model from the given Mesh.
*
* @throws raylib::RaylibException Throws if failed to load the Model.
*/
ModelUnmanaged(const ::Mesh& mesh) { Load(mesh); }
GETTERSETTER(::Matrix, Transform, transform)
GETTERSETTER(int, MeshCount, meshCount)
GETTERSETTER(int, MaterialCount, materialCount)
GETTERSETTER(::Mesh*, Meshes, meshes)
GETTERSETTER(::Material*, Materials, materials)
GETTERSETTER(int*, MeshMaterial, meshMaterial)
GETTERSETTER(int, BoneCount, skeleton.boneCount)
GETTERSETTER(::BoneInfo*, Bones, skeleton.bones)
GETTERSETTER(::Transform*, BindPose, skeleton.bindPose)
GETTERSETTER(::ModelAnimPose, CurrentPose, currentPose)
GETTERSETTER(::Matrix*, BoneMatrices, boneMatrices)
ModelUnmanaged& operator=(const ::Model& model) {
set(model);
return *this;
}
/**
* Loads a Model from the given file.
*
* @throws raylib::RaylibException Throws if failed to load the Model.
*/
void Load(const std::string& fileName) {
set(::LoadModel(fileName.c_str()));
if (!IsValid()) {
throw RaylibException("Failed to load Model from " + fileName);
}
}
/**
* Loads a Model from the given Mesh.
*
* @throws raylib::RaylibException Throws if failed to load the Model.
*/
void Load(const ::Mesh& mesh) {
set(::LoadModelFromMesh(mesh));
if (!IsValid()) {
throw RaylibException("Failed to load Model from Mesh");
}
}
/**
* Unload model (including meshes) from memory (RAM and/or VRAM).
*/
void Unload() {
if (meshes != nullptr || materials != nullptr) {
::UnloadModel(*this);
meshes = nullptr;
materials = nullptr;
}
}
/**
* Set material for a mesh.
*/
ModelUnmanaged& SetMeshMaterial(int meshId, int materialId) {
::SetModelMeshMaterial(this, meshId, materialId);
return *this;
}
/**
* Update model animation pose.
*/
ModelUnmanaged& UpdateAnimation(const ::ModelAnimation& anim, float frame) {
::UpdateModelAnimation(*this, anim, frame);
return *this;
}
/**
* Blend two model animation poses.
*/
ModelUnmanaged& BlendAnimation(
const ::ModelAnimation& animA,
float frameA,
const ::ModelAnimation& animB,
float frameB,
float blend) {
::UpdateModelAnimationEx(*this, animA, frameA, animB, frameB, blend);
return *this;
}
/**
* Check model animation skeleton match.
*/
[[nodiscard]] bool IsModelAnimationValid(const ::ModelAnimation& anim) const {
return ::IsModelAnimationValid(*this, anim);
}
/**
* Draw a model (with texture if set).
*/
void Draw(::Vector3 position, float scale = 1.0f, ::Color tint = {255, 255, 255, 255}) const {
::DrawModel(*this, position, scale, tint);
}
/**
* Draw a model with extended parameters.
*/
void Draw(
::Vector3 position,
::Vector3 rotationAxis,
float rotationAngle = 0.0f,
::Vector3 scale = {1.0f, 1.0f, 1.0f},
::Color tint = {255, 255, 255, 255}) const {
::DrawModelEx(*this, position, rotationAxis, rotationAngle, scale, tint);
}
/**
* Draw a model wires (with texture if set).
*/
void DrawWires(::Vector3 position, float scale = 1.0f, ::Color tint = {255, 255, 255, 255}) const {
::DrawModelWires(*this, position, scale, tint);
}
/**
* Draw a model wires (with texture if set) with extended parameters.
*/
void DrawWires(
::Vector3 position,
::Vector3 rotationAxis,
float rotationAngle = 0.0f,
::Vector3 scale = {1.0f, 1.0f, 1.0f},
::Color tint = {255, 255, 255, 255}) const {
::DrawModelWiresEx(*this, position, rotationAxis, rotationAngle, scale, tint);
}
/**
* Compute model bounding box limits (considers all meshes).
*/
[[nodiscard]] BoundingBox GetBoundingBox() const { return ::GetModelBoundingBox(*this); }
/**
* Compute model bounding box limits (considers all meshes).
*/
explicit operator BoundingBox() const { return ::GetModelBoundingBox(*this); }
/**
* Determines whether or not the Model has data in it.
*/
[[nodiscard]] bool IsValid() const { return ::IsModelValid(*this); }
protected:
void set(const ::Model& model) {
transform = model.transform;
meshCount = model.meshCount;
materialCount = model.materialCount;
meshes = model.meshes;
materials = model.materials;
meshMaterial = model.meshMaterial;
skeleton.boneCount = model.skeleton.boneCount;
skeleton.bones = model.skeleton.bones;
skeleton.bindPose = model.skeleton.bindPose;
currentPose = model.currentPose;
boneMatrices = model.boneMatrices;
}
};
} // namespace raylib
using RModelUnmanaged = raylib::ModelUnmanaged;
#endif // RAYLIB_CPP_INCLUDE_MODELUNMANAGED_HPP_