为Material结构与Mesh头建立独立的.h文件,以使转换器和LOADER共用

This commit is contained in:
hyzboy 2019-05-30 19:51:20 +08:00
parent bd77d6d50e
commit d70c564474
4 changed files with 123 additions and 112 deletions

61
inc/hgl/graph/Material.h Normal file
View File

@ -0,0 +1,61 @@
#ifndef HGL_GRAPH_MATERIAL_INCLUDE
#define HGL_GRAPH_MATERIAL_INCLUDE
#include<hgl/graph/TextureType.h>
namespace hgl
{
namespace graph
{
struct MaterialTextureData
{
TextureType type=TextureType::None;
int32 tex_id=-1;
uint8 uvindex=0;
float blend=0;
uint8 op=0;
uint8 wrap_mode[2]={0,0};
};//struct MaterialTextureData
struct MaterialData
{
uint8 tex_count;
MaterialTextureData *tex_list;
Set<int> uv_use;
Color4f diffuse;
Color4f specular;
Color4f ambient;
Color4f emission;
float shininess=0;
bool wireframe=false;
bool two_sided=false;
public:
MaterialData()
{
tex_count=0;
tex_list=nullptr;
}
void Init(const uint32 tc)
{
tex_count=tc;
tex_list=new MaterialTextureData[tc];
}
~MaterialData()
{
delete[] tex_list;
}
};//struct MaterialData
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_MATERIAL_INCLUDE

45
inc/hgl/graph/Mesh.h Normal file
View File

@ -0,0 +1,45 @@
#ifndef HGL_GRAPH_MESH_INCLUDE
#define HGL_GRAPH_MESH_INCLUDE
namespace hgl
{
namespace graph
{
#pragma pack(push,1)
struct MeshFileHeader
{
uint8 flag[4]; ///<'MESH'
uint8 sperator; ///<0x1a
uint8 version; ///<1
uint8 primitive_type; ///<图元类型
uint32 vertices_number; ///<顶点数量
uint32 faces_number; ///<面数量
uint8 color_channels; ///<顶点色数量
uint8 texcoord_channels; ///<纹理坐标数量
uint32 material_index; ///<材质索引
uint8 ntb; ///<ntb信息位合集
uint32 bones_number; ///<骨骼数量
public:
MeshFileHeader()
{
memset(this,0,sizeof(MeshFileHeader));
flag[0]='M';
flag[1]='E';
flag[2]='S';
flag[3]='H';
sperator=0x1A;
version=1;
}
};//struct MeshFileHeader
#pragma pack(pop)
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_MESH_INCLUDE

View File

@ -137,25 +137,25 @@ constexpr VkSamplerAddressMode vk_wrap[4]= //对应aiTextureMapMode
VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE //这个是declas VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE //这个是declas
}; };
void OutMaterialTexture(const MaterialTextureStruct &mt,io::DataOutputStream *dos) void OutMaterialTexture(const MaterialTextureData &mt,io::DataOutputStream *dos)
{ {
dos->WriteUint8(mt.type); dos->WriteUint8((uint8)mt.type);
dos->WriteInt32(mt.tex_id); dos->WriteInt32(mt.tex_id);
dos->WriteUint8(mt.new_uvindex); dos->WriteUint8(mt.uvindex);
dos->WriteFloat(mt.blend); dos->WriteFloat(mt.blend);
dos->WriteUint8(mt.op); dos->WriteUint8(mt.op);
dos->WriteUint8(mt.wrap_mode,2); dos->WriteUint8(mt.wrap_mode,2);
LOG_INFO(OS_TEXT("\tTexture Type: ")+OSString(mt.type)); LOG_INFO(OS_TEXT("\tTexture Type: ")+OSString((uint)mt.type));
LOG_INFO(OS_TEXT("\tTexture ID: ")+OSString(mt.tex_id)); LOG_INFO(OS_TEXT("\tTexture ID: ")+OSString(mt.tex_id));
LOG_INFO(OS_TEXT("\tuvindex: ")+OSString(mt.old_uvindex)); LOG_INFO(OS_TEXT("\tuvindex: ")+OSString(mt.uvindex));
LOG_INFO(OS_TEXT("\tblend: ")+OSString(mt.blend)); LOG_INFO(OS_TEXT("\tblend: ")+OSString(mt.blend));
LOG_INFO(OS_TEXT("\top: ")+OSString(mt.op)); LOG_INFO(OS_TEXT("\top: ")+OSString(mt.op));
LOG_INFO(OS_TEXT("\twrap_mode: ")+OSString(mt.wrap_mode[0])+OS_TEXT(",")+OSString(mt.wrap_mode[1])); LOG_INFO(OS_TEXT("\twrap_mode: ")+OSString(mt.wrap_mode[0])+OS_TEXT(",")+OSString(mt.wrap_mode[1]));
} }
void OutMaterial(const MaterialStruct *ms,const OSString &filename) void OutMaterial(const MaterialData *ms,const OSString &filename)
{ {
io::FileOutputStream fos; io::FileOutputStream fos;
io::LEDataOutputStream dos(&fos); io::LEDataOutputStream dos(&fos);
@ -219,7 +219,7 @@ void AssimpLoader::LoadMaterial()
material_count=scene->mNumMaterials; material_count=scene->mNumMaterials;
material_list=new MaterialStruct[material_count]; material_list=new MaterialData[material_count];
for(unsigned int m=0;m<scene->mNumMaterials;m++) for(unsigned int m=0;m<scene->mNumMaterials;m++)
{ {
@ -236,7 +236,7 @@ void AssimpLoader::LoadMaterial()
tex_count+=mtl->GetTextureCount((aiTextureType)tt); tex_count+=mtl->GetTextureCount((aiTextureType)tt);
} }
MaterialStruct *ms=&(material_list[m]); MaterialData *ms=&(material_list[m]);
ms->Init(tex_count); ms->Init(tex_count);
@ -248,7 +248,7 @@ void AssimpLoader::LoadMaterial()
{ {
mtl->GetTexture((aiTextureType)tt,t,&filename,&tm,&uvindex,&blend,&op,wrap_mode); mtl->GetTexture((aiTextureType)tt,t,&filename,&tm,&uvindex,&blend,&op,wrap_mode);
ms->tex_list[tex_index].type=tt; ms->tex_list[tex_index].type=(TextureType)tt;
ms->uv_use.Add(uvindex); ms->uv_use.Add(uvindex);
@ -282,7 +282,7 @@ void AssimpLoader::LoadMaterial()
ms->tex_list[tex_index].tex_id=tex_id; ms->tex_list[tex_index].tex_id=tex_id;
} }
ms->tex_list[tex_index].old_uvindex=uvindex; ms->tex_list[tex_index].uvindex=uvindex;
ms->tex_list[tex_index].blend=blend; ms->tex_list[tex_index].blend=blend;
ms->tex_list[tex_index].op=op; ms->tex_list[tex_index].op=op;
@ -293,8 +293,6 @@ void AssimpLoader::LoadMaterial()
} }
} }
ms->ProcUVIndex();
set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f); set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f);
if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse)) if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
color4_to_float4(&diffuse, c); color4_to_float4(&diffuse, c);
@ -351,7 +349,7 @@ void AssimpLoader::LoadMaterial()
if(aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)==AI_SUCCESS) if(aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)==AI_SUCCESS)
ms->two_sided=two_sided; ms->two_sided=two_sided;
else else
ms->two_sided=true; ms->two_sided=false;
OutMaterial(ms,main_filename+OS_TEXT(".")+OSString(m)+OS_TEXT(".material")); OutMaterial(ms,main_filename+OS_TEXT(".")+OSString(m)+OS_TEXT(".material"));
LOG_BR; LOG_BR;
@ -521,7 +519,7 @@ void AssimpLoader::LoadMesh()
if(pn!=3) if(pn!=3)
continue; continue;
MaterialStruct *mtl=&(material_list[mesh->mMaterialIndex]); MaterialData *mtl=&(material_list[mesh->mMaterialIndex]);
const uint uv_channels=mtl->uv_use.GetCount(); const uint uv_channels=mtl->uv_use.GetCount();
@ -538,7 +536,7 @@ void AssimpLoader::LoadMesh()
} }
{ {
MeshStruct ms; MeshFileHeader ms;
ms.primitive_type =PRIM_TRIANGLES; ms.primitive_type =PRIM_TRIANGLES;
ms.vertices_number =mesh->mNumVertices; ms.vertices_number =mesh->mNumVertices;
@ -557,7 +555,7 @@ void AssimpLoader::LoadMesh()
ms.bones_number =mesh->mNumBones; ms.bones_number =mesh->mNumBones;
fos.WriteFully(&ms,sizeof(MeshStruct)); fos.WriteFully(&ms,sizeof(MeshFileHeader));
} }
if(mesh->HasPositions()) if(mesh->HasPositions())

