optimize Skybox

This commit is contained in:
MihailRis 2024-11-04 15:25:55 +03:00
parent 1717b74042
commit 22cb3acd11
2 changed files with 37 additions and 17 deletions

View File

@ -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 {

View File

@ -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> mesh;
std::unique_ptr<Batch3D> batch3d;
std::vector<skysprite> 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();