Crafter.Graphics/Crafter.Graphics-Mesh.cppm

131 lines
5.9 KiB
C++

/*
Crafter®.Graphics
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
module;
#include <cstdint>
#include <vulkan/vulkan.h>
#include <cstring>
#include <iostream>
export module Crafter.Graphics:Mesh;
import Crafter.Component;
import Crafter.Math;
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;
}
static Mesh* FromAssetUV(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*5; i+=5) {
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].u = verticies[i+3];
mesh->verticies.value[counter].v = verticies[i+4];
mesh->verticies.value[counter].w = 1.0f;
counter++;
}
memcpy(mesh->indicies.value, asset+(sizeof(std::uint32_t)*2)+(vertexCount*sizeof(float)*5), 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;
}
static Mesh* FromHeightMapUV(float* heights, uint32_t sizeX, uint32_t sizeZ, float spacing) {
Mesh* mesh = new Mesh(sizeX*sizeZ, ((sizeX-1)*(sizeZ-1))*6);
for (float x = 0; x < sizeX; x++)
{
for (float z = 0; z < sizeZ; z++)
{
uint32_t xInt = static_cast<uint32_t>(x);
uint32_t zInt = static_cast<uint32_t>(z);
Vector<float, 3> pos((x * spacing) - ((sizeX*spacing) / 2.0f), heights[xInt * sizeX + zInt], (z * spacing) - ((sizeZ*spacing) / 2.0f));
Vector<float, 2> uv(x / sizeX, z / sizeZ);
VertexType vertex;
vertex.x = pos.x;
vertex.y = pos.y;
vertex.z = pos.z;
vertex.w = 1.0f;
vertex.u = uv.x;
vertex.v = uv.y;
mesh->verticies.value[xInt * sizeX + zInt] = vertex;
}
}
for (uint32_t x = 0; x < sizeX - 1; x++)
{
for (uint32_t z = 0; z < sizeZ - 1; z++)
{
uint32_t topLeftIndex = x * sizeZ + z;
uint32_t topRightIndex = topLeftIndex + 1;
uint32_t bottomLeftIndex = topLeftIndex + sizeZ;
uint32_t bottomRightIndex = bottomLeftIndex + 1;
uint32_t index = (x * sizeX + z)*6;
mesh->indicies.value[index] = topRightIndex;
mesh->indicies.value[index + 1] = bottomLeftIndex;
mesh->indicies.value[index + 2] = topLeftIndex;
mesh->indicies.value[index + 3] = bottomRightIndex;
mesh->indicies.value[index + 4] = bottomLeftIndex;
mesh->indicies.value[index + 5] = topRightIndex;
}
}
return mesh;
}
};
}