diff options
author | Frederico Linhares <fred@linhares.blue> | 2025-03-11 14:40:03 -0300 |
---|---|---|
committer | Frederico Linhares <fred@linhares.blue> | 2025-03-11 14:40:03 -0300 |
commit | d08bd8ff81344e02649cc3bed02b95d895a66bcf (patch) | |
tree | dcd2c944284e9e64969f4c1eb1fb9ffa30d1d820 | |
parent | a6c3a1e9325aae702b30d334acc6a6b8ff2f37f4 (diff) |
feat Add header to static mesh files
-rw-r--r-- | src/blu_cat/com/binary_reader.hpp | 8 | ||||
-rw-r--r-- | src/blu_cat/gra/static_mesh.cpp | 44 | ||||
-rw-r--r-- | test/meshes/cube.cgmesh | bin | 920 -> 932 bytes |
3 files changed, 41 insertions, 11 deletions
diff --git a/src/blu_cat/com/binary_reader.hpp b/src/blu_cat/com/binary_reader.hpp index fa5d495..a17dc88 100644 --- a/src/blu_cat/com/binary_reader.hpp +++ b/src/blu_cat/com/binary_reader.hpp @@ -23,8 +23,8 @@ class BinaryReader { - I64F _pointer; - I64F _size; + UI64F _pointer; + UI64F _size; UI8 *data; public: @@ -33,10 +33,10 @@ public: BinaryReader(const char *file_path); ~BinaryReader(); - inline int + inline UI64F pointer(){return this->_pointer;}; - inline int + inline UI64F size(){return this->_size;}; UI8 diff --git a/src/blu_cat/gra/static_mesh.cpp b/src/blu_cat/gra/static_mesh.cpp index 0d95590..b52d124 100644 --- a/src/blu_cat/gra/static_mesh.cpp +++ b/src/blu_cat/gra/static_mesh.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,16 @@ namespace { +struct StaticMeshHeader +{ + char file_magic[8]; + UI32 version; + UI32 num_vertexes; + UI32 num_indexes; +}; + +constexpr UI64F STATIC_MESH_HEADER_SIZE{20}; + // Data that is only needed for the command chain but not for the StaticMesh // goes here. struct MeshBuilder @@ -51,16 +61,36 @@ load_mesh(void *obj) { auto self = static_cast<MeshBuilder*>(obj); + self->mesh->queue_family = + BluCat::INT::core.vk_device_with_swapchain-> + get_queue_family_with_graphics(); + + StaticMeshHeader header; BinaryReader input{self->mesh_path}; - self->mesh->queue_family = - BluCat::INT::core.vk_device_with_swapchain->get_queue_family_with_graphics(); + { // 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(); + + size_t total_file_size{ + STATIC_MESH_HEADER_SIZE + + header.num_vertexes * 8 * 4 + + header.num_indexes * 4}; + + if(total_file_size != input.size()) + { + std::string error{"File size does not match header information: "}; + error += self->mesh_path; + throw CommandError{error}; + } + } { // Load vertexes. - auto vertex_count{input.read_ui32()}; - std::vector<BluCat::GRA::StaticMeshVertex> vertexes{vertex_count}; + std::vector<BluCat::GRA::StaticMeshVertex> 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(); @@ -77,7 +107,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++) diff --git a/test/meshes/cube.cgmesh b/test/meshes/cube.cgmesh Binary files differindex a75185c..046a955 100644 --- a/test/meshes/cube.cgmesh +++ b/test/meshes/cube.cgmesh |