feat: actually working slow prototype

This commit is contained in:
MihailRis 2024-11-16 07:41:36 +03:00
parent faadfca8c3
commit 4400366719
4 changed files with 83 additions and 10 deletions

View File

@ -452,9 +452,9 @@ void BlocksRenderer::render(
if (id == 0 || def.drawGroup != drawGroup || state.segment) {
continue;
}
//if (def.translucent) {
// continue;
//}
if (def.translucent) {
continue;
}
const UVRegion texfaces[6] {
cache.getRegion(id, 0), cache.getRegion(id, 1),
cache.getRegion(id, 2), cache.getRegion(id, 3),
@ -496,7 +496,6 @@ void BlocksRenderer::render(
SortingMeshData BlocksRenderer::renderTranslucent(
const voxel* voxels, int beginEnds[256][2]
) {
timeutil::ScopeLogTimer log(555);
SortingMeshData sortingMesh {{}};
for (const auto drawGroup : *content.drawGroups) {
@ -550,16 +549,23 @@ SortingMeshData BlocksRenderer::renderTranslucent(
if (vertexOffset == 0) {
continue;
}
SortingMeshEntry entry {glm::vec3(
x + chunk->x * CHUNK_W, y, z + chunk->z * CHUNK_D
), util::Buffer<float>(indexSize * VERTEX_SIZE)};
SortingMeshEntry entry {
glm::vec3(
x + chunk->x * CHUNK_W + 0.5f,
y + 0.5f,
z + chunk->z * CHUNK_D + 0.5f
),
util::Buffer<float>(indexSize * VERTEX_SIZE)};
for (int j = 0; j < indexSize; j++) {
std::memcpy(
entry.vertexData.data(),
entry.vertexData.data() + j * VERTEX_SIZE,
vertexBuffer.get() + indexBuffer[j] * VERTEX_SIZE,
sizeof(float) * VERTEX_SIZE
);
entry.vertexData[j * VERTEX_SIZE + 0] += chunk->x * CHUNK_W + 0.5f;
entry.vertexData[j * VERTEX_SIZE + 1] += 0.5f;
entry.vertexData[j * VERTEX_SIZE + 2] += chunk->z * CHUNK_D + 0.5f;
}
sortingMesh.entries.push_back(std::move(entry));
vertexOffset = 0;
@ -569,7 +575,7 @@ SortingMeshData BlocksRenderer::renderTranslucent(
return sortingMesh;
}
void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {
void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {;
this->chunk = chunk;
voxelsBuffer->setPosition(
chunk->x * CHUNK_W - voxelBufferPadding, 0,

View File

@ -18,6 +18,8 @@
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include "util/timeutil.hpp"
static debug::Logger logger("chunks-render");
size_t ChunksRenderer::visibleChunks = 0;
@ -204,4 +206,62 @@ void ChunksRenderer::drawChunks(
visibleChunks += drawChunk(indices[i].index, camera, shader, culling);
}
//}
drawSortedMeshes(camera, shader);
}
void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
const vattr attrs[]{ {3}, {2}, {1}, {0} };
std::vector<const SortingMeshEntry*> entries;
auto pposition = camera.position;
size_t size = 0;
bool culling = settings.graphics.frustumCulling.get();
for (size_t i = 0; i < indices.size(); i++) {
auto chunk = level.chunks->getChunks()[indices[i].index];
if (chunk == nullptr || !chunk->flags.lighted) {
continue;
}
const auto& found = meshes.find(glm::ivec2(chunk->x, chunk->z));
if (found == meshes.end()) {
continue;
}
glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D);
glm::vec3 max(
chunk->x * CHUNK_W + CHUNK_W,
chunk->top,
chunk->z * CHUNK_D + CHUNK_D
);
if (!frustum.isBoxVisible(min, max)) continue;
auto& chunkEntries = found->second.sortingMesh.entries;
for (auto& entry : chunkEntries) {
entry.distance = glm::distance2(entry.position, pposition);
}
for (const auto& entry : chunkEntries) {
size += entry.vertexData.size();
entries.push_back(&entry);
}
}
std::sort(entries.begin(), entries.end(), [=](const auto& a, const auto& b) {
return *a < *b;
});
util::Buffer<float> buffer(size);
size_t offset = 0;
for (const auto& entry : entries) {
std::memcpy(
(buffer.data() + offset),
entry->vertexData.data(),
entry->vertexData.size() * sizeof(float)
);
offset += entry->vertexData.size();
}
Mesh mesh(buffer.data(), size / 6, attrs);
shader.uniformMatrix("u_model", glm::mat4(1.0f));
mesh.draw();
}

View File

@ -75,6 +75,8 @@ public:
);
void drawChunks(const Camera& camera, Shader& shader);
void drawSortedMeshes(const Camera& camera, Shader& shader);
void update();
static size_t visibleChunks;

View File

@ -12,6 +12,11 @@ class Mesh;
struct SortingMeshEntry {
glm::vec3 position;
util::Buffer<float> vertexData;
float distance;
inline bool operator<(const SortingMeshEntry& o) const noexcept {
return distance > o.distance;
}
};
struct SortingMeshData {