add example program LoadStaticMesh

This commit is contained in:
hyzboy 2020-06-19 22:07:14 +08:00
parent 43966ce52d
commit 858a7e26ab
6 changed files with 354 additions and 13 deletions

@ -1 +1 @@
Subproject commit bf7152aee5cffa219c6675c03d1e075b9c98c3bf Subproject commit 7c4583fa5b670c67f45d457f3ea2696a29f3819f

View File

@ -18,6 +18,7 @@ CreateProject(05.HQFilterTexture HQFilterTexture.cpp)
CreateProject(06.Geometry2D Geometry2D.cpp) CreateProject(06.Geometry2D Geometry2D.cpp)
CreateProject(07.Geometry3D Geometry3D.cpp) CreateProject(07.Geometry3D Geometry3D.cpp)
CreateProject(08.SceneTree SceneTree.cpp) CreateProject(08.SceneTree SceneTree.cpp)
CreateProject(09.LoadStaticMesh LoadStaticMesh.cpp LoadScene.cpp)
CreateProject(10.InlineGeometryScene InlineGeometryScene.cpp) CreateProject(10.InlineGeometryScene InlineGeometryScene.cpp)
CreateProject(11.Atomsphere Atomsphere.cpp) CreateProject(11.Atomsphere Atomsphere.cpp)
#CreateProject(12.PBRBasic PBRBasic.cpp) #CreateProject(12.PBRBasic PBRBasic.cpp)

View File

@ -0,0 +1,224 @@
#include<hgl/graph/data/SceneNodeData.h>
#include<hgl/graph/NTB.h>
#include<hgl/filesystem/FileSystem.h>
#include<hgl/log/LogInfo.h>
namespace hgl
{
namespace graph
{
namespace
{
constexpr char FILE_HEADER_STRING[]="StaticMesh\x1A";
constexpr uint FILE_HEADER_STRLEN=sizeof(FILE_HEADER_STRING)-1;
}//namespace
namespace
{
struct LoadModelData
{
const uint8 *sp;
ModelData *md;
private:
bool CheckFileHeader()
{
if(memcmp(sp,FILE_HEADER_STRING,FILE_HEADER_STRLEN)) //比较文件头
return(false);
sp+=FILE_HEADER_STRLEN;
md->version=*sp++;
if(md->version!=1)
return(false);
LOG_INFO(OS_TEXT("Load StaticMesh file begin, ver:")+OSString::valueOf(version));
return(true);
}
bool LoadMeshNames(const uint32 mesh_count)
{
uint8 len;
UTF8String str;
const uint32 total_bytes=*(uint32 *)sp;
sp+=sizeof(uint32);
for(uint32 i=0;i<mesh_count;i++)
{
len=*sp++;
str.SetString((char *)sp,len);
md->mesh_name.Add(str);
LOG_INFO(U8_TEXT("\tMesh ")+UTF8String::valueOf(i)+U8_TEXT(" : ")+str);
sp+=len;
}
return(true);
}
void Load(AABB &box)
{
box.minPoint.x=*(float *)sp;sp+=sizeof(float);
box.minPoint.y=*(float *)sp;sp+=sizeof(float);
box.minPoint.z=*(float *)sp;sp+=sizeof(float);
box.maxPoint.x=*(float *)sp;sp+=sizeof(float);
box.maxPoint.y=*(float *)sp;sp+=sizeof(float);
box.maxPoint.z=*(float *)sp;sp+=sizeof(float);
}
bool LoadMesh(MeshData *mesh)
{
LOG_INFO(U8_TEXT("Load Mesh ")+UTF8String::valueOf(mesh->index)+U8_TEXT(" : ")+md->mesh_name[mesh->index]);
mesh->ntb=*sp++;
mesh->vertex_count=*(uint32 *)sp;
sp+=sizeof(uint32);
LOG_INFO(OS_TEXT("\tVertex: ")+OSString::valueOf(mesh->vertex_count));
mesh->position=(float *)sp;
sp+=sizeof(float)*mesh->vertex_count;
if(mesh->ntb&NTB_BIT_NORMAL)
{
mesh->normal=(float *)sp;
sp+=sizeof(float)*mesh->vertex_count;
LOG_INFO(OS_TEXT("\thas normal"));
}
if(mesh->ntb&NTB_BIT_TANGENT)
{
mesh->tangent=(float *)sp;
sp+=sizeof(float)*mesh->vertex_count;
LOG_INFO(OS_TEXT("\thas tangent"));
}
if(mesh->ntb&NTB_BIT_BINORMAL)
{
mesh->binormal=(float *)sp;
sp+=sizeof(float)*mesh->vertex_count;
LOG_INFO(OS_TEXT("\thas bitangent/binormal"));
}
{
mesh->color_count=*sp++;
LOG_INFO(OS_TEXT("\tColor: ")+OSString::valueOf(mesh->vertex_count));
mesh->colors=new uint8 *[mesh->color_count];
for(uint i=0;i<mesh->color_count;i++)
{
mesh->colors[i]=(uint8 *)sp;
sp+=4*mesh->vertex_count;
}
}
{
mesh->uv_count=*sp++;
LOG_INFO(OS_TEXT("\tUV: ")+OSString::valueOf(mesh->uv_count));
mesh->uv_component=sp;
sp+=mesh->uv_count;
mesh->uv=new float *[mesh->uv_count];
for(uint i=0;i<mesh->uv_count;i++)
{
mesh->uv[i]=(float *)sp;
sp+=sizeof(float)*mesh->uv_component[i]*mesh->vertex_count;
LOG_INFO(OS_TEXT("\t\tUV ")+OSString::valueOf(i)+OS_TEXT(" : ")+OSString::valueOf(mesh->uv_component[i]));
}
}
{
mesh->indices_count=*(uint32 *)sp;
sp+=sizeof(uint32);
LOG_INFO(OS_TEXT("\tFaces: ")+OSString::valueOf(mesh->indices_count/3));
mesh->indices=(void *)sp;
if(mesh->vertex_count>0xFFFF)
sp+=sizeof(uint32)*mesh->indices_count;
else
sp+=sizeof(uint16)*mesh->indices_count;
}
Load(mesh->bounding_box);
return(true);
}
public:
LoadModelData(uint8 *source,const uint8 *s)
{
sp=s;
md=new ModelData;
md->source_filedata=(uint8 *)source;
}
bool Load()
{
if(!CheckFileHeader())
return(false);
const uint32 mesh_count=*(uint32 *)sp;
sp+=sizeof(uint32);
LOG_INFO(OS_TEXT("Mesh Count: ")+OSString::valueOf(mesh_count));
if(!LoadMeshNames(mesh_count))
return(false);
for(uint32 i=0;i<mesh_count;i++)
{
MeshData *mesh=new MeshData;
mesh->index=i;
if(!LoadMesh(mesh))
{
delete mesh;
return(false);
}
md->mesh_list.Add(mesh);
}
return(true);
}
};//struct LoadModelData
}//namespace
ModelData *LoadModelFromFile(const OSString &filename)
{
if(filename.IsEmpty())
return(nullptr);
int64 filelength;
uint8 *filedata=(uint8 *)filesystem::LoadFileToMemory(filename,filelength);
const uint8 *sp=filedata;
if(!filedata)
return(nullptr);
LoadModelData lmd(filedata,sp);
if(!lmd.Load())
{
delete lmd.md;
return nullptr;
}
return(lmd.md);
}
}//namespace graph
}//namespace hgl

