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 if (!blank::Intersection(ray, Bounds(), M)) {
51 if (!blkid && !dist && !normal) {
55 // TODO: should be possible to heavily optimize this
58 float closest_dist = std::numeric_limits<float>::infinity();
59 glm::vec3 closest_normal(0, 1, 0);
60 for (int z = 0; z < Depth(); ++z) {
61 for (int y = 0; y < Height(); ++y) {
62 for (int x = 0; x < Width(); ++x, ++id) {
63 if (!blocks[id].type->visible) {
68 glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
69 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
70 if (cur_dist < closest_dist) {
72 closest_dist = cur_dist;
73 closest_normal = cur_norm;
91 *normal = closest_normal;
96 void Chunk::Position(const glm::vec3 &pos) {
100 glm::mat4 Chunk::Transform(const glm::vec3 &offset) const {
101 return glm::translate((position - offset) * Extent());
105 int Chunk::VertexCount() const {
107 for (const auto &block : blocks) {
108 count += block.type->shape->VertexCount();
113 void Chunk::Update() {
115 model.Reserve(VertexCount());
117 for (size_t i = 0; i < Size(); ++i) {
118 blocks[i].type->FillModel(ToCoords(i), model);