From 22cb3acd11e02ce87f8936fc8cd053e20178dc63 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 4 Nov 2024 15:25:55 +0300 Subject: [PATCH] optimize Skybox --- src/graphics/render/Skybox.cpp | 47 ++++++++++++++++++++++------------ src/graphics/render/Skybox.hpp | 7 +++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/graphics/render/Skybox.cpp b/src/graphics/render/Skybox.cpp index 22927b3c..b1975187 100644 --- a/src/graphics/render/Skybox.cpp +++ b/src/graphics/render/Skybox.cpp @@ -138,6 +138,7 @@ void Skybox::draw( } void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality) { + frameid++; float dayTime = t; DrawContext ctx = pctx.sub(); ctx.setDepthMask(false); @@ -152,7 +153,31 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality) glActiveTexture(GL_TEXTURE1); cubemap->bind(); shader->use(); + t *= M_PI*2.0f; + + lightDir = glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f)); + shader->uniform1i("u_quality", quality); + shader->uniform1f("u_mie", mie); + shader->uniform1f("u_fog", mie - 1.0f); + shader->uniform3f("u_lightDir", lightDir); + shader->uniform1f("u_dayTime", dayTime); + if (glm::abs(mie-prevMie) + glm::abs(t-prevT) >= 0.01) { + for (uint face = 0; face < 6; face++) { + refreshFace(face, cubemap); + } + } else { + uint face = frameid % 6; + refreshFace(face, cubemap); + } + prevMie = mie; + prevT = t; + + cubemap->unbind(); + glActiveTexture(GL_TEXTURE0); +} + +void Skybox::refreshFace(uint face, Cubemap* cubemap) { const glm::vec3 xaxs[] = { {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 1.0f}, @@ -181,23 +206,11 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality) {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 1.0f}, }; - t *= M_PI*2.0f; - - lightDir = glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f)); - shader->uniform1i("u_quality", quality); - shader->uniform1f("u_mie", mie); - shader->uniform1f("u_fog", mie - 1.0f); - shader->uniform3f("u_lightDir", lightDir); - shader->uniform1f("u_dayTime", dayTime); - for (uint face = 0; face < 6; face++) { - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, cubemap->getId(), 0); - shader->uniform3f("u_xaxis", xaxs[face]); - shader->uniform3f("u_yaxis", yaxs[face]); - shader->uniform3f("u_zaxis", zaxs[face]); - mesh->draw(); - } - cubemap->unbind(); - glActiveTexture(GL_TEXTURE0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, cubemap->getId(), 0); + shader->uniform3f("u_xaxis", xaxs[face]); + shader->uniform3f("u_yaxis", yaxs[face]); + shader->uniform3f("u_zaxis", zaxs[face]); + mesh->draw(); } void Skybox::bind() const { diff --git a/src/graphics/render/Skybox.hpp b/src/graphics/render/Skybox.hpp index 2781a95a..902d8fe2 100644 --- a/src/graphics/render/Skybox.hpp +++ b/src/graphics/render/Skybox.hpp @@ -12,6 +12,8 @@ class Shader; class Assets; class Camera; class Batch3D; +class Shader; +class Cubemap; class Framebuffer; class DrawContext; @@ -33,11 +35,16 @@ class Skybox { std::unique_ptr mesh; std::unique_ptr batch3d; std::vector sprites; + int frameid = 0; + + float prevMie = -1.0f; + float prevT = -1.0f; void drawStars(float angle, float opacity); void drawBackground( const Camera& camera, const Assets& assets, int width, int height ); + void refreshFace(uint face, Cubemap* cubemap); public: Skybox(uint size, Shader* shader); ~Skybox();