BlocksRenderer simplified (part 1)

This commit is contained in:
MihailRis
2023-12-28 03:49:24 +03:00
parent 86ab2d8145
commit 837fd2f32f
3 changed files with 72 additions and 75 deletions
+1 -1
View File
@@ -85,7 +85,7 @@ bool WorldRenderer::drawChunk(size_t index,
if (!frustumCulling->IsBoxVisible(min, max)) return false; if (!frustumCulling->IsBoxVisible(min, max)) return false;
} }
vec3 coord = vec3(chunk->x*CHUNK_W, 0.0f, chunk->z*CHUNK_D+1); vec3 coord = vec3(chunk->x*CHUNK_W+0.5f, 0.5f, chunk->z*CHUNK_D+0.5f);
mat4 model = glm::translate(mat4(1.0f), coord); mat4 model = glm::translate(mat4(1.0f), coord);
shader->uniformMatrix("u_model", model); shader->uniformMatrix("u_model", model);
mesh->draw(); mesh->draw();
+59 -55
View File
@@ -18,6 +18,7 @@ using glm::vec3;
using glm::vec4; using glm::vec4;
const uint BlocksRenderer::VERTEX_SIZE = 6; const uint BlocksRenderer::VERTEX_SIZE = 6;
const vec3 BlocksRenderer::SUN_VECTOR (0.411934f, 0.863868f, -0.279161f);
BlocksRenderer::BlocksRenderer(size_t capacity, BlocksRenderer::BlocksRenderer(size_t capacity,
const Content* content, const Content* content,
@@ -78,9 +79,10 @@ void BlocksRenderer::index(int a, int b, int c, int d, int e, int f) {
/* Add face with precalculated lights */ /* Add face with precalculated lights */
void BlocksRenderer::face(const vec3& coord, void BlocksRenderer::face(const vec3& coord,
float w, float h, float w, float h, float d,
const vec3& axisX, const vec3& axisX,
const vec3& axisY, const vec3& axisY,
const vec3& axisZ,
const UVRegion& region, const UVRegion& region,
const vec4(&lights)[4], const vec4(&lights)[4],
const vec4& tint) { const vec4& tint) {
@@ -88,20 +90,24 @@ void BlocksRenderer::face(const vec3& coord,
overflow = true; overflow = true;
return; return;
} }
vertex(coord, region.u1, region.v1, lights[0] * tint); vec3 X = axisX * w;
vertex(coord + axisX * w, region.u2, region.v1, lights[1] * tint); vec3 Y = axisY * h;
vertex(coord + axisX * w + axisY * h, region.u2, region.v2, lights[2] * tint); vec3 Z = axisZ * d;
vertex(coord + axisY * h, region.u1, region.v2, lights[3] * tint); float s = 0.5f;
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, lights[0] * tint);
vertex(coord + (X - Y + Z) * s, region.u2, region.v1, lights[1] * tint);
vertex(coord + (X + Y + Z) * s, region.u2, region.v2, lights[2] * tint);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, lights[3] * tint);
index(0, 1, 3, 1, 2, 3); index(0, 1, 3, 1, 2, 3);
} }
void BlocksRenderer::vertex(const ivec3& coord, void BlocksRenderer::vertex(const vec3& coord,
float u, float v, float u, float v,
const vec4& tint, const vec4& tint,
const ivec3& axisX, const vec3& axisX,
const ivec3& axisY, const vec3& axisY,
const ivec3& axisZ) { const vec3& axisZ) {
vec4 light = pickSoftLight(coord+axisZ, axisX, axisY); vec4 light = pickSoftLight(coord+axisZ*0.5f+(axisX+axisY)*0.5f, axisX, axisY);
vertex(coord, u, v, light * tint); vertex(coord, u, v, light * tint);
} }
@@ -115,27 +121,26 @@ void BlocksRenderer::vertex(const vec3& coord,
vertex(coord, u, v, light * tint); vertex(coord, u, v, light * tint);
} }
void BlocksRenderer::face(const ivec3& coord, void BlocksRenderer::face(const vec3& coord,
const ivec3& axisX, const vec3& X,
const ivec3& axisY, const vec3& Y,
const ivec3& axisZ, const vec3& Z,
const ivec3& laxisZ,
const UVRegion& region) { const UVRegion& region) {
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) {
overflow = true; overflow = true;
return; return;
} }
const vec3 sunVector = vec3(0.411934f, 0.863868f, -0.279161f); float d = glm::dot(Z, SUN_VECTOR);
float d = glm::dot(vec3(axisZ.x, axisZ.y, axisZ.z), sunVector); d = 0.7f + d * 0.3f;
d = 0.7f + d*0.3f;
vec4 tint(d); vec4 tint(d);
vertex(coord, region.u1, region.v1, tint, axisX, axisY, laxisZ); float s = 0.5f;
vertex(coord + axisX, region.u2, region.v1, tint, axisX, axisY, laxisZ); vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, X, Y, Z);
vertex(coord + axisX + axisY, region.u2, region.v2, tint, axisX, axisY, laxisZ); vertex(coord + (X - Y + Z) * s, region.u2, region.v1, tint, X, Y, Z);
vertex(coord + axisY, region.u1, region.v2, tint, axisX, axisY, laxisZ); vertex(coord + (X + Y + Z) * s, region.u2, region.v2, tint, X, Y, Z);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, X, Y, Z);
index(0, 1, 2, 0, 2, 3); index(0, 1, 2, 0, 2, 3);
} }
@@ -159,9 +164,8 @@ void BlocksRenderer::face(const vec3& coord,
const vec3 Z(axisZ); const vec3 Z(axisZ);
if (lights) { if (lights) {
const vec3 sunVector = vec3(0.431934f, 0.863868f, -0.259161f); float d = glm::dot(Z, SUN_VECTOR);
float d = glm::dot(Z, sunVector); d = 0.75f + d * 0.25f;
d = 0.75f + d*0.25f;
vec4 tint(d); vec4 tint(d);
vertex(coord + Z*depth, region.u1, region.v1, tint, axisX, axisY, laxisZ); vertex(coord + Z*depth, region.u1, region.v1, tint, axisX, axisY, laxisZ);
@@ -179,26 +183,27 @@ void BlocksRenderer::face(const vec3& coord,
void BlocksRenderer::blockCube(int x, int y, int z, const UVRegion(&texfaces)[6], ubyte group) { void BlocksRenderer::blockCube(int x, int y, int z, 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) };
vec3 coord(x, y, z);
if (isOpen(x, y, z + 1, group)) { if (isOpen(x, y, z + 1, group)) {
face(vec3(x, y, z), 1, 1, vec3(1, 0, 0), vec3(0, 1, 0), texfaces[5], lights, vec4(1.0f)); face(coord, 1, 1, 1, vec3(1, 0, 0), vec3(0, 1, 0), vec3(0, 0, 1), texfaces[5], lights, vec4(1.0f));
} }
if (isOpen(x, y, z - 1, group)) { if (isOpen(x, y, z - 1, group)) {
face(vec3(x + 1, y, z - 1), 1, 1, vec3(-1, 0, 0), vec3(0, 1, 0), texfaces[4], lights, vec4(1.0f)); face(coord, 1, 1, 1, vec3(-1, 0, 0), vec3(0, 1, 0), vec3(0, 0, -1), texfaces[4], lights, vec4(1.0f));
} }
if (isOpen(x, y + 1, z, group)) { if (isOpen(x, y + 1, z, group)) {
face(vec3(x, y + 1, z), 1, 1, vec3(1, 0, 0), vec3(0, 0, -1), texfaces[3], lights); face(coord, 1, 1, 1, vec3(1, 0, 0), vec3(0, 0, -1), vec3(0, 1, 0), texfaces[3], lights, vec4(1.0f));
} }
if (isOpen(x, y - 1, z, group)) { if (isOpen(x, y - 1, z, group)) {
face(vec3(x, y, z - 1), 1, 1, vec3(1, 0, 0), vec3(0, 0, 1), texfaces[2], lights, vec4(1.0f)); face(coord, 1, 1, 1, vec3(1, 0, 0), vec3(0, 0, 1), vec3(0, -1, 0), texfaces[2], lights, vec4(1.0f));
} }
if (isOpen(x - 1, y, z, group)) { if (isOpen(x - 1, y, z, group)) {
face(vec3(x, y, z - 1), 1, 1, vec3(0, 0, 1), vec3(0, 1, 0), texfaces[0], lights, vec4(1.0f)); face(coord, 1, 1, 1, vec3(0, 0, 1), vec3(0, 1, 0), vec3(-1, 0, 0), texfaces[0], lights, vec4(1.0f));
} }
if (isOpen(x + 1, y, z, group)) { if (isOpen(x + 1, y, z, group)) {
face(vec3(x + 1, y, z), 1, 1, vec3(0, 0, -1), vec3(0, 1, 0), texfaces[1], lights, vec4(1.0f)); face(coord, 1, 1, 1, vec3(0, 0, -1), vec3(0, 1, 0), vec3(1, 0, 0), texfaces[1], lights, vec4(1.0f));
} }
} }
@@ -220,24 +225,25 @@ void BlocksRenderer::blockXSprite(int x, int y, int z,
const float w = size.x / 1.41f; const float w = size.x / 1.41f;
const float tint = 0.8f; const float tint = 0.8f;
face(vec3(x + xs + (1.0 - w) * 0.5f, y, z + zs - 1 + (1.0 - w) * 0.5f),
w, size.y, vec3(1.0f, 0, 1.0f), vec3(0, 1, 0), face(vec3(x + xs, y, z + zs),
w, size.y, 0, vec3(1, 0, 1), vec3(0, 1, 0), vec3(),
texface1, lights, vec4(tint)); texface1, lights, vec4(tint));
face(vec3(x + xs - (1.0 - w) * 0.5f + 1, y, z + zs - (1.0 - w) * 0.5f), face(vec3(x + xs, y, z + zs),
w, size.y, vec3(-1.0f, 0, -1.0f), vec3(0, 1, 0), w, size.y, 0, vec3(-1, 0, -1), vec3(0, 1, 0), vec3(),
texface1, lights, vec4(tint)); texface1, lights, vec4(tint));
face(vec3(x + xs + (1.0 - w) * 0.5f, y, z + zs - (1.0 - w) * 0.5f), face(vec3(x + xs, y, z + zs),
w, size.y, vec3(1.0f, 0, -1.0f), vec3(0, 1, 0), w, size.y, 0, vec3(1, 0, -1), vec3(0, 1, 0), vec3(),
texface2, lights, vec4(tint)); texface1, lights, vec4(tint));
face(vec3(x + xs - (1.0 - w) * 0.5f + 1, y, z + zs + (1.0 - w) * 0.5f - 1), face(vec3(x + xs, y, z + zs),
w, size.y, vec3(-1.0f, 0, 1.0f), vec3(0, 1, 0), w, size.y, 0, vec3(-1, 0, 1), vec3(0, 1, 0), vec3(),
texface2, lights, vec4(tint)); texface1, lights, vec4(tint));
} }
// HINT: texture faces order: {east, west, bottom, top, south, north} // HINT: texture faces order: {east, west, bottom, top, south, north}
/* AABB blocks render method (WIP) */ /* AABB blocks render method (WIP) //FIXME */
void BlocksRenderer::blockAABB(const ivec3& icoord, void BlocksRenderer::blockAABB(const ivec3& icoord,
const UVRegion(&texfaces)[6], const UVRegion(&texfaces)[6],
const Block* block, ubyte rotation, const Block* block, ubyte rotation,
@@ -263,6 +269,7 @@ void BlocksRenderer::blockAABB(const ivec3& icoord,
coord += orient.fix; coord += orient.fix;
loff -= orient.fix; loff -= orient.fix;
} }
coord -= vec3(0.5, 0.5f, -0.5f);
vec3 fX(X); vec3 fX(X);
vec3 fY(Y); vec3 fY(Y);
vec3 fZ(Z); vec3 fZ(Z);
@@ -297,38 +304,35 @@ void BlocksRenderer::blockCubeShaded(int x, int y, int z,
ubyte states) { ubyte states) {
ubyte group = block->drawGroup; ubyte group = block->drawGroup;
ivec3 X(1, 0, 0); vec3 X(1, 0, 0);
ivec3 Y(0, 1, 0); vec3 Y(0, 1, 0);
ivec3 Z(0, 0, 1); vec3 Z(0, 0, 1);
ivec3 loff(0); vec3 coord(x, y, z);
ivec3 coord(x, y, z);
if (block->rotatable) { if (block->rotatable) {
auto& rotations = block->rotations; auto& rotations = block->rotations;
auto& orient = rotations.variants[states & BLOCK_ROT_MASK]; auto& orient = rotations.variants[states & BLOCK_ROT_MASK];
X = orient.axisX; X = orient.axisX;
Y = orient.axisY; Y = orient.axisY;
Z = orient.axisZ; Z = orient.axisZ;
coord += orient.fix;
loff -= orient.fix;
} }
if (isOpen(x+Z.x, y+Z.y, z+Z.z, group)) { if (isOpen(x+Z.x, y+Z.y, z+Z.z, group)) {
face(coord, X, Y, Z, Z+loff, texfaces[5]); face(coord, X, Y, Z, texfaces[5]);
} }
if (isOpen(x-Z.x, y-Z.y, z-Z.z, group)) { if (isOpen(x-Z.x, y-Z.y, z-Z.z, group)) {
face(coord+X-Z, -X, Y, -Z, Z-Z-X+loff, texfaces[4]); face(coord, -X, Y, -Z, texfaces[4]);
} }
if (isOpen(x+Y.x, y+Y.y, z+Y.z, group)) { if (isOpen(x+Y.x, y+Y.y, z+Y.z, group)) {
face(coord+Y, X, -Z, Y, Y-Y+loff, texfaces[3]); face(coord, X, -Z, Y, texfaces[3]);
} }
if (isOpen(x-Y.x, y-Y.y, z-Y.z, group)) { if (isOpen(x-Y.x, y-Y.y, z-Y.z, group)) {
face(coord-Z, X, Z, -Y, -Y+Z+loff, texfaces[2]); face(coord, X, Z, -Y, texfaces[2]);
} }
if (isOpen(x+X.x, y+X.y, z+X.z, group)) { if (isOpen(x+X.x, y+X.y, z+X.z, group)) {
face(coord+X, -Z, Y, X, X-X+loff, texfaces[1]); face(coord, -Z, Y, X, texfaces[1]);
} }
if (isOpen(x-X.x, y-X.y, z-X.z, group)) { if (isOpen(x-X.x, y-X.y, z-X.z, group)) {
face(coord-Z, Z, Y, -X, -X+Z+loff, texfaces[0]); face(coord, Z, Y, -X, texfaces[0]);
} }
} }
+11 -18
View File
@@ -18,6 +18,7 @@ class ChunksStorage;
class ContentGfxCache; class ContentGfxCache;
class BlocksRenderer { class BlocksRenderer {
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; float* vertexBuffer;
@@ -38,11 +39,11 @@ 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::ivec3& 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::ivec3& axisX, const glm::vec3& axisX,
const glm::ivec3& axisY, const glm::vec3& axisY,
const glm::ivec3& axisZ); const glm::vec3& axisZ);
void vertex(const glm::vec3& coord, void vertex(const glm::vec3& coord,
float u, float v, float u, float v,
@@ -51,18 +52,18 @@ class BlocksRenderer {
const glm::ivec3& axisY, const glm::ivec3& axisY,
const glm::ivec3& axisZ); const glm::ivec3& axisZ);
void face(const glm::vec3& coord, float w, float h, 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 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::ivec3& coord, void face(const glm::vec3& coord,
const glm::ivec3& axisX, const glm::vec3& axisX,
const glm::ivec3& axisY, const glm::vec3& axisY,
const glm::ivec3& axisZ, const glm::vec3& axisZ,
const glm::ivec3& laxisZ,
const UVRegion& region); const UVRegion& region);
void face(const glm::vec3& coord, void face(const glm::vec3& coord,
@@ -76,14 +77,6 @@ class BlocksRenderer {
const UVRegion& region, const UVRegion& region,
bool lights); bool lights);
void face(const glm::vec3& coord, float w, float h,
const glm::vec3& axisX,
const glm::vec3& axisY,
const UVRegion& region,
const glm::vec4(&lights)[4]) {
face(coord, w, h, axisX, axisY, region, lights, glm::vec4(1.0f));
}
void blockCube(int x, int y, int z, const UVRegion(&faces)[6], ubyte group); void blockCube(int x, int y, int z, const UVRegion(&faces)[6], ubyte group);
void blockCubeShaded(int x, int y, int z, const UVRegion(&faces)[6], const Block* block, ubyte states); void blockCubeShaded(int x, int y, int z, const UVRegion(&faces)[6], const Block* block, ubyte states);