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 void Chunk::CheckUpdate() {
121 void Chunk::Update() {
122 int vtx_count = 0, idx_count = 0;
123 for (const auto &block : blocks) {
124 const Shape *shape = Type(block).shape;
125 vtx_count += shape->VertexCount();
126 idx_count += shape->VertexIndexCount();
129 model.Reserve(vtx_count, idx_count);
131 Model::Index vtx_counter = 0;
132 for (size_t i = 0; i < Size(); ++i) {
133 const BlockType &type = Type(blocks[i]);
134 type.FillModel(model, ToCoords(i), vtx_counter);
135 vtx_counter += type.shape->VertexCount();