diff options
-rw-r--r-- | src/blu_cat/com/numbers.hpp | 3 | ||||
-rw-r--r-- | src/blu_cat/gra/skeletal_mesh.cpp | 158 | ||||
-rw-r--r-- | src/blu_cat/gra/static_mesh.cpp | 4 | ||||
-rw-r--r-- | test/meshes/cuboid.cgmesh | bin | 2772 -> 2800 bytes |
4 files changed, 108 insertions, 57 deletions
diff --git a/src/blu_cat/com/numbers.hpp b/src/blu_cat/com/numbers.hpp index 7fddbe0..1fd3400 100644 --- a/src/blu_cat/com/numbers.hpp +++ b/src/blu_cat/com/numbers.hpp @@ -60,4 +60,7 @@ typedef uint_fast64_t UI64F; typedef std::float32_t F32; typedef std::float64_t F64; +constexpr UI32F SIZE_32_BIT{4}; +constexpr UI32F SIZE_64_BIT{8}; + #endif /* BLU_CAT_COM_NUMBERS_H */ diff --git a/src/blu_cat/gra/skeletal_mesh.cpp b/src/blu_cat/gra/skeletal_mesh.cpp index 1beed1a..1515dda 100644 --- a/src/blu_cat/gra/skeletal_mesh.cpp +++ b/src/blu_cat/gra/skeletal_mesh.cpp @@ -24,6 +24,22 @@ namespace { +struct StaticMeshHeader +{ + char file_magic[8]; + UI32 version; + UI32 num_vertexes; + UI32 num_indexes; + UI32 num_bones; + UI32 num_animations; + UI32 num_bone_transforms; // For all animations + UI32 num_bone_positions; // For all animations + UI32 num_bone_rotations; // For all animations + UI32 num_bone_scales; // For all animations +}; + +constexpr UI64F SKELETAL_MESH_HEADER_SIZE{44}; + // Data that is only needed for the command chain but not for the SkeletalMesh // goes here. struct MeshBuilder @@ -51,28 +67,62 @@ load_mesh(void *obj) { auto self = static_cast<MeshBuilder*>(obj); + StaticMeshHeader header; BinaryReader input{self->mesh_path}; + { // Read header + input.read_chars(header.file_magic, 8); + header.version = input.read_ui32(); + header.num_vertexes = input.read_ui32(); + header.num_indexes = input.read_ui32(); + header.num_bones = input.read_ui32(); + header.num_animations = input.read_ui32(); + header.num_bone_transforms = input.read_ui32(); + header.num_bone_positions = input.read_ui32(); + header.num_bone_rotations = input.read_ui32(); + header.num_bone_scales = input.read_ui32(); + + size_t total_file_size{ + SKELETAL_MESH_HEADER_SIZE + + header.num_vertexes * 16 * SIZE_32_BIT + + header.num_indexes * SIZE_32_BIT + + header.num_bones * 16 * SIZE_32_BIT + + header.num_animations * (2 * SIZE_64_BIT + SIZE_32_BIT) + + header.num_bone_transforms * 4 * SIZE_32_BIT + + header.num_bone_positions * (3 * SIZE_32_BIT + SIZE_64_BIT) + + header.num_bone_rotations * (4 * SIZE_32_BIT + SIZE_64_BIT) + + header.num_bone_scales * (3 * SIZE_32_BIT + SIZE_64_BIT)}; + + if(total_file_size != input.size()) + { + std::string error{"File size does not match header information: "}; + error += self->mesh_path; + throw CommandError{error}; + } + } + self->mesh->queue_family = - BluCat::INT::core.vk_device_with_swapchain->get_queue_family_with_graphics(); + BluCat::INT::core.vk_device_with_swapchain-> + get_queue_family_with_graphics(); { // Load vertexes. - auto vertex_count{input.read_ui32()}; - std::vector<BluCat::GRA::SkeletalMeshVertex> vertexes{vertex_count}; + std::vector<BluCat::GRA::SkeletalMeshVertex> vertexes{header.num_vertexes}; - for(auto i{0}; i < vertex_count; i++) + for(auto i{0}; i < header.num_vertexes; i++) { vertexes[i].position = input.read_vec3(); vertexes[i].normal = input.read_vec3(); vertexes[i].texture_coord = input.read_vec2(); - for(auto ii{0}; ii < BluCat::GRA::SKELETAL_MESH_MAX_NUM_OF_INFLUENCING_BONES; - ii++) - vertexes[i].bone_ids[ii] = input.read_ui32(); + for(auto ii{0}; + ii < BluCat::GRA::SKELETAL_MESH_MAX_NUM_OF_INFLUENCING_BONES; + ii++) + vertexes[i].bone_ids[ii] = input.read_ui32(); - for(auto ii{0}; ii < BluCat::GRA::SKELETAL_MESH_MAX_NUM_OF_INFLUENCING_BONES; - ii++) - vertexes[i].bone_weights[ii] = input.read_f32(); + for(auto ii{0}; + ii < BluCat::GRA::SKELETAL_MESH_MAX_NUM_OF_INFLUENCING_BONES; + ii++) + vertexes[i].bone_weights[ii] = input.read_f32(); } void *vertexes_data{vertexes.data()}; @@ -85,7 +135,7 @@ load_mesh(void *obj) } { // Load indexes. - self->mesh->index_count = input.read_ui32(); + self->mesh->index_count = header.num_indexes; std::vector<uint32_t> indexes(self->mesh->index_count); for(auto i{0}; i < self->mesh->index_count; i++) @@ -101,16 +151,14 @@ load_mesh(void *obj) } { // Load bones - auto bone_count{input.read_ui32()}; - self->mesh->bones.reserve(bone_count); - for(int i{0}; i < bone_count; i++) + self->mesh->bones.reserve(header.num_bones); + for(int i{0}; i < header.num_bones; i++) self->mesh->bones.emplace_back(input.read_mat4()); } { // Load animations - auto num_animations{input.read_ui32()}; - self->mesh->animations.resize(num_animations); - for(uint32_t i{0}; i < num_animations; i++) + self->mesh->animations.resize(header.num_animations); + for(uint32_t i{0}; i < header.num_animations; i++) { auto duration{input.read_f64()}; self->mesh->animations[i].final_time = (float)duration; @@ -119,47 +167,47 @@ load_mesh(void *obj) auto num_bone_transforms{input.read_ui32()}; std::vector<BluCat::GRA::BoneTransform> *bone_transforms = - &(self->mesh->animations[i].bone_transforms); + &(self->mesh->animations[i].bone_transforms); bone_transforms->resize(num_bone_transforms); for(uint32_t bone_transform_index{0}; - bone_transform_index < num_bone_transforms; bone_transform_index++) + bone_transform_index < num_bone_transforms; bone_transform_index++) { - auto bone_id{input.read_ui32()}; - - auto num_positions{input.read_ui32()}; - BluCat::GRA::Channel<glm::vec3> *positions = - &((*bone_transforms)[bone_transform_index].positions); - for(auto position_key_index{0}; position_key_index < num_positions; - position_key_index++) - { - auto vec3{input.read_vec3()}; - auto timestamp{input.read_f64()}; - positions->key_frames.emplace_back( - vec3, static_cast<float>(timestamp)); - } - - auto num_rotations{input.read_ui32()}; - BluCat::GRA::Channel<glm::quat> *rotations = - &((*bone_transforms)[bone_transform_index].rotations); - for(auto rotation_key_index{0}; rotation_key_index < num_rotations; - rotation_key_index++) - { - auto quat{input.read_quat()}; - auto timestamp{input.read_f64()}; - rotations->key_frames.emplace_back( - quat, static_cast<float>(timestamp)); - } - - auto num_scales{input.read_ui32()}; - BluCat::GRA::Channel<glm::vec3> *scales = - &((*bone_transforms)[bone_transform_index].scales); - for(auto scaling_key_index{0}; scaling_key_index < num_scales; - scaling_key_index++) - { - auto vec3{input.read_vec3()}; - auto timestamp{input.read_f64()}; - scales->key_frames.emplace_back(vec3, static_cast<float>(timestamp)); - } + auto bone_id{input.read_ui32()}; + + auto num_positions{input.read_ui32()}; + BluCat::GRA::Channel<glm::vec3> *positions = + &((*bone_transforms)[bone_transform_index].positions); + for(auto position_key_index{0}; position_key_index < num_positions; + position_key_index++) + { + auto vec3{input.read_vec3()}; + auto timestamp{input.read_f64()}; + positions->key_frames.emplace_back( + vec3, static_cast<float>(timestamp)); + } + + auto num_rotations{input.read_ui32()}; + BluCat::GRA::Channel<glm::quat> *rotations = + &((*bone_transforms)[bone_transform_index].rotations); + for(auto rotation_key_index{0}; rotation_key_index < num_rotations; + rotation_key_index++) + { + auto quat{input.read_quat()}; + auto timestamp{input.read_f64()}; + rotations->key_frames.emplace_back( + quat, static_cast<float>(timestamp)); + } + + auto num_scales{input.read_ui32()}; + BluCat::GRA::Channel<glm::vec3> *scales = + &((*bone_transforms)[bone_transform_index].scales); + for(auto scaling_key_index{0}; scaling_key_index < num_scales; + scaling_key_index++) + { + auto vec3{input.read_vec3()}; + auto timestamp{input.read_f64()}; + scales->key_frames.emplace_back(vec3, static_cast<float>(timestamp)); + } } } } diff --git a/src/blu_cat/gra/static_mesh.cpp b/src/blu_cat/gra/static_mesh.cpp index b52d124..0b6088c 100644 --- a/src/blu_cat/gra/static_mesh.cpp +++ b/src/blu_cat/gra/static_mesh.cpp @@ -76,8 +76,8 @@ load_mesh(void *obj) size_t total_file_size{ STATIC_MESH_HEADER_SIZE + - header.num_vertexes * 8 * 4 + - header.num_indexes * 4}; + header.num_vertexes * 8 * SIZE_32_BIT + + header.num_indexes * SIZE_32_BIT}; if(total_file_size != input.size()) { diff --git a/test/meshes/cuboid.cgmesh b/test/meshes/cuboid.cgmesh Binary files differindex ac280ae..aa95118 100644 --- a/test/meshes/cuboid.cgmesh +++ b/test/meshes/cuboid.cgmesh |