From c5821a8026d7970916b7d570f2d534a19aa2bed9 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 17 Jun 2020 02:05:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4Assimp=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 3 - 3rdpty/assimp | 1 - CMCore | 2 +- CMakeLists.txt | 2 - example/Vulkan/AssimpLoader.cpp | 748 ---------------------------- example/Vulkan/AssimpLoader.h | 68 --- example/Vulkan/AssimpLoaderMesh.cpp | 330 ------------ example/Vulkan/AssimpLoaderMesh.h | 54 -- example/Vulkan/CMakeLists.txt | 12 - res | 2 +- 10 files changed, 2 insertions(+), 1220 deletions(-) delete mode 160000 3rdpty/assimp delete mode 100644 example/Vulkan/AssimpLoader.cpp delete mode 100644 example/Vulkan/AssimpLoader.h delete mode 100644 example/Vulkan/AssimpLoaderMesh.cpp delete mode 100644 example/Vulkan/AssimpLoaderMesh.h diff --git a/.gitmodules b/.gitmodules index 517dd228..5bbf455f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,9 +7,6 @@ [submodule "3rdpty/jsoncpp"] path = 3rdpty/jsoncpp url = https://github.com/open-source-parsers/jsoncpp -[submodule "3rdpty/assimp"] - path = 3rdpty/assimp - url = https://github.com/assimp/assimp.git [submodule "CMCMakeModule"] path = CMCMakeModule url = https://github.com/hyzboy/CMCMakeModule diff --git a/3rdpty/assimp b/3rdpty/assimp deleted file mode 160000 index ab0dcdc3..00000000 --- a/3rdpty/assimp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ab0dcdc3216115d039c51cc5b02ca23cd3db3fba diff --git a/CMCore b/CMCore index 73ca4a13..e9798719 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 73ca4a13d2b662fde9e876909e9f92b50610aeb3 +Subproject commit e9798719710b05bb352fe1e70ea59f18f67371ef diff --git a/CMakeLists.txt b/CMakeLists.txt index 3614eb1b..fa9bd8e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,6 @@ SET(ULRE CMCore ${RENDER_LIBRARY} ${Vulkan_LIBRARIES}) -include_directories(${ULRE_3RDPTY_ROOT_PATH}/assimp/include) include_directories(${ULRE_3RDPTY_ROOT_PATH}/NvTriStrip) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc) @@ -47,7 +46,6 @@ SET(ROOT_INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/inc) SET(ULRE_RUNTIME_PATH ${CMAKE_CURRENT_SOURCE_DIR}) -add_subdirectory(3rdpty/assimp) add_subdirectory(src) add_subdirectory(example) diff --git a/example/Vulkan/AssimpLoader.cpp b/example/Vulkan/AssimpLoader.cpp deleted file mode 100644 index 3f00c136..00000000 --- a/example/Vulkan/AssimpLoader.cpp +++ /dev/null @@ -1,748 +0,0 @@ -#include"AssimpLoader.h" -#include -#include -#include -#include -#include -#include - -using namespace hgl; -using namespace hgl::graph; - -namespace -{ - #define LOG_BR LOG_INFO(OS_TEXT("------------------------------------------------------------")); - - #define aisgl_min(x,y) (xx?y:x) - - const Color4f COLOR_PURE_BLACK(0,0,0,1); - const Color4f COLOR_PURE_WHITE(1,1,1,1); -} - -void AssimpLoader::get_bounding_box_for_node (const aiNode* nd, - aiVector3D* min, - aiVector3D* max, - aiMatrix4x4* trafo) -{ - aiMatrix4x4 prev; - unsigned int n = 0, t; - - prev = *trafo; - aiMultiplyMatrix4(trafo,&nd->mTransformation); - - for (; n < nd->mNumMeshes; ++n) - { - const aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]]; - - for (t = 0; t < mesh->mNumVertices; ++t) - { - aiVector3D tmp = mesh->mVertices[t]; - aiTransformVecByMatrix4(&tmp,trafo); - - min->x = aisgl_min(min->x,tmp.x); - min->y = aisgl_min(min->y,tmp.y); - min->z = aisgl_min(min->z,tmp.z); - - max->x = aisgl_max(max->x,tmp.x); - max->y = aisgl_max(max->y,tmp.y); - max->z = aisgl_max(max->z,tmp.z); - } - } - - for (n = 0; n < nd->mNumChildren; ++n) - get_bounding_box_for_node(nd->mChildren[n],min,max,trafo); - - *trafo = prev; -} - -void AssimpLoader::get_bounding_box (const aiNode *node,aiVector3D* min, aiVector3D* max) -{ - aiMatrix4x4 trafo; - - aiIdentityMatrix4(&trafo); - - min->x = min->y = min->z = 1e10f; - max->x = max->y = max->z = -1e10f; - - get_bounding_box_for_node(node,min,max,&trafo); -} - -AssimpLoader::AssimpLoader() -{ - scene=0; - material_list=nullptr; -} - -AssimpLoader::~AssimpLoader() -{ - delete[] material_list; - - if(scene) - aiReleaseImport(scene); -} - -//void AssimpLoader::SaveFile(const void *data,const uint &size,const OSString &ext_name) -//{ -// if(!data||size<=0)return; -// -// OSString filename=main_filename+OS_TEXT(".")+ext_name; -// -// if(filesystem::SaveMemoryToFile(filename,data,size)==size) -// { -// LOG_INFO(OS_TEXT("SaveFile: ")+filename+OS_TEXT(" , size: ")+OSString(size)); -// -// total_file_bytes+=size; -// } -//} -// -//void AssimpLoader::SaveFile(void **data,const int64 *size,const int &count,const OSString &ext_name) -//{ -// if(!data||size<=0)return; -// -// OSString filename=main_filename+OS_TEXT(".")+ext_name; -// -// int64 result=filesystem::SaveMemoryToFile(filename,data,size,count); -// -// if(result>0) -// { -// LOG_INFO(OS_TEXT("SaveFile: ")+filename+OS_TEXT(" , size: ")+OSString(result)); -// -// total_file_bytes+=result; -// } -//} -// -//void OutFloat3(const OSString &front,const Color4f &c) -//{ -// LOG_INFO(front+OS_TEXT(": ")+OSString(c.r) -// +OS_TEXT("," )+OSString(c.g) -// +OS_TEXT("," )+OSString(c.b)); -//} - -constexpr VkSamplerAddressMode vk_wrap[4]= //对应aiTextureMapMode,注意最后一个其实不对 -{ - VK_SAMPLER_ADDRESS_MODE_REPEAT, - VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, - VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT, - VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE //这个是declas -}; - -//void OutMaterialTexture(const MaterialTextureData &mt,io::DataOutputStream *dos) -//{ -// dos->WriteUint8((uint8)mt.type); -// dos->WriteInt32(mt.tex_id); -// dos->WriteUint8(mt.uvindex); -// dos->WriteFloat(mt.blend); -// dos->WriteUint8(mt.op); -// dos->WriteUint8(mt.wrap_mode,2); -// -// LOG_INFO(OS_TEXT("\tTexture Type: ")+OSString((uint)mt.type)); -// LOG_INFO(OS_TEXT("\tTexture ID: ")+OSString(mt.tex_id)); -// -// LOG_INFO(OS_TEXT("\tuvindex: ")+OSString(mt.uvindex)); -// LOG_INFO(OS_TEXT("\tblend: ")+OSString(mt.blend)); -// 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])); -//} -// -//void OutMaterial(const MaterialData *ms,const OSString &filename) -//{ -// io::FileOutputStream fos; -// io::LEDataOutputStream dos(&fos); -// -// fos.CreateTrunc(filename); -// -// dos.WriteFully("Material\x1A\x1",10); -// dos.WriteFloat(ms->diffuse,3); -// dos.WriteFloat(ms->specular,3); -// dos.WriteFloat(ms->ambient,3); -// dos.WriteFloat(ms->emission,3); -// dos.WriteFloat(ms->shininess); -// dos.WriteBool(ms->wireframe); -// dos.WriteBool(ms->two_sided); -// dos.WriteUint8(ms->tex_count); -// -// OutFloat3(OSString(OS_TEXT("diffuse")),ms->diffuse); -// OutFloat3(OSString(OS_TEXT("specular")),ms->specular); -// OutFloat3(OSString(OS_TEXT("ambient")),ms->ambient); -// OutFloat3(OSString(OS_TEXT("emission")),ms->emission); -// -// LOG_INFO(OS_TEXT("shininess: ")+OSString(ms->shininess)); -// -// LOG_INFO(OS_TEXT("wireframe: ")+OSString(ms->wireframe?OS_TEXT("true"):OS_TEXT("false"))); -// LOG_INFO(OS_TEXT("two_sided: ")+OSString(ms->two_sided?OS_TEXT("true"):OS_TEXT("false"))); -// -// LOG_INFO(OS_TEXT("Material Texture Count ")+OSString(ms->tex_count)); -// -// for(int i=0;itex_count;i++) -// OutMaterialTexture(ms->tex_list[i],&dos); -// -// fos.Close(); -//} - -bool GetMaterialBool(const aiMaterial *mtl,const char *key,unsigned int type,unsigned int index,const bool default_value) -{ - int value; - - if(aiGetMaterialInteger(mtl, key,type,index, &value)==AI_SUCCESS) - return value; - else - return default_value; -} - -uint GetMaterialInteger(const aiMaterial *mtl,const char *key,unsigned int type,unsigned int index,const uint default_value) -{ - int value; - - if(aiGetMaterialInteger(mtl, key,type,index, &value)==AI_SUCCESS) - return value; - else - return default_value; -} - -float GetMaterialFloat(const aiMaterial *mtl,const char *key,unsigned int type,unsigned int index,const float default_value) -{ - float value; - - if(aiGetMaterialFloat(mtl, key,type,index, &value)==AI_SUCCESS) - return value; - else - return default_value; -} - -Color4f GetMaterialColor(const aiMaterial *mtl,const char *key,unsigned int type,unsigned int index,const Color4f &default_value) -{ - unsigned int max = 1; - aiColor4D color; - - if(aiGetMaterialColor(mtl, key,type,index, &color)==AI_SUCCESS) - return Color4f(color.r,color.g,color.b,color.a); - else - return default_value; -} - -//bool AssimpLoader::Convert(MaterialData &md,aiMaterial *mtl) -//{ -// uint tex_count=0; -// uint tt_count=0; -// int32 tex_id; -// aiReturn found=AI_SUCCESS; -// -// md.name=mtl->GetName().C_Str(); -// -// for(uint tt=aiTextureType_DIFFUSE;ttGetTextureCount((aiTextureType)tt); -// -// md.Init(tex_count); -// -// MaterialTextureData *mtd=md.tex_list; -// -// for(uint tt=aiTextureType_DIFFUSE;ttGetTextureCount((aiTextureType)tt); -// -// for(int t=0;tGetTexture((aiTextureType)tt,t,&filename,&tm,&uvindex,&blend,&op,wrap_mode); -// -// mtd->type=(TextureType)tt; -// -// md.uv_use.Add(uvindex); -// -// { -// char *fn=strrchr(filename.data,'\\'); -// char *ext; -// -// if(!fn) -// fn=strrchr(filename.data,'/'); -// else -// ++fn; -// -// if(fn) -// ++fn; -// else -// fn=filename.data; -// -// ext=strrchr(fn,'.'); -// -// if(ext)*ext=0; -// -// #if HGL_OS == HGL_OS_Window -// tex_id=tex_list.CaseFind(fn); -// #else -// tex_id=tex_list.Find(fn); -// #endif// -// -// if(tex_id==-1) -// tex_id=tex_list.Add(fn); -// -// mtd->tex_id=tex_id; -// } -// -// mtd->uvindex=uvindex; -// mtd->blend=blend; -// mtd->op=op; -// -// mtd->wrap_mode[0]=vk_wrap[wrap_mode[0]]; -// mtd->wrap_mode[1]=vk_wrap[wrap_mode[1]]; -// -// ++mtd; -// } -// } -// -// md.two_sided =GetMaterialBool (mtl,AI_MATKEY_TWOSIDED, true); -// md.shading_model =GetMaterialInteger (mtl,AI_MATKEY_SHADING_MODEL, aiShadingMode_Phong); -// md.wireframe =GetMaterialBool (mtl,AI_MATKEY_ENABLE_WIREFRAME,false); -// // md.blend_func = -// md.opacity =GetMaterialFloat(mtl,AI_MATKEY_OPACITY, 0); -// // md.transparency_factor = -// -// md.bump_scaling =GetMaterialFloat(mtl,AI_MATKEY_BUMPSCALING, 0); -// md.shininess =GetMaterialFloat(mtl,AI_MATKEY_SHININESS, 0); -// md.shininess_strength =GetMaterialFloat(mtl,AI_MATKEY_SHININESS_STRENGTH, 0); -// md.reflectivity =GetMaterialFloat(mtl,AI_MATKEY_REFLECTIVITY, 0); -// md.refracti =GetMaterialFloat(mtl,AI_MATKEY_REFRACTI, 0); -// -// md.diffuse =GetMaterialColor(mtl,AI_MATKEY_COLOR_DIFFUSE, COLOR_PURE_WHITE); -// md.ambient =GetMaterialColor(mtl,AI_MATKEY_COLOR_AMBIENT, Color4f(0.2,0.2,0.2,1.0)); -// md.specular =GetMaterialColor(mtl,AI_MATKEY_COLOR_SPECULAR, COLOR_PURE_BLACK); -// md.emission =GetMaterialColor(mtl,AI_MATKEY_COLOR_EMISSIVE, COLOR_PURE_BLACK); -// -// md.transparent =GetMaterialColor(mtl,AI_MATKEY_COLOR_TRANSPARENT, COLOR_PURE_BLACK); -// md.reflective =GetMaterialColor(mtl,AI_MATKEY_COLOR_REFLECTIVE, COLOR_PURE_BLACK); -// -// //OutMaterial(ms,main_filename+OS_TEXT(".")+OSString(m)+OS_TEXT(".material")); -// LOG_BR; -//} - -//void AssimpLoader::LoadMaterial() -//{ -// if(!scene->HasMaterials())return; -// -// LOG_BR; -// LOG_INFO(OS_TEXT("Material Count ")+OSString(scene->mNumMaterials)); -// LOG_BR; -// -// aiString filename; -// aiTextureMapping tm; -// uint uvindex=0; -// float blend; -// aiTextureOp op; -// aiTextureMapMode wrap_mode[2]; -// -// material_count=scene->mNumMaterials; -// -// material_list=new MaterialData[material_count]; -// -// for(uint m=0;mmNumMaterials;m++) -// Convert(material_list[m],scene->mMaterials[m]); -//} - -//void AssimpLoader::SaveTextures() -//{ -// const int tex_count=tex_list.GetCount(); -// -// LOG_INFO(OS_TEXT("TOTAL Texture Count: ")+OSString(tex_count)); -// -// if(tex_count<=0) -// return; -// -// for(int i=0;i -//void AssimpLoader::SaveFaces(io::FileOutputStream *fos,const aiFace *face,const T count) -//{ -// T *data=new T[count*3]; -// -// const aiFace *sp=face; -// T *tp=data; -// -// for(T i=0;imIndices[0];++tp; -// *tp=sp->mIndices[1];++tp; -// *tp=sp->mIndices[2];++tp; -// -// ++sp; -// } -// -// fos->WriteFully(data,sizeof(T)*3*count); -// -// delete[] data; -//} -// -//void AssimpLoader::SaveTexCoord(float *tp,const aiVector3D *texcoord,const uint comp,const uint count) -//{ -// if(comp<=0||comp>3) -// return; -// -// const uint size=1+sizeof(float)*comp*count; -// -// const aiVector3D *sp=texcoord; -// -// if(comp==1) -// { -// for(uint i=0;ix;++tp; -// ++sp; -// } -// } -// else -// if(comp==2) -// { -// for(uint i=0;ix;++tp; -// *tp=sp->y;++tp; -// ++sp; -// } -// } -// else -// if(comp==3) -// { -// for(uint i=0;ix;++tp; -// *tp=sp->y;++tp; -// *tp=sp->z;++tp; -// ++sp; -// } -// } -//} - -void c4f_to_4b(uint8 *tp,const aiColor4D *c4,const int count) -{ - for(int i=0;ir*255;++tp; - *tp=c4->g*255;++tp; - *tp=c4->b*255;++tp; - *tp=c4->a*255;++tp; - } -} - -void AxisY2Z(aiVector3D *v,const uint32 count) -{ - ai_real t; - - for(uint32 i=0;iy; - v->y=-(v->z); - v->z=t; - - ++v; - } -} - -void AssimpLoader::LoadMesh() -{ - if(!scene->HasMeshes())return; - - UTF8String mesh_name; - - total_file_bytes=0; - - LOG_BR; - LOG_INFO(OS_TEXT("Mesh Count ")+OSString(scene->mNumMeshes)); - - uint vertex_total=0; - uint tri_total=0; - uint bone_total=0; - - for(unsigned int i=0;imNumMeshes;i++) - { - const aiMesh *mesh=scene->mMeshes[i]; - - const uint v3fdata_size=mesh->mNumVertices*3*sizeof(float); - const uint v4fdata_size=mesh->mNumVertices*4*sizeof(float); - - if(mesh->mName.length) - mesh_name=U8_TEXT("Mesh [")+UTF8String(i)+U8_TEXT(":")+UTF8String(mesh->mName.C_Str())+U8_TEXT("]"); - else - mesh_name=U8_TEXT("Mesh [")+UTF8String(i)+U8_TEXT("]"); - - int pn=mesh->mFaces[0].mNumIndices; - - LOG_BR; - - LOG_INFO(mesh_name+U8_TEXT(" PrimitiveTypes is ")+UTF8String(mesh->mPrimitiveTypes)); - - LOG_INFO(mesh_name+U8_TEXT(" vertex color count is ")+UTF8String(mesh->GetNumColorChannels())); - LOG_INFO(mesh_name+U8_TEXT(" texcoord count is ")+UTF8String(mesh->GetNumUVChannels())); - - if(mesh->GetNumUVChannels()>0) - LOG_INFO(mesh_name+U8_TEXT(" Material Index is ")+UTF8String(mesh->mMaterialIndex)); - - if(mesh->HasPositions())LOG_INFO(mesh_name+U8_TEXT(" have Position, vertices count ")+UTF8String(mesh->mNumVertices)); - if(mesh->HasFaces())LOG_INFO(mesh_name+U8_TEXT(" have Faces,faces count ")+UTF8String(mesh->mNumFaces)); - if(mesh->HasNormals())LOG_INFO(mesh_name+U8_TEXT(" have Normals")); - if(mesh->HasTangentsAndBitangents())LOG_INFO(mesh_name+U8_TEXT(" have Tangent & Bitangents")); - if(mesh->HasBones())LOG_INFO(mesh_name+U8_TEXT(" have Bones, Bones Count ")+UTF8String(mesh->mNumBones)); - - vertex_total+=mesh->mNumVertices; - tri_total+=mesh->mNumFaces; - bone_total+=mesh->mNumBones; - - LOG_INFO(mesh_name+U8_TEXT(" PN is ")+UTF8String(pn)); - - if(pn!=3) - continue; - - MaterialData *mtl=&(material_list[mesh->mMaterialIndex]); - - const uint uv_channels=mtl->uv_use.GetCount(); - - LOG_INFO(mesh_name+U8_TEXT(" use UV Channels is ")+UTF8String(uv_channels)); - - io::FileOutputStream fos; - - OSString mesh_filename=main_filename+OS_TEXT(".")+OSString(i)+OS_TEXT(".mesh"); - - if(!fos.CreateTrunc(mesh_filename)) - { - LOG_INFO(OS_TEXT("Create Mesh file Failed,filename: ")+mesh_filename); - continue; - } - - { - MeshFileHeader ms; - - ms.primitive_type =PRIM_TRIANGLES; - ms.vertices_number =mesh->mNumVertices; - ms.faces_number =mesh->mNumFaces; - ms.color_channels =mesh->GetNumColorChannels(); - ms.texcoord_channels=uv_channels; - ms.material_index =mesh->mMaterialIndex; - - if(mesh->HasNormals()) - { - if(mesh->HasTangentsAndBitangents()) - ms.ntb=NTB_BIT_ALL; - else - ms.ntb=NTB_BIT_NORMAL; - } - - ms.bones_number =mesh->mNumBones; - - fos.WriteFully(&ms,sizeof(MeshFileHeader)); - } - - if(mesh->HasPositions()) - { - AxisY2Z(mesh->mVertices,mesh->mNumVertices); - fos.WriteFully(mesh->mVertices,v3fdata_size); - } - - if(mesh->HasNormals()) - { - AxisY2Z(mesh->mNormals,mesh->mNumVertices); - fos.WriteFully(mesh->mNormals,v3fdata_size); - - if(mesh->HasTangentsAndBitangents()) - { - AxisY2Z(mesh->mTangents,mesh->mNumVertices); - AxisY2Z(mesh->mBitangents,mesh->mNumVertices); - - fos.WriteFully(mesh->mTangents,v3fdata_size); - fos.WriteFully(mesh->mBitangents,v3fdata_size); - } - } - - if(mesh->GetNumColorChannels()>0) - { - const int64 vertex_color_size=mesh->mNumVertices*4*mesh->GetNumColorChannels(); - - uint8 *vertex_color=new uint8[vertex_color_size]; - uint8 *tp=vertex_color; - - for(int c=0;cGetNumColorChannels();c++) - { - c4f_to_4b(tp,mesh->mColors[c],mesh->mNumVertices); - tp+=mesh->mNumVertices*4; - } - - fos.WriteFully(vertex_color,vertex_color_size); - - delete[] vertex_color; - } - - if(mtl->uv_use.GetCount()>0 - &&mesh->GetNumUVChannels()) - { - int tc=0; - int comp_total=0; - uint *uv_use=mtl->uv_use.GetData(); - - //这里要重新审视数据来源,并不是每一个纹理通道都有数据,并且和材质对应。 - //、、材质中的uv index具体对应啥 还不是很清楚 - - for(int c=0;c=mesh->GetNumUVChannels()) - uv_use[c]=0; - - comp_total+=mesh->mNumUVComponents[uv_use[c]]; - } - - comp_total*=mesh->mNumVertices; - - uint64 tc_size=mesh->GetNumUVChannels()+comp_total*sizeof(float); - - uint8 *tc_data=new uint8[tc_size]; - float *tp=(float *)(tc_data+mesh->GetNumUVChannels()); - for(int c=0;cmNumUVComponents[uv_use[c]]; - -// SaveTexCoord(tp,mesh->mTextureCoords[c],mesh->mNumUVComponents[c],mesh->mNumVertices); - - tp+=mesh->mNumUVComponents[c]*mesh->mNumVertices; - } - - fos.WriteFully(tc_data,tc_size); - - delete[] tc_data; - } - - if(mesh->HasFaces()) - { - //if(mesh->mNumVertices>0xFFFF) - // SaveFaces(&fos,mesh->mFaces,mesh->mNumFaces); - //else - // SaveFaces(&fos,mesh->mFaces,mesh->mNumFaces); - } - - fos.Close(); - } - - LOG_BR; - LOG_INFO(OS_TEXT("TOTAL Vertices Count ")+OSString(vertex_total)); - LOG_INFO(OS_TEXT("TOTAL Faces Count ")+OSString(tri_total)); - LOG_INFO(OS_TEXT("TOTAL Bones Count ")+OSString(bone_total)); - - LOG_INFO(OS_TEXT("TOTAL File Size ")+OSString(total_file_bytes)); - LOG_BR; -} - -void AssimpLoader::LoadScene(const UTF8String &front,io::DataOutputStream *dos,const aiNode *nd) -{ - aiVector3D bb_min,bb_max; - float box[6]; - get_bounding_box(nd,&bb_min,&bb_max); - - box[0]=bb_min.x;box[1]=bb_min.y;box[2]=bb_min.z; - box[3]=bb_max.x;box[4]=bb_max.y;box[5]=bb_max.z; - - dos->WriteUTF8String(nd->mName.C_Str()); - dos->WriteFloat(box,6); - - dos->WriteFloat((float *)&(nd->mTransformation),16); - - dos->WriteUint32(nd->mNumMeshes); - - LOG_INFO(front+U8_TEXT("Node[")+UTF8String(nd->mName.C_Str())+U8_TEXT("][Mesh:")+UTF8String(nd->mNumMeshes)+U8_TEXT("][SubNode:")+UTF8String(nd->mNumChildren)+U8_TEXT("]")) - - const UTF8String new_front=U8_TEXT(" ")+front; - - if(nd->mNumMeshes>0) - { - dos->WriteUint32(nd->mMeshes,nd->mNumMeshes); - - for(unsigned int i=0;imNumMeshes;i++) - { - const aiMesh *mesh=scene->mMeshes[nd->mMeshes[i]]; - - LOG_INFO(new_front+U8_TEXT("Mesh[")+UTF8String(i)+U8_TEXT("] ")+UTF8String(mesh->mName.C_Str())); - } - } - - dos->WriteUint32(nd->mNumChildren); - - for(unsigned int n=0;nmNumChildren;++n) - LoadScene(new_front,dos,nd->mChildren[n]); -} - -bool AssimpLoader::LoadFile(const OSString &filename) -{ - uint filesize; - char *filedata; - - filesize=filesystem::LoadFileToMemory(filename,(void **)&filedata); - - if(!filedata) - return(false); - -#ifdef _WIN32 - const UTF8String ext_name=ToUTF8String(filesystem::ClipFileExtName(filename)); -#else - const UTF8String ext_name=filesystem::ClipFileExtName(filename); -#endif// - - scene=aiImportFileFromMemory(filedata,filesize,aiProcessPreset_TargetRealtime_MaxQuality|aiProcess_FlipUVs,ext_name.c_str()); - - delete[] filedata; - - if(!scene) - return(false); - - filename.SubString(main_filename,0,filename.FindRightChar(OS_TEXT('.'))); - - get_bounding_box(scene->mRootNode,&scene_min,&scene_max); - - scene_center.x = (scene_min.x + scene_max.x) / 2.0f; - scene_center.y = (scene_min.y + scene_max.y) / 2.0f; - scene_center.z = (scene_min.z + scene_max.z) / 2.0f; - - //这个顺序是假设了所有的材质和模型都有被使用 - //aiProcessPreset_TargetRealtime_Quality已经指定了会删除多余材质,所以这里不用处理。 - //但多余模型并不确定是否存在 - - io::FileOutputStream fos; - io::LEDataOutputStream dos(&fos); - - OSString scene_filename=main_filename+OS_TEXT(".scene"); - - if(!fos.CreateTrunc(scene_filename)) - { - LOG_INFO(OS_TEXT("Create Scene file Failed,filename: ")+scene_filename); - return(false); - } - - dos.WriteFully("SCENE\x1a\x01",7); - - dos.WriteUint32(scene->mNumMaterials); - dos.WriteUint32(scene->mNumMeshes); - - LoadMaterial(); //载入所有材质 - - dos.WriteUint32(tex_list.GetCount()); - - LoadMesh(); //载入所有mesh - LoadScene(UTF8String(""),&dos,scene->mRootNode); //载入场景节点 - - fos.Close(); - - //SaveTextures(); - - return(true); -} diff --git a/example/Vulkan/AssimpLoader.h b/example/Vulkan/AssimpLoader.h deleted file mode 100644 index 59744249..00000000 --- a/example/Vulkan/AssimpLoader.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef HGL_TOOL_MODEL_CONVERT_ASSIMP_LOADER_INCLUDE -#define HGL_TOOL_MODEL_CONVERT_ASSIMP_LOADER_INCLUDE - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace hgl; -using namespace hgl::graph; - -class AssimpLoader -{ - OSString main_filename; - - uint total_file_bytes; - - const aiScene *scene; - -public: - - aiVector3D scene_min, scene_max, scene_center; - -private: - - UTF8StringList tex_list; - - int material_count; - MaterialData *material_list; - -private: - - void get_bounding_box_for_node(const aiNode *,aiVector3D *,aiVector3D *,aiMatrix4x4 *); - void get_bounding_box(const aiNode *,aiVector3D *,aiVector3D *); - - //bool Convert(MaterialData &md,aiMaterial *mtl); - - //void LoadMaterial(); - void LoadMesh(); - void LoadScene(const UTF8String &,io::DataOutputStream *,const aiNode *); - - //void SaveFile(const void *,const uint &,const OSString &); - //void SaveFile(void **,const int64 *,const int &,const OSString &); - - //void SaveTextures(); - - //template - //void SaveFaces(io::FileOutputStream *,const aiFace *,const T); - - //void SaveTexCoord(float *,const aiVector3D *,const uint,const uint); - -public: - - AssimpLoader(); - ~AssimpLoader(); - - bool LoadFile(const OSString &); - //bool SaveFile(const OSString &); -};//class AssimpLoader -#endif//HGL_TOOL_MODEL_CONVERT_ASSIMP_LOADER_INCLUDE diff --git a/example/Vulkan/AssimpLoaderMesh.cpp b/example/Vulkan/AssimpLoaderMesh.cpp deleted file mode 100644 index 873c04f5..00000000 --- a/example/Vulkan/AssimpLoaderMesh.cpp +++ /dev/null @@ -1,330 +0,0 @@ -#include"AssimpLoaderMesh.h" -#include -#include -#include -#include -#include -#include -#include - -using namespace hgl::filesystem; - -namespace -{ - const aiScene *LoadSceneFromFile(const OSString &filename) - { - if(filename.IsEmpty())return(nullptr); - - uint filesize; - char *filedata; - - filesize=filesystem::LoadFileToMemory(filename,(void **)&filedata); - - if(!filedata) - return(nullptr); - - #ifdef _WIN32 - const UTF8String ext_name=ToUTF8String(filesystem::ClipFileExtName(filename,false)); - #else - const UTF8String ext_name=filesystem::ClipFileExtName(filename); - #endif// - - const aiScene *scene=aiImportFileFromMemory(filedata,filesize,aiProcessPreset_TargetRealtime_MaxQuality|aiProcess_FlipUVs|aiProcess_FlipWindingOrder|aiProcess_Triangulate,ext_name.c_str()); - - delete[] filedata; - - if(!scene) - return(nullptr); - - return scene; - } - - #define LOG_BR LOG_INFO(OS_TEXT("------------------------------------------------------------")); - - #define aisgl_min(x,y) (xx?y:x) - - const Color4f COLOR_PURE_BLACK(0,0,0,1); - const Color4f COLOR_PURE_WHITE(1,1,1,1); - - inline const vec pos_to_vec(const aiVector3D &v3d){return vec(v3d.x,v3d.y,v3d.z,1.0);} - - void VecCopy(List &target,const aiVector3D *source,const uint count) - { - target.SetCount(count); - - if(count<=0)return; - - Vector3f *tp=target.GetData(); - - for(uint i=0;ix=source->x; - tp->y=source->y; - tp->z=source->z; - - ++source; - ++tp; - } - } - - uint8 *ColorF2B(uint8 *tp,const aiColor4D *c4,const int count) - { - for(int i=0;ir*255;++tp; - *tp=c4->g*255;++tp; - *tp=c4->b*255;++tp; - *tp=c4->a*255;++tp; - - ++c4; - } - - return tp; - } - - template - void Face2Indices(List &indices,const aiFace *face,const uint count) - { - indices.SetCount(count*3); - - T *tp=indices.GetData(); - - for(uint i=0;imIndices[0];++tp; - *tp=face->mIndices[1];++tp; - *tp=face->mIndices[2];++tp; - - ++face; - } - } - - AABB GetAABB(const List &pos) - { - const Vector3f *vertex=pos.GetData(); - const uint count=pos.GetCount(); - - Vector3f minPoint=*vertex; - Vector3f maxPoint=*vertex; - - ++vertex; - for(uint i=1;ix); - minPoint.y = aisgl_min(minPoint.y,vertex->y); - minPoint.z = aisgl_min(minPoint.z,vertex->z); - - maxPoint.x = aisgl_max(maxPoint.x,vertex->x); - maxPoint.y = aisgl_max(maxPoint.y,vertex->y); - maxPoint.z = aisgl_max(maxPoint.z,vertex->z); - - ++vertex; - } - - return AABB(POINT_VEC(minPoint),POINT_VEC(maxPoint)); - } - - Matrix4f MatrixConvert(const aiMatrix4x4 &s) - { - return Matrix4f(s.a1,s.a2,s.a3,s.a4, - s.b1,s.b2,s.b3,s.b4, - s.c1,s.c2,s.c3,s.c4, - s.d1,s.d2,s.d3,s.d4); - } -}//namespace - -class AssimpLoaderMesh -{ - OSString filename; - const aiScene *scene; - - ModelData *model_data; - -private: - - void GetBoundingBox(const aiNode * node, - aiVector3D *min_pos, - aiVector3D *max_pos, - const Matrix4f &up_matrix) - { - Matrix4f cur_matrix=up_matrix*MatrixConvert(node->mTransformation); - uint n, t; - - for (n=0; n < node->mNumMeshes; ++n) - { - const aiMesh *mesh=scene->mMeshes[node->mMeshes[n]]; - - for (t = 0; t < mesh->mNumVertices; ++t) - { - aiVector3D gl_tmp = mesh->mVertices[t]; - Vector4f tmp=cur_matrix*Vector4f(gl_tmp.x, - gl_tmp.y, - gl_tmp.z, - 1.0f); - - min_pos->x = aisgl_min(min_pos->x,tmp.x); - min_pos->y = aisgl_min(min_pos->y,tmp.y); - min_pos->z = aisgl_min(min_pos->z,tmp.z); - - max_pos->x = aisgl_max(max_pos->x,tmp.x); - max_pos->y = aisgl_max(max_pos->y,tmp.y); - max_pos->z = aisgl_max(max_pos->z,tmp.z); - } - } - - for (n = 0; n < node->mNumChildren; ++n) - GetBoundingBox(node->mChildren[n],min_pos,max_pos,cur_matrix); - } - - void AssimpLoaderMesh::GetBoundingBox(const aiNode *node,aiVector3D *min_pos,aiVector3D *max_pos) - { - Matrix4f root_matrix=hgl::graph::GetOpenGL2VulkanMatrix(); - - min_pos->x = min_pos->y = min_pos->z = 1e10f; - max_pos->x = max_pos->y = max_pos->z = -1e10f; - - GetBoundingBox(node,min_pos,max_pos,root_matrix); - } - -public: - - AssimpLoaderMesh(const OSString &fn,const aiScene *s):filename(fn),scene(s) - { - model_data=new ModelData; - - aiVector3D scene_min,scene_max; - - GetBoundingBox(scene->mRootNode,&scene_min,&scene_max); - - model_data->bounding_box.minPoint=pos_to_vec(scene_min); - model_data->bounding_box.maxPoint=pos_to_vec(scene_max); - } - - ~AssimpLoaderMesh()=default; - - ModelData *GetModelData(){return model_data;} - -private: - - bool Load(const aiMesh *mesh) - { - MeshData *md=new MeshData; - - md->name=mesh->mName.C_Str(); - - if(mesh->HasPositions()) - VecCopy(md->position,mesh->mVertices,mesh->mNumVertices); - - if(mesh->HasNormals()) - { - VecCopy(md->normal,mesh->mNormals,mesh->mNumVertices); - - if(mesh->HasTangentsAndBitangents()) - { - VecCopy(md->tangent, mesh->mTangents, mesh->mNumVertices); - VecCopy(md->bitangent, mesh->mBitangents, mesh->mNumVertices); - } - } - - const uint num_color_channels=mesh->GetNumColorChannels(); - - if(num_color_channels>0) - { - md->colors.SetCount(num_color_channels*4*mesh->mNumVertices); - - uint8 *ctp=md->colors.GetData(); - - for(uint i=0;imColors[i],mesh->mNumVertices); - } - - if(mesh->mNumVertices>0xFFFF) - Face2Indices(md->indices32,mesh->mFaces,mesh->mNumFaces); - else - Face2Indices(md->indices16,mesh->mFaces,mesh->mNumFaces); - - md->bounding_box=GetAABB(md->position); - model_data->Add(md); - return(true); - } - -public: - - bool LoadMesh() - { - if(!scene->HasMeshes())return(false); - - for(uint i=0;imNumMeshes;i++) - if(!Load(scene->mMeshes[i])) - return(false); - - return(true); - } - -private: - - bool Load(int level,ModelSceneNode *msn,const aiNode *node) - { - for(int i=0;imName.C_Str()<name=node->mName.C_Str(); - msn->local_matrix=MatrixConvert(node->mTransformation); - - if(node->mNumMeshes>0) - { - msn->mesh_index.SetCount(node->mNumMeshes); - hgl_cpy(msn->mesh_index.GetData(),node->mMeshes,node->mNumMeshes); - } - - if(node->mNumChildren<=0) - return(true); - - for(uint i=0;imNumChildren;i++) - { - ModelSceneNode *sub=new ModelSceneNode; - - if(!Load(level+1,sub,node->mChildren[i])) - { - delete sub; - return(false); - } - - msn->children_node.Add(sub); - } - - return(true); - } - -public: - - bool LoadScene() - { - model_data->root_node=new ModelSceneNode; - - std::cout<<"SceneRoot"<root_node,scene->mRootNode)) - return(false); - - model_data->root_node->local_matrix=hgl::graph::GetOpenGL2VulkanMatrix()*model_data->root_node->local_matrix; - return(true); - } -};//class AssimpLoaderMesh - -ModelData *AssimpLoadModel(const OSString &filename) -{ - const aiScene *scene=LoadSceneFromFile(filename); - - if(!scene)return(nullptr); - - AutoDelete alm=new AssimpLoaderMesh(ClipFileMainname(filename),scene); - - if(alm->LoadMesh()) - if(alm->LoadScene()) - return alm->GetModelData(); - - return(nullptr); -} diff --git a/example/Vulkan/AssimpLoaderMesh.h b/example/Vulkan/AssimpLoaderMesh.h deleted file mode 100644 index f709ec92..00000000 --- a/example/Vulkan/AssimpLoaderMesh.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once -#include -#include -#include -#include - -using namespace hgl; - -struct MeshData -{ - UTF8String name; - - List position, //顶点 - normal, //法线 - tangent, //切线 - bitangent; //副切线 - - List colors; //颜色 - - List indices16; //16位索引 - List indices32; //32位索引 - - AABB bounding_box; -};//struct MeshData - -struct ModelSceneNode -{ - UTF8String name; - - Matrix4f local_matrix; - - List mesh_index; - - ObjectList children_node; //子节点 -};//struct MeshNode - -struct ModelData -{ - ObjectList mesh_data; - Map mesh_by_name; - - ModelSceneNode *root_node; - AABB bounding_box; - -public: - - void Add(MeshData *md) - { - mesh_data.Add(md); - mesh_by_name.Add(md->name,md); - } -};//struct ModelData - -ModelData *AssimpLoadModel(const OSString &filename); diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index 553ad052..5ad35937 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -18,21 +18,9 @@ CreateProject(05.HQFilterTexture HQFilterTexture.cpp) CreateProject(06.Geometry2D Geometry2D.cpp) CreateProject(07.Geometry3D Geometry3D.cpp) CreateProject(08.SceneTree SceneTree.cpp) - -CreateProject(09.LoadModel LoadModel.cpp AssimpLoaderMesh.h AssimpLoaderMesh.cpp ViewModelFramework.h) -target_link_libraries(09.LoadModel assimp) - CreateProject(10.InlineGeometryScene InlineGeometryScene.cpp) - CreateProject(11.Atomsphere Atomsphere.cpp) - #CreateProject(12.PBRBasic PBRBasic.cpp) - #CreateProject(12.Deferred Deferred.cpp) - CreateProject(13.DeferredModel DeferredModel.cpp) - #CreateProject(14.AutoMaterial auto_material.cpp) - -CreateProject(14.SimplePBRForward SimplePBRForward.cpp AssimpLoaderMesh.h AssimpLoaderMesh.cpp) -target_link_libraries(14.SimplePBRForward assimp) \ No newline at end of file diff --git a/res b/res index 519871e4..23685cd8 160000 --- a/res +++ b/res @@ -1 +1 @@ -Subproject commit 519871e43c903472282286746df5d3281037c297 +Subproject commit 23685cd868ff941da4f0974035c8f4aa23594e03