Merge pull request #19 from clasher113/main

Optimization
This commit is contained in:
MihailRis 2023-11-19 17:54:11 +03:00 committed by GitHub
commit 12db8a1448
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 66 deletions

View File

@ -28,9 +28,9 @@ BlocksRenderer::~BlocksRenderer() {
delete[] buffer; delete[] buffer;
} }
void BlocksRenderer::vertex(vec3 coord, void BlocksRenderer::vertex(const vec3& coord,
float u, float v, float u, float v,
vec4 light) { const vec4& light) {
buffer[offset++] = coord.x; buffer[offset++] = coord.x;
buffer[offset++] = coord.y; buffer[offset++] = coord.y;
buffer[offset++] = coord.z; buffer[offset++] = coord.z;
@ -44,12 +44,12 @@ void BlocksRenderer::vertex(vec3 coord,
buffer[offset++] = light.a; buffer[offset++] = light.a;
} }
void BlocksRenderer::face(vec3 coord, float w, float h, void BlocksRenderer::face(const vec3& coord, float w, float h,
const vec3 axisX, const vec3& axisX,
const vec3 axisY, const vec3& axisY,
const UVRegion& region, const UVRegion& region,
const vec4 lights[4], const vec4 (&lights)[4],
const vec4 tint) { const vec4& tint) {
if (offset + VERTEX_SIZE * 6 > capacity) { if (offset + VERTEX_SIZE * 6 > capacity) {
overflow = true; overflow = true;
return; return;
@ -63,12 +63,12 @@ void BlocksRenderer::face(vec3 coord, float w, float h,
vertex(coord + axisY * h, region.u1, region.v2, lights[3] * tint); vertex(coord + axisY * h, region.u1, region.v2, lights[3] * tint);
} }
void BlocksRenderer::face(vec3 coord, float w, float h, void BlocksRenderer::face(const vec3& coord, float w, float h,
const vec3 axisX, const vec3& axisX,
const vec3 axisY, const vec3& axisY,
const UVRegion& region, const UVRegion& region,
const vec4 lights[4], const vec4 (&lights)[4],
const vec4 tint, const vec4& tint,
bool rotated) { bool rotated) {
if (offset + VERTEX_SIZE * 6 > capacity) { if (offset + VERTEX_SIZE * 6 > capacity) {
overflow = true; overflow = true;
@ -94,7 +94,7 @@ void BlocksRenderer::face(vec3 coord, float w, float h,
} }
} }
void BlocksRenderer::cube(vec3 coord, vec3 size, const UVRegion texfaces[6]) { void BlocksRenderer::cube(const vec3& coord, const vec3& size, const UVRegion (&texfaces)[6]) {
vec4 lights[]{ vec4(),vec4(),vec4(),vec4() }; vec4 lights[]{ vec4(),vec4(),vec4(),vec4() };
face(coord, size.x, size.y, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[0], lights); face(coord, size.x, size.y, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[0], lights);
@ -111,7 +111,7 @@ inline vec4 do_tint(float value) {
return vec4(value); return vec4(value);
} }
void BlocksRenderer::blockCube(int x, int y, int z, vec3 size, const UVRegion texfaces[6], ubyte group) { void BlocksRenderer::blockCube(int x, int y, int z, const vec3& size, const UVRegion (&texfaces)[6], ubyte group) {
vec4 lights[]{ vec4(1.0f), vec4(1.0f), vec4(1.0f), vec4(1.0f) }; vec4 lights[]{ vec4(1.0f), vec4(1.0f), vec4(1.0f), vec4(1.0f) };
if (isOpen(x, y, z + 1, group)) { if (isOpen(x, y, z + 1, group)) {
face(vec3(x, y, z), size.x, size.y, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[5], lights, do_tint(0.9f)); face(vec3(x, y, z), size.x, size.y, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[5], lights, do_tint(0.9f));
@ -136,7 +136,7 @@ void BlocksRenderer::blockCube(int x, int y, int z, vec3 size, const UVRegion te
} }
} }
void BlocksRenderer::blockXSprite(int x, int y, int z, vec3 size, const UVRegion texface1, const UVRegion texface2, float spread) { void BlocksRenderer::blockXSprite(int x, int y, int z, const vec3& size, const UVRegion& texface1, const UVRegion& texface2, float spread) {
vec4 lights[]{ vec4 lights[]{
pickSoftLight(x, y+1, z, {1, 0, 0}, {0, 1, 0}), pickSoftLight(x, y+1, z, {1, 0, 0}, {0, 1, 0}),
pickSoftLight(x + 1, y+1, z, {1, 0, 0}, {0, 1, 0}), pickSoftLight(x + 1, y+1, z, {1, 0, 0}, {0, 1, 0}),
@ -164,7 +164,7 @@ void BlocksRenderer::blockXSprite(int x, int y, int z, vec3 size, const UVRegion
vec3(-1.0f, 0, 1.0f), vec3(0, 1, 0), texface2, lights, do_tint(0.8f)); vec3(-1.0f, 0, 1.0f), vec3(0, 1, 0), texface2, lights, do_tint(0.8f));
} }
void BlocksRenderer::blockCubeShaded(int x, int y, int z, vec3 size, const UVRegion texfaces_[6], const Block* block, ubyte states) { void BlocksRenderer::blockCubeShaded(int x, int y, int z, const vec3& size, const UVRegion (&texfaces_)[6], const Block* block, ubyte states) {
ubyte group = block->drawGroup; ubyte group = block->drawGroup;
UVRegion texfaces[6]; UVRegion texfaces[6];
int rot = 0; int rot = 0;
@ -282,7 +282,7 @@ vec4 BlocksRenderer::pickLight(int x, int y, int z) const {
} }
} }
vec4 BlocksRenderer::pickSoftLight(int x, int y, int z, ivec3 right, ivec3 up) const { vec4 BlocksRenderer::pickSoftLight(int x, int y, int z, const ivec3& right, const ivec3& up) const {
return (pickLight(x - right.x - up.x, y - right.y - up.y, z - right.z - up.z) + return (pickLight(x - right.x - up.x, y - right.y - up.y, z - right.z - up.z) +
pickLight(x - up.x, y - up.y, z - up.z) + pickLight(x - up.x, y - up.y, z - up.z) +
pickLight(x, y, z) + pickLight(x, y, z) +
@ -300,38 +300,39 @@ inline UVRegion uvfor(const Block& def, uint face, int atlas_size) {
} }
void BlocksRenderer::render(const voxel* voxels, int atlas_size) { 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);
for (ubyte group = 0; group < 8; group++) { for (ubyte group = 0; group < 8; group++) {
for (uint y = 0; y < CHUNK_H; y++) { for (int i = begin; i < end; i++) {
for (uint z = 0; z < CHUNK_D; z++) { const voxel& vox = voxels[i];
for (uint x = 0; x < CHUNK_W; x++) { blockid_t id = vox.id;
const voxel& vox = voxels[((y * CHUNK_D) + z) * CHUNK_W + x]; const Block& def = *Block::blocks[id];
blockid_t id = vox.id; if (!id || def.drawGroup != group)
const Block& def = *Block::blocks[id]; continue;
if (!id || def.drawGroup != group) const UVRegion texfaces[6]{ uvfor(def, 0, atlas_size), uvfor(def, 1, atlas_size),
continue; uvfor(def, 2, atlas_size), uvfor(def, 3, atlas_size),
const UVRegion texfaces[6]{ uvfor(def, 0, atlas_size), uvfor(def, 1, atlas_size), uvfor(def, 4, atlas_size), uvfor(def, 5, atlas_size) };
uvfor(def, 2, atlas_size), uvfor(def, 3, atlas_size), int x = i % CHUNK_W;
uvfor(def, 4, atlas_size), uvfor(def, 5, atlas_size) }; int y = i / (CHUNK_D * CHUNK_W);
switch (def.model) { int z = (i / CHUNK_D) % CHUNK_W;
case BlockModel::block: switch (def.model) {
if (*((light_t*)&def.emission)) { case BlockModel::block:
blockCube(x, y, z, vec3(1, 1, 1), texfaces, def.drawGroup); if (*((light_t*)&def.emission)) {
} blockCube(x, y, z, vec3(1, 1, 1), texfaces, def.drawGroup);
else {
blockCubeShaded(x, y, z, vec3(1, 1, 1), texfaces, &def, vox.states);
}
break;
case BlockModel::xsprite: {
blockXSprite(x, y, z, vec3(1, 1, 1), texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f);
break;
}
default:
break;
}
if (overflow)
return;
} }
else {
blockCubeShaded(x, y, z, vec3(1, 1, 1), texfaces, &def, vox.states);
}
break;
case BlockModel::xsprite: {
blockXSprite(x, y, z, vec3(1, 1, 1), texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f);
break;
} }
default:
break;
}
if (overflow)
return;
} }
} }
} }

View File

@ -24,41 +24,41 @@ class BlocksRenderer {
const Chunk* chunk = nullptr; const Chunk* chunk = nullptr;
VoxelsVolume* voxelsBuffer; VoxelsVolume* voxelsBuffer;
void vertex(glm::vec3 coord, float u, float v, glm::vec4 light); void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light);
void face(glm::vec3 coord, float w, float h, void face(const glm::vec3& coord, float w, float h,
const glm::vec3 axisX, const glm::vec3& axisX,
const glm::vec3 axisY, const glm::vec3& axisY,
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(glm::vec3 coord, float w, float h, void face(const glm::vec3& coord, float w, float h,
const glm::vec3 axisX, const glm::vec3& axisX,
const glm::vec3 axisY, const glm::vec3& axisY,
const UVRegion& region, const UVRegion& region,
const glm::vec4 lights[4], const glm::vec4 (&lights)[4],
const glm::vec4 tint, const glm::vec4& tint,
bool rotated); bool rotated);
void face(glm::vec3 coord, float w, float h, void face(const glm::vec3& coord, float w, float h,
const glm::vec3 axisX, const glm::vec3& axisX,
const glm::vec3 axisY, const glm::vec3& axisY,
const UVRegion& region, const UVRegion& region,
const glm::vec4 lights[4]) { const glm::vec4 (&lights)[4]) {
face(coord, w, h, axisX, axisY, region, lights, glm::vec4(1.0f)); face(coord, w, h, axisX, axisY, region, lights, glm::vec4(1.0f));
} }
void cube(glm::vec3 coord, glm::vec3 size, const UVRegion faces[6]); void cube(const glm::vec3& coord, const glm::vec3& size, const UVRegion (&faces)[6]);
void blockCube(int x, int y, int z, glm::vec3 size, const UVRegion faces[6], ubyte group); void blockCube(int x, int y, int z, const glm::vec3& size, const UVRegion (&faces)[6], ubyte group);
void blockCubeShaded(int x, int y, int z, glm::vec3 size, const UVRegion faces[6], const Block* block, ubyte states); void blockCubeShaded(int x, int y, int z, const glm::vec3& size, const UVRegion (&faces)[6], const Block* block, ubyte states);
void blockXSprite(int x, int y, int z, glm::vec3 size, const UVRegion face1, const UVRegion face2, float spread); void blockXSprite(int x, int y, int z, const glm::vec3& size, const UVRegion& face1, const UVRegion& face2, float spread);
bool isOpenForLight(int x, int y, int z) const; bool isOpenForLight(int x, int y, int z) const;
bool isOpen(int x, int y, int z, ubyte group) const; bool isOpen(int x, int y, int z, ubyte group) const;
glm::vec4 pickLight(int x, int y, int z) const; glm::vec4 pickLight(int x, int y, int z) const;
glm::vec4 pickSoftLight(int x, int y, int z, glm::ivec3 right, glm::ivec3 up) const; 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); void render(const voxel* voxels, int atlas_size);
public: public:
BlocksRenderer(size_t capacity); BlocksRenderer(size_t capacity);