AABB blocks rotation support (WIP part 2)

This commit is contained in:
MihailRis 2023-12-05 10:01:26 +03:00
parent 9d941e934c
commit 7f9e87cea2
3 changed files with 48 additions and 27 deletions

View File

@ -8,7 +8,7 @@
"pane" "pane"
], ],
"model": "aabb", "model": "aabb",
"hitbox": [0.0, 0.0, 0.0, 1.0, 1.0, 0.1], "hitbox": [0.0, 0.0, 0.0, 1.0, 1.0, 0.2],
"light-passing": true, "light-passing": true,
"sky-light-passing": true, "sky-light-passing": true,
"rotation": "pipe" "rotation": "pipe"

View File

@ -42,6 +42,7 @@ BlocksRenderer::~BlocksRenderer() {
delete[] indexBuffer; delete[] indexBuffer;
} }
/* Basic vertex add method */
void BlocksRenderer::vertex(const vec3& coord, void BlocksRenderer::vertex(const vec3& coord,
float u, float v, float u, float v,
const vec4& light) { const vec4& light) {
@ -75,7 +76,9 @@ void BlocksRenderer::index(int a, int b, int c, int d, int e, int f) {
indexOffset += 4; indexOffset += 4;
} }
void BlocksRenderer::face(const vec3& coord, float w, float h, /* Add face with precalculated lights */
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,
@ -227,19 +230,18 @@ void BlocksRenderer::blockXSprite(int x, int y, int z,
texface2, lights, vec4(tint)); texface2, lights, vec4(tint));
} }
void BlocksRenderer::blockCubeShaded(const vec3& pos, /* AABB blocks render method (WIP) */
void BlocksRenderer::blockCubeShaded(const ivec3& icoord,
const vec3& offset,
const vec3& size, const vec3& size,
const UVRegion(&texfaces)[6], const UVRegion(&texfaces)[6],
const Block* block, ubyte states) { const Block* block, ubyte states) {
float x = pos.x;
float y = pos.y;
float z = pos.z;
ivec3 X(1, 0, 0); ivec3 X(1, 0, 0);
ivec3 Y(0, 1, 0); ivec3 Y(0, 1, 0);
ivec3 Z(0, 0, 1); ivec3 Z(0, 0, 1);
ivec3 loff(0); ivec3 loff(0);
ivec3 coord(x, y, z); ivec3 coord = icoord;
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];
@ -250,18 +252,23 @@ void BlocksRenderer::blockCubeShaded(const vec3& pos,
loff -= orient.fix; loff -= orient.fix;
} }
vec3 local = pos - vec3(coord); vec3 fX(X);
local -= loff; vec3 fY(Y);
face(coord, X, Y, Z, Z+loff, local, size.x, size.y, -size.z, texfaces[5]); vec3 fZ(Z);
face(coord+X-Z, -X, Y, -Z, Z-Z-X+loff, local, size.x, size.y, 0.0f, texfaces[4]);
face(coord+Y, X, -Z, Y, Y-Y+loff, local-vec3(Z)*size.z, size.x, size.z, 0.0f, texfaces[3]); vec3 local = offset.x*vec3(X)+offset.y*vec3(Y)+offset.z*vec3(-Z);
face(coord-Z, X, Z, -Y, -Y+Z+loff, local, size.x, size.z, 0.0f, texfaces[2]); //local -= loff;
face(coord, X, Y, Z, Z+loff, local-size.z*fZ, size.x, size.y, size.z, texfaces[5]);
face(coord+X, -X, Y, -Z, Z-Z-X+loff, local-size.z*fZ, size.x, size.y, 0.0f, texfaces[4]);
face(coord+X, -Z, Y, X, X-X+loff, local-vec3(Z)*size.z, size.z, size.y, 0.0f, texfaces[1]); face(coord+Y, X, -Z, Y, Y-Y+loff, local, size.x, size.z, 0.0f, texfaces[3]); //;
face(coord-Z, Z, Y, -X, -X+Z+loff, local, size.z, size.y, 0.0f, texfaces[0]); face(coord+X, -X, -Z, -Y, -Y+Z+loff, local, size.x, size.z, 0.0f, texfaces[2]); //;
face(coord+X, -Z, Y, X, X-X+loff, local, size.z, size.y, 0.0f, texfaces[1]); //;
face(coord+Y, -Z, -Y, -X, -X+Z+loff, local, size.z, size.y, 0.0f, texfaces[0]); //;
} }
/* Fastest solid shaded blocks render method */
void BlocksRenderer::blockCubeShaded(int x, int y, int z, void BlocksRenderer::blockCubeShaded(int x, int y, int z,
const UVRegion(&texfaces)[6], const UVRegion(&texfaces)[6],
const Block* block, const Block* block,
@ -365,6 +372,7 @@ vec4 BlocksRenderer::pickSoftLight(float x, float y, float z,
return pickSoftLight({int(round(x)), int(round(y)), int(round(z))}, right, up); return pickSoftLight({int(round(x)), int(round(y)), int(round(z))}, right, up);
} }
#include <iostream>
void BlocksRenderer::render(const voxel* voxels) { void BlocksRenderer::render(const voxel* voxels) {
int begin = chunk->bottom * (CHUNK_W * CHUNK_D); int begin = chunk->bottom * (CHUNK_W * CHUNK_D);
int end = chunk->top * (CHUNK_W * CHUNK_D); int end = chunk->top * (CHUNK_W * CHUNK_D);
@ -399,9 +407,18 @@ void BlocksRenderer::render(const voxel* voxels) {
break; break;
} }
case BlockModel::aabb: { case BlockModel::aabb: {
vec3 size = def.hitbox.size(); AABB hitbox = def.hitbox;
vec3 off = def.hitbox.min(); hitbox.a = vec3(1.0f)-hitbox.a;
blockCubeShaded(off+vec3(x,y,z), size, texfaces, &def, vox.states); hitbox.b = vec3(1.0f)-hitbox.b;
std::cout << hitbox.a.x << " " << hitbox.a.y << " " << hitbox.a.z << " --- ";
std::cout << hitbox.b.x << " " << hitbox.b.y << " " << hitbox.b.z << std::endl;
vec3 size = hitbox.size();
std::cout << "s " << size.x << " " << size.y << " " << size.z << std::endl;
vec3 off = hitbox.min();
std::cout << "m " << off.x << " " << off.y << " " << off.z << std::endl;
blockCubeShaded(ivec3(x,y,z), off, size, texfaces, &def, vox.states);
break; break;
} }
default: default:

View File

@ -84,10 +84,14 @@ class BlocksRenderer {
} }
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);
/* Fastest solid shaded blocks render method */
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);
/* AABB blocks render method (WIP)*/ void blockCubeShaded(const glm::ivec3& coord,
void blockCubeShaded(const glm::vec3& pos, const glm::vec3& size, const UVRegion(&faces)[6], const Block* block, ubyte states); const glm::vec3& offset,
const glm::vec3& size,
const UVRegion(&faces)[6],
const Block* block,
ubyte states);
void blockXSprite(int x, int y, int z, const 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;