Files
dl_old/dl/render/dl_model_object.h
T
2024-08-31 11:25:02 +08:00

198 lines
4.1 KiB
C++

/**
* @file dl_model_object.h
* @brief 中转模型数据
*
*
* @version 1.0
* @author lveyou
* @date 21-03-28
*
* @note 一个模型文件加载后变为多个子模型(它们拥有不同的材质、贴图,但顶点都放在一起,没有变换矩阵)
* @note 子模型必定为单一drawcall,名字为读取到的前加冒号。例如a:b:c,c为b的子节点,b为a的子节点
*/
#pragma once
#ifndef DL_DISABLE_RENDER
#include <string>
#include <unordered_map>
#include "io/dl_file.h"
#include "vk/dl_vertex.h"
#include "memory/dl_stream_input.h"
#include "memory/dl_stream_output.h"
#include "render/dl_model_common.h"
#include "render/dl_model_data.h"
namespace dl
{
//模型对象 材质
struct ModelObjectMaterial
{
//最后转为Model时,变为可以用的着色器资源
Material _material;
std::string _materialName;
//路径为空,则表示没有(VK加载时会返回0)
std::string _pathTexDiffuse;
std::string _pathTexNormal;
void Load(StreamInput& s)
{
s.Read(&_material);
s.Read(&_materialName);
s.Read(&_pathTexDiffuse);
s.Read(&_pathTexNormal);
}
void Save(StreamOutput& s)
{
s.Write(&_material);
s.Write(&_materialName);
s.Write(&_pathTexDiffuse);
s.Write(&_pathTexNormal);
}
};
//子模型对象 范围
struct ModelObjectSub
{
ModelTriangleRange _range;
size_t _indexMaterial;//材质下标,-1代表没有
ModelObjectSub():
_indexMaterial{(size_t)-1}
{
}
};
//一个文件会有多个ModelObject,一个ModelObject根据材质分为多个ModelSub
//注意ModelSub为一个材质,需要读取时合并网格
class ModelObject
{
friend class VK;
friend class ModelLoader;
public:
//从源文件加载模型
static std::vector<ModelObject*> Create(std::string_view path_name, std::string_view res_name);
void Load(std::string_view path_name)
{
/*StreamInput s;
if (!s.LoadFromFile(path_name, true))
{
log_warn("文件加载失败:{}", path_name );
return;
}
s.Read(&_allVertex);
s.Read(&_allVertexBone);
s.Read(&_allIndex);
s.Read(&_allSub);
size_t size;
s.Read64(&size);
_allMaterial.resize(size);
for (size_t i = 0; i < size; ++i)
{
_allMaterial[i].Load(s);
}
bool has_bone;
s.Read(&has_bone);
if (has_bone)
{
_boneData = new BoneData;
_boneData->Load(s);
}
else
_boneData = nullptr;*/
}
//保存到文件
void SaveToFile(std::string_view path_name)
{
/*StreamOutput s;
s.Write(&_allVertex);
s.Write(&_allVertexBone);
s.Write(&_allIndex);
s.Write(&_allSub);
size_t size = _allMaterial.size();
s.Write64(&size);
for (size_t i = 0; i < size; ++i)
{
_allMaterial[i].Save(s);
}
bool has_bone = _boneData;
s.Write(&has_bone);
if (has_bone)
{
_boneData->Save(s);
}
s.Save(path_name, true);*/
}
bool IsHaveTexture()
{
for (auto& iter : _allMaterial)
{
if (!iter._pathTexDiffuse.empty()
|| !iter._pathTexNormal.empty())
return true;
}
return false;
}
//转换贴图路径
void ConvertTexturePath(std::string_view path)
{
//避免重复导出同一张图像
std::unordered_map<std::string, std::string> map_out;
auto fn_output = [&map_out, path](std::string& path_name, size_t i, std::string_view tag)
{
if (path_name.empty())
return;
auto iter = map_out.find(path_name);
if (iter == map_out.end())
{
std::string fmt = File::GetExtension(path_name);
std::string name = fmt::format("{}{}{}.{}", path, tag, i, fmt);
File::Copy(path_name, name, File::CopyMode::OVERRIDE);
map_out[path_name] = name;
//返回path_name前,先记录到表
path_name = name;
}
else
path_name = iter->second;
};
size_t i = 0;
for (auto& iter : _allMaterial)
{
fn_output(iter._pathTexDiffuse, i, "diffuse#");
fn_output(iter._pathTexNormal, i, "normal#");
++i;
}
}
ModelObject() :
_modelData{ nullptr }
{}
private:
std::vector<ModelObjectSub> _allSub;
std::vector<Vertex> _allVertex;//顶点缓存
std::vector<VertexBone> _allVertexBone;//顶点缓存(有骨骼动画时使用)
std::vector<uint32_t> _allIndex;//索引缓存
// 所有材质
std::vector<ModelObjectMaterial> _allMaterial;
ModelData* _modelData;
};
}
#endif