View File

@ -8,6 +8,8 @@
#include<hgl/graph/VertexBuffer.h> #include<hgl/graph/VertexBuffer.h>
#include<hgl/graph/SceneNode.h> #include<hgl/graph/SceneNode.h>
#include<hgl/graph/NTB.h> #include<hgl/graph/NTB.h>
#include<hgl/graph/Material.h>
#include<hgl/graph/Mesh.h>
#include<hgl/io/FileOutputStream.h> #include<hgl/io/FileOutputStream.h>
#include<assimp/Importer.hpp> #include<assimp/Importer.hpp>
#include<assimp/scene.h> #include<assimp/scene.h>
@ -15,101 +17,6 @@
using namespace hgl; using namespace hgl;
using namespace hgl::graph; using namespace hgl::graph;
#pragma pack(push,1)
struct MaterialTextureStruct
{
uint8 type=0;
int32 tex_id=-1;
uint32 old_uvindex=0;
uint32 new_uvindex=0;
float blend=0;
uint8 op=0;
uint8 wrap_mode[2]={0,0};
};//
struct MaterialStruct
{
uint32 tex_count;
MaterialTextureStruct *tex_list;
Set<int> uv_use;
Color4f diffuse;
Color4f specular;
Color4f ambient;
Color4f emission;
float shininess=0;
bool wireframe=false;
bool two_sided=false;
public:
MaterialStruct()
{
tex_count=0;
tex_list=nullptr;
}
void Init(const uint32 tc)
{
tex_count=tc;
tex_list=new MaterialTextureStruct[tc];
}
~MaterialStruct()
{
delete[] tex_list;
}
void ProcUVIndex()
{
for(uint i=0;i<tex_count;i++)
tex_list[i].new_uvindex=i;
}
};
struct MeshStruct
{
uint8 flag[4]; ///<'MESH'
uint8 sperator; ///<0x1a
uint8 version; ///<1
uint8 primitive_type; ///<图元类型
uint32 vertices_number; ///<顶点数量
uint32 faces_number; ///<面数量
uint8 color_channels; ///<顶点色数量
uint8 texcoord_channels; ///<纹理坐标数量
uint32 material_index; ///<材质索引
uint8 ntb; ///<ntb信息位合集
uint32 bones_number; ///<骨骼数量
public:
MeshStruct()
{
memset(this,0,sizeof(MeshStruct));
flag[0]='M';
flag[1]='E';
flag[2]='S';
flag[3]='H';
sperator=0x1A;
version=1;
}
};
#pragma pack(pop)
class AssimpLoader class AssimpLoader
{ {
OSString main_filename; OSString main_filename;
@ -127,7 +34,7 @@ private:
UTF8StringList tex_list; UTF8StringList tex_list;
int material_count; int material_count;
MaterialStruct *material_list; MaterialData *material_list;
private: private: