voxel-engine/include/chunk.hpp

71 lines
1.9 KiB
C++
Raw Normal View History

#ifndef CHUNK_H
#define CHUNK_H
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
2023-04-23 16:28:36 +02:00
#include <atomic>
#include <array>
#include <bitset>
#include <mutex>
#include <vector>
#include "block.hpp"
2022-11-20 19:14:11 +01:00
#include "spacefilling.hpp"
#include "intervalmap.hpp"
#include "shader.hpp"
#define CHUNK_SIZE 32
#define CHUNK_VOLUME (CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE)
#define CHUNK_MAX_INDEX (CHUNK_VOLUME - 1)
namespace Chunk
{
constexpr uint8_t CHUNK_STATE_GENERATED = 1;
constexpr uint8_t CHUNK_STATE_MESHED = 2;
2023-04-23 16:28:36 +02:00
constexpr uint8_t CHUNK_STATE_MESH_LOADED = 4;
constexpr uint8_t CHUNK_STATE_LOADED = 8;
constexpr uint8_t CHUNK_STATE_OUTOFVISION = 16;
constexpr uint8_t CHUNK_STATE_UNLOADED = 32;
2023-04-23 16:28:36 +02:00
constexpr uint8_t CHUNK_STATE_EMPTY = 64;
int coord3DTo1D(int x, int y, int z);
class Chunk
{
public:
Chunk(glm::vec3 pos = glm::vec3(0.0f)); // a default value for the argument satisfies the need for a default constructor when using the type in an unordered_map (i.e. in chunkmanager)
~Chunk();
public:
void createBuffers();
void deleteBuffers();
glm::vec3 getPosition() { return this->position; }
2023-04-23 16:28:36 +02:00
uint8_t getTotalState() { return this->state; }
bool getState(uint8_t n) { return (this->state & n) == n; }
void setState(uint8_t nstate, bool value);
void setBlock(Block b, int x, int y, int z);
void setBlocks(int start, int end, Block b);
Block getBlock(int x, int y, int z);
2022-11-20 19:14:11 +01:00
IntervalMap<Block>& getBlocks() { return (this->blocks); }
std::unique_ptr<Block[]> getBlocksArray(int* len) { return (this->blocks.toArray(len)); }
public:
2023-07-18 21:18:12 +02:00
GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, normalsBuffer{0}, numTriangles{0};
std::atomic<float> unload_timer{0};
private:
glm::vec3 position{};
2022-11-20 19:14:11 +01:00
IntervalMap<Block> blocks{};
2023-04-23 16:28:36 +02:00
std::atomic_uint8_t state{0};
};
};
#endif