refactor and fix

This commit is contained in:
MihailRis 2025-02-08 21:13:32 +03:00
parent 5cb119cc4c
commit 64fd5d7504

View File

@ -1,16 +1,16 @@
#include "PrecipitationRenderer.hpp"
#include "MainBatch.hpp"
#include "assets/Assets.hpp"
#include "assets/assets_util.hpp"
#include "graphics/core/Shader.hpp"
#include "graphics/core/Texture.hpp"
#include "lighting/Lightmap.hpp"
#include "maths/util.hpp"
#include "settings.hpp"
#include "voxels/Chunks.hpp"
#include "window/Camera.hpp"
#include "world/Level.hpp"
#include "voxels/Chunks.hpp"
#include "MainBatch.hpp"
#include "settings.hpp"
PrecipitationRenderer::PrecipitationRenderer(
const Assets& assets,
@ -28,7 +28,7 @@ PrecipitationRenderer::PrecipitationRenderer(
PrecipitationRenderer::~PrecipitationRenderer() = default;
int PrecipitationRenderer::getHeightAt(int x, int z) {
int y = CHUNK_H-1;
int y = CHUNK_H - 1;
while (y > 0) {
if (auto voxel = chunks.get({x, y, z})) {
if (voxel->id == 0) {
@ -77,104 +77,53 @@ void PrecipitationRenderer::render(const Camera& camera, float delta) {
float scale = 0.4f;
int quads = 0;
float k = 21.41149;
for (int lx = -radius; lx <= radius; lx++) {
for (int lz = -depth; lz < 0; lz++) {
glm::vec4 light = light_at(chunks, x + lx, y, z + lz);
random.setSeed(lx + x, lz + z);
float hspeed = (random.randFloat() * 2.0f - 1.0f) * horizontal;
glm::vec3 pos {
x + lx + 0.5f,
glm::max(y - size.y / 2, getHeightAt(x + lx, z + lz)) + size.y / 2 + 1,
z + lz + 0.5f};
batch->quad(
pos,
{1, 0, 0},
{0, 1, 0},
size,
light,
glm::vec3(1.0f),
UVRegion(
(lx + x) * scale + timer * hspeed,
timer + y * scale + (z + lz) * k,
(lx + x + 1) * scale + timer * hspeed,
timer + (size.y + y) * scale + (z + lz) * k
)
);
}
}
for (int lx = -radius; lx <= radius; lx++) {
for (int lz = depth; lz > 0; lz--) {
glm::vec4 light = light_at(chunks, x + lx, y, z + lz);
random.setSeed(lx + x, lz + z);
float hspeed = (random.randFloat() * 2.0f - 1.0f) * horizontal;
glm::vec3 pos {
x + lx + 0.5f,
glm::max(y - size.y / 2, getHeightAt(x + lx, z + lz)) + size.y / 2 + 1,
z + lz + 0.5f};
batch->quad(
pos,
{-1, 0, 0},
{0, 1, 0},
size,
light,
glm::vec3(1.0f),
UVRegion(
(lx + x) * scale + timer * hspeed,
timer + y * scale + (z + lz) * k,
(lx + x + 1) * scale + timer * hspeed,
timer + (size.y + y) * scale + (z + lz) * k
)
);
}
}
for (int lz = -radius; lz <= radius; lz++) {
for (int lx = -depth; lx < 0; lx++) {
glm::vec4 light = light_at(chunks, x + lx, y, z + lz);
random.setSeed(lx + x, lz + z);
float hspeed = (random.randFloat() * 2.0f - 1.0f) * horizontal;
glm::vec3 pos {
x + lx + 0.5f,
glm::max(y - size.y / 2, getHeightAt(x + lx, z + lz)) + size.y / 2 + 1,
z + lz + 0.5f};
batch->quad(
pos,
{0, 0, -1},
{0, 1, 0},
size,
light,
glm::vec3(1.0f),
UVRegion(
(lz + z) * scale + timer * hspeed,
timer + y * scale + (x + lx) * k,
(lz + z + 1) * scale + timer * hspeed,
timer + (size.y + y) * scale + (x + lx) * k
)
);
}
}
for (int lz = -radius; lz <= radius; lz++) {
for (int lx = depth; lx > 0; lx--) {
glm::vec4 light = light_at(chunks, x + lx, y, z + lz);
random.setSeed(lx + x, lz + z);
float hspeed = (random.randFloat() * 2.0f - 1.0f) * horizontal;
glm::vec3 pos {
x + lx + 0.5f,
glm::max(y - size.y / 2, getHeightAt(x + lx, z + lz)) + size.y / 2 + 1,
z + lz + 0.5f};
batch->quad(
pos,
{0, 0, 1},
{0, 1, 0},
size,
light,
glm::vec3(1.0f),
UVRegion(
(lz + z) * scale + timer * hspeed,
timer + y * scale + (x + lx) * k,
(lz + z + 1) * scale + timer * hspeed,
timer + (size.y + y) * scale + (x + lx) * k
)
);
const struct {
glm::vec3 right;
glm::vec3 front;
} faces[] {
{{-1, 0, 0}, {0, 0, 1}},
{{1, 0, 0}, {0, 0, -1}},
{{0, 0, -1}, {-1, 0, 0}},
{{0, 0, 1}, {1, 0, 0}},
};
for (const auto& face : faces) {
for (int lx = -radius; lx <= radius; lx++) {
for (int lz = depth; lz > 0; lz--) {
glm::vec3 pos = face.right * static_cast<float>(lx) +
face.front * static_cast<float>(lz);
pos += glm::vec3(x, 0, z);
pos.y = glm::max(y - size.y / 2, getHeightAt(pos.x, pos.z)) +
size.y / 2 + 1;
pos += glm::vec3(0.5f, 0.0f, 0.5f);
float m = glm::sign(face.right.x + face.right.z);
int ux = pos.x;
int uz = pos.z;
if (glm::abs(face.right.x) < glm::abs(face.right.z)) {
std::swap(ux, uz);
}
glm::vec4 light = light_at(chunks, pos.x, y, pos.z);
random.setSeed(uz);
float hspeed = (random.randFloat() * 2.0f - 1.0f) * horizontal;
batch->quad(
pos,
face.right,
{0, 1, 0},
size,
light,
glm::vec3(1.0f),
UVRegion(
ux * scale + timer * hspeed * -m,
timer + pos.y * scale + uz * k,
(ux + m) * scale + timer * hspeed * -m,
timer + (size.y + pos.y) * scale + uz * k
)
);
}
}
}
batch->flush();