feat: actually working slow prototype
This commit is contained in:
parent
faadfca8c3
commit
4400366719
@ -452,9 +452,9 @@ void BlocksRenderer::render(
|
|||||||
if (id == 0 || def.drawGroup != drawGroup || state.segment) {
|
if (id == 0 || def.drawGroup != drawGroup || state.segment) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//if (def.translucent) {
|
if (def.translucent) {
|
||||||
// continue;
|
continue;
|
||||||
//}
|
}
|
||||||
const UVRegion texfaces[6] {
|
const UVRegion texfaces[6] {
|
||||||
cache.getRegion(id, 0), cache.getRegion(id, 1),
|
cache.getRegion(id, 0), cache.getRegion(id, 1),
|
||||||
cache.getRegion(id, 2), cache.getRegion(id, 3),
|
cache.getRegion(id, 2), cache.getRegion(id, 3),
|
||||||
@ -496,7 +496,6 @@ void BlocksRenderer::render(
|
|||||||
SortingMeshData BlocksRenderer::renderTranslucent(
|
SortingMeshData BlocksRenderer::renderTranslucent(
|
||||||
const voxel* voxels, int beginEnds[256][2]
|
const voxel* voxels, int beginEnds[256][2]
|
||||||
) {
|
) {
|
||||||
timeutil::ScopeLogTimer log(555);
|
|
||||||
SortingMeshData sortingMesh {{}};
|
SortingMeshData sortingMesh {{}};
|
||||||
|
|
||||||
for (const auto drawGroup : *content.drawGroups) {
|
for (const auto drawGroup : *content.drawGroups) {
|
||||||
@ -550,16 +549,23 @@ SortingMeshData BlocksRenderer::renderTranslucent(
|
|||||||
if (vertexOffset == 0) {
|
if (vertexOffset == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
SortingMeshEntry entry {glm::vec3(
|
SortingMeshEntry entry {
|
||||||
x + chunk->x * CHUNK_W, y, z + chunk->z * CHUNK_D
|
glm::vec3(
|
||||||
), util::Buffer<float>(indexSize * VERTEX_SIZE)};
|
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++) {
|
for (int j = 0; j < indexSize; j++) {
|
||||||
std::memcpy(
|
std::memcpy(
|
||||||
entry.vertexData.data(),
|
entry.vertexData.data() + j * VERTEX_SIZE,
|
||||||
vertexBuffer.get() + indexBuffer[j] * VERTEX_SIZE,
|
vertexBuffer.get() + indexBuffer[j] * VERTEX_SIZE,
|
||||||
sizeof(float) * 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));
|
sortingMesh.entries.push_back(std::move(entry));
|
||||||
vertexOffset = 0;
|
vertexOffset = 0;
|
||||||
@ -569,7 +575,7 @@ SortingMeshData BlocksRenderer::renderTranslucent(
|
|||||||
return sortingMesh;
|
return sortingMesh;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {
|
void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {;
|
||||||
this->chunk = chunk;
|
this->chunk = chunk;
|
||||||
voxelsBuffer->setPosition(
|
voxelsBuffer->setPosition(
|
||||||
chunk->x * CHUNK_W - voxelBufferPadding, 0,
|
chunk->x * CHUNK_W - voxelBufferPadding, 0,
|
||||||
|
|||||||
@ -18,6 +18,8 @@
|
|||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/ext.hpp>
|
#include <glm/ext.hpp>
|
||||||
|
|
||||||
|
#include "util/timeutil.hpp"
|
||||||
|
|
||||||
static debug::Logger logger("chunks-render");
|
static debug::Logger logger("chunks-render");
|
||||||
|
|
||||||
size_t ChunksRenderer::visibleChunks = 0;
|
size_t ChunksRenderer::visibleChunks = 0;
|
||||||
@ -204,4 +206,62 @@ void ChunksRenderer::drawChunks(
|
|||||||
visibleChunks += drawChunk(indices[i].index, camera, shader, culling);
|
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();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,6 +75,8 @@ public:
|
|||||||
);
|
);
|
||||||
void drawChunks(const Camera& camera, Shader& shader);
|
void drawChunks(const Camera& camera, Shader& shader);
|
||||||
|
|
||||||
|
void drawSortedMeshes(const Camera& camera, Shader& shader);
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
static size_t visibleChunks;
|
static size_t visibleChunks;
|
||||||
|
|||||||
@ -12,6 +12,11 @@ class Mesh;
|
|||||||
struct SortingMeshEntry {
|
struct SortingMeshEntry {
|
||||||
glm::vec3 position;
|
glm::vec3 position;
|
||||||
util::Buffer<float> vertexData;
|
util::Buffer<float> vertexData;
|
||||||
|
float distance;
|
||||||
|
|
||||||
|
inline bool operator<(const SortingMeshEntry& o) const noexcept {
|
||||||
|
return distance > o.distance;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SortingMeshData {
|
struct SortingMeshData {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user