move chunk vertex attributes info to commons.hpp

This commit is contained in:
MihailRis
2024-11-17 00:54:10 +03:00
parent 608eb60514
commit 65287b3273
4 changed files with 32 additions and 30 deletions
+23 -22
View File
@@ -14,7 +14,6 @@
#include "util/timeutil.hpp" #include "util/timeutil.hpp"
const uint BlocksRenderer::VERTEX_SIZE = 6;
const glm::vec3 BlocksRenderer::SUN_VECTOR (0.411934f, 0.863868f, -0.279161f); const glm::vec3 BlocksRenderer::SUN_VECTOR (0.411934f, 0.863868f, -0.279161f);
BlocksRenderer::BlocksRenderer( BlocksRenderer::BlocksRenderer(
@@ -23,7 +22,7 @@ BlocksRenderer::BlocksRenderer(
const ContentGfxCache& cache, const ContentGfxCache& cache,
const EngineSettings& settings const EngineSettings& settings
) : content(content), ) : content(content),
vertexBuffer(std::make_unique<float[]>(capacity * VERTEX_SIZE)), vertexBuffer(std::make_unique<float[]>(capacity * CHUNK_VERTEX_SIZE)),
indexBuffer(std::make_unique<int[]>(capacity)), indexBuffer(std::make_unique<int[]>(capacity)),
vertexOffset(0), vertexOffset(0),
indexOffset(0), indexOffset(0),
@@ -87,7 +86,7 @@ void BlocksRenderer::face(
const glm::vec4(&lights)[4], const glm::vec4(&lights)[4],
const glm::vec4& tint const glm::vec4& tint
) { ) {
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { if (vertexOffset + CHUNK_VERTEX_SIZE * 4 > capacity) {
overflow = true; overflow = true;
return; return;
} }
@@ -127,7 +126,7 @@ void BlocksRenderer::faceAO(
const UVRegion& region, const UVRegion& region,
bool lights bool lights
) { ) {
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { if (vertexOffset + CHUNK_VERTEX_SIZE * 4 > capacity) {
overflow = true; overflow = true;
return; return;
} }
@@ -165,7 +164,7 @@ void BlocksRenderer::face(
glm::vec4 tint, glm::vec4 tint,
bool lights bool lights
) { ) {
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { if (vertexOffset + CHUNK_VERTEX_SIZE * 4 > capacity) {
overflow = true; overflow = true;
return; return;
} }
@@ -290,7 +289,7 @@ void BlocksRenderer::blockCustomModel(
const auto& model = cache.getModel(block->rt.id); const auto& model = cache.getModel(block->rt.id);
for (const auto& mesh : model.meshes) { for (const auto& mesh : model.meshes) {
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * mesh.vertices.size() > capacity) { if (vertexOffset + CHUNK_VERTEX_SIZE * mesh.vertices.size() > capacity) {
overflow = true; overflow = true;
return; return;
} }
@@ -558,19 +557,19 @@ SortingMeshData BlocksRenderer::renderTranslucent(
y + 0.5f, y + 0.5f,
z + chunk->z * CHUNK_D + 0.5f z + chunk->z * CHUNK_D + 0.5f
), ),
util::Buffer<float>(indexSize * VERTEX_SIZE)}; util::Buffer<float>(indexSize * CHUNK_VERTEX_SIZE)};
totalSize += entry.vertexData.size(); totalSize += entry.vertexData.size();
for (int j = 0; j < indexSize; j++) { for (int j = 0; j < indexSize; j++) {
std::memcpy( std::memcpy(
entry.vertexData.data() + j * VERTEX_SIZE, entry.vertexData.data() + j * CHUNK_VERTEX_SIZE,
vertexBuffer.get() + indexBuffer[j] * VERTEX_SIZE, vertexBuffer.get() + indexBuffer[j] * CHUNK_VERTEX_SIZE,
sizeof(float) * VERTEX_SIZE sizeof(float) * CHUNK_VERTEX_SIZE
); );
float& vx = entry.vertexData[j * VERTEX_SIZE + 0]; float& vx = entry.vertexData[j * CHUNK_VERTEX_SIZE + 0];
float& vy = entry.vertexData[j * VERTEX_SIZE + 1]; float& vy = entry.vertexData[j * CHUNK_VERTEX_SIZE + 1];
float& vz = entry.vertexData[j * VERTEX_SIZE + 2]; float& vz = entry.vertexData[j * CHUNK_VERTEX_SIZE + 2];
if (!aabbInit) { if (!aabbInit) {
aabbInit = true; aabbInit = true;
@@ -653,21 +652,23 @@ void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {;
} }
ChunkMeshData BlocksRenderer::createMesh() { ChunkMeshData BlocksRenderer::createMesh() {
const vattr attrs[]{ {3}, {2}, {1}, {0} }; return ChunkMeshData {
return ChunkMeshData{MeshData( MeshData(
util::Buffer<float>(vertexBuffer.get(), vertexOffset), util::Buffer<float>(vertexBuffer.get(), vertexOffset),
util::Buffer<int>(indexBuffer.get(), indexSize), util::Buffer<int>(indexBuffer.get(), indexSize),
util::Buffer<vattr>({{3}, {2}, {1}, {0}}) util::Buffer<vattr>(
), std::move(sortingMesh)}; CHUNK_VATTRS, sizeof(CHUNK_VATTRS) / sizeof(vattr)
)
),
std::move(sortingMesh)};
} }
ChunkMesh BlocksRenderer::render(const Chunk* chunk, const Chunks* chunks) { ChunkMesh BlocksRenderer::render(const Chunk* chunk, const Chunks* chunks) {
build(chunk, chunks); build(chunk, chunks);
const vattr attrs[]{ {3}, {2}, {1}, {0} }; size_t vcount = vertexOffset / CHUNK_VERTEX_SIZE;
size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE;
return ChunkMesh{std::make_unique<Mesh>( return ChunkMesh{std::make_unique<Mesh>(
vertexBuffer.get(), vcount, indexBuffer.get(), indexSize, attrs vertexBuffer.get(), vcount, indexBuffer.get(), indexSize, CHUNK_VATTRS
), std::move(sortingMesh)}; ), std::move(sortingMesh)};
} }
-1
View File
@@ -27,7 +27,6 @@ struct UVRegion;
class BlocksRenderer { class BlocksRenderer {
static const glm::vec3 SUN_VECTOR; static const glm::vec3 SUN_VECTOR;
static const uint VERTEX_SIZE;
const Content& content; const Content& content;
std::unique_ptr<float[]> vertexBuffer; std::unique_ptr<float[]> vertexBuffer;
std::unique_ptr<int[]> indexBuffer; std::unique_ptr<int[]> indexBuffer;
+4 -7
View File
@@ -49,9 +49,6 @@ public:
} }
}; };
const vattr ATTRS[]{ {3}, {2}, {1}, {0} };
inline constexpr int VERTEX_SIZE = 6;
ChunksRenderer::ChunksRenderer( ChunksRenderer::ChunksRenderer(
const Level* level, const Level* level,
const Assets& assets, const Assets& assets,
@@ -84,7 +81,7 @@ ChunksRenderer::ChunksRenderer(
logger.info() << "created " << threadPool.getWorkersCount() << " workers"; logger.info() << "created " << threadPool.getWorkersCount() << " workers";
float buf[1]{}; float buf[1]{};
sortedMesh = std::make_unique<Mesh>(buf, 0, ATTRS); sortedMesh = std::make_unique<Mesh>(buf, 0, CHUNK_VATTRS);
} }
ChunksRenderer::~ChunksRenderer() { ChunksRenderer::~ChunksRenderer() {
@@ -265,8 +262,8 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
if (found->second.sortedMesh == nullptr) { if (found->second.sortedMesh == nullptr) {
found->second.sortedMesh = std::make_unique<Mesh>( found->second.sortedMesh = std::make_unique<Mesh>(
entry.vertexData.data(), entry.vertexData.data(),
entry.vertexData.size() / VERTEX_SIZE, entry.vertexData.size() / CHUNK_VERTEX_SIZE,
ATTRS CHUNK_VATTRS
); );
} }
found->second.sortedMesh->draw(); found->second.sortedMesh->draw();
@@ -297,7 +294,7 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
offset += entry.vertexData.size(); offset += entry.vertexData.size();
} }
found->second.sortedMesh = std::make_unique<Mesh>( found->second.sortedMesh = std::make_unique<Mesh>(
buffer.data(), size / VERTEX_SIZE, ATTRS buffer.data(), size / CHUNK_VERTEX_SIZE, CHUNK_VATTRS
); );
} }
found->second.sortedMesh->draw(); found->second.sortedMesh->draw();
+5
View File
@@ -7,6 +7,11 @@
#include "graphics/core/MeshData.hpp" #include "graphics/core/MeshData.hpp"
#include "util/Buffer.hpp" #include "util/Buffer.hpp"
/// @brief Chunk mesh vertex attributes
inline const vattr CHUNK_VATTRS[]{ {3}, {2}, {1}, {0} };
/// @brief Chunk mesh vertex size divided by sizeof(float)
inline constexpr int CHUNK_VERTEX_SIZE = 6;
class Mesh; class Mesh;
struct SortingMeshEntry { struct SortingMeshEntry {