AABB blocks rotation support (WIP part 3)

This commit is contained in:
MihailRis 2023-12-05 10:25:43 +03:00
parent 7f9e87cea2
commit 64f6f17386
5 changed files with 21 additions and 26 deletions

View File

@ -27,20 +27,14 @@ Content* ContentBuilder::build() {
// Generating runtime info
def->rt.id = blockDefsIndices.size();
def->rt.emissive = *((uint32_t*)def->emission);
// building hitbox grid 3d for raycasts
const AABB& hitbox = def->hitbox;
for (uint gy = 0; gy < BLOCK_AABB_GRID; gy++) {
for (uint gz = 0; gz < BLOCK_AABB_GRID; gz++) {
for (uint gx = 0; gx < BLOCK_AABB_GRID; gx++) {
float x = gx / float(BLOCK_AABB_GRID);
float y = gy / float(BLOCK_AABB_GRID);
float z = gz / float(BLOCK_AABB_GRID);
bool flag = hitbox.inside({x, y, z});
if (!flag)
def->rt.solid = false;
def->rt.hitboxGrid[gy][gz][gx] = flag;
}
def->rt.solid = def->model == BlockModel::block;
if (def->rotatable) {
const AABB& hitbox = def->hitbox;
for (uint i = 0; i < BlockRotProfile::MAX_COUNT; i++) {
AABB aabb = hitbox;
def->rotations.variants[i].transform(aabb);
def->rt.hitboxes[i] = aabb;
}
}
@ -48,6 +42,8 @@ Content* ContentBuilder::build() {
if (groups->find(def->drawGroup) == groups->end()) {
groups->insert(def->drawGroup);
}
}
ContentIndices* indices = new ContentIndices(blockDefsIndices);
return new Content(indices, groups, blockDefs);

View File

@ -372,7 +372,6 @@ vec4 BlocksRenderer::pickSoftLight(float x, float y, float z,
return pickSoftLight({int(round(x)), int(round(y)), int(round(z))}, right, up);
}
#include <iostream>
void BlocksRenderer::render(const voxel* voxels) {
int begin = chunk->bottom * (CHUNK_W * CHUNK_D);
int end = chunk->top * (CHUNK_W * CHUNK_D);
@ -410,14 +409,9 @@ void BlocksRenderer::render(const voxel* voxels) {
AABB hitbox = def.hitbox;
hitbox.a = vec3(1.0f)-hitbox.a;
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;
}

View File

@ -30,4 +30,5 @@ Block::Block(std::string name)
Block::Block(std::string name, std::string texture) : name(name),
textureFaces{texture,texture,texture,texture,texture,texture} {
rotations = BlockRotProfile::PIPE;
}

View File

@ -28,7 +28,8 @@ struct CoordSystem {
};
struct BlockRotProfile {
CoordSystem variants[16];
static const int MAX_COUNT = 16;
CoordSystem variants[MAX_COUNT];
/* Wood logs, pillars, pipes
3 orientations supported
@ -65,7 +66,7 @@ public:
blockid_t id;
bool solid = true;
bool emissive = false;
bool hitboxGrid[BLOCK_AABB_GRID][BLOCK_AABB_GRID][BLOCK_AABB_GRID];
AABB hitboxes[BlockRotProfile::MAX_COUNT];
} rt;
Block(std::string name);

View File

@ -73,11 +73,14 @@ const AABB* Chunks::isObstacle(float x, float y, float z){
return nullptr;
const Block* def = contentIds->getBlockDef(v->id);
if (def->obstacle) {
const AABB& hitbox = def->rotatable
? def->rt.hitboxes[v->states & BLOCK_ROT_MASK]
: def->hitbox;
if (def->rt.solid) {
return &def->hitbox;
return &hitbox;
} else {
if (def->hitbox.inside({x - ix, y - iy, z - iz}))
return &def->hitbox;
if (hitbox.inside({x - ix, y - iy, z - iz}))
return &hitbox;
return nullptr;
}
}
@ -222,7 +225,7 @@ voxel* Chunks::rayCast(vec3 start,
// TODO: replace this dumb solution with something better
if (def && !def->rt.solid) {
const int gridSize = BLOCK_AABB_GRID * 2;
const AABB& box = def->hitbox;
const AABB& box = def->rotatable ? def->rt.hitboxes[voxel->states & BLOCK_ROT_MASK] : def->hitbox;
const int subs = gridSize;
iend = vec3(ix, iy, iz);
end -= iend;