Refactor SkinnedMesh (#15522)

This commit is contained in:
Lars Müller 2024-12-12 15:33:08 +01:00 committed by GitHub
parent d123bc0951
commit 1e59b9a756
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 387 additions and 896 deletions

@ -9,6 +9,7 @@
#include "SMeshBuffer.h"
#include "SSkinMeshBuffer.h"
#include "quaternion.h"
#include "vector3d.h"
#include <optional>
#include <string>
@ -18,18 +19,6 @@ namespace irr
namespace scene
{
enum E_INTERPOLATION_MODE
{
// constant does use the current key-values without interpolation
EIM_CONSTANT = 0,
// linear interpolation
EIM_LINEAR,
//! count of all available interpolation modes
EIM_COUNT
};
class IAnimatedMeshSceneNode;
class IBoneSceneNode;
class ISceneManager;
@ -38,7 +27,14 @@ class SkinnedMesh : public IAnimatedMesh
{
public:
//! constructor
SkinnedMesh();
SkinnedMesh() :
EndFrame(0.f), FramesPerSecond(25.f),
LastAnimatedFrame(-1), SkinnedLastFrame(false),
HasAnimation(false), PreparedForSkinning(false),
AnimateNormals(true), HardwareSkinning(false)
{
SkinningBuffers = &LocalBuffers;
}
//! destructor
virtual ~SkinnedMesh();
@ -58,9 +54,8 @@ public:
//! returns the animated mesh for the given frame
IMesh *getMesh(f32) override;
//! Animates this mesh's joints based on frame input
//! blend: {0-old position, 1-New position}
void animateMesh(f32 frame, f32 blend);
//! Animates joints based on frame input
void animateMesh(f32 frame);
//! Performs a software skin on this mesh based of joint positions
void skinMesh();
@ -82,10 +77,14 @@ public:
void setTextureSlot(u32 meshbufNr, u32 textureSlot);
//! returns an axis aligned bounding box
const core::aabbox3d<f32> &getBoundingBox() const override;
const core::aabbox3d<f32> &getBoundingBox() const override {
return BoundingBox;
}
//! set user axis aligned bounding box
void setBoundingBox(const core::aabbox3df &box) override;
void setBoundingBox(const core::aabbox3df &box) override {
BoundingBox = box;
}
//! set the hardware mapping hint, for driver
void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override;
@ -94,7 +93,9 @@ public:
void setDirty(E_BUFFER_TYPE buffer = EBT_VERTEX_AND_INDEX) override;
//! Returns the type of the animated mesh.
E_ANIMATED_MESH_TYPE getMeshType() const override;
E_ANIMATED_MESH_TYPE getMeshType() const override {
return EAMT_SKINNED;
}
//! Gets joint count.
u32 getJointCount() const;
@ -112,18 +113,19 @@ public:
//! Update Normals when Animating
/** \param on If false don't animate, which is faster.
Else update normals, which allows for proper lighting of
animated meshes. */
void updateNormalsWhenAnimating(bool on);
//! Sets Interpolation Mode
void setInterpolationMode(E_INTERPOLATION_MODE mode);
animated meshes (default). */
void updateNormalsWhenAnimating(bool on) {
AnimateNormals = on;
}
//! converts the vertex type of all meshbuffers to tangents.
/** E.g. used for bump mapping. */
void convertMeshToTangents();
//! Does the mesh have no animation
bool isStatic() const;
bool isStatic() const {
return !HasAnimation;
}
//! Allows to enable hardware skinning.
/* This feature is not implemented in Irrlicht yet */
@ -138,16 +140,13 @@ public:
virtual void updateBoundingBox();
//! Recovers the joints from the mesh
void recoverJointsFromMesh(core::array<IBoneSceneNode *> &jointChildSceneNodes);
void recoverJointsFromMesh(std::vector<IBoneSceneNode *> &jointChildSceneNodes);
//! Transfers the joint data to the mesh
void transferJointsToMesh(const core::array<IBoneSceneNode *> &jointChildSceneNodes);
//! Transfers the joint hints to the mesh
void transferOnlyJointsHintsToMesh(const core::array<IBoneSceneNode *> &jointChildSceneNodes);
void transferJointsToMesh(const std::vector<IBoneSceneNode *> &jointChildSceneNodes);
//! Creates an array of joints from this mesh as children of node
void addJoints(core::array<IBoneSceneNode *> &jointChildSceneNodes,
void addJoints(std::vector<IBoneSceneNode *> &jointChildSceneNodes,
IAnimatedMeshSceneNode *node,
ISceneManager *smgr);
@ -171,35 +170,133 @@ public:
core::vector3df StaticNormal;
};
//! Animation keyframe which describes a new position
struct SPositionKey
{
f32 frame;
core::vector3df position;
template <class T>
struct Channel {
struct Frame {
f32 time;
T value;
};
std::vector<Frame> frames;
bool interpolate = true;
bool empty() const {
return frames.empty();
}
f32 getEndFrame() const {
return frames.empty() ? 0 : frames.back().time;
}
void pushBack(f32 time, const T &value) {
frames.push_back({time, value});
}
void append(const Channel<T> &other) {
frames.insert(frames.end(), other.frames.begin(), other.frames.end());
}
void cleanup() {
if (frames.empty())
return;
std::vector<Frame> ordered;
ordered.push_back(frames.front());
// Drop out-of-order frames
for (auto it = frames.begin() + 1; it != frames.end(); ++it) {
if (it->time > ordered.back().time) {
ordered.push_back(*it);
}
}
frames.clear();
// Drop redundant middle keys
frames.push_back(ordered.front());
for (u32 i = 1; i < ordered.size() - 1; ++i) {
if (ordered[i - 1].value != ordered[i].value
|| ordered[i + 1].value != ordered[i].value) {
frames.push_back(ordered[i]);
}
}
if (ordered.size() > 1)
frames.push_back(ordered.back());
frames.shrink_to_fit();
}
static core::quaternion interpolateValue(core::quaternion from, core::quaternion to, f32 time) {
core::quaternion result;
result.slerp(from, to, time, 0.001f);
return result;
}
static core::vector3df interpolateValue(core::vector3df from, core::vector3df to, f32 time) {
// Note: `from` and `to` are swapped here compared to quaternion slerp
return to.getInterpolated(from, time);
}
std::optional<T> get(f32 time) const {
if (frames.empty())
return std::nullopt;
const auto next = std::lower_bound(frames.begin(), frames.end(), time, [](const auto& frame, f32 time) {
return frame.time < time;
});
if (next == frames.begin())
return next->value;
if (next == frames.end())
return frames.back().value;
const auto prev = next - 1;
if (!interpolate)
return prev->value;
return interpolateValue(prev->value, next->value, (time - prev->time) / (next->time - prev->time));
}
};
//! Animation keyframe which describes a new scale
struct SScaleKey
{
f32 frame;
core::vector3df scale;
};
struct Keys {
Channel<core::vector3df> position;
Channel<core::quaternion> rotation;
Channel<core::vector3df> scale;
//! Animation keyframe which describes a new rotation
struct SRotationKey
{
f32 frame;
core::quaternion rotation;
bool empty() const {
return position.empty() && rotation.empty() && scale.empty();
}
void append(const Keys &other) {
position.append(other.position);
rotation.append(other.rotation);
scale.append(other.scale);
}
f32 getEndFrame() const {
return std::max({
position.getEndFrame(),
rotation.getEndFrame(),
scale.getEndFrame()
});
}
void updateTransform(f32 frame,
core::vector3df &t, core::quaternion &r, core::vector3df &s) const
{
if (auto pos = position.get(frame))
t = *pos;
if (auto rot = rotation.get(frame))
r = *rot;
if (auto scl = scale.get(frame))
s = *scl;
}
void cleanup() {
position.cleanup();
rotation.cleanup();
scale.cleanup();
}
};
//! Joints
struct SJoint
{
SJoint() :
UseAnimationFrom(0), GlobalSkinningSpace(false),
positionHint(-1), scaleHint(-1), rotationHint(-1)
{
}
SJoint() : GlobalSkinningSpace(false) {}
//! The name of this joint
std::optional<std::string> Name;
@ -208,22 +305,16 @@ public:
core::matrix4 LocalMatrix;
//! List of child joints
core::array<SJoint *> Children;
std::vector<SJoint *> Children;
//! List of attached meshes
core::array<u32> AttachedMeshes;
std::vector<u32> AttachedMeshes;
//! Animation keys causing translation change
core::array<SPositionKey> PositionKeys;
//! Animation keys causing scale change
core::array<SScaleKey> ScaleKeys;
//! Animation keys causing rotation change
core::array<SRotationKey> RotationKeys;
// Animation keyframes for translation, rotation, scale
Keys keys;
//! Skin weights
core::array<SWeight> Weights;
std::vector<SWeight> Weights;
//! Unnecessary for loaders, will be overwritten on finalize
core::matrix4 GlobalMatrix; // loaders may still choose to set this (temporarily) to calculate absolute vertex data.
@ -241,49 +332,14 @@ public:
//! Internal members used by SkinnedMesh
friend class SkinnedMesh;
SJoint *UseAnimationFrom;
bool GlobalSkinningSpace;
s32 positionHint;
s32 scaleHint;
s32 rotationHint;
};
// Interface for the mesh loaders (finalize should lock these functions, and they should have some prefix like loader_
// these functions will use the needed arrays, set values, etc to help the loaders
const std::vector<SJoint *> &getAllJoints() const {
return AllJoints;
}
//! exposed for loaders to add mesh buffers
core::array<SSkinMeshBuffer *> &getMeshBuffers();
//! alternative method for adding joints
core::array<SJoint *> &getAllJoints();
//! alternative method for reading joints
const core::array<SJoint *> &getAllJoints() const;
//! loaders should call this after populating the mesh
void finalize();
//! Adds a new meshbuffer to the mesh, access it as last one
SSkinMeshBuffer *addMeshBuffer();
//! Adds a new meshbuffer to the mesh, access it as last one
void addMeshBuffer(SSkinMeshBuffer *meshbuf);
//! Adds a new joint to the mesh, access it as last one
SJoint *addJoint(SJoint *parent = 0);
//! Adds a new position key to the mesh, access it as last one
SPositionKey *addPositionKey(SJoint *joint);
//! Adds a new rotation key to the mesh, access it as last one
SRotationKey *addRotationKey(SJoint *joint);
//! Adds a new scale key to the mesh, access it as last one
SScaleKey *addScaleKey(SJoint *joint);
//! Adds a new weight to the mesh, access it as last one
SWeight *addWeight(SJoint *joint);
private:
protected:
void checkForAnimation();
void normalizeWeights();
@ -292,11 +348,6 @@ private:
void buildAllGlobalAnimatedMatrices(SJoint *Joint = 0, SJoint *ParentJoint = 0);
void getFrameData(f32 frame, SJoint *Node,
core::vector3df &position, s32 &positionHint,
core::vector3df &scale, s32 &scaleHint,
core::quaternion &rotation, s32 &rotationHint);
void calculateGlobalMatrices(SJoint *Joint, SJoint *ParentJoint);
void skinJoint(SJoint *Joint, SJoint *ParentJoint);
@ -306,18 +357,18 @@ private:
const core::vector3df &vt1, const core::vector3df &vt2, const core::vector3df &vt3,
const core::vector2df &tc1, const core::vector2df &tc2, const core::vector2df &tc3);
core::array<SSkinMeshBuffer *> *SkinningBuffers; // Meshbuffer to skin, default is to skin localBuffers
std::vector<SSkinMeshBuffer *> *SkinningBuffers; // Meshbuffer to skin, default is to skin localBuffers
core::array<SSkinMeshBuffer *> LocalBuffers;
std::vector<SSkinMeshBuffer *> LocalBuffers;
//! Mapping from meshbuffer number to bindable texture slot
std::vector<u32> TextureSlots;
core::array<SJoint *> AllJoints;
core::array<SJoint *> RootJoints;
std::vector<SJoint *> AllJoints;
std::vector<SJoint *> RootJoints;
// bool can't be used here because std::vector<bool>
// doesn't allow taking a reference to individual elements.
core::array<core::array<char>> Vertices_Moved;
std::vector<std::vector<char>> Vertices_Moved;
core::aabbox3d<f32> BoundingBox;
@ -327,13 +378,42 @@ private:
f32 LastAnimatedFrame;
bool SkinnedLastFrame;
E_INTERPOLATION_MODE InterpolationMode : 8;
bool HasAnimation;
bool PreparedForSkinning;
bool AnimateNormals;
bool HardwareSkinning;
};
// Interface for mesh loaders
class SkinnedMeshBuilder : public SkinnedMesh {
public:
SkinnedMeshBuilder() : SkinnedMesh() {}
//! loaders should call this after populating the mesh
// returns *this, so do not try to drop the mesh builder instance
SkinnedMesh *finalize();
//! alternative method for adding joints
std::vector<SJoint *> &getAllJoints() {
return AllJoints;
}
//! Adds a new meshbuffer to the mesh, access it as last one
SSkinMeshBuffer *addMeshBuffer();
//! Adds a new meshbuffer to the mesh, access it as last one
void addMeshBuffer(SSkinMeshBuffer *meshbuf);
//! Adds a new joint to the mesh, access it as last one
SJoint *addJoint(SJoint *parent = nullptr);
void addPositionKey(SJoint *joint, f32 frame, core::vector3df pos);
void addRotationKey(SJoint *joint, f32 frame, core::quaternion rotation);
void addScaleKey(SJoint *joint, f32 frame, core::vector3df scale);
//! Adds a new weight to the mesh, access it as last one
SWeight *addWeight(SJoint *joint);
};
} // end namespace scene
} // end namespace irr

@ -170,7 +170,7 @@ IMesh *CAnimatedMeshSceneNode::getMeshForCurrentFrame()
if (JointMode == EJUOR_CONTROL) // write to mesh
skinnedMesh->transferJointsToMesh(JointChildSceneNodes);
else
skinnedMesh->animateMesh(getFrameNr(), 1.0f);
skinnedMesh->animateMesh(getFrameNr());
// Update the skinned mesh for the current joint transforms.
skinnedMesh->skinMesh();
@ -299,12 +299,10 @@ void CAnimatedMeshSceneNode::render()
if (Mesh->getMeshType() == EAMT_SKINNED) {
// draw skeleton
for (u32 g = 0; g < ((SkinnedMesh *)Mesh)->getAllJoints().size(); ++g) {
auto *joint = ((SkinnedMesh *)Mesh)->getAllJoints()[g];
for (u32 n = 0; n < joint->Children.size(); ++n) {
for (auto *joint : ((SkinnedMesh *)Mesh)->getAllJoints()) {
for (const auto *childJoint : joint->Children) {
driver->draw3DLine(joint->GlobalAnimatedMatrix.getTranslation(),
joint->Children[n]->GlobalAnimatedMatrix.getTranslation(),
childJoint->GlobalAnimatedMatrix.getTranslation(),
video::SColor(255, 51, 66, 255));
}
}
@ -598,8 +596,7 @@ void CAnimatedMeshSceneNode::animateJoints(bool CalculateAbsolutePositions)
SkinnedMesh *skinnedMesh = static_cast<SkinnedMesh *>(Mesh);
skinnedMesh->transferOnlyJointsHintsToMesh(JointChildSceneNodes);
skinnedMesh->animateMesh(frame, 1.0f);
skinnedMesh->animateMesh(frame);
skinnedMesh->recoverJointsFromMesh(JointChildSceneNodes);
//-----------------------------------------

@ -169,7 +169,7 @@ private:
IAnimationEndCallBack *LoopCallBack;
s32 PassCount;
core::array<IBoneSceneNode *> JointChildSceneNodes;
std::vector<IBoneSceneNode *> JointChildSceneNodes;
core::array<core::matrix4> PretransitingSave;
};

@ -10,6 +10,7 @@
#include "IVideoDriver.h"
#include "IFileSystem.h"
#include "SkinnedMesh.h"
#include "coreutil.h"
#include "os.h"
@ -51,12 +52,12 @@ IAnimatedMesh *CB3DMeshFileLoader::createMesh(io::IReadFile *file)
return 0;
B3DFile = file;
AnimatedMesh = new scene::SkinnedMesh();
AnimatedMesh = new scene::SkinnedMeshBuilder();
ShowWarning = true; // If true a warning is issued if too many textures are used
VerticesStart = 0;
if (load()) {
AnimatedMesh->finalize();
return AnimatedMesh->finalize();
} else {
AnimatedMesh->drop();
AnimatedMesh = 0;
@ -254,7 +255,7 @@ bool CB3DMeshFileLoader::readChunkMESH(SkinnedMesh::SJoint *inJoint)
meshBuffer->Material = Materials[brushID].Material;
}
if (readChunkTRIS(meshBuffer, AnimatedMesh->getMeshBuffers().size() - 1, VerticesStart) == false)
if (readChunkTRIS(meshBuffer, AnimatedMesh->getMeshBufferCount() - 1, VerticesStart) == false)
return false;
if (!NormalsInFile) {
@ -569,7 +570,7 @@ bool CB3DMeshFileLoader::readChunkKEYS(SkinnedMesh::SJoint *inJoint)
{
#ifdef _B3D_READER_DEBUG
// Only print first, that's just too much output otherwise
if (!inJoint || (inJoint->PositionKeys.empty() && inJoint->ScaleKeys.empty() && inJoint->RotationKeys.empty())) {
if (!inJoint || inJoint->keys.empty()) {
core::stringc logStr;
for (u32 i = 1; i < B3dStack.size(); ++i)
logStr += "-";
@ -584,13 +585,6 @@ bool CB3DMeshFileLoader::readChunkKEYS(SkinnedMesh::SJoint *inJoint)
flags = os::Byteswap::byteswap(flags);
#endif
SkinnedMesh::SPositionKey *oldPosKey = 0;
core::vector3df oldPos[2];
SkinnedMesh::SScaleKey *oldScaleKey = 0;
core::vector3df oldScale[2];
SkinnedMesh::SRotationKey *oldRotKey = 0;
core::quaternion oldRot[2];
bool isFirst[3] = {true, true, true};
while ((B3dStack.getLast().startposition + B3dStack.getLast().length) > B3DFile->getPos()) // this chunk repeats
{
s32 frame;
@ -600,91 +594,24 @@ bool CB3DMeshFileLoader::readChunkKEYS(SkinnedMesh::SJoint *inJoint)
frame = os::Byteswap::byteswap(frame);
#endif
if (frame < 1) {
os::Printer::log("Illegal frame number found", B3DFile->getFileName(), ELL_ERROR);
frame = 1;
}
// Add key frames, frames in Irrlicht are zero-based
f32 data[4];
if (flags & 1) {
readFloats(data, 3);
if ((oldPosKey != 0) && (oldPos[0] == oldPos[1])) {
const core::vector3df pos(data[0], data[1], data[2]);
if (oldPos[1] == pos)
oldPosKey->frame = (f32)frame - 1;
else {
oldPos[0] = oldPos[1];
oldPosKey = AnimatedMesh->addPositionKey(inJoint);
oldPosKey->frame = (f32)frame - 1;
oldPos[1].set(oldPosKey->position.set(pos));
}
} else if (oldPosKey == 0 && isFirst[0]) {
oldPosKey = AnimatedMesh->addPositionKey(inJoint);
oldPosKey->frame = (f32)frame - 1;
oldPos[0].set(oldPosKey->position.set(data[0], data[1], data[2]));
oldPosKey = 0;
isFirst[0] = false;
} else {
if (oldPosKey != 0)
oldPos[0] = oldPos[1];
oldPosKey = AnimatedMesh->addPositionKey(inJoint);
oldPosKey->frame = (f32)frame - 1;
oldPos[1].set(oldPosKey->position.set(data[0], data[1], data[2]));
}
AnimatedMesh->addPositionKey(inJoint, frame - 1, {data[0], data[1], data[2]});
}
if (flags & 2) {
readFloats(data, 3);
if ((oldScaleKey != 0) && (oldScale[0] == oldScale[1])) {
const core::vector3df scale(data[0], data[1], data[2]);
if (oldScale[1] == scale)
oldScaleKey->frame = (f32)frame - 1;
else {
oldScale[0] = oldScale[1];
oldScaleKey = AnimatedMesh->addScaleKey(inJoint);
oldScaleKey->frame = (f32)frame - 1;
oldScale[1].set(oldScaleKey->scale.set(scale));
}
} else if (oldScaleKey == 0 && isFirst[1]) {
oldScaleKey = AnimatedMesh->addScaleKey(inJoint);
oldScaleKey->frame = (f32)frame - 1;
oldScale[0].set(oldScaleKey->scale.set(data[0], data[1], data[2]));
oldScaleKey = 0;
isFirst[1] = false;
} else {
if (oldScaleKey != 0)
oldScale[0] = oldScale[1];
oldScaleKey = AnimatedMesh->addScaleKey(inJoint);
oldScaleKey->frame = (f32)frame - 1;
oldScale[1].set(oldScaleKey->scale.set(data[0], data[1], data[2]));
}
AnimatedMesh->addScaleKey(inJoint, frame - 1, {data[0], data[1], data[2]});
}
if (flags & 4) {
readFloats(data, 4);
if ((oldRotKey != 0) && (oldRot[0] == oldRot[1])) {
// meant to be in this order since b3d stores W first
const core::quaternion rot(data[1], data[2], data[3], data[0]);
if (oldRot[1] == rot)
oldRotKey->frame = (f32)frame - 1;
else {
oldRot[0] = oldRot[1];
oldRotKey = AnimatedMesh->addRotationKey(inJoint);
oldRotKey->frame = (f32)frame - 1;
oldRot[1].set(oldRotKey->rotation.set(data[1], data[2], data[3], data[0]));
oldRot[1].normalize();
}
} else if (oldRotKey == 0 && isFirst[2]) {
oldRotKey = AnimatedMesh->addRotationKey(inJoint);
oldRotKey->frame = (f32)frame - 1;
// meant to be in this order since b3d stores W first
oldRot[0].set(oldRotKey->rotation.set(data[1], data[2], data[3], data[0]));
oldRot[0].normalize();
oldRotKey = 0;
isFirst[2] = false;
} else {
if (oldRotKey != 0)
oldRot[0] = oldRot[1];
oldRotKey = AnimatedMesh->addRotationKey(inJoint);
oldRotKey->frame = (f32)frame - 1;
// meant to be in this order since b3d stores W first
oldRot[1].set(oldRotKey->rotation.set(data[1], data[2], data[3], data[0]));
oldRot[1].normalize();
}
AnimatedMesh->addRotationKey(inJoint, frame - 1, core::quaternion(data[1], data[2], data[3], data[0]));
}
}

@ -63,7 +63,7 @@ private:
core::array<video::S3DVertex2TCoords> BaseVertices;
SkinnedMesh *AnimatedMesh;
SkinnedMeshBuilder *AnimatedMesh;
io::IReadFile *B3DFile;
// B3Ds have Vertex ID's local within the mesh I don't want this

@ -346,14 +346,14 @@ IAnimatedMesh* SelfType::createMesh(io::IReadFile* file)
const char *filename = file->getFileName().c_str();
try {
tiniergltf::GlTF model = parseGLTF(file);
irr_ptr<SkinnedMesh> mesh(new SkinnedMesh());
irr_ptr<SkinnedMeshBuilder> mesh(new SkinnedMeshBuilder());
MeshExtractor extractor(std::move(model), mesh.get());
try {
extractor.load();
for (const auto &warning : extractor.getWarnings()) {
os::Printer::log(filename, warning.c_str(), ELL_WARNING);
}
return mesh.release();
return mesh.release()->finalize();
} catch (const std::runtime_error &e) {
os::Printer::log("error converting gltf to irrlicht mesh", e.what(), ELL_ERROR);
}
@ -691,27 +691,27 @@ void SelfType::MeshExtractor::loadAnimation(const std::size_t animIdx)
case tiniergltf::AnimationChannelTarget::Path::TRANSLATION: {
const auto outputAccessor = Accessor<core::vector3df>::make(m_gltf_model, sampler.output);
for (std::size_t i = 0; i < n_frames; ++i) {
auto *key = m_irr_model->addPositionKey(joint);
key->frame = inputAccessor.get(i);
key->position = convertHandedness(outputAccessor.get(i));
f32 frame = inputAccessor.get(i);
core::vector3df position = outputAccessor.get(i);
m_irr_model->addPositionKey(joint, frame, convertHandedness(position));
}
break;
}
case tiniergltf::AnimationChannelTarget::Path::ROTATION: {
const auto outputAccessor = Accessor<core::quaternion>::make(m_gltf_model, sampler.output);
for (std::size_t i = 0; i < n_frames; ++i) {
auto *key = m_irr_model->addRotationKey(joint);
key->frame = inputAccessor.get(i);
key->rotation = convertHandedness(outputAccessor.get(i));
f32 frame = inputAccessor.get(i);
core::quaternion rotation = outputAccessor.get(i);
m_irr_model->addRotationKey(joint, frame, convertHandedness(rotation));
}
break;
}
case tiniergltf::AnimationChannelTarget::Path::SCALE: {
const auto outputAccessor = Accessor<core::vector3df>::make(m_gltf_model, sampler.output);
for (std::size_t i = 0; i < n_frames; ++i) {
auto *key = m_irr_model->addScaleKey(joint);
key->frame = inputAccessor.get(i);
key->scale = outputAccessor.get(i);
f32 frame = inputAccessor.get(i);
core::vector3df scale = outputAccessor.get(i);
m_irr_model->addScaleKey(joint, frame, scale);
}
break;
}
@ -756,8 +756,6 @@ void SelfType::MeshExtractor::load()
} catch (const std::bad_optional_access &e) {
throw std::runtime_error(e.what());
}
m_irr_model->finalize();
}
/**

@ -100,7 +100,7 @@ private:
{
public:
MeshExtractor(tiniergltf::GlTF &&model,
SkinnedMesh *mesh) noexcept
SkinnedMeshBuilder *mesh) noexcept
: m_gltf_model(std::move(model)), m_irr_model(mesh) {};
/* Gets indices for the given mesh/primitive.
@ -124,7 +124,7 @@ private:
private:
const tiniergltf::GlTF m_gltf_model;
SkinnedMesh *m_irr_model;
SkinnedMeshBuilder *m_irr_model;
std::vector<std::function<void()>> m_mesh_loaders;
std::vector<SkinnedMesh::SJoint *> m_loaded_nodes;

@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in irrlicht.h
#include "CXMeshFileLoader.h"
#include "SkinnedMesh.h"
#include "os.h"
#include "fast_atof.h"
@ -57,7 +58,7 @@ IAnimatedMesh *CXMeshFileLoader::createMesh(io::IReadFile *file)
u32 time = os::Timer::getRealTime();
#endif
AnimatedMesh = new SkinnedMesh();
AnimatedMesh = new SkinnedMeshBuilder();
if (load(file)) {
AnimatedMesh->finalize();
@ -92,7 +93,7 @@ IAnimatedMesh *CXMeshFileLoader::createMesh(io::IReadFile *file)
delete Meshes[i];
Meshes.clear();
return AnimatedMesh;
return AnimatedMesh->finalize();
}
bool CXMeshFileLoader::load(io::IReadFile *file)
@ -124,7 +125,7 @@ bool CXMeshFileLoader::load(io::IReadFile *file)
if (!mesh->HasSkinning) {
// Set up rigid animation
if (mesh->AttachedJointID != -1) {
AnimatedMesh->getAllJoints()[mesh->AttachedJointID]->AttachedMeshes.push_back(AnimatedMesh->getMeshBuffers().size() - 1);
AnimatedMesh->getAllJoints()[mesh->AttachedJointID]->AttachedMeshes.push_back(AnimatedMesh->getMeshBufferCount() - 1);
}
}
}
@ -965,7 +966,7 @@ bool CXMeshFileLoader::parseDataObjectSkinWeights(SXMesh &mesh)
u32 i;
const u32 jointStart = joint->Weights.size();
joint->Weights.reallocate(jointStart + nWeights);
joint->Weights.resize(jointStart + nWeights);
mesh.WeightJoint.reallocate(mesh.WeightJoint.size() + nWeights);
mesh.WeightNum.reallocate(mesh.WeightNum.size() + nWeights);
@ -1411,20 +1412,7 @@ bool CXMeshFileLoader::parseDataObjectAnimation()
joint->Name = FrameName.c_str();
}
joint->PositionKeys.reallocate(joint->PositionKeys.size() + animationDump.PositionKeys.size());
for (u32 n = 0; n < animationDump.PositionKeys.size(); ++n) {
joint->PositionKeys.push_back(animationDump.PositionKeys[n]);
}
joint->ScaleKeys.reallocate(joint->ScaleKeys.size() + animationDump.ScaleKeys.size());
for (u32 n = 0; n < animationDump.ScaleKeys.size(); ++n) {
joint->ScaleKeys.push_back(animationDump.ScaleKeys[n]);
}
joint->RotationKeys.reallocate(joint->RotationKeys.size() + animationDump.RotationKeys.size());
for (u32 n = 0; n < animationDump.RotationKeys.size(); ++n) {
joint->RotationKeys.push_back(animationDump.RotationKeys[n]);
}
joint->keys.append(animationDump.keys);
} else
os::Printer::log("joint name was never given", ELL_WARNING);
@ -1488,10 +1476,9 @@ bool CXMeshFileLoader::parseDataObjectAnimationKey(SkinnedMesh::SJoint *joint)
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
}
SkinnedMesh::SRotationKey *key = AnimatedMesh->addRotationKey(joint);
key->frame = time;
key->rotation.set(X, Y, Z, W);
key->rotation.normalize();
core::quaternion rotation(X, Y, Z, W);
rotation.normalize();
AnimatedMesh->addRotationKey(joint, time, rotation);
} break;
case 1: // scale
case 2: // position
@ -1514,13 +1501,9 @@ bool CXMeshFileLoader::parseDataObjectAnimationKey(SkinnedMesh::SJoint *joint)
}
if (keyType == 2) {
SkinnedMesh::SPositionKey *key = AnimatedMesh->addPositionKey(joint);
key->frame = time;
key->position = vector;
AnimatedMesh->addPositionKey(joint, time, vector);
} else {
SkinnedMesh::SScaleKey *key = AnimatedMesh->addScaleKey(joint);
key->frame = time;
key->scale = vector;
AnimatedMesh->addScaleKey(joint, time, vector);
}
} break;
case 3:
@ -1547,16 +1530,8 @@ bool CXMeshFileLoader::parseDataObjectAnimationKey(SkinnedMesh::SJoint *joint)
// core::vector3df rotation = mat.getRotationDegrees();
SkinnedMesh::SRotationKey *keyR = AnimatedMesh->addRotationKey(joint);
keyR->frame = time;
// IRR_TEST_BROKEN_QUATERNION_USE: TODO - switched from mat to mat.getTransposed() for downward compatibility.
// Not tested so far if this was correct or wrong before quaternion fix!
keyR->rotation = core::quaternion(mat.getTransposed());
SkinnedMesh::SPositionKey *keyP = AnimatedMesh->addPositionKey(joint);
keyP->frame = time;
keyP->position = mat.getTranslation();
AnimatedMesh->addRotationKey(joint, time, core::quaternion(mat.getTransposed()));
AnimatedMesh->addPositionKey(joint, time, mat.getTranslation());
/*
core::vector3df scale=mat.getScale();

@ -155,7 +155,7 @@ private:
bool readRGB(video::SColor &color);
bool readRGBA(video::SColor &color);
SkinnedMesh *AnimatedMesh;
SkinnedMeshBuilder *AnimatedMesh;
c8 *Buffer;
const c8 *P;

File diff suppressed because it is too large Load Diff