/* * Copyright 2022 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. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mesh.hpp" #include #include #include "../command.hpp" #include "../core.hpp" #include "vertex_3d.hpp" namespace { // Data that is only needed for the command chain but not for the Mesh goes // here. struct MeshBuilder { std::string mesh_path; VK::Mesh *mesh; MeshBuilder(VK::Mesh *m, std::string mp); MeshBuilder(VK::Mesh *m, const char* mp); }; MeshBuilder::MeshBuilder(VK::Mesh *m, std::string mp): mesh{m}, mesh_path{mp} { } MeshBuilder::MeshBuilder(VK::Mesh *m, const char *mp): MeshBuilder{m, std::string(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(obj); std::ifstream input_file{self->mesh_path}; if(!input_file.is_open()) throw CommandError{"Failed to open file."}; 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)}; std::vector 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); } void *vertexes_data{vertexes.data()}; size_t vertexes_size = sizeof(vertexes[0]) * vertexes.size(); self->mesh->source_vertex_buffer = new VK::SourceBuffer{ self->mesh->queue_family->device, vertexes_data, vertexes_size}; self->mesh->vertex_buffer = new VK::DestinationBuffer{ self->mesh->queue_family, self->mesh->source_vertex_buffer, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT}; } // Load indexes. { self->mesh->index_count = read_uint32_from_file(input_file); std::vector indexes(self->mesh->index_count); for(auto i{0}; i < self->mesh->index_count; i++) indexes[i] = read_uint32_from_file(input_file); void *indexes_data{indexes.data()}; size_t indexes_size{sizeof(indexes[0]) * indexes.size()}; VK::SourceBuffer source_index_buffer{ self->mesh->queue_family->device, indexes_data, indexes_size}; self->mesh->index_buffer = new VK::DestinationBuffer{ self->mesh->queue_family, &source_index_buffer, VK_BUFFER_USAGE_INDEX_BUFFER_BIT}; } } void unload_mesh(void *obj) { auto self = static_cast(obj); delete self->mesh->index_buffer; delete self->mesh->vertex_buffer; delete self->mesh->source_vertex_buffer; } static const CommandChain loader{ {&load_mesh, &unload_mesh} }; } namespace VK { Mesh::Mesh(std::string mesh_path) { MeshBuilder mesh_builder(this, mesh_path); loader.execute(&mesh_builder); } Mesh::Mesh(const char* mesh_path): Mesh{std::string(mesh_path)} { } Mesh::~Mesh() { MeshBuilder mesh_builder(this, ""); loader.revert(&mesh_builder); } }