4 #include <glm/gtx/transform.hpp>
16 Chunk::Chunk(Chunk &&other)
17 : blocks(std::move(other.blocks))
18 , model(std::move(other.model))
19 , dirty(other.dirty) {
23 Chunk &Chunk::operator =(Chunk &&other) {
24 blocks = std::move(other.blocks);
25 model = std::move(other.model);
39 bool Chunk::Intersection(
44 glm::vec3 *normal) const {
46 const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}};
47 if (!blank::Intersection(ray, bb, M)) {
52 if (!blkid && !dist && !normal) {
56 // TODO: should be possible to heavily optimize this
59 float closest_dist = std::numeric_limits<float>::infinity();
60 glm::vec3 closest_normal(0, 1, 0);
61 for (int z = 0; z < Depth(); ++z) {
62 for (int y = 0; y < Height(); ++y) {
63 for (int x = 0; x < Width(); ++x, ++id) {
64 if (!blocks[id].type->visible) {
69 glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
70 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
71 if (cur_dist < closest_dist) {
73 closest_dist = cur_dist;
74 closest_normal = cur_norm;
92 *normal = closest_normal;
97 void Chunk::Position(const glm::vec3 &pos) {
101 glm::mat4 Chunk::Transform(const glm::vec3 &offset) const {
102 return glm::translate((position - offset) * Extent());
106 int Chunk::VertexCount() const {
108 for (const auto &block : blocks) {
109 count += block.type->shape->VertexCount();
114 void Chunk::Update() {
116 model.Reserve(VertexCount());
118 for (size_t i = 0; i < Size(); ++i) {
119 blocks[i].type->FillModel(ToCoords(i), model);