feat: deferred shading (day 2)

This commit is contained in:
MihailRis
2025-07-08 23:56:22 +03:00
parent a585b52aa2
commit 0bd901d1a6
23 changed files with 236 additions and 92 deletions
+21 -20
View File
@@ -18,9 +18,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/constants.hpp>
#ifndef M_PI
#define M_PI 3.141592
#endif // M_PI
using namespace advanced_pipeline;
const int STARS_COUNT = 3000;
const int STARS_SEED = 632;
@@ -47,14 +45,6 @@ Skybox::Skybox(uint size, Shader& shader)
mesh = std::make_unique<Mesh<SkyboxVertex>>(vertices, 6);
sprites.push_back(SkySprite {
"misc/moon_flare",
glm::pi<float>() * 0.5f,
0.5f,
false,
glm::pi<float>() * 0.25f,
});
sprites.push_back(SkySprite {
"misc/moon",
glm::pi<float>() * 0.5f,
@@ -62,6 +52,14 @@ Skybox::Skybox(uint size, Shader& shader)
false,
glm::pi<float>() * 0.25f,
});
sprites.push_back(SkySprite {
"misc/moon_flare",
glm::pi<float>() * 0.5f,
0.5f,
false,
glm::pi<float>() * 0.25f,
});
sprites.push_back(SkySprite {
"misc/sun",
@@ -118,6 +116,8 @@ void Skybox::draw(
{
const glm::uvec2& viewport = pctx.getViewport();
glActiveTexture(GL_TEXTURE0);
drawBackground(camera, assets, viewport.x, viewport.y);
DrawContext ctx = pctx.sub();
@@ -130,22 +130,23 @@ void Skybox::draw(
batch3d->begin();
float angle = daytime * glm::pi<float>() * 2.0f;
float opacity = glm::pow(1.0f-fog, 7.0f);
float opacity = glm::pow(1.0f - fog, 7.0f);
float depthScale = 1e3;
for (auto& sprite : sprites) {
batch3d->texture(assets.get<Texture>(sprite.texture));
float sangle = daytime * glm::pi<float>() * 2.0 + sprite.phase;
float distance = sprite.distance;
float distance = sprite.distance * depthScale;
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), -sangle + glm::pi<float>() * 0.5f, glm::vec3(0, 0, -1));
rotation = glm::rotate(rotation, sprite.altitude, glm::vec3(1, 0, 0));
glm::vec3 pos = glm::vec4(0, distance, 0, 1) * rotation;
glm::vec3 up = glm::vec4(1, 0, 0, 1) * rotation;
glm::vec3 right = glm::vec4(0, 0, 1, 1) * rotation;
glm::vec3 up = glm::vec4(depthScale, 0, 0, 1) * rotation;
glm::vec3 right = glm::vec4(0, 0, depthScale, 1) * rotation;
glm::vec4 tint (1,1,1, opacity);
if (!sprite.emissive) {
tint *= 0.6f+std::cos(angle)*0.4;
tint *= 0.6f + std::cos(angle)*0.4;
}
batch3d->sprite(pos, right,
up, 1, 1, UVRegion(), tint);
@@ -167,10 +168,10 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality)
assert(cubemap != nullptr);
ready = true;
glActiveTexture(GL_TEXTURE1);
glActiveTexture(GL_TEXTURE0 + TARGET_SKYBOX);
cubemap->bind();
shader.use();
t *= glm::pi<float>()*2.0f;
t *= glm::two_pi<float>();
lightDir = glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f));
shader.uniform1i("u_quality", quality);
@@ -237,13 +238,13 @@ void Skybox::refreshFace(uint face, Cubemap* cubemap) {
}
void Skybox::bind() const {
glActiveTexture(GL_TEXTURE1);
glActiveTexture(GL_TEXTURE0 + TARGET_SKYBOX);
fbo->getTexture()->bind();
glActiveTexture(GL_TEXTURE0);
}
void Skybox::unbind() const {
glActiveTexture(GL_TEXTURE1);
glActiveTexture(GL_TEXTURE0 + TARGET_SKYBOX);
fbo->getTexture()->unbind();
glActiveTexture(GL_TEXTURE0);
}