ContentGfxCache

This commit is contained in:
MihailRis
2023-11-22 14:53:35 +03:00
parent bf4ad5ca58
commit d92347097e
14 changed files with 86 additions and 51 deletions
+9 -10
View File
@@ -11,6 +11,7 @@
#include "../voxels/VoxelsVolume.h"
#include "../voxels/ChunksStorage.h"
#include "../lighting/Lightmap.h"
#include "../frontend/ContentGfxCache.h"
using glm::ivec3;
using glm::vec3;
@@ -18,12 +19,15 @@ using glm::vec4;
#define VERTEX_SIZE 9
BlocksRenderer::BlocksRenderer(size_t capacity, const Content* content)
BlocksRenderer::BlocksRenderer(size_t capacity,
const Content* content,
const ContentGfxCache* cache)
: content(content),
vertexOffset(0),
indexOffset(0),
indexSize(0),
capacity(capacity) {
capacity(capacity),
cache(cache) {
vertexBuffer = new float[capacity];
indexBuffer = new int[capacity];
voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2);
@@ -301,11 +305,6 @@ vec4 BlocksRenderer::pickSoftLight(int x, int y, int z, const ivec3& right, cons
pickLight(x - right.x, y - right.y, z - right.z)) * 0.25f;
}
// Get texture atlas UV region for block face
inline UVRegion uvfor(const Block& def, uint face, int atlas_size) {
return *reinterpret_cast<const UVRegion*>(def.uvdata + face * 4);
}
void BlocksRenderer::render(const voxel* voxels, int atlas_size) {
int begin = chunk->bottom * (CHUNK_W * CHUNK_D);
int end = chunk->top * (CHUNK_W * CHUNK_D);
@@ -316,9 +315,9 @@ void BlocksRenderer::render(const voxel* voxels, int atlas_size) {
const Block& def = *blockDefsCache[id];
if (!id || def.drawGroup != group)
continue;
const UVRegion texfaces[6]{ uvfor(def, 0, atlas_size), uvfor(def, 1, atlas_size),
uvfor(def, 2, atlas_size), uvfor(def, 3, atlas_size),
uvfor(def, 4, atlas_size), uvfor(def, 5, atlas_size) };
const UVRegion texfaces[6]{ cache->getRegion(id, 0), cache->getRegion(id, 1),
cache->getRegion(id, 2), cache->getRegion(id, 3),
cache->getRegion(id, 4), cache->getRegion(id, 5)};
int x = i % CHUNK_W;
int y = i / (CHUNK_D * CHUNK_W);
int z = (i / CHUNK_D) % CHUNK_W;
+3 -1
View File
@@ -14,6 +14,7 @@ class Chunk;
class Chunks;
class VoxelsVolume;
class ChunksStorage;
class ContentGfxCache;
class BlocksRenderer {
const Content* const content;
@@ -29,6 +30,7 @@ class BlocksRenderer {
VoxelsVolume* voxelsBuffer;
const Block* const* blockDefsCache;
const ContentGfxCache* const cache;
void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light);
void index(int a, int b, int c, int d, int e, int f);
@@ -68,7 +70,7 @@ class BlocksRenderer {
glm::vec4 pickSoftLight(int x, int y, int z, const glm::ivec3& right, const glm::ivec3& up) const;
void render(const voxel* voxels, int atlas_size);
public:
BlocksRenderer(size_t capacity, const Content* content);
BlocksRenderer(size_t capacity, const Content* content, const ContentGfxCache* cache);
virtual ~BlocksRenderer();
Mesh* render(const Chunk* chunk, int atlas_size, const ChunksStorage* chunks);
+2 -2
View File
@@ -11,9 +11,9 @@
using glm::ivec2;
using std::shared_ptr;
ChunksRenderer::ChunksRenderer(Level* level) : level(level) {
ChunksRenderer::ChunksRenderer(Level* level, const ContentGfxCache* cache) : level(level) {
const int MAX_FULL_CUBES = 3000;
renderer = new BlocksRenderer(9 * 6 * 6 * MAX_FULL_CUBES, level->content);
renderer = new BlocksRenderer(9 * 6 * 6 * MAX_FULL_CUBES, level->content, cache);
}
ChunksRenderer::~ChunksRenderer() {
+2 -1
View File
@@ -11,13 +11,14 @@ class Mesh;
class Chunk;
class Level;
class BlocksRenderer;
class ContentGfxCache;
class ChunksRenderer {
BlocksRenderer* renderer;
Level* level;
std::unordered_map<glm::ivec2, std::shared_ptr<Mesh>> meshes;
public:
ChunksRenderer(Level* level);
ChunksRenderer(Level* level, const ContentGfxCache* cache);
virtual ~ChunksRenderer();
std::shared_ptr<Mesh> render(Chunk* chunk);