X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld.cpp;h=c958a65cf3144942f8d47af09576872fd48aae9c;hb=2d8c7c015478a4528c0909f11d43998b1393948d;hp=96b48ee120d96ee4e79bec833936fb7ae310c2e8;hpb=ac8765b510707d77cac9620778f40ddf3a4ad2a2;p=blank.git diff --git a/src/world.cpp b/src/world.cpp index 96b48ee..c958a65 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,5 +1,7 @@ #include "world.hpp" +#include + namespace blank { @@ -87,6 +89,54 @@ void Chunk::Draw() { } +bool Chunk::Intersection(const Ray &ray, const glm::mat4 &M, int *blkid, float *dist) const { + { // rough check + const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}}; + if (!blank::Intersection(ray, bb, M)) { + return false; + } + } + + if (!blkid && !dist) { + return true; + } + + // TODO: should be possible to heavily optimize this + int id = 0; + int closest_id = -1; + float closest_dist = std::numeric_limits::infinity(); + for (int z = 0; z < Depth(); ++z) { + for (int y = 0; y < Height(); ++y) { + for (int x = 0; x < Width(); ++x, ++id) { + if (!blocks[id].type->visible) { + continue; + } + const AABB bb{{x, y, z}, {x+1, y+1, z+1}}; + float cur_dist; + if (blank::Intersection(ray, bb, M, &cur_dist)) { + if (cur_dist < closest_dist) { + closest_id = id; + closest_dist = cur_dist; + } + } + } + } + } + + if (closest_id < 0) { + return false; + } + + if (blkid) { + *blkid = closest_id; + } + if (dist) { + *dist = closest_dist; + } + return true; +} + + int Chunk::VertexCount() const { // TODO: query blocks as soon as type shapes are implemented return Size() * 6 * 6;