4 #include <glm/gtx/transform.hpp>
17 Chunk::Chunk(Chunk &&other)
18 : blocks(std::move(other.blocks))
19 , model(std::move(other.model))
20 , transform(other.transform)
21 , dirty(other.dirty) {
25 Chunk &Chunk::operator =(Chunk &&other) {
26 blocks = std::move(other.blocks);
27 model = std::move(other.model);
28 transform = other.transform;
42 bool Chunk::Intersection(
47 glm::vec3 *normal) const {
49 const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}};
50 if (!blank::Intersection(ray, bb, M)) {
55 if (!blkid && !dist && !normal) {
59 // TODO: should be possible to heavily optimize this
62 float closest_dist = std::numeric_limits<float>::infinity();
63 glm::vec3 closest_normal(0, 1, 0);
64 for (int z = 0; z < Depth(); ++z) {
65 for (int y = 0; y < Height(); ++y) {
66 for (int x = 0; x < Width(); ++x, ++id) {
67 if (!blocks[id].type->visible) {
72 glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
73 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
74 if (cur_dist < closest_dist) {
76 closest_dist = cur_dist;
77 closest_normal = cur_norm;
95 *normal = closest_normal;
100 void Chunk::Position(const glm::vec3 &pos) {
102 transform = glm::translate(pos * 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);