summaryrefslogtreecommitdiff
path: root/src/blu_cat/gra/static_mesh.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/blu_cat/gra/static_mesh.cpp')
-rw-r--r--src/blu_cat/gra/static_mesh.cpp44
1 files changed, 37 insertions, 7 deletions
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++)