View File

@ -5,17 +5,25 @@
#include<hgl/filesystem/FileSystem.h> #include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/InlineGeometry.h> #include<hgl/graph/InlineGeometry.h>
#include<hgl/graph/RenderableInstance.h> #include<hgl/graph/RenderableInstance.h>
#include"AssimpLoaderMesh.h"
#include<hgl/graph/VertexBuffer.h> #include<hgl/graph/VertexBuffer.h>
#include<hgl/graph/data/SceneNodeData.h>
namespace hgl
{
namespace graph
{
ModelData *LoadModelFromFile(const OSString &filename);
}
}
using namespace hgl; using namespace hgl;
using namespace hgl::graph; using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=128; constexpr uint32_t SCREEN_WIDTH=128;
constexpr uint32_t SCREEN_HEIGHT=128; constexpr uint32_t SCREEN_HEIGHT=128;
vulkan::Renderable *CreateMeshRenderable(SceneDB *db,vulkan::Material *mtl,const MeshData *mesh) /*vulkan::Renderable *CreateMeshRenderable(SceneDB *db,vulkan::Material *mtl,const MeshData *mesh)
{ {
const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule(); const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule();
@ -243,22 +251,19 @@ public:
return(true); return(true);
} }
};//class TestApp:public ViewModelFramework };//class TestApp:public ViewModelFramework
*/
#ifdef _WIN32 int os_main(const int argc,const os_char **argv)
int wmain(int argc,wchar_t **argv)
#else
int main(int argc,char **argv)
#endif//
{ {
TestApp app; //TestApp app;
ModelData *model_data=AssimpLoadModel(argv[1]); ModelData *model_data=LoadModelFromFile(argv[1]);
if(!model_data) if(!model_data)
return -1; return -1;
if(app.Init(model_data)) //if(app.Init(model_data))
while(app.Run()); // while(app.Run());
if(model_data) if(model_data)
delete model_data; delete model_data;

View File

@ -0,0 +1,66 @@
#ifndef HGL_GRAPH_MESH_DATA_INCLUDE
#define HGL_GRAPH_MESH_DATA_INCLUDE
#include<hgl/type/DataType.h>
#include<hgl/math/Math.h>
namespace hgl
{
namespace graph
{
struct MeshData
{
int index;
uint32 vertex_count; ///<顶点数量
uint8 ntb;
float *position;
float *normal;
float *tangent;
union
{
float *bitangent;
float *binormal;
};
uint32 color_count;
uint8 **colors;
uint32 uv_count;
const uint8 *uv_component;
float **uv;
uint32 indices_count;
union
{
void *indices;
uint16 *indices16;
uint32 *indices32;
};
AABB bounding_box;
public:
MeshData()
{
hgl_zero(this,sizeof(MeshData));
index=-1;
}
~MeshData()
{
if(colors)
delete[] colors;
if(uv)
delete[] uv;
}
};//struct MeshData
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_MESH_DATA_INCLUDE

View File

@ -0,0 +1,45 @@
#ifndef HGL_GRAPH_SCENE_NODE_DATA_INCLUDE
#define HGL_GRAPH_SCENE_NODE_DATA_INCLUDE
#include<hgl/graph/data/MeshData.h>
#include<hgl/type/StringList.h>
namespace hgl
{
namespace graph
{
struct SceneNodeData
{
UTF8String name;
Matrix4f local_matrix;
List<uint32> mesh_index;
ObjectList<SceneNodeData> sub_nodes;
};//struct SceneNodeData
struct ModelData
{
uint8 *source_filedata=nullptr;
uint8 version=0;
UTF8StringList mesh_name;
ObjectList<MeshData> mesh_list;
SceneNodeData *root_node=nullptr;
AABB bounding_box;
public:
~ModelData()
{
SAFE_CLEAR(root_node);
delete[] source_filedata;
}
};//struct ModelData
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_SCENE_NODE_DATA_INCLUDE