new structure
This commit is contained in:
parent
dfe9b1abe9
commit
1155e67d63
37 changed files with 2399 additions and 751 deletions
153
interfaces/Crafter.Graphics-Mesh.cppm
Normal file
153
interfaces/Crafter.Graphics-Mesh.cppm
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
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 <vulkan/vulkan.h>
|
||||
|
||||
export module Crafter.Graphics:Mesh;
|
||||
import std;
|
||||
import Crafter.Math;
|
||||
import :VulkanBuffer;
|
||||
import :Types;
|
||||
|
||||
namespace Crafter {
|
||||
/**
|
||||
* @brief Holder for a indexed mesh.
|
||||
* @tparam VertexType The vertex type to use that is internally stored, this must match the type the glsl shader expects.
|
||||
*/
|
||||
export template <typename VertexType>
|
||||
class Mesh {
|
||||
public:
|
||||
std::uint32_t vertexCount;
|
||||
std::uint32_t indexCount;
|
||||
Buffer<VertexType> verticies;
|
||||
Buffer<std::uint32_t> indicies;
|
||||
|
||||
/**
|
||||
* @brief Constructs Mesh with empty vertex and index buffer.
|
||||
* @param vertexCount count of the vertex buffer
|
||||
* @param vertexCount count of the index buffer
|
||||
*/
|
||||
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) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Constructs Mesh from an in memory char buffer.
|
||||
* @param asset pointer to the char buffer.
|
||||
*/
|
||||
Mesh(const char* asset) requires(std::same_as<VertexType, Vertex>) : 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) {
|
||||
uint32_t indexCountNoPadding = reinterpret_cast<const std::uint32_t*>(asset)[1];
|
||||
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) {
|
||||
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;
|
||||
counter++;
|
||||
}
|
||||
|
||||
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++) {
|
||||
indicies.value[i] = 0;//pad indicies to nearest 64
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Constructs UV Mesh from an in memory char buffer.
|
||||
* @param asset pointer to the char buffer.
|
||||
*/
|
||||
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) {
|
||||
uint32_t indexCountNoPadding = reinterpret_cast<const std::uint32_t*>(asset)[1];
|
||||
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) {
|
||||
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;
|
||||
counter++;
|
||||
}
|
||||
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++) {
|
||||
indicies.value[i] = 0;//pad indicies to nearest 64
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Constructs heightmap Mesh from an in memory char buffer.
|
||||
* @param heights pointer to heights.
|
||||
* @param sizeX size in the X dimension.
|
||||
* @param sizeZ size in the Y dimension.
|
||||
* @param spacing spacing between the points .
|
||||
*/
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
|
||||
indicies.value[index] = topRightIndex;
|
||||
indicies.value[index + 1] = bottomLeftIndex;
|
||||
indicies.value[index + 2] = topLeftIndex;
|
||||
|
||||
indicies.value[index + 3] = bottomRightIndex;
|
||||
indicies.value[index + 4] = bottomLeftIndex;
|
||||
indicies.value[index + 5] = topRightIndex;
|
||||
}
|
||||
}
|
||||
for(std::uint32_t i = indexCountNoPadding; i < indexCountNoPadding+(indexCountNoPadding%64); i++) {
|
||||
indicies.value[i] = 0;//pad indicies to nearest 64
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue