[ClientWorld] No longer handles chunk renderering.

All of it is handled by ChunkRenderer now.
This commit is contained in:
Quentin Bazin 2021-06-10 18:44:38 +02:00
parent a5de07c03b
commit c001308ae9
4 changed files with 122 additions and 100 deletions

View File

@ -24,6 +24,8 @@
*
* =====================================================================================
*/
#include <glm/gtx/norm.hpp>
#include <gk/gl/GLCheck.hpp>
#include <gk/gl/Shader.hpp>
#include <gk/gl/Texture.hpp>
@ -31,7 +33,94 @@
#include "ChunkRenderer.hpp"
#include "ClientChunk.hpp"
void ChunkRenderer::draw(gk::RenderTarget &target, gk::RenderStates states, const std::vector<std::pair<ClientChunk*, gk::Transform>> &chunks) {
void ChunkRenderer::draw(gk::RenderTarget &target, gk::RenderStates states, const ChunkMap &chunks, gk::Camera &camera, const Sky *currentSky) const {
// Changing the values sent to the GPU to double precision is suicidal,
// performance wise, if possible at all. Therefore we want to keep the
// GL rendering numbers in single precision format. But that introduces
// an issue at larger coordinates, because the precision of floats
// quickly degrades as the numbers grow, with a random wobbling being
// very noticeable at e.g. coordinates >= 65536 or so, and the waving
// leaves effect being very jerky in comparison with the effect near the
// origin.
//
// To gain rendering precision, we subtract the camera position from the
// coordinates of the models to be rendered, to make them all small in
// relation to the camera, prior to converting them to floats. Then the
// camera itself is moved to (0, 0, 0) for rendering purposes. Now the
// vertex coordinates passed to the renderer are all small, and single
// precision floats suffice for the drawing.
gk::Vector3d cameraPos(camera.getDPosition());
camera.setDPosition(0, 0, 0); // Temporarily move the camera to the origin
// Prepare a list of chunks to draw
std::vector<std::pair<ClientChunk*, gk::Transform>> chunksToDraw;
for(auto &it : chunks) {
it.second->setHasBeenDrawn(false);
gk::Transform tf = glm::translate(glm::mat4(1.0f),
glm::vec3(it.second->x() * CHUNK_WIDTH - cameraPos.x,
it.second->y() * CHUNK_DEPTH - cameraPos.y,
it.second->z() * CHUNK_HEIGHT - cameraPos.z));
// Is the chunk close enough?
glm::vec4 center = target.getView()->getViewTransform().getMatrix()
* tf.getMatrix()
* glm::vec4(CHUNK_WIDTH / 2, CHUNK_DEPTH / 2, CHUNK_HEIGHT / 2, 1);
// Nope, too far, don't render it
if(glm::length(center) > (Config::renderDistance + 1.f) * CHUNK_WIDTH) {
if(floor(glm::length(center)) > (Config::renderDistance + 2.f) * CHUNK_WIDTH) {
it.second->setTooFar(true);
if (!it.second->isInitialized())
m_onChunkDeletionRequested(gk::Vector3i{it.second->x(), it.second->y(), it.second->z()});
}
continue;
}
it.second->setTooFar(false);
if (!it.second->isInitialized()) continue;
// Is this chunk's centre on the screen?
float d = glm::length2(center);
center.x /= center.w;
center.y /= center.w;
// If it is behind the camera, don't bother drawing it.
// Our screen coordinates are +X right, +Y up, and for a right-handed
// coordinate system, depth must be negative Z, so anything with a
// positive Z is behind the camera.
if (center.z > CHUNK_MAXSIZE) {
continue;
}
// If it is outside the screen, don't bother drawing it
if (fabsf(center.x) > 1 + fabsf(CHUNK_HEIGHT * 2 / center.w)
|| fabsf(center.y) > 1 + fabsf(CHUNK_HEIGHT * 2 / center.w)) {
// FIXME: Disable this test; one that considers all eight corners of the chunk is needed instead.
//continue;
}
// If this chunk is not initialized, skip it and request meshing
if(!it.second->isReadyForMeshing()) {
m_onMeshingRequested(d, gk::Vector3i{it.second->x(), it.second->y(), it.second->z()});
continue;
}
chunksToDraw.emplace_back(it.second.get(), tf);
}
drawChunks(target, states, chunksToDraw, currentSky);
// Restore the camera to its original position
camera.setDPosition(cameraPos);
}
void ChunkRenderer::drawChunks(gk::RenderTarget &target, gk::RenderStates states, const std::vector<std::pair<ClientChunk*, gk::Transform>> &chunks, const Sky *currentSky) const {
if(Config::isWireframeModeEnabled) glCheck(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE));
glCheck(glEnable(GL_DEPTH_TEST));
@ -39,6 +128,11 @@ void ChunkRenderer::draw(gk::RenderTarget &target, gk::RenderStates states, cons
states.texture = &m_textureAtlas.texture();
target.beginSceneDraw(states);
states.shader->setUniform("u_renderDistance", Config::renderDistance * CHUNK_WIDTH);
if (currentSky)
states.shader->setUniform("u_fogColor", currentSky->fogColor());
for (u8 layer = 0 ; layer < ChunkMeshLayer::Count ; ++layer) {
// Disable mipmaps for specific layers
glCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL,

View File

@ -27,21 +27,37 @@
#ifndef CHUNKRENDERER_HPP_
#define CHUNKRENDERER_HPP_
#include <gk/gl/Transform.hpp>
#include <functional>
#include <gk/gl/Camera.hpp>
#include <gk/gl/RenderTarget.hpp>
#include "Sky.hpp"
#include "TextureAtlas.hpp"
class ClientChunk;
class ChunkRenderer {
using OnChunkDeletionRequestedCallback = std::function<void(const gk::Vector3i &)>;
using OnMeshingRequestedCallback = std::function<void(float, const gk::Vector3i &)>;
using ChunkMap = std::unordered_map<gk::Vector3i, std::unique_ptr<ClientChunk>>;
public:
ChunkRenderer(const TextureAtlas &textureAtlas) : m_textureAtlas(textureAtlas) {}
void draw(gk::RenderTarget &target, gk::RenderStates states, const std::vector<std::pair<ClientChunk*, gk::Transform>> &chunks);
void draw(gk::RenderTarget &target, gk::RenderStates states, const ChunkMap &chunks, gk::Camera &camera, const Sky *currentSky) const;
void setOnChunkDeletionRequestedCallback(const OnChunkDeletionRequestedCallback &callback) { m_onChunkDeletionRequested = callback; }
void setOnMeshingRequestedCallback(const OnMeshingRequestedCallback &callback) { m_onMeshingRequested = callback; }
public:
void drawChunks(gk::RenderTarget &target, gk::RenderStates states, const std::vector<std::pair<ClientChunk*, gk::Transform>> &chunks, const Sky *currentSky) const;
const TextureAtlas &m_textureAtlas;
OnChunkDeletionRequestedCallback m_onChunkDeletionRequested;
OnMeshingRequestedCallback m_onMeshingRequested;
};
#endif // CHUNKRENDERER_HPP_

View File

@ -24,11 +24,6 @@
*
* =====================================================================================
*/
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/norm.hpp>
#include <gk/gl/GLCheck.hpp>
#include <gk/gl/Shader.hpp>
#include <gk/resource/ResourceHandler.hpp>
#include "ClientCommandHandler.hpp"
@ -45,6 +40,13 @@ ClientWorld::ClientWorld()
: m_textureAtlas(gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks")),
m_chunkRenderer(m_textureAtlas)
{
m_chunkRenderer.setOnChunkDeletionRequestedCallback([this](const gk::Vector3i &chunkPos) {
m_chunksToRemove.emplace(chunkPos);
});
m_chunkRenderer.setOnMeshingRequestedCallback([this](float distance, const gk::Vector3i &chunkPos) {
m_chunksToMesh.emplace(distance, chunkPos);
});
}
void ClientWorld::update(bool allowWorldReload) {
@ -266,97 +268,7 @@ void ClientWorld::draw(gk::RenderTarget &target, gk::RenderStates states) const
states.vertexAttributes = VertexAttribute::All;
gk::Shader::bind(states.shader);
states.shader->setUniform("u_renderDistance", Config::renderDistance * CHUNK_WIDTH);
if (m_sky)
states.shader->setUniform("u_fogColor", m_sky->fogColor());
gk::Shader::bind(nullptr);
// Changing the values sent to the GPU to double precision is suicidal,
// performance wise, if possible at all. Therefore we want to keep the
// GL rendering numbers in single precision format. But that introduces
// an issue at larger coordinates, because the precision of floats
// quickly degrades as the numbers grow, with a random wobbling being
// very noticeable at e.g. coordinates >= 65536 or so, and the waving
// leaves effect being very jerky in comparison with the effect near the
// origin.
//
// To gain rendering precision, we subtract the camera position from the
// coordinates of the models to be rendered, to make them all small in
// relation to the camera, prior to converting them to floats. Then the
// camera itself is moved to (0, 0, 0) for rendering purposes. Now the
// vertex coordinates passed to the renderer are all small, and single
// precision floats suffice for the drawing.
gk::Vector3d cameraPos(m_camera->getDPosition());
m_camera->setDPosition(0, 0, 0); // Temporarily move the camera to the origin
std::vector<std::pair<ClientChunk*, gk::Transform>> chunks;
for(auto &it : m_chunks) {
it.second->setHasBeenDrawn(false);
gk::Transform tf = glm::translate(glm::mat4(1.0f),
glm::vec3(it.second->x() * CHUNK_WIDTH - cameraPos.x,
it.second->y() * CHUNK_DEPTH - cameraPos.y,
it.second->z() * CHUNK_HEIGHT - cameraPos.z));
// Is the chunk close enough?
glm::vec4 center = target.getView()->getViewTransform().getMatrix()
* tf.getMatrix()
* glm::vec4(CHUNK_WIDTH / 2, CHUNK_DEPTH / 2, CHUNK_HEIGHT / 2, 1);
// Nope, too far, don't render it
if(glm::length(center) > (Config::renderDistance + 1.f) * CHUNK_WIDTH) {
if(floor(glm::length(center)) > (Config::renderDistance + 2.f) * CHUNK_WIDTH) {
it.second->setTooFar(true);
if (!it.second->isInitialized())
m_chunksToRemove.emplace(gk::Vector3i{it.second->x(), it.second->y(), it.second->z()});
}
continue;
}
it.second->setTooFar(false);
if (!it.second->isInitialized()) continue;
// Is this chunk's centre on the screen?
float d = glm::length2(center);
center.x /= center.w;
center.y /= center.w;
// If it is behind the camera, don't bother drawing it.
// Our screen coordinates are +X right, +Y up, and for a right-handed
// coordinate system, depth must be negative Z, so anything with a
// positive Z is behind the camera.
if (center.z > CHUNK_MAXSIZE) {
continue;
}
// If it is outside the screen, don't bother drawing it
if (fabsf(center.x) > 1 + fabsf(CHUNK_HEIGHT * 2 / center.w)
|| fabsf(center.y) > 1 + fabsf(CHUNK_HEIGHT * 2 / center.w)) {
// FIXME: Disable this test; one that considers all eight corners of the chunk is needed instead.
//continue;
}
// If this chunk is not initialized, skip it and request meshing
if(!it.second->isReadyForMeshing()) {
m_chunksToMesh.emplace(d, gk::Vector3i{it.second->x(), it.second->y(), it.second->z()});
continue;
}
chunks.emplace_back(it.second.get(), tf);
}
m_chunkRenderer.draw(target, states, chunks);
m_camera->setDPosition(cameraPos); // Restore the camera to its original position
m_chunkRenderer.draw(target, states, m_chunks, *m_camera, m_sky);
states.transform = gk::Transform::Identity;
target.draw(m_scene, states);

View File

@ -104,7 +104,7 @@ class ClientWorld : public World, public gk::Drawable {
ChunkMeshBuilder m_chunkMeshBuilder{*this};
mutable ChunkRenderer m_chunkRenderer;
ChunkRenderer m_chunkRenderer;
};
#endif // CLIENTWORLD_HPP_