2 #include "block_def.hpp"
5 #include "face_dir.hpp"
10 #define SIZE DRAGONBLOCKS_CHUNK_SIZE
14 using namespace dragonblocks;
16 void Chunk::checkPos(const ivec3 &pos)
18 if (pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= SIZE || pos.y >= SIZE || pos.z >= SIZE)
19 throw out_of_range("Block position out of range");
22 const Block *Chunk::getBlock(const ivec3 &pos) const
25 return &data.blocks[pos.x][pos.y][pos.z];
28 const Block *Chunk::getBlockNoEx(const ivec3 &pos) const
32 } catch (std::out_of_range &) {
37 void Chunk::setBlock(const ivec3 &pos, const Block &block)
40 data.blocks[pos.x][pos.y][pos.z] = block;
43 void Chunk::setBlockNoEx(const ivec3 &pos, const Block &block)
47 } catch (std::out_of_range &) {
51 void Chunk::updateMesh()
53 std::vector<GLfloat> vertices;
54 std::vector<Texture> textures;
56 for (int x = 0; x < SIZE; x++) {
57 for (int y = 0; y < SIZE; y++) {
58 for (int z = 0; z < SIZE; z++) {
59 Block *block = &data.blocks[x][y][z];
60 BlockDef *def = block->getDef();
64 vec3 pos_from_mesh_origin = pos - vec3(SIZE / 2 + 0.5);
65 for (int facenr = 0; facenr < 6; facenr++) {
66 const Block *neighbor = map->getBlockRelativePos(this, pos + (vec3)face_dir[facenr]);
67 if (neighbor && ! neighbor->getDef()->drawable) {
68 textures.push_back(def->tiles[facenr]);
69 for (int vertex_index = 0; vertex_index < 6; vertex_index++) {
70 for (int attribute_index = 0; attribute_index < 5; attribute_index++) {
71 int index = facenr * 6 * 5 + vertex_index * 5 + attribute_index;
72 GLdouble value = cube[index];
73 switch (attribute_index) {
75 value += pos_from_mesh_origin.x;
78 value += pos_from_mesh_origin.y;
81 value += pos_from_mesh_origin.z;
84 vertices.push_back(value);
93 bool is_new = ! deleteMesh();
95 if (textures.size() == 0) {
99 mesh = new Mesh(scene);
100 mesh->pos = pos * SIZE + SIZE / 2;
101 mesh->size = vec3(1.0);
102 mesh->configureVertexObjects(&vertices[0], vertices.size() * sizeof(GLfloat));
103 mesh->textures = textures;
104 mesh->vertices_per_texture = 6;
106 mesh->effect = Mesh::Effect::Type::FLYIN;
110 void Chunk::addMeshUpdateTask()
112 mesh_gen_thread->addTask(this);
115 bool Chunk::deleteMesh()
118 mesh->removeFromScene();
126 Chunk::Chunk(Map *m, const glm::ivec3 &p, const Data &d, MeshGenThread *mgt, Scene *s) : map(m), data(d), pos(p), mesh_gen_thread(mgt), scene(s)