chunkmgr: do not update same chunk multiple times

Iterating on radius from 0 up to RENDER_DISTANCE made the algorithm check the same "sphere" multiple times
intervalmaps-array-y
emamaker 2022-12-01 23:27:22 +01:00
parent 1ccbf8b157
commit 773b7999d4
1 changed files with 39 additions and 41 deletions

View File

@ -21,6 +21,7 @@ namespace chunkmanager
}
int total{0}, toGpu{0};
int rr{RENDER_DISTANCE * RENDER_DISTANCE};
void update(float deltaTime)
{
// Iterate over all chunks, in concentric spheres starting fron the player and going outwards
@ -28,23 +29,21 @@ namespace chunkmanager
glm::vec3 cameraPos = theCamera.getPos();
int chunkX{(static_cast<int>(cameraPos.x)) / CHUNK_SIZE}, chunkY{(static_cast<int>(cameraPos.y)) / CHUNK_SIZE}, chunkZ{(static_cast<int>(cameraPos.z)) / CHUNK_SIZE};
// std::cout << "Player at: " << chunkX << ", " << chunkY << ", " << chunkZ << "\n";
for (int r = 0; r <= RENDER_DISTANCE; r++)
{
// Possible change: coordinates everything at the origin, then translate later?
// Step 1. Eq. of a circle. Fix the x coordinate, get the 2 possible y's
for (int x = chunkX - r; x <= chunkX + r; x++)
for (int x = chunkX - RENDER_DISTANCE; x <= chunkX + RENDER_DISTANCE; x++)
{
// Possible optimization: use sqrt lookup
int y1 = sqrt((r * r) - (x - chunkX) * (x - chunkX)) + chunkY;
int y2 = -sqrt((r * r) - (x - chunkX) * (x - chunkX)) + chunkY;
int y1 = sqrt((rr) - (x - chunkX) * (x - chunkX)) + chunkY;
int y2 = -sqrt((rr) - (x - chunkX) * (x - chunkX)) + chunkY;
for (int y = y2 + 1; y <= y1; y++)
{
// Step 2. At both y's, get the corresponding z values
int z1 = sqrt((r * r) - (x - chunkX) * (x - chunkX) - (y - chunkY) * (y - chunkY)) + chunkZ;
int z2 = -sqrt((r * r) - (x - chunkX) * (x - chunkX) - (y - chunkY) * (y - chunkY)) + chunkZ;
int z1 = sqrt((rr) - (x - chunkX) * (x - chunkX) - (y - chunkY) * (y - chunkY)) + chunkZ;
int z2 = -sqrt((rr) - (x - chunkX) * (x - chunkX) - (y - chunkY) * (y - chunkY)) + chunkZ;
// std::cout << "RENDER DISTANCE " << RENDER_DISTANCE << " Current radius: " << r << " X: " << x << " Y Limits: " << y1 << " - " << y2 << "(" << y << ") Z Limits: " << z1 << " - " << z2 << std::endl;
@ -79,7 +78,6 @@ namespace chunkmanager
}
}
}
}
std::cout << "Total chunks to draw: " << total << ". Sent to GPU: " << toGpu << "\n";
total = 0;
toGpu = 0;