4 #include <glm/gtx/transform.hpp>
17 Chunk::Chunk(Chunk &&other)
18 : blocks(std::move(other.blocks))
19 , model(std::move(other.model))
20 , dirty(other.dirty) {
24 Chunk &Chunk::operator =(Chunk &&other) {
25 blocks = std::move(other.blocks);
26 model = std::move(other.model);
32 void Chunk::Allocate() {
33 blocks.resize(Size());
45 bool Chunk::Intersection(
50 glm::vec3 *normal) const {
52 if (!blank::Intersection(ray, Bounds(), M)) {
57 if (!blkid && !dist && !normal) {
61 // TODO: should be possible to heavily optimize this
64 float closest_dist = std::numeric_limits<float>::infinity();
65 glm::vec3 closest_normal(0, 1, 0);
66 for (int z = 0; z < Depth(); ++z) {
67 for (int y = 0; y < Height(); ++y) {
68 for (int x = 0; x < Width(); ++x, ++id) {
69 if (!blocks[id].type->visible) {
74 glm::vec3 pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
75 if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
76 if (cur_dist < closest_dist) {
78 closest_dist = cur_dist;
79 closest_normal = cur_norm;
97 *normal = closest_normal;
102 void Chunk::Position(const glm::tvec3<int> &pos) {
106 glm::mat4 Chunk::Transform(const glm::tvec3<int> &offset) const {
107 return glm::translate((position - offset) * Extent());
111 int Chunk::VertexCount() const {
113 for (const auto &block : blocks) {
114 count += block.type->shape->VertexCount();
119 void Chunk::Update() {
121 model.Reserve(VertexCount());
123 for (size_t i = 0; i < Size(); ++i) {
124 blocks[i].type->FillModel(ToCoords(i), model);