WorldRenderer refactor
This commit is contained in:
parent
8ea3db9962
commit
07dc12143e
@ -138,37 +138,15 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible){
|
||||
EngineSettings& settings = engine->getSettings();
|
||||
skybox->refresh(pctx, level->world->daytime, 1.0f+fog*2.0f, 4);
|
||||
|
||||
const Content* content = level->content;
|
||||
auto indices = content->getIndices();
|
||||
void WorldRenderer::renderLevel(
|
||||
const GfxContext& ctx,
|
||||
Camera* camera,
|
||||
const EngineSettings& settings
|
||||
) {
|
||||
Assets* assets = engine->getAssets();
|
||||
Atlas* atlas = assets->getAtlas("blocks");
|
||||
Shader* shader = assets->getShader("main");
|
||||
Shader* linesShader = assets->getShader("lines");
|
||||
|
||||
const Viewport& viewport = pctx.getViewport();
|
||||
int displayWidth = viewport.getWidth();
|
||||
int displayHeight = viewport.getHeight();
|
||||
|
||||
// World render scope with diegetic HUD included
|
||||
{
|
||||
GfxContext wctx = pctx.sub();
|
||||
postProcessing->use(wctx);
|
||||
|
||||
Window::clearDepth();
|
||||
|
||||
// Drawing background sky plane
|
||||
skybox->draw(pctx, camera, assets, level->getWorld()->daytime, fog);
|
||||
|
||||
// Actually world render with depth buffer on
|
||||
{
|
||||
GfxContext ctx = wctx.sub();
|
||||
ctx.setDepthTest(true);
|
||||
ctx.setCullFace(true);
|
||||
auto indices = level->content->getIndices();
|
||||
|
||||
float fogFactor = 15.0f / ((float)settings.chunks.loadDistance-2);
|
||||
|
||||
@ -203,8 +181,11 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
||||
|
||||
drawChunks(level->chunks.get(), camera, shader);
|
||||
|
||||
// Selected block
|
||||
if (PlayerController::selectedBlockId != -1 && hudVisible){
|
||||
skybox->unbind();
|
||||
}
|
||||
|
||||
void WorldRenderer::renderBlockSelection(Camera* camera, Shader* linesShader) {
|
||||
auto indices = level->content->getIndices();
|
||||
blockid_t id = PlayerController::selectedBlockId;
|
||||
auto block = indices->getBlockDef(id);
|
||||
const glm::vec3 pos = PlayerController::selectedBlockPosition;
|
||||
@ -227,12 +208,19 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
||||
}
|
||||
}
|
||||
lineBatch->render();
|
||||
}
|
||||
skybox->unbind();
|
||||
}
|
||||
}
|
||||
|
||||
if (hudVisible && player->debug) {
|
||||
void WorldRenderer::renderDebugLines(
|
||||
const GfxContext& pctx,
|
||||
Camera* camera,
|
||||
Shader* linesShader,
|
||||
const EngineSettings& settings
|
||||
) {
|
||||
GfxContext ctx = pctx.sub();
|
||||
const auto& viewport = ctx.getViewport();
|
||||
uint displayWidth = viewport.getWidth();
|
||||
uint displayHeight = viewport.getHeight();
|
||||
|
||||
ctx.setDepthTest(true);
|
||||
|
||||
linesShader->use();
|
||||
@ -245,8 +233,10 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
||||
int cx = floordiv((int)coord.x, CHUNK_W);
|
||||
int cz = floordiv((int)coord.z, CHUNK_D);
|
||||
|
||||
drawBorders(cx * CHUNK_W, 0, cz * CHUNK_D,
|
||||
(cx + 1) * CHUNK_W, CHUNK_H, (cz + 1) * CHUNK_D);
|
||||
drawBorders(
|
||||
cx * CHUNK_W, 0, cz * CHUNK_D,
|
||||
(cx + 1) * CHUNK_W, CHUNK_H, (cz + 1) * CHUNK_D
|
||||
);
|
||||
}
|
||||
|
||||
float length = 40.f;
|
||||
@ -255,7 +245,8 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
||||
linesShader->uniformMatrix("u_projview", glm::ortho(
|
||||
0.f, (float)displayWidth,
|
||||
0.f, (float)displayHeight,
|
||||
-length, length) * model * glm::inverse(camera->rotation));
|
||||
-length, length) * model * glm::inverse(camera->rotation)
|
||||
);
|
||||
|
||||
ctx.setDepthTest(false);
|
||||
lineBatch->lineWidth(4.0f);
|
||||
@ -270,20 +261,48 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
|
||||
lineBatch->line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 1.f);
|
||||
lineBatch->line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 1.f);
|
||||
lineBatch->render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible){
|
||||
EngineSettings& settings = engine->getSettings();
|
||||
skybox->refresh(pctx, level->world->daytime, 1.0f+fog*2.0f, 4);
|
||||
|
||||
Assets* assets = engine->getAssets();
|
||||
Shader* linesShader = assets->getShader("lines");
|
||||
|
||||
// World render scope with diegetic HUD included
|
||||
{
|
||||
GfxContext ctx = pctx.sub();
|
||||
ctx.setDepthTest(false);
|
||||
GfxContext wctx = pctx.sub();
|
||||
postProcessing->use(wctx);
|
||||
|
||||
auto shader = assets->getShader("screen");
|
||||
shader->use();
|
||||
shader->uniform1f("u_timer", Window::time());
|
||||
shader->uniform1f("u_dayTime", level->world->daytime);
|
||||
Window::clearDepth();
|
||||
|
||||
postProcessing->render(ctx, shader);
|
||||
// Drawing background sky plane
|
||||
skybox->draw(pctx, camera, assets, level->getWorld()->daytime, fog);
|
||||
|
||||
// Actually world render with depth buffer on
|
||||
{
|
||||
GfxContext ctx = wctx.sub();
|
||||
ctx.setDepthTest(true);
|
||||
ctx.setCullFace(true);
|
||||
renderLevel(ctx, camera, settings);
|
||||
// Selected block
|
||||
if (PlayerController::selectedBlockId != -1 && hudVisible){
|
||||
renderBlockSelection(camera, linesShader);
|
||||
}
|
||||
}
|
||||
|
||||
if (hudVisible && player->debug) {
|
||||
renderDebugLines(wctx, camera, linesShader, settings);
|
||||
}
|
||||
}
|
||||
|
||||
// Rendering fullscreen quad with
|
||||
auto screenShader = assets->getShader("screen");
|
||||
screenShader->use();
|
||||
screenShader->uniform1f("u_timer", Window::time());
|
||||
screenShader->uniform1f("u_dayTime", level->world->daytime);
|
||||
postProcessing->render(pctx, screenShader);
|
||||
}
|
||||
|
||||
void WorldRenderer::drawBorders(int sx, int sy, int sz, int ex, int ey, int ez) {
|
||||
|
||||
@ -39,12 +39,38 @@ class WorldRenderer {
|
||||
std::unique_ptr<Batch3D> batch3d;
|
||||
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
|
||||
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
|
||||
|
||||
/// @brief Render level without diegetic interface
|
||||
/// @param context graphics context
|
||||
/// @param camera active camera
|
||||
/// @param settings engine settings
|
||||
void renderLevel(
|
||||
const GfxContext& context,
|
||||
Camera* camera,
|
||||
const EngineSettings& settings
|
||||
);
|
||||
|
||||
/// @brief Render block selection lines
|
||||
/// @param camera active camera
|
||||
/// @param linesShader shader used
|
||||
void renderBlockSelection(Camera* camera, Shader* linesShader);
|
||||
|
||||
/// @brief Render all debug lines (chunks borders, coord system guides)
|
||||
/// @param context graphics context
|
||||
/// @param camera active camera
|
||||
/// @param linesShader shader used
|
||||
/// @param settings engine settings
|
||||
void renderDebugLines(
|
||||
const GfxContext& context,
|
||||
Camera* camera,
|
||||
Shader* linesShader,
|
||||
const EngineSettings& settings
|
||||
);
|
||||
public:
|
||||
WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player);
|
||||
~WorldRenderer();
|
||||
|
||||
void draw(const GfxContext& context, Camera* camera, bool hudVisible);
|
||||
void drawDebug(const GfxContext& context, Camera* camera);
|
||||
void drawBorders(int sx, int sy, int sz, int ex, int ey, int ez);
|
||||
|
||||
static float fog;
|
||||
|
||||
@ -29,7 +29,7 @@ void PostProcessing::use(GfxContext& context) {
|
||||
context.setFramebuffer(fbo.get());
|
||||
}
|
||||
|
||||
void PostProcessing::render(GfxContext& context, Shader* screenShader) {
|
||||
void PostProcessing::render(const GfxContext& context, Shader* screenShader) {
|
||||
if (fbo == nullptr) {
|
||||
throw std::runtime_error("'use(...)' was never called");
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ public:
|
||||
/// @param context graphics context
|
||||
/// @param screenShader shader used for fullscreen quad
|
||||
/// @throws std::runtime_error if use(...) wasn't called before
|
||||
void render(GfxContext& context, Shader* screenShader);
|
||||
void render(const GfxContext& context, Shader* screenShader);
|
||||
};
|
||||
|
||||
#endif // GRAPHICS_POST_PROCESSING_H_
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user