From d08bd8ff81344e02649cc3bed02b95d895a66bcf Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Tue, 11 Mar 2025 14:40:03 -0300 Subject: feat Add header to static mesh files --- src/blu_cat/com/binary_reader.hpp | 8 +++---- src/blu_cat/gra/static_mesh.cpp | 44 ++++++++++++++++++++++++++++++++------ 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(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 vertexes{vertex_count}; + std::vector 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 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 index a75185c..046a955 100644 Binary files a/test/meshes/cube.cgmesh and b/test/meshes/cube.cgmesh differ -- cgit v1.2.3