summaryrefslogtreecommitdiff
path: root/src/vk/static_mesh.cpp
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2023-09-07 15:14:14 -0300
committerFrederico Linhares <fred@linhares.blue>2023-09-15 14:12:15 -0300
commit25bf78bfb4785e2cbed683cc56d3cec4271d8b5a (patch)
tree6fa728091bd662ee0c90a7490bc5ee8e6e23cac4 /src/vk/static_mesh.cpp
parente1399befee43ab4549c31ce179e900ad71651edc (diff)
feat Create skeletal mesh
Diffstat (limited to 'src/vk/static_mesh.cpp')
-rw-r--r--src/vk/static_mesh.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/vk/static_mesh.cpp b/src/vk/static_mesh.cpp
new file mode 100644
index 0000000..29034df
--- /dev/null
+++ b/src/vk/static_mesh.cpp
@@ -0,0 +1,132 @@
+/*
+ * 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.
+ * 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 "static_mesh.hpp"
+
+#include "../binary_reader.hpp"
+#include "../command.hpp"
+#include "../core.hpp"
+#include "static_mesh_vertex.hpp"
+
+namespace
+{
+
+// Data that is only needed for the command chain but not for the StaticMesh
+// goes here.
+struct MeshBuilder
+{
+ std::string mesh_path;
+ VK::StaticMesh *mesh;
+
+ MeshBuilder(VK::StaticMesh *m, std::string mp);
+ MeshBuilder(VK::StaticMesh *m, const char* mp);
+};
+
+MeshBuilder::MeshBuilder(VK::StaticMesh *m, std::string mp):
+ mesh{m},
+ mesh_path{mp}
+{
+}
+
+MeshBuilder::MeshBuilder(VK::StaticMesh *m, const char *mp):
+ MeshBuilder{m, std::string(mp)}
+{
+}
+
+void
+load_mesh(void *obj)
+{
+ auto self = static_cast<MeshBuilder*>(obj);
+
+ 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{input.read_ui32()};
+ std::vector<VK::StaticMeshVertex> vertexes{vertex_count};
+
+ for(auto i{0}; i < vertex_count; i++)
+ {
+ vertexes[i].position = input.read_vec3();
+ vertexes[i].normal = input.read_vec3();
+ vertexes[i].texture_coord = input.read_vec2();
+ }
+
+ 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 = input.read_ui32();
+ std::vector<uint32_t> indexes(self->mesh->index_count);
+
+ for(auto i{0}; i < self->mesh->index_count; i++)
+ indexes[i] = input.read_ui32();
+
+ 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<MeshBuilder*>(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
+{
+
+StaticMesh::StaticMesh(std::string mesh_path)
+{
+ MeshBuilder mesh_builder(this, mesh_path);
+ loader.execute(&mesh_builder);
+}
+
+StaticMesh::StaticMesh(const char* mesh_path):
+ StaticMesh{std::string(mesh_path)}
+{
+}
+
+StaticMesh::~StaticMesh()
+{
+ MeshBuilder mesh_builder(this, "");
+ loader.revert(&mesh_builder);
+}
+
+}