2023-02-12 15:25:52 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett on 11/02/23.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FINALPROJECT_STORAGE_H
|
|
|
|
#define FINALPROJECT_STORAGE_H
|
|
|
|
|
2023-03-01 00:52:27 -05:00
|
|
|
#include <blt/math/math.h>
|
2023-02-12 15:25:52 -05:00
|
|
|
#include <vector>
|
|
|
|
#include "blt/std/logging.h"
|
2023-03-06 00:33:43 -05:00
|
|
|
#include "blt/std/format.h"
|
2023-02-13 16:30:01 -05:00
|
|
|
#include <world/chunk/typedefs.h>
|
2023-02-13 23:37:18 -05:00
|
|
|
#include <world/registry.h>
|
2023-02-14 18:19:39 -05:00
|
|
|
#include <unordered_map>
|
2023-03-06 23:39:25 -05:00
|
|
|
#ifdef __EMSCRIPTEN__
|
2023-03-06 00:33:43 -05:00
|
|
|
#include <phmap.h>
|
2023-03-06 23:39:25 -05:00
|
|
|
#else
|
2023-03-06 00:33:43 -05:00
|
|
|
#include <spp.h>
|
2023-03-06 23:39:25 -05:00
|
|
|
#endif
|
2023-02-12 15:25:52 -05:00
|
|
|
|
|
|
|
// contains storage classes for block IDs inside chunks plus eventual lookup of block states
|
|
|
|
|
|
|
|
namespace fp {
|
|
|
|
|
|
|
|
class block_storage {
|
|
|
|
private:
|
2023-02-13 23:37:18 -05:00
|
|
|
block_type* blocks;
|
2023-02-12 15:25:52 -05:00
|
|
|
public:
|
|
|
|
block_storage() {
|
2023-02-13 23:37:18 -05:00
|
|
|
blocks = new unsigned char[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
|
2023-02-12 15:25:52 -05:00
|
|
|
for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE; i++)
|
2023-02-13 23:37:18 -05:00
|
|
|
blocks[i] = fp::registry::AIR;
|
2023-02-12 15:25:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
~block_storage() {
|
|
|
|
delete[] blocks;
|
|
|
|
}
|
|
|
|
|
2023-02-13 23:37:18 -05:00
|
|
|
[[nodiscard]] inline block_type get(const block_pos& pos) const {
|
2023-02-13 16:30:01 -05:00
|
|
|
return blocks[pos.z * CHUNK_SIZE * CHUNK_SIZE + pos.y * CHUNK_SIZE + pos.x];
|
2023-02-12 15:25:52 -05:00
|
|
|
}
|
|
|
|
|
2023-03-05 15:21:35 -05:00
|
|
|
[[nodiscard]] inline bool checkBlockVisibility(const block_pos& pos) const {
|
2023-03-06 10:19:48 -05:00
|
|
|
// blocks outside the check are always not visible to us. This requires special logic
|
2023-02-13 23:37:18 -05:00
|
|
|
if (pos.x < 0 || pos.x >= CHUNK_SIZE || pos.y < 0 || pos.y >= CHUNK_SIZE || pos.z < 0 || pos.z >= CHUNK_SIZE) {
|
2023-03-05 15:21:35 -05:00
|
|
|
return false;
|
2023-02-13 23:37:18 -05:00
|
|
|
}
|
2023-03-06 23:39:25 -05:00
|
|
|
return fp::registry::get(get(pos)).visibility > fp::registry::OPAQUE;
|
2023-02-13 23:37:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void set(const block_pos& pos, block_type blockID) {
|
2023-02-13 16:30:01 -05:00
|
|
|
blocks[pos.z * CHUNK_SIZE * CHUNK_SIZE + pos.y * CHUNK_SIZE + pos.x] = blockID;
|
2023-02-12 15:25:52 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class mesh_storage {
|
|
|
|
private:
|
2023-03-06 23:39:25 -05:00
|
|
|
// spp doesn't support emscripten, but phmap does work
|
|
|
|
// slightly more memory consumption but still much lower than std::unordered_map (plus much faster access)
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
phmap::flat_hash_map<vertex, unsigned int, _static::vertex_hash, _static::vertex_equality> created_vertices_index;
|
|
|
|
#else
|
2023-03-06 00:33:43 -05:00
|
|
|
spp::sparse_hash_map<vertex, unsigned int, _static::vertex_hash, _static::vertex_equality> created_vertices_index;
|
2023-03-06 23:39:25 -05:00
|
|
|
#endif
|
2023-02-14 18:19:39 -05:00
|
|
|
std::vector<vertex> vertices;
|
|
|
|
std::vector<unsigned int> indices;
|
2023-02-12 15:25:52 -05:00
|
|
|
public:
|
2023-02-14 18:19:39 -05:00
|
|
|
/**
|
|
|
|
* since a chunk mesh contains all the faces for all the blocks inside the chunk
|
|
|
|
* we can add the translated values of predefined "unit" faces. This is for the simple "fast" chunk mesh generator.
|
|
|
|
* @param face the direction the face is facing to be added to the mesh.
|
|
|
|
* @param pos position of the face
|
|
|
|
*/
|
2023-03-05 00:01:54 -05:00
|
|
|
void addFace(face face, const block_pos& pos, unsigned char texture_index);
|
2023-02-12 15:25:52 -05:00
|
|
|
|
2023-02-14 18:19:39 -05:00
|
|
|
inline std::vector<vertex>& getVertices() {
|
2023-02-12 15:25:52 -05:00
|
|
|
return vertices;
|
|
|
|
}
|
2023-02-14 18:19:39 -05:00
|
|
|
inline std::vector<unsigned int>& getIndices() {
|
|
|
|
return indices;
|
|
|
|
}
|
2023-02-12 15:25:52 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace mesh {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif //FINALPROJECT_STORAGE_H
|