summaryrefslogtreecommitdiff
path: root/src/vk/mesh.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/vk/mesh.cpp')
-rw-r--r--src/vk/mesh.cpp45
1 files changed, 9 insertions, 36 deletions
diff --git a/src/vk/mesh.cpp b/src/vk/mesh.cpp
index 70b9d45..9afeac3 100644
--- a/src/vk/mesh.cpp
+++ b/src/vk/mesh.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright 2022 Frederico de Oliveira Linhares
+ * Copyright 2022-2023 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.
@@ -16,9 +16,7 @@
#include "mesh.hpp"
-#include <array>
-#include <fstream>
-
+#include "../binary_reader.hpp"
#include "../command.hpp"
#include "../core.hpp"
#include "vertex_3d.hpp"
@@ -48,51 +46,26 @@ MeshBuilder::MeshBuilder(VK::Mesh *m, const char *mp):
{
}
-uint32_t read_uint32_from_file(std::ifstream &input_file)
-{
- uint32_t data{};
- input_file.read((char*)&data, sizeof(uint32_t));
- return data;
-}
-
-glm::vec2 read_vec2_from_file(std::ifstream &input_file)
-{
- glm::vec2 data{};
- input_file.read((char*)&data.x, sizeof(glm::vec2::value_type));
- input_file.read((char*)&data.y, sizeof(glm::vec2::value_type));
- return data;
-}
-
-glm::vec3 read_vec3_from_file(std::ifstream &input_file)
-{
- glm::vec3 data{};
- input_file.read((char*)&data.x, sizeof(glm::vec3::value_type));
- input_file.read((char*)&data.y, sizeof(glm::vec3::value_type));
- input_file.read((char*)&data.z, sizeof(glm::vec3::value_type));
- return data;
-}
-
void
load_mesh(void *obj)
{
auto self = static_cast<MeshBuilder*>(obj);
- std::ifstream input_file{self->mesh_path};
- if(!input_file.is_open()) throw CommandError{"Failed to open file."};
+ BinaryReader input{self->mesh_path};
self->mesh->queue_family =
cg_core.vk_device_with_swapchain->get_queue_family_with_graphics();
// Load vertexes.
{
- auto vertex_count{read_uint32_from_file(input_file)};
+ auto vertex_count{input.read_ui32()};
std::vector<VK::Vertex3D> vertexes{vertex_count};
for(auto i{0}; i < vertex_count; i++)
{
- vertexes[i].position = read_vec3_from_file(input_file);
- vertexes[i].normal = read_vec3_from_file(input_file);
- vertexes[i].texture_coord = read_vec2_from_file(input_file);
+ vertexes[i].position = input.read_vec3();
+ vertexes[i].normal = input.read_vec3();
+ vertexes[i].texture_coord = input.read_vec2();
}
void *vertexes_data{vertexes.data()};
@@ -106,11 +79,11 @@ load_mesh(void *obj)
// Load indexes.
{
- self->mesh->index_count = read_uint32_from_file(input_file);
+ self->mesh->index_count = input.read_ui32();
std::vector<uint32_t> indexes(self->mesh->index_count);
for(auto i{0}; i < self->mesh->index_count; i++)
- indexes[i] = read_uint32_from_file(input_file);
+ indexes[i] = input.read_ui32();
void *indexes_data{indexes.data()};
size_t indexes_size{sizeof(indexes[0]) * indexes.size()};