refactor & fix random deleting texture
This commit is contained in:
parent
e158b384fa
commit
8bb03a004f
@ -60,17 +60,17 @@ void Batch2D::vertex(
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Batch2D::texture(const Texture* new_texture){
|
void Batch2D::texture(const Texture* newTexture){
|
||||||
if (currentTexture == new_texture) {
|
if (currentTexture == newTexture) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
flush();
|
flush();
|
||||||
currentTexture = new_texture;
|
currentTexture = newTexture;
|
||||||
if (new_texture == nullptr) {
|
if (newTexture == nullptr) {
|
||||||
blank->bind();
|
blank->bind();
|
||||||
region = blank->getUVRegion();
|
region = blank->getUVRegion();
|
||||||
} else {
|
} else {
|
||||||
new_texture->bind();
|
newTexture->bind();
|
||||||
region = currentTexture->getUVRegion();
|
region = currentTexture->getUVRegion();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,8 +8,6 @@
|
|||||||
|
|
||||||
Batch3D::Batch3D(size_t capacity)
|
Batch3D::Batch3D(size_t capacity)
|
||||||
: capacity(capacity) {
|
: capacity(capacity) {
|
||||||
|
|
||||||
|
|
||||||
buffer = std::make_unique<Batch3DVertex[]>(capacity);
|
buffer = std::make_unique<Batch3DVertex[]>(capacity);
|
||||||
mesh = std::make_unique<Mesh<Batch3DVertex>>(buffer.get(), 0);
|
mesh = std::make_unique<Mesh<Batch3DVertex>>(buffer.get(), 0);
|
||||||
index = 0;
|
index = 0;
|
||||||
|
|||||||
@ -42,9 +42,6 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha)
|
|||||||
// Setup color attachment (texture)
|
// Setup color attachment (texture)
|
||||||
texture = create_texture(width, height, format);
|
texture = create_texture(width, height, format);
|
||||||
|
|
||||||
unsigned int attachments[1] = { GL_COLOR_ATTACHMENT0 };
|
|
||||||
glDrawBuffers(1, attachments);
|
|
||||||
|
|
||||||
// Setup depth attachment
|
// Setup depth attachment
|
||||||
glGenRenderbuffers(1, &depth);
|
glGenRenderbuffers(1, &depth);
|
||||||
glBindRenderbuffer(GL_RENDERBUFFER, depth);
|
glBindRenderbuffer(GL_RENDERBUFFER, depth);
|
||||||
@ -60,7 +57,7 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha)
|
|||||||
|
|
||||||
Framebuffer::~Framebuffer() {
|
Framebuffer::~Framebuffer() {
|
||||||
glDeleteFramebuffers(1, &fbo);
|
glDeleteFramebuffers(1, &fbo);
|
||||||
glDeleteTextures(1, &depth);
|
glDeleteRenderbuffers(1, &depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Framebuffer::bind() {
|
void Framebuffer::bind() {
|
||||||
|
|||||||
@ -135,20 +135,21 @@ void PostProcessing::render(
|
|||||||
const auto& vp = context.getViewport();
|
const auto& vp = context.getViewport();
|
||||||
refreshFbos(vp.x, vp.y);
|
refreshFbos(vp.x, vp.y);
|
||||||
|
|
||||||
|
glActiveTexture(GL_TEXTURE4);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, shadowMap);
|
||||||
|
|
||||||
if (gbuffer) {
|
if (gbuffer) {
|
||||||
gbuffer->bindBuffers();
|
gbuffer->bindBuffers();
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE3);
|
glActiveTexture(GL_TEXTURE3);
|
||||||
glBindTexture(GL_TEXTURE_2D, noiseTexture);
|
glBindTexture(GL_TEXTURE_2D, noiseTexture);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE4);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, shadowMap);
|
|
||||||
} else {
|
} else {
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
fbo->getTexture()->bind();
|
fbo->getTexture()->bind();
|
||||||
}
|
}
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
|
||||||
|
|
||||||
if (totalPasses == 0) {
|
if (totalPasses == 0) {
|
||||||
auto& effect = assets.require<PostEffect>("default");
|
auto& effect = assets.require<PostEffect>("default");
|
||||||
auto& shader = effect.use();
|
auto& shader = effect.use();
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
#include "frontend/ContentGfxCache.hpp"
|
#include "frontend/ContentGfxCache.hpp"
|
||||||
|
|
||||||
const glm::vec3 BlocksRenderer::SUN_VECTOR(0.528265f, 0.833149f, -0.163704f);
|
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(
|
BlocksRenderer::BlocksRenderer(
|
||||||
size_t capacity,
|
size_t capacity,
|
||||||
|
|||||||
@ -26,7 +26,6 @@
|
|||||||
#include "voxels/Block.hpp"
|
#include "voxels/Block.hpp"
|
||||||
#include "voxels/Chunk.hpp"
|
#include "voxels/Chunk.hpp"
|
||||||
#include "voxels/Chunks.hpp"
|
#include "voxels/Chunks.hpp"
|
||||||
#include "window/Camera.hpp"
|
|
||||||
#include "window/Window.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "world/LevelEvents.hpp"
|
#include "world/LevelEvents.hpp"
|
||||||
@ -102,10 +101,6 @@ WorldRenderer::WorldRenderer(
|
|||||||
settings.graphics.skyboxResolution.get(),
|
settings.graphics.skyboxResolution.get(),
|
||||||
assets->require<Shader>("skybox_gen")
|
assets->require<Shader>("skybox_gen")
|
||||||
);
|
);
|
||||||
|
|
||||||
shadowMap = std::make_unique<ShadowMap>(1024 * 8);
|
|
||||||
|
|
||||||
shadowCamera = std::make_unique<Camera>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WorldRenderer::~WorldRenderer() = default;
|
WorldRenderer::~WorldRenderer() = default;
|
||||||
@ -136,8 +131,11 @@ void WorldRenderer::setupWorldShader(
|
|||||||
shader.uniform1i("u_enableShadows", shadows);
|
shader.uniform1i("u_enableShadows", shadows);
|
||||||
|
|
||||||
if (shadows) {
|
if (shadows) {
|
||||||
shader.uniformMatrix("u_shadowsMatrix", shadowCamera->getProjView());
|
if (shadowMap == nullptr) {
|
||||||
shader.uniform3f("u_sunDir", shadowCamera->front);
|
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());
|
shader.uniform1i("u_shadowsRes", shadowMap->getResolution());
|
||||||
glActiveTexture(GL_TEXTURE4);
|
glActiveTexture(GL_TEXTURE4);
|
||||||
shader.uniform1i("u_shadows", 4);
|
shader.uniform1i("u_shadows", 4);
|
||||||
@ -342,6 +340,44 @@ void WorldRenderer::renderHands(
|
|||||||
skybox->unbind();
|
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(
|
void WorldRenderer::draw(
|
||||||
const DrawContext& pctx,
|
const DrawContext& pctx,
|
||||||
Camera& camera,
|
Camera& camera,
|
||||||
@ -370,43 +406,11 @@ void WorldRenderer::draw(
|
|||||||
|
|
||||||
skybox->refresh(pctx, worldInfo.daytime, mie, 4);
|
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) {
|
if (shadows) {
|
||||||
int resolution = shadowMap->getResolution();
|
generateShadowsMap(camera, pctx);
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto& linesShader = assets.require<Shader>("lines");
|
||||||
/* World render scope with diegetic HUD included */ {
|
/* World render scope with diegetic HUD included */ {
|
||||||
DrawContext wctx = pctx.sub();
|
DrawContext wctx = pctx.sub();
|
||||||
postProcessing.use(wctx, gbufferPipeline);
|
postProcessing.use(wctx, gbufferPipeline);
|
||||||
@ -446,10 +450,9 @@ void WorldRenderer::draw(
|
|||||||
camera,
|
camera,
|
||||||
shadows ? shadowMap->getDepthMap() : 0
|
shadows ? shadowMap->getDepthMap() : 0
|
||||||
);
|
);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
|
|
||||||
renderBlockOverlay(pctx);
|
renderBlockOverlay(pctx);
|
||||||
|
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {
|
void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include "presets/WeatherPreset.hpp"
|
#include "presets/WeatherPreset.hpp"
|
||||||
#include "world/Weather.hpp"
|
#include "world/Weather.hpp"
|
||||||
|
#include "window/Camera.hpp"
|
||||||
|
|
||||||
class Level;
|
class Level;
|
||||||
class Player;
|
class Player;
|
||||||
@ -48,14 +49,13 @@ class WorldRenderer {
|
|||||||
std::unique_ptr<Skybox> skybox;
|
std::unique_ptr<Skybox> skybox;
|
||||||
std::unique_ptr<ShadowMap> shadowMap;
|
std::unique_ptr<ShadowMap> shadowMap;
|
||||||
Weather weather {};
|
Weather weather {};
|
||||||
|
Camera shadowCamera;
|
||||||
std::unique_ptr<Camera> shadowCamera;
|
|
||||||
|
|
||||||
float timer = 0.0f;
|
float timer = 0.0f;
|
||||||
bool debug = false;
|
bool debug = false;
|
||||||
bool lightsDebug = false;
|
bool lightsDebug = false;
|
||||||
bool gbufferPipeline = false;
|
bool gbufferPipeline = false;
|
||||||
bool shadows = true;
|
bool shadows = false;
|
||||||
|
|
||||||
/// @brief Render block selection lines
|
/// @brief Render block selection lines
|
||||||
void renderBlockSelection();
|
void renderBlockSelection();
|
||||||
@ -77,6 +77,8 @@ class WorldRenderer {
|
|||||||
const EngineSettings& settings,
|
const EngineSettings& settings,
|
||||||
float fogFactor
|
float fogFactor
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void generateShadowsMap(const Camera& camera, const DrawContext& pctx);
|
||||||
public:
|
public:
|
||||||
std::unique_ptr<ParticlesRenderer> particles;
|
std::unique_ptr<ParticlesRenderer> particles;
|
||||||
std::unique_ptr<TextsRenderer> texts;
|
std::unique_ptr<TextsRenderer> texts;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user