2025-05-07 19:21:51 +02:00
|
|
|
/*
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
2025-04-27 01:57:25 +02:00
|
|
|
module;
|
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <vulkan/vulkan.h>
|
2025-04-27 22:16:56 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
#include <iostream>
|
2025-04-27 01:57:25 +02:00
|
|
|
|
|
|
|
|
export module Crafter.Graphics:Mesh;
|
2025-05-06 12:39:11 +02:00
|
|
|
import Crafter.Math;
|
2025-04-27 01:57:25 +02:00
|
|
|
import :VulkanBuffer;
|
2025-05-08 01:13:59 +02:00
|
|
|
import :Types;
|
2025-04-27 01:57:25 +02:00
|
|
|
|
|
|
|
|
namespace Crafter {
|
|
|
|
|
export template <typename VertexType>
|
2025-05-08 01:13:59 +02:00
|
|
|
class Mesh {
|
2025-04-27 01:57:25 +02:00
|
|
|
public:
|
2025-04-27 22:16:56 +02:00
|
|
|
std::uint32_t vertexCount;
|
|
|
|
|
std::uint32_t indexCount;
|
2025-04-27 01:57:25 +02:00
|
|
|
Buffer<VertexType> verticies;
|
|
|
|
|
Buffer<std::uint32_t> indicies;
|
2025-04-27 22:16:56 +02:00
|
|
|
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) {
|
2025-04-27 01:57:25 +02:00
|
|
|
|
|
|
|
|
}
|
2025-05-08 01:13:59 +02:00
|
|
|
|
|
|
|
|
Mesh(const char* asset) requires(std::same_as<VertexType, Vertex_xf32_yf32_zf32_wf32>) : vertexCount(reinterpret_cast<const std::uint32_t*>(asset)[0]), indexCount(((reinterpret_cast<const std::uint32_t*>(asset)[1]) + 63) & ~63), 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) {
|
2025-05-08 01:35:54 +02:00
|
|
|
uint32_t indexCountNoPadding = reinterpret_cast<const std::uint32_t*>(asset)[1];
|
2025-04-27 22:16:56 +02:00
|
|
|
const float* verticies = reinterpret_cast<const float*>(asset+sizeof(std::uint32_t)*2);
|
|
|
|
|
std::uint32_t counter = 0;
|
2025-05-08 01:13:59 +02:00
|
|
|
|
2025-04-27 22:16:56 +02:00
|
|
|
for(std::uint32_t i = 0; i < vertexCount*3; i+=3) {
|
2025-05-08 01:13:59 +02:00
|
|
|
this->verticies.value[counter].x = verticies[i];
|
|
|
|
|
this->verticies.value[counter].y = verticies[i+1];
|
|
|
|
|
this->verticies.value[counter].z = verticies[i+2];
|
|
|
|
|
this->verticies.value[counter].w = 1.0f;
|
2025-04-27 22:16:56 +02:00
|
|
|
counter++;
|
|
|
|
|
}
|
2025-05-08 01:13:59 +02:00
|
|
|
|
2025-05-08 01:35:54 +02:00
|
|
|
memcpy(indicies.value, asset+(sizeof(std::uint32_t)*2)+(vertexCount*sizeof(float)*3), indexCountNoPadding*sizeof(std::uint32_t));
|
|
|
|
|
for(std::uint32_t i = indexCountNoPadding; i < indexCountNoPadding+(indexCountNoPadding%64); i++) {
|
2025-05-08 01:13:59 +02:00
|
|
|
indicies.value[i] = 0;//pad indicies to nearest 64
|
2025-04-27 22:16:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-08 01:13:59 +02:00
|
|
|
|
2025-05-08 01:35:54 +02:00
|
|
|
|
2025-05-08 01:13:59 +02:00
|
|
|
Mesh(const char* asset) requires(std::same_as<VertexType, VertexUV>) : vertexCount(reinterpret_cast<const std::uint32_t*>(asset)[0]), indexCount(((reinterpret_cast<const std::uint32_t*>(asset)[1]) + 63) & ~63), 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) {
|
2025-05-08 01:35:54 +02:00
|
|
|
uint32_t indexCountNoPadding = reinterpret_cast<const std::uint32_t*>(asset)[1];
|
2025-05-03 06:51:33 +02:00
|
|
|
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) {
|
2025-05-08 01:13:59 +02:00
|
|
|
this->verticies.value[counter].x = verticies[i];
|
|
|
|
|
this->verticies.value[counter].y = verticies[i+1];
|
|
|
|
|
this->verticies.value[counter].z = verticies[i+2];
|
|
|
|
|
this->verticies.value[counter].u = verticies[i+3];
|
|
|
|
|
this->verticies.value[counter].v = verticies[i+4];
|
|
|
|
|
this->verticies.value[counter].w = 1.0f;
|
2025-05-03 06:51:33 +02:00
|
|
|
counter++;
|
|
|
|
|
}
|
2025-05-08 01:35:54 +02:00
|
|
|
memcpy(indicies.value, asset+(sizeof(std::uint32_t)*2)+(vertexCount*sizeof(float)*5), indexCountNoPadding*sizeof(std::uint32_t));
|
|
|
|
|
for(std::uint32_t i = indexCountNoPadding; i < indexCountNoPadding+(indexCountNoPadding%64); i++) {
|
2025-05-08 01:13:59 +02:00
|
|
|
indicies.value[i] = 0;//pad indicies to nearest 64
|
2025-05-03 06:51:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-06 12:39:11 +02:00
|
|
|
|
2025-05-08 01:35:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
Mesh(float* heights, uint32_t sizeX, uint32_t sizeZ, float spacing) : vertexCount(sizeX*sizeZ), indexCount(((((sizeX-1)*(sizeZ-1))*6)+ 63) & ~63), 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) {
|
|
|
|
|
uint32_t indexCountNoPadding = ((sizeX-1)*(sizeZ-1))*6;
|
2025-05-06 12:39:11 +02:00
|
|
|
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;
|
2025-05-08 01:13:59 +02:00
|
|
|
verticies.value[xInt * sizeX + zInt] = vertex;
|
2025-05-06 12:39:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2025-05-08 01:13:59 +02:00
|
|
|
indicies.value[index] = topRightIndex;
|
|
|
|
|
indicies.value[index + 1] = bottomLeftIndex;
|
|
|
|
|
indicies.value[index + 2] = topLeftIndex;
|
2025-05-06 12:39:11 +02:00
|
|
|
|
2025-05-08 01:13:59 +02:00
|
|
|
indicies.value[index + 3] = bottomRightIndex;
|
|
|
|
|
indicies.value[index + 4] = bottomLeftIndex;
|
|
|
|
|
indicies.value[index + 5] = topRightIndex;
|
2025-05-06 12:39:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-08 01:35:54 +02:00
|
|
|
for(std::uint32_t i = indexCountNoPadding; i < indexCountNoPadding+(indexCountNoPadding%64); i++) {
|
|
|
|
|
indicies.value[i] = 0;//pad indicies to nearest 64
|
|
|
|
|
}
|
2025-05-06 12:39:11 +02:00
|
|
|
}
|
2025-04-27 01:57:25 +02:00
|
|
|
};
|
|
|
|
|
}
|