optimize it even more
This commit is contained in:
parent
a21d87717e
commit
fc3d5c5484
@ -139,38 +139,32 @@ void WorldRenderer::drawChunks(
|
|||||||
|
|
||||||
// [warning] this whole method is not thread-safe for chunks
|
// [warning] this whole method is not thread-safe for chunks
|
||||||
|
|
||||||
|
int chunksWidth = chunks->getWidth();
|
||||||
|
int chunksOffsetX = chunks->getOffsetX();
|
||||||
|
int chunksOffsetY = chunks->getOffsetY();
|
||||||
|
|
||||||
if (indices.size() != chunks->getVolume()) {
|
if (indices.size() != chunks->getVolume()) {
|
||||||
indices.clear();
|
indices.clear();
|
||||||
for (size_t i = 0; i < chunks->getVolume(); i++) {
|
for (int i = 0; i < chunks->getVolume(); i++) {
|
||||||
indices.emplace_back(i);
|
indices.push_back(ChunksSortEntry {i, 0});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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;
|
||||||
int chunksWidth = chunks->getWidth();
|
for (auto& index : indices) {
|
||||||
int chunksOffsetX = chunks->getOffsetX();
|
int x = index.index % chunksWidth + chunksOffsetX - px;
|
||||||
int chunksOffsetY = chunks->getOffsetY();
|
int z = index.index / chunksWidth + chunksOffsetY - pz;
|
||||||
util::insertion_sort(indices.begin(), indices.end(),
|
index.d = x * x + z * z;
|
||||||
[chunks, px, pz, chunksWidth, chunksOffsetX, chunksOffsetY]
|
}
|
||||||
(auto i, auto j)
|
util::insertion_sort(indices.begin(), indices.end());
|
||||||
{
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
||||||
if (culling) {
|
if (culling) {
|
||||||
frustumCulling->update(camera.getProjView());
|
frustumCulling->update(camera.getProjView());
|
||||||
}
|
}
|
||||||
chunks->visible = 0;
|
chunks->visible = 0;
|
||||||
for (size_t i = 0; i < indices.size(); i++) {
|
for (size_t i = 0; i < indices.size(); i++) {
|
||||||
chunks->visible += drawChunk(indices[i], camera, shader, culling);
|
chunks->visible += drawChunk(indices[i].index, camera, shader, culling);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -34,6 +34,15 @@ namespace model {
|
|||||||
struct Model;
|
struct Model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ChunksSortEntry {
|
||||||
|
int index;
|
||||||
|
int d;
|
||||||
|
|
||||||
|
inline bool operator<(const ChunksSortEntry& o) const noexcept {
|
||||||
|
return d > o.d;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class WorldRenderer {
|
class WorldRenderer {
|
||||||
Engine* engine;
|
Engine* engine;
|
||||||
Level* level;
|
Level* level;
|
||||||
@ -45,7 +54,7 @@ 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;
|
std::vector<ChunksSortEntry> indices;
|
||||||
|
|
||||||
float timer = 0.0f;
|
float timer = 0.0f;
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,15 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
|
template<typename Iter>
|
||||||
|
inline void insertion_sort(Iter first, Iter last) {
|
||||||
|
for (Iter it = first; it != last; ++it) {
|
||||||
|
std::rotate(
|
||||||
|
std::upper_bound(first, it, *it), it, std::next(it)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Iter, typename Compare>
|
template<typename Iter, typename Compare>
|
||||||
inline void insertion_sort(Iter first, Iter last, Compare compare) {
|
inline void insertion_sort(Iter first, Iter last, Compare compare) {
|
||||||
for (Iter it = first; it != last; ++it) {
|
for (Iter it = first; it != last; ++it) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user