refactor & fix random deleting texture

This commit is contained in:
MihailRis 2025-05-10 16:13:06 +03:00
parent e158b384fa
commit 8bb03a004f
7 changed files with 65 additions and 64 deletions

View File

@ -60,17 +60,17 @@ void Batch2D::vertex(
index++;
}
void Batch2D::texture(const Texture* new_texture){
if (currentTexture == new_texture) {
void Batch2D::texture(const Texture* newTexture){
if (currentTexture == newTexture) {
return;
}
flush();
currentTexture = new_texture;
if (new_texture == nullptr) {
currentTexture = newTexture;
if (newTexture == nullptr) {
blank->bind();
region = blank->getUVRegion();
} else {
new_texture->bind();
newTexture->bind();
region = currentTexture->getUVRegion();
}
}

View File

@ -8,8 +8,6 @@
Batch3D::Batch3D(size_t capacity)
: capacity(capacity) {
buffer = std::make_unique<Batch3DVertex[]>(capacity);
mesh = std::make_unique<Mesh<Batch3DVertex>>(buffer.get(), 0);
index = 0;

View File

@ -42,9 +42,6 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha)
// Setup color attachment (texture)
texture = create_texture(width, height, format);
unsigned int attachments[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, attachments);
// Setup depth attachment
glGenRenderbuffers(1, &depth);
glBindRenderbuffer(GL_RENDERBUFFER, depth);
@ -60,7 +57,7 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha)
Framebuffer::~Framebuffer() {
glDeleteFramebuffers(1, &fbo);
glDeleteTextures(1, &depth);
glDeleteRenderbuffers(1, &depth);
}
void Framebuffer::bind() {

View File

@ -135,20 +135,21 @@ void PostProcessing::render(
const auto& vp = context.getViewport();
refreshFbos(vp.x, vp.y);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, shadowMap);
if (gbuffer) {
gbuffer->bindBuffers();
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, noiseTexture);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, shadowMap);
glActiveTexture(GL_TEXTURE0);
} else {
glActiveTexture(GL_TEXTURE0);
fbo->getTexture()->bind();
}
glActiveTexture(GL_TEXTURE0);
if (totalPasses == 0) {
auto& effect = assets.require<PostEffect>("default");
auto& shader = effect.use();

View File

@ -10,7 +10,7 @@
#include "frontend/ContentGfxCache.hpp"
const glm::vec3 BlocksRenderer::SUN_VECTOR(0.528265f, 0.833149f, -0.163704f);
const float DIRECTIONAL_LIGHT_FACTOR = 0.2f;
const float DIRECTIONAL_LIGHT_FACTOR = 0.3f;
BlocksRenderer::BlocksRenderer(
size_t capacity,

View File

@ -26,7 +26,6 @@
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
#include "voxels/Chunks.hpp"
#include "window/Camera.hpp"
#include "window/Window.hpp"
#include "world/Level.hpp"
#include "world/LevelEvents.hpp"
@ -102,10 +101,6 @@ WorldRenderer::WorldRenderer(
settings.graphics.skyboxResolution.get(),
assets->require<Shader>("skybox_gen")
);
shadowMap = std::make_unique<ShadowMap>(1024 * 8);
shadowCamera = std::make_unique<Camera>();
}
WorldRenderer::~WorldRenderer() = default;
@ -136,8 +131,11 @@ void WorldRenderer::setupWorldShader(
shader.uniform1i("u_enableShadows", shadows);
if (shadows) {
shader.uniformMatrix("u_shadowsMatrix", shadowCamera->getProjView());
shader.uniform3f("u_sunDir", shadowCamera->front);
if (shadowMap == nullptr) {
shadowMap = std::make_unique<ShadowMap>(1024 * 8);
}
shader.uniformMatrix("u_shadowsMatrix", shadowCamera.getProjView());
shader.uniform3f("u_sunDir", shadowCamera.front);
shader.uniform1i("u_shadowsRes", shadowMap->getResolution());
glActiveTexture(GL_TEXTURE4);
shader.uniform1i("u_shadows", 4);
@ -342,6 +340,44 @@ void WorldRenderer::renderHands(
skybox->unbind();
}
void WorldRenderer::generateShadowsMap(const Camera& camera, const DrawContext& pctx) {
auto& shadowsShader = assets.require<Shader>("shadows");
auto world = level.getWorld();
const auto& worldInfo = world->getInfo();
const auto& settings = engine.getSettings();
int resolution = shadowMap->getResolution();
float shadowMapScale = 0.05f;
float shadowMapSize = resolution * shadowMapScale;
shadowCamera = Camera(camera.position, shadowMapSize);
shadowCamera.near = 0.1f;
shadowCamera.far = 800.0f;
shadowCamera.perspective = false;
shadowCamera.setAspectRatio(1.0f);
shadowCamera.rotate(
glm::radians(fmod(90.0f - worldInfo.daytime * 360.0f, 180.0f)),
glm::radians(-40.0f),
glm::radians(-0.0f)
);
shadowCamera.updateVectors();
shadowCamera.position -= shadowCamera.front * 200.0f;
shadowCamera.position -= shadowCamera.right * (resolution * shadowMapScale) * 0.5f;
shadowCamera.position -= shadowCamera.up * (resolution * shadowMapScale) * 0.5f;
shadowCamera.position = glm::floor(shadowCamera.position * 0.25f) * 4.0f;
{
frustumCulling->update(shadowCamera.getProjView());
auto sctx = pctx.sub();
sctx.setDepthTest(true);
sctx.setCullFace(true);
sctx.setViewport({resolution, resolution});
shadowMap->bind();
setupWorldShader(shadowsShader, shadowCamera, settings, 0.0f);
chunks->drawChunksShadowsPass(shadowCamera, shadowsShader);
shadowMap->unbind();
}
}
void WorldRenderer::draw(
const DrawContext& pctx,
Camera& camera,
@ -370,43 +406,11 @@ void WorldRenderer::draw(
skybox->refresh(pctx, worldInfo.daytime, mie, 4);
const auto& assets = *engine.getAssets();
auto& linesShader = assets.require<Shader>("lines");
auto& shadowsShader = assets.require<Shader>("shadows");
if (shadows) {
int resolution = shadowMap->getResolution();
float shadowMapScale = 0.05f;
float shadowMapSize = resolution * shadowMapScale;
*shadowCamera = Camera(camera.position, shadowMapSize);
shadowCamera->near = 0.1f;
shadowCamera->far = 800.0f;
shadowCamera->perspective = false;
shadowCamera->setAspectRatio(1.0f);
shadowCamera->rotate(
glm::radians(fmod(90.0f - worldInfo.daytime * 360.0f, 180.0f)),
glm::radians(-40.0f),
glm::radians(-0.0f)
);
shadowCamera->updateVectors();
//shadowCamera->position += camera.dir * shadowMapSize * 0.5f;
shadowCamera->position -= shadowCamera->front * 200.0f;
shadowCamera->position -= shadowCamera->right * (resolution * shadowMapScale) * 0.5f;
shadowCamera->position -= shadowCamera->up * (resolution * shadowMapScale) * 0.5f;
shadowCamera->position = glm::floor(shadowCamera->position * 0.25f) * 4.0f;
{
frustumCulling->update(shadowCamera->getProjView());
auto sctx = pctx.sub();
sctx.setDepthTest(true);
sctx.setCullFace(true);
sctx.setViewport({resolution, resolution});
shadowMap->bind();
setupWorldShader(shadowsShader, *shadowCamera, settings, 0.0f);
chunks->drawChunksShadowsPass(*shadowCamera, shadowsShader);
shadowMap->unbind();
}
generateShadowsMap(camera, pctx);
}
auto& linesShader = assets.require<Shader>("lines");
/* World render scope with diegetic HUD included */ {
DrawContext wctx = pctx.sub();
postProcessing.use(wctx, gbufferPipeline);
@ -446,10 +450,9 @@ void WorldRenderer::draw(
camera,
shadows ? shadowMap->getDepthMap() : 0
);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
renderBlockOverlay(pctx);
glActiveTexture(GL_TEXTURE0);
}
void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {

View File

@ -9,6 +9,7 @@
#include "presets/WeatherPreset.hpp"
#include "world/Weather.hpp"
#include "window/Camera.hpp"
class Level;
class Player;
@ -48,14 +49,13 @@ class WorldRenderer {
std::unique_ptr<Skybox> skybox;
std::unique_ptr<ShadowMap> shadowMap;
Weather weather {};
std::unique_ptr<Camera> shadowCamera;
Camera shadowCamera;
float timer = 0.0f;
bool debug = false;
bool lightsDebug = false;
bool gbufferPipeline = false;
bool shadows = true;
bool shadows = false;
/// @brief Render block selection lines
void renderBlockSelection();
@ -77,6 +77,8 @@ class WorldRenderer {
const EngineSettings& settings,
float fogFactor
);
void generateShadowsMap(const Camera& camera, const DrawContext& pctx);
public:
std::unique_ptr<ParticlesRenderer> particles;
std::unique_ptr<TextsRenderer> texts;