voxelcore/src/graphics/render/BlockWrapsRenderer.hpp
ostermine 89bdadd6aa render the world relative to an origin near the camera
World coordinates were passed to GPU as floats: u_model of a chunk and
of a model had the world translation, vertices of translucent blocks,
particles, precipitation, block wrappers, lines and 3D texts were built
in the world space, and the camera position was subtracted in shaders.
The float step grows with the distance from the world origin (6 cm at
1M blocks, 2 m at 30M): blocks got skewed, then stuck together.

Camera::getOrigin() is an integer point near the camera (X and Z are
rounded to 1024 blocks, Y is 0). Camera::getView() is relative to it,
and every position passed to GPU is "world - origin" calculated on CPU,
so the shaders keep working with small numbers and do not change:
u_cameraPos, a_modelpos, u_inverseView and the shadows matrices are in
the same space as before, shifted by the origin. Matrices of a shadow
map are converted from the origin of its camera to the origin of the
player camera. The clouds shader gets the origin wrapped by the period
of its waves, the waves stay bound to the world.

Vertices of translucent blocks are local to the chunk now, as vertices
of opaque ones, and drawn with u_model of the chunk.
2026-11-12 10:05:06 +03:00

69 lines
1.6 KiB
C++

#pragma once
#include "assets/assets_util.hpp"
#include "voxels/Block.hpp"
#include "MainBatch.hpp"
#include "typedefs.hpp"
#include <array>
#include <string>
#include <memory>
#include <unordered_map>
#include <map>
class Assets;
class Camera;
class Content;
class Chunks;
class Texture;
class DrawContext;
struct BlockWrapper {
glm::ivec3 position;
std::array<std::string, 6> textureFaces {};
std::array<glm::vec4, 6> tints {};
float emission = 0.0f;
// --- render cache ---
util::TextureRegion texRegions[6] {};
UVRegion uvRegions[6] {};
BlockModelType modelType {};
uint8_t cullingBits = 0xFF;
uint8_t dirtySides = 0xFF;
};
class BlockWrapsRenderer {
const Assets& assets;
const Content& content;
const Chunks& chunks;
std::unique_ptr<MainBatch> batch;
std::multimap<const Texture*, BlockWrapper*> renderOrder;
std::unordered_map<u64id_t, std::unique_ptr<BlockWrapper>> wrappers;
u64id_t nextWrapper = 1;
void draw(
BlockWrapper& wrapper, const Texture* texture, const glm::ivec3& origin
);
void refreshWrapper(BlockWrapper& wrapper);
void clearOrder(const BlockWrapper* const wrapper);
public:
BlockWrapsRenderer(
const Assets& assets, const Content& content, const Chunks& chunks
);
~BlockWrapsRenderer();
void draw(const DrawContext& ctx, const Camera& camera);
u64id_t add(
const glm::ivec3& position,
const std::string& texture,
const glm::vec4& tint,
float emission
);
BlockWrapper* get(u64id_t id) const;
void remove(u64id_t id);
};