271 lines
5.1 KiB
C++
271 lines
5.1 KiB
C++
/**
|
|
* @file dl_model_data.h
|
|
* @brief 读取到的原生数据
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 22-08-17
|
|
*
|
|
* @info
|
|
*/
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
#include <limits>
|
|
|
|
#include "memory/dl_stream_output.h"
|
|
#include "memory/dl_stream_input.h"
|
|
#include "math/dl_matrix.h"
|
|
#include "dl_model_common.h"
|
|
#include "vk/dl_vertex.h"
|
|
#include "base/dl_tree.h"
|
|
|
|
namespace dl
|
|
{
|
|
// 模型节点变换
|
|
struct ModelNodeTransform
|
|
{
|
|
Position3 _t;
|
|
Position4 _r; // 四元数
|
|
Position3 _s;
|
|
Matrix4 _mat;
|
|
ModelNodeTransform() :
|
|
_t{},
|
|
_r{ 1,0,0,0 },
|
|
_s{ 1,1,1 }
|
|
{
|
|
_mat = Mat::CreateIdentity();
|
|
}
|
|
|
|
Matrix4 GetMatrix() const
|
|
{
|
|
Matrix4 mat_t = Mat::CreateTranslation(_t);
|
|
Matrix4 mat_r = Mat::CreateRotation(_r);
|
|
Matrix4 mat_s = Mat::CreateScaling(_s);
|
|
return mat_t * mat_r * mat_s * _mat;
|
|
}
|
|
};
|
|
|
|
|
|
//
|
|
struct BoundingBox
|
|
{
|
|
Position3 _min;
|
|
Position3 _max;
|
|
bool _vaild;
|
|
public:
|
|
BoundingBox() :
|
|
_vaild{ false }
|
|
{
|
|
}
|
|
BoundingBox(Position3 min, Position3 max) :
|
|
_min{ min },
|
|
_max{ max },
|
|
_vaild{ true }
|
|
{
|
|
}
|
|
|
|
void Add(const BoundingBox& b);
|
|
|
|
BoundingBox getAABB(Matrix4 m);
|
|
};
|
|
|
|
// 模型节点子网格
|
|
struct ModelDrawCall
|
|
{
|
|
ModelTriangleRange _range;
|
|
size_t _indexMaterial;//材质下标,-1代表没有
|
|
|
|
ModelDrawCall() :
|
|
_indexMaterial{ (size_t)-1 }
|
|
{
|
|
}
|
|
};
|
|
|
|
struct ModelPrimitive
|
|
{
|
|
uint32_t _indexBeg;
|
|
uint32_t _indexCount;
|
|
uint32_t _vertexIndex;
|
|
uint32_t _vertexCount;
|
|
uint32_t _indexMaterial;
|
|
BoundingBox _aabb;
|
|
bool _bHasIndices;
|
|
};
|
|
|
|
// 模型网格
|
|
struct ModelMesh
|
|
{
|
|
std::vector<ModelPrimitive> _primitives;
|
|
BoundingBox _aabb;
|
|
};
|
|
|
|
struct ModelSkin;
|
|
// 模型节点
|
|
struct ModelNode
|
|
{
|
|
int _index; // 自己id
|
|
int _indexSkin; // 皮肤下标
|
|
ModelNodeTransform _trans2Parent;
|
|
ModelMesh _mesh; // 网格
|
|
ModelNode* _parent; // 父节点
|
|
|
|
|
|
ModelNode() :
|
|
_index{ -1 },
|
|
_indexSkin{ -1 },
|
|
_parent{ nullptr }
|
|
{
|
|
}
|
|
};
|
|
|
|
class ModelAnimationInstance;
|
|
//! 运行时节点树
|
|
struct ModelNodeRt
|
|
{
|
|
int _index;
|
|
int _indexSkin; // 整个骨骼是在一个节点上
|
|
int _indexParent; // 父节点索引
|
|
ModelNodeTransform _trans2Parent; // 到父节点的变换(具有初始值,动画只改变srt)
|
|
BoundingBox _aabb;
|
|
|
|
bool _bNeedUpdateMatrixWorld;
|
|
bool _bNeedUpdateMatrixLocal;
|
|
Matrix4 _matWorld;
|
|
Matrix4 _matLocal;
|
|
|
|
ModelNodeRt() :
|
|
_index{ -1 },
|
|
_indexSkin{ -1 },
|
|
_indexParent{ -1 },
|
|
_bNeedUpdateMatrixWorld{ true },
|
|
_bNeedUpdateMatrixLocal{ true }
|
|
{
|
|
}
|
|
|
|
void Save(StreamOutput& s);
|
|
void Load(StreamInput& s);
|
|
|
|
// 返回局部矩阵(到父节点,动画设置)
|
|
Matrix4 GetMatrixLocal();
|
|
|
|
// 返回世界矩阵
|
|
Matrix4 GetMatrixWorld(ModelAnimationInstance* instance);
|
|
|
|
void Update(const std::vector<ModelSkin>& all_skin, ModelAnimationInstance* instance);
|
|
};
|
|
|
|
struct ModelSkin
|
|
{
|
|
std::string name;
|
|
int skeletonRoot = -1;
|
|
std::vector<Matrix4> _allOffsetMatrix; // 偏移矩阵(世界坐标系->节点)
|
|
std::vector<int> _allJoint; // 夹具所控制的节点
|
|
|
|
Matrix4 GetOffsetMatrix(int index)
|
|
{
|
|
for (size_t i = 0; i < _allJoint.size(); ++i)
|
|
{
|
|
if (index == _allJoint[i])
|
|
return _allOffsetMatrix[i];
|
|
}
|
|
return Mat::CreateIdentity();
|
|
}
|
|
|
|
void Save(StreamOutput& s);
|
|
void Load(StreamInput& s);
|
|
};
|
|
|
|
// 动画通道
|
|
struct ModelAnimationChannel
|
|
{
|
|
enum PathType { TRANSLATION, ROTATION, SCALE };
|
|
|
|
PathType path;
|
|
int indexNode;
|
|
uint32_t samplerIndex;
|
|
};
|
|
// 动画采样
|
|
struct ModelAnimationSampler
|
|
{
|
|
enum InterpolationType { LINEAR, STEP, CUBICSPLINE };
|
|
|
|
InterpolationType interpolation;
|
|
std::vector<float> inputs;
|
|
std::vector<Position4> outputsVec4;
|
|
std::vector<float> outputs;
|
|
|
|
Position4 cubicSplineInterpolation(size_t index, float time, uint32_t stride) const;
|
|
void translate(size_t index, float time, ModelNodeRt* node) const;
|
|
void scale(size_t index, float time, ModelNodeRt* node) const;
|
|
void rotate(size_t index, float time, ModelNodeRt* node) const;
|
|
|
|
void Save(StreamOutput& s);
|
|
void Load(StreamInput& s);
|
|
};
|
|
|
|
struct ModelAnimation
|
|
{
|
|
std::string _name;
|
|
std::vector<ModelAnimationSampler> _allSampler;
|
|
std::vector<ModelAnimationChannel> _allChannel;
|
|
float start;
|
|
float end;
|
|
|
|
void Save(StreamOutput& s);
|
|
void Load(StreamInput& s);
|
|
};
|
|
|
|
// 模型材质
|
|
struct ModelMaterial
|
|
{
|
|
// 最后转为Model时,变为可以用的着色器资源
|
|
Material _material;
|
|
std::string _materialName;
|
|
// 路径为空,则表示没有(VK加载时会返回0)
|
|
std::string _pathTexDiffuse;
|
|
std::string _pathTexPhysical;
|
|
std::string _pathTexNormal;
|
|
std::string _pathTexAo;
|
|
std::string _pathTexEmissvie;
|
|
|
|
void Save(StreamOutput& s);
|
|
void Load(StreamInput& s);
|
|
};
|
|
|
|
|
|
// 模型数据
|
|
struct ModelData
|
|
{
|
|
NTree<ModelNode*> _allNode; // 所有节点
|
|
std::vector<ModelAnimation> _allAnimation; // 所有动画
|
|
std::vector<ModelSkin> _allSkin; // 所有蒙皮
|
|
std::vector<ModelMaterial> _allMaterial; // 所有材质
|
|
std::vector<ModelDrawCall> _allSub; // 所有网格
|
|
std::vector<uint32_t> _allIndex; // 索引
|
|
std::vector<Vertex> _allVertex; // 顶点
|
|
std::vector<VertexBone> _allVertexSkin; // 顶点
|
|
size_t _indexPos;
|
|
size_t _vertexPos;
|
|
|
|
ModelData() :
|
|
_indexPos{},
|
|
_vertexPos{}
|
|
{
|
|
}
|
|
|
|
~ModelData()
|
|
{
|
|
for (auto& iter : _allNode)
|
|
{
|
|
delete iter->GetValue();
|
|
}
|
|
}
|
|
|
|
void Update();
|
|
};
|
|
|
|
|
|
|
|
} |