refactor BlocksRenderer
This commit is contained in:
parent
16e7d64db3
commit
c89d31ca20
@ -34,16 +34,16 @@ BlocksRenderer::BlocksRenderer(
|
|||||||
cache(cache),
|
cache(cache),
|
||||||
settings(settings)
|
settings(settings)
|
||||||
{
|
{
|
||||||
vertexBuffer = new float[capacity];
|
vertexBuffer = std::make_unique<float[]>(capacity);
|
||||||
indexBuffer = new int[capacity];
|
indexBuffer = std::make_unique<int[]>(capacity);
|
||||||
voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2);
|
voxelsBuffer = std::make_unique<VoxelsVolume>(
|
||||||
|
CHUNK_W + voxelBufferPadding*2,
|
||||||
|
CHUNK_H,
|
||||||
|
CHUNK_D + voxelBufferPadding*2);
|
||||||
blockDefsCache = content->getIndices()->getBlockDefs();
|
blockDefsCache = content->getIndices()->getBlockDefs();
|
||||||
}
|
}
|
||||||
|
|
||||||
BlocksRenderer::~BlocksRenderer() {
|
BlocksRenderer::~BlocksRenderer() {
|
||||||
delete voxelsBuffer;
|
|
||||||
delete[] vertexBuffer;
|
|
||||||
delete[] indexBuffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Basic vertex add method */
|
/* Basic vertex add method */
|
||||||
@ -416,12 +416,14 @@ void BlocksRenderer::render(const voxel* voxels) {
|
|||||||
if (id == 0 || def.drawGroup != drawGroup || state.segment) {
|
if (id == 0 || def.drawGroup != drawGroup || state.segment) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const UVRegion texfaces[6]{ cache->getRegion(id, 0),
|
const UVRegion texfaces[6] {
|
||||||
|
cache->getRegion(id, 0),
|
||||||
cache->getRegion(id, 1),
|
cache->getRegion(id, 1),
|
||||||
cache->getRegion(id, 2),
|
cache->getRegion(id, 2),
|
||||||
cache->getRegion(id, 3),
|
cache->getRegion(id, 3),
|
||||||
cache->getRegion(id, 4),
|
cache->getRegion(id, 4),
|
||||||
cache->getRegion(id, 5)};
|
cache->getRegion(id, 5)
|
||||||
|
};
|
||||||
int x = i % CHUNK_W;
|
int x = i % CHUNK_W;
|
||||||
int y = i / (CHUNK_D * CHUNK_W);
|
int y = i / (CHUNK_D * CHUNK_W);
|
||||||
int z = (i / CHUNK_D) % CHUNK_W;
|
int z = (i / CHUNK_D) % CHUNK_W;
|
||||||
@ -445,16 +447,21 @@ void BlocksRenderer::render(const voxel* voxels) {
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (overflow)
|
if (overflow) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void BlocksRenderer::build(const Chunk* chunk, const ChunksStorage* chunks) {
|
void BlocksRenderer::build(const Chunk* chunk, const ChunksStorage* chunks) {
|
||||||
this->chunk = chunk;
|
this->chunk = chunk;
|
||||||
voxelsBuffer->setPosition(chunk->x * CHUNK_W - 1, 0, chunk->z * CHUNK_D - 1);
|
voxelsBuffer->setPosition(
|
||||||
chunks->getVoxels(voxelsBuffer, settings->graphics.backlight.get());
|
chunk->x * CHUNK_W - voxelBufferPadding,
|
||||||
|
0,
|
||||||
|
chunk->z * CHUNK_D - voxelBufferPadding
|
||||||
|
);
|
||||||
|
chunks->getVoxels(voxelsBuffer.get(), settings->graphics.backlight.get());
|
||||||
overflow = false;
|
overflow = false;
|
||||||
vertexOffset = 0;
|
vertexOffset = 0;
|
||||||
indexOffset = indexSize = 0;
|
indexOffset = indexSize = 0;
|
||||||
@ -465,7 +472,9 @@ void BlocksRenderer::build(const Chunk* chunk, const ChunksStorage* chunks) {
|
|||||||
std::shared_ptr<Mesh> BlocksRenderer::createMesh() {
|
std::shared_ptr<Mesh> BlocksRenderer::createMesh() {
|
||||||
const vattr attrs[]{ {3}, {2}, {1}, {0} };
|
const vattr attrs[]{ {3}, {2}, {1}, {0} };
|
||||||
size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE;
|
size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE;
|
||||||
return std::make_shared<Mesh>(vertexBuffer, vcount, indexBuffer, indexSize, attrs);
|
return std::make_shared<Mesh>(
|
||||||
|
vertexBuffer.get(), vcount, indexBuffer.get(), indexSize, attrs
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Mesh> BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) {
|
std::shared_ptr<Mesh> BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) {
|
||||||
@ -474,5 +483,5 @@ std::shared_ptr<Mesh> BlocksRenderer::render(const Chunk* chunk, const ChunksSto
|
|||||||
}
|
}
|
||||||
|
|
||||||
VoxelsVolume* BlocksRenderer::getVoxelsBuffer() const {
|
VoxelsVolume* BlocksRenderer::getVoxelsBuffer() const {
|
||||||
return voxelsBuffer;
|
return voxelsBuffer.get();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include "../../voxels/voxel.hpp"
|
#include "../../voxels/voxel.hpp"
|
||||||
#include "../../typedefs.hpp"
|
#include "../../typedefs.hpp"
|
||||||
@ -22,16 +23,15 @@ class BlocksRenderer {
|
|||||||
static const glm::vec3 SUN_VECTOR;
|
static const glm::vec3 SUN_VECTOR;
|
||||||
static const uint VERTEX_SIZE;
|
static const uint VERTEX_SIZE;
|
||||||
const Content* const content;
|
const Content* const content;
|
||||||
float* vertexBuffer;
|
std::unique_ptr<float[]> vertexBuffer;
|
||||||
int* indexBuffer;
|
std::unique_ptr<int[]> indexBuffer;
|
||||||
size_t vertexOffset;
|
size_t vertexOffset;
|
||||||
size_t indexOffset, indexSize;
|
size_t indexOffset, indexSize;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
|
int voxelBufferPadding = 2;
|
||||||
bool overflow = false;
|
bool overflow = false;
|
||||||
|
|
||||||
const Chunk* chunk = nullptr;
|
const Chunk* chunk = nullptr;
|
||||||
VoxelsVolume* voxelsBuffer;
|
std::unique_ptr<VoxelsVolume> voxelsBuffer;
|
||||||
|
|
||||||
const Block* const* blockDefsCache;
|
const Block* const* blockDefsCache;
|
||||||
const ContentGfxCache* const cache;
|
const ContentGfxCache* const cache;
|
||||||
@ -40,36 +40,41 @@ class BlocksRenderer {
|
|||||||
void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light);
|
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);
|
void index(int a, int b, int c, int d, int e, int f);
|
||||||
|
|
||||||
void vertex(const glm::vec3& coord, float u, float v,
|
void vertex(
|
||||||
|
const glm::vec3& coord, float u, float v,
|
||||||
const glm::vec4& brightness,
|
const glm::vec4& brightness,
|
||||||
const glm::vec3& axisX,
|
const glm::vec3& axisX,
|
||||||
const glm::vec3& axisY,
|
const glm::vec3& axisY,
|
||||||
const glm::vec3& axisZ);
|
const glm::vec3& axisZ
|
||||||
|
);
|
||||||
void face(const glm::vec3& coord, float w, float h, float d,
|
void face(
|
||||||
|
const glm::vec3& coord,
|
||||||
|
float w, float h, float d,
|
||||||
const glm::vec3& axisX,
|
const glm::vec3& axisX,
|
||||||
const glm::vec3& axisY,
|
const glm::vec3& axisY,
|
||||||
const glm::vec3& axisZ,
|
const glm::vec3& axisZ,
|
||||||
const UVRegion& region,
|
const UVRegion& region,
|
||||||
const glm::vec4(&lights)[4],
|
const glm::vec4(&lights)[4],
|
||||||
const glm::vec4& tint);
|
const glm::vec4& tint
|
||||||
|
);
|
||||||
void face(const glm::vec3& coord,
|
void face(
|
||||||
|
const glm::vec3& coord,
|
||||||
const glm::vec3& axisX,
|
const glm::vec3& axisX,
|
||||||
const glm::vec3& axisY,
|
const glm::vec3& axisY,
|
||||||
const glm::vec3& axisZ,
|
const glm::vec3& axisZ,
|
||||||
const UVRegion& region,
|
const UVRegion& region,
|
||||||
bool lights);
|
bool lights
|
||||||
|
);
|
||||||
void tetragonicFace(const glm::vec3& coord,
|
void tetragonicFace(
|
||||||
|
const glm::vec3& coord,
|
||||||
const glm::vec3& p1, const glm::vec3& p2,
|
const glm::vec3& p1, const glm::vec3& p2,
|
||||||
const glm::vec3& p3, const glm::vec3& p4,
|
const glm::vec3& p3, const glm::vec3& p4,
|
||||||
const glm::vec3& X,
|
const glm::vec3& X,
|
||||||
const glm::vec3& Y,
|
const glm::vec3& Y,
|
||||||
const glm::vec3& Z,
|
const glm::vec3& Z,
|
||||||
const UVRegion& texreg,
|
const UVRegion& texreg,
|
||||||
bool lights);
|
bool lights
|
||||||
|
);
|
||||||
void blockCube(
|
void blockCube(
|
||||||
int x, int y, int z,
|
int x, int y, int z,
|
||||||
const UVRegion(&faces)[6],
|
const UVRegion(&faces)[6],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user