4 #include <glm/gtx/transform.hpp>
9 Chunk::Chunk(const BlockTypeRegistry &types)
18 Chunk::Chunk(Chunk &&other)
20 , blocks(std::move(other.blocks))
21 , model(std::move(other.model))
22 , dirty(other.dirty) {
26 Chunk &Chunk::operator =(Chunk &&other) {
28 blocks = std::move(other.blocks);
29 model = std::move(other.model);
35 void Chunk::Allocate() {
36 blocks.resize(Size());
48 bool Chunk::Intersection(
53 glm::vec3 *normal) const {
55 if (!blank::Intersection(ray, Bounds(), M)) {
60 if (!blkid && !dist && !normal) {
64 // TODO: should be possible to heavily optimize this
67 float closest_dist = std::numeric_limits<float>::infinity();
68 glm::vec3 closest_normal(0, 1, 0);
69 for (int z = 0; z < Depth(); ++z) {
70 for (int y = 0; y < Height(); ++y) {
71 for (int x = 0; x < Width(); ++x, ++id) {
72 if (!Type(blocks[id]).visible) {
77 Block::Pos pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
78 if (Type(blocks[id]).shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
79 if (cur_dist < closest_dist) {
81 closest_dist = cur_dist;
82 closest_normal = cur_norm;
100 *normal = closest_normal;
105 void Chunk::Position(const Pos &pos) {
109 glm::mat4 Chunk::Transform(const Pos &offset) const {
110 return glm::translate((position - offset) * Extent());
114 int Chunk::VertexCount() const {
116 for (const auto &block : blocks) {
117 count += Type(block).shape->VertexCount();
122 void Chunk::Update() {
124 model.Reserve(VertexCount());
126 for (size_t i = 0; i < Size(); ++i) {
127 Type(blocks[i]).FillModel(ToCoords(i), model);