Introduced Content, ContentIndices, no more integer ids hardcoded, common refactor

This commit is contained in:
MihailRis
2023-11-21 11:38:59 +03:00
parent f2f9343737
commit 3b03464d27
35 changed files with 472 additions and 279 deletions
+11 -4
View File
@@ -5,6 +5,7 @@
#include "Mesh.h"
#include "UVRegion.h"
#include "../constants.h"
#include "../content/Content.h"
#include "../voxels/Block.h"
#include "../voxels/Chunk.h"
#include "../voxels/VoxelsVolume.h"
@@ -17,10 +18,16 @@ using glm::vec4;
#define VERTEX_SIZE 9
BlocksRenderer::BlocksRenderer(size_t capacity) : vertexOffset(0), indexOffset(0), indexSize(0), capacity(capacity) {
BlocksRenderer::BlocksRenderer(size_t capacity, const Content* content)
: content(content),
vertexOffset(0),
indexOffset(0),
indexSize(0),
capacity(capacity) {
vertexBuffer = new float[capacity];
indexBuffer = new int[capacity];
voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2);
blockDefsCache = content->indices->getBlockDefs();
}
BlocksRenderer::~BlocksRenderer() {
@@ -256,7 +263,7 @@ bool BlocksRenderer::isOpen(int x, int y, int z, ubyte group) const {
blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x, y, chunk->z * CHUNK_D + z);
if (id == BLOCK_VOID)
return false;
const Block& block = *Block::blocks[id];
const Block& block = *blockDefsCache[id];
if (block.drawGroup != group && block.lightPassing) {
return true;
}
@@ -267,7 +274,7 @@ bool BlocksRenderer::isOpenForLight(int x, int y, int z) const {
blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x, y, chunk->z * CHUNK_D + z);
if (id == BLOCK_VOID)
return false;
const Block& block = *Block::blocks[id];
const Block& block = *blockDefsCache[id];
if (block.lightPassing) {
return true;
}
@@ -311,7 +318,7 @@ void BlocksRenderer::render(const voxel* voxels, int atlas_size) {
for (int i = begin; i < end; i++) {
const voxel& vox = voxels[i];
blockid_t id = vox.id;
const Block& def = *Block::blocks[id];
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),
+5 -1
View File
@@ -7,6 +7,7 @@
#include "../typedefs.h"
#include "../voxels/voxel.h"
class Content;
class Mesh;
class Block;
class Chunk;
@@ -15,6 +16,7 @@ class VoxelsVolume;
class ChunksStorage;
class BlocksRenderer {
const Content* const content;
float* vertexBuffer;
int* indexBuffer;
size_t vertexOffset;
@@ -26,6 +28,8 @@ class BlocksRenderer {
const Chunk* chunk = nullptr;
VoxelsVolume* voxelsBuffer;
const Block* const* blockDefsCache;
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);
@@ -64,7 +68,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);
BlocksRenderer(size_t capacity, const Content* content);
virtual ~BlocksRenderer();
Mesh* render(const Chunk* chunk, int atlas_size, const ChunksStorage* chunks);
+1 -1
View File
@@ -13,7 +13,7 @@ using std::shared_ptr;
ChunksRenderer::ChunksRenderer(Level* level) : level(level) {
const int MAX_FULL_CUBES = 3000;
renderer = new BlocksRenderer(9 * 6 * 6 * MAX_FULL_CUBES);
renderer = new BlocksRenderer(9 * 6 * 6 * MAX_FULL_CUBES, level->content);
}
ChunksRenderer::~ChunksRenderer() {