44 lines
2 KiB
C++
44 lines
2 KiB
C++
module;
|
|
|
|
#include <cstdint>
|
|
#include <vulkan/vulkan.h>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
|
|
export module Crafter.Graphics:Mesh;
|
|
import Crafter.Component;
|
|
import :VulkanBuffer;
|
|
|
|
namespace Crafter {
|
|
export template <typename VertexType>
|
|
class Mesh : public Component {
|
|
public:
|
|
std::uint32_t vertexCount;
|
|
std::uint32_t indexCount;
|
|
Buffer<VertexType> verticies;
|
|
Buffer<std::uint32_t> indicies;
|
|
Mesh(std::uint32_t vertexCount, std::uint32_t indexCount) : vertexCount(vertexCount), indexCount(indexCount), verticies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, vertexCount), indicies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, indexCount) {
|
|
|
|
}
|
|
static Mesh* FromAsset(const char* asset) {
|
|
std::uint32_t vertexCount = reinterpret_cast<const std::uint32_t*>(asset)[0];
|
|
std::uint32_t indexCount = reinterpret_cast<const std::uint32_t*>(asset)[1];
|
|
|
|
Mesh* mesh = new Mesh(vertexCount, (indexCount+ 63) & ~63);
|
|
const float* verticies = reinterpret_cast<const float*>(asset+sizeof(std::uint32_t)*2);
|
|
std::uint32_t counter = 0;
|
|
for(std::uint32_t i = 0; i < vertexCount*3; i+=3) {
|
|
mesh->verticies.value[counter].x = verticies[i];
|
|
mesh->verticies.value[counter].y = verticies[i+1];
|
|
mesh->verticies.value[counter].z = verticies[i+2];
|
|
mesh->verticies.value[counter].w = 1.0f;
|
|
counter++;
|
|
}
|
|
memcpy(mesh->indicies.value, asset+(sizeof(std::uint32_t)*2)+(vertexCount*sizeof(float)*3), indexCount*sizeof(std::uint32_t));
|
|
for(std::uint32_t i = indexCount; i < indexCount+(indexCount%64); i++) {
|
|
mesh->indicies.value[i] = 0;//pad indicies to nearest 64
|
|
}
|
|
return mesh;
|
|
}
|
|
};
|
|
}
|