optimize WorldRenderer chunks sort
This commit is contained in:
parent
b23d31a321
commit
a21d87717e
@ -22,6 +22,7 @@
|
|||||||
#include "maths/voxmaths.hpp"
|
#include "maths/voxmaths.hpp"
|
||||||
#include "objects/Entities.hpp"
|
#include "objects/Entities.hpp"
|
||||||
#include "objects/Player.hpp"
|
#include "objects/Player.hpp"
|
||||||
|
#include "util/listutil.hpp"
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
#include "voxels/Block.hpp"
|
#include "voxels/Block.hpp"
|
||||||
#include "voxels/Chunk.hpp"
|
#include "voxels/Chunk.hpp"
|
||||||
@ -92,6 +93,9 @@ bool WorldRenderer::drawChunk(
|
|||||||
size_t index, const Camera& camera, Shader& shader, bool culling
|
size_t index, const Camera& camera, Shader& shader, bool culling
|
||||||
) {
|
) {
|
||||||
auto chunk = level->chunks->getChunks()[index];
|
auto chunk = level->chunks->getChunks()[index];
|
||||||
|
if (chunk == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (!chunk->flags.lighted) {
|
if (!chunk->flags.lighted) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -135,21 +139,29 @@ void WorldRenderer::drawChunks(
|
|||||||
|
|
||||||
// [warning] this whole method is not thread-safe for chunks
|
// [warning] this whole method is not thread-safe for chunks
|
||||||
|
|
||||||
std::vector<size_t> indices;
|
if (indices.size() != chunks->getVolume()) {
|
||||||
for (size_t i = 0; i < chunks->getVolume(); i++) {
|
indices.clear();
|
||||||
if (chunks->getChunks()[i] == nullptr) continue;
|
for (size_t i = 0; i < chunks->getVolume(); i++) {
|
||||||
indices.emplace_back(i);
|
indices.emplace_back(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
float px = camera.position.x / static_cast<float>(CHUNK_W) - 0.5f;
|
float px = camera.position.x / static_cast<float>(CHUNK_W) - 0.5f;
|
||||||
float pz = camera.position.z / static_cast<float>(CHUNK_D) - 0.5f;
|
float pz = camera.position.z / static_cast<float>(CHUNK_D) - 0.5f;
|
||||||
std::sort(indices.begin(), indices.end(), [chunks, px, pz](auto i, auto j) {
|
int chunksWidth = chunks->getWidth();
|
||||||
const auto& chunksBuffer = chunks->getChunks();
|
int chunksOffsetX = chunks->getOffsetX();
|
||||||
const auto a = chunksBuffer[i].get();
|
int chunksOffsetY = chunks->getOffsetY();
|
||||||
const auto b = chunksBuffer[j].get();
|
util::insertion_sort(indices.begin(), indices.end(),
|
||||||
auto adx = (a->x - px);
|
[chunks, px, pz, chunksWidth, chunksOffsetX, chunksOffsetY]
|
||||||
auto adz = (a->z - pz);
|
(auto i, auto j)
|
||||||
auto bdx = (b->x - px);
|
{
|
||||||
auto bdz = (b->z - pz);
|
int ax = i % chunksWidth + chunksOffsetX;
|
||||||
|
int az = i / chunksWidth + chunksOffsetY;
|
||||||
|
int bx = j % chunksWidth + chunksOffsetX;
|
||||||
|
int bz = j / chunksWidth + chunksOffsetY;
|
||||||
|
auto adx = (ax - px);
|
||||||
|
auto adz = (az - pz);
|
||||||
|
auto bdx = (bx - px);
|
||||||
|
auto bdz = (bz - pz);
|
||||||
return (adx * adx + adz * adz > bdx * bdx + bdz * bdz);
|
return (adx * adx + adz * adz > bdx * bdx + bdz * bdz);
|
||||||
});
|
});
|
||||||
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
||||||
|
|||||||
@ -45,6 +45,8 @@ class WorldRenderer {
|
|||||||
std::unique_ptr<Batch3D> batch3d;
|
std::unique_ptr<Batch3D> batch3d;
|
||||||
std::unique_ptr<ModelBatch> modelBatch;
|
std::unique_ptr<ModelBatch> modelBatch;
|
||||||
|
|
||||||
|
std::vector<size_t> indices;
|
||||||
|
|
||||||
float timer = 0.0f;
|
float timer = 0.0f;
|
||||||
|
|
||||||
bool drawChunk(size_t index, const Camera& camera, Shader& shader, bool culling);
|
bool drawChunk(size_t index, const Camera& camera, Shader& shader, bool culling);
|
||||||
|
|||||||
@ -5,6 +5,15 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
|
template<typename Iter, typename Compare>
|
||||||
|
inline void insertion_sort(Iter first, Iter last, Compare compare) {
|
||||||
|
for (Iter it = first; it != last; ++it) {
|
||||||
|
std::rotate(
|
||||||
|
std::upper_bound(first, it, *it, compare), it, std::next(it)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline bool contains(const std::vector<T>& vec, const T& value) {
|
inline bool contains(const std::vector<T>& vec, const T& value) {
|
||||||
return std::find(vec.begin(), vec.end(), value) != vec.end();
|
return std::find(vec.begin(), vec.end(), value) != vec.end();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user