2025-04-06 02:03:23 +00:00
|
|
|
|
#ifndef VOXELGAME_H
|
|
|
|
|
#define VOXELGAME_H
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
2025-04-06 03:14:06 +00:00
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <unordered_map>
|
2025-04-06 02:03:23 +00:00
|
|
|
|
#include "GreedyMesher.h"
|
|
|
|
|
|
2025-04-06 03:14:06 +00:00
|
|
|
|
// Structure representing a chunk (16×16×16 voxels)
|
|
|
|
|
struct Chunk {
|
|
|
|
|
int x, z; // Chunk grid coordinates
|
|
|
|
|
// 3D voxel data: dimensions [16][16][16]
|
|
|
|
|
std::vector<std::vector<std::vector<int>>> voxels;
|
|
|
|
|
// **Merged mesh faces** produced by the greedy mesher
|
|
|
|
|
std::vector<Quad> mesh;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// A key for identifying a chunk by its grid coordinates.
|
|
|
|
|
struct ChunkKey {
|
|
|
|
|
int x, z;
|
|
|
|
|
bool operator==(const ChunkKey &other) const {
|
|
|
|
|
return x == other.x && z == other.z;
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-04-06 02:09:28 +00:00
|
|
|
|
|
2025-04-06 03:14:06 +00:00
|
|
|
|
struct ChunkKeyHash {
|
|
|
|
|
std::size_t operator()(const ChunkKey &key) const {
|
|
|
|
|
return std::hash<int>()(key.x) ^ (std::hash<int>()(key.z) << 1);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-04-06 02:09:28 +00:00
|
|
|
|
|
2025-04-06 02:03:23 +00:00
|
|
|
|
class VoxelGame {
|
|
|
|
|
public:
|
|
|
|
|
VoxelGame();
|
|
|
|
|
~VoxelGame();
|
2025-04-06 03:14:06 +00:00
|
|
|
|
|
2025-04-06 02:03:23 +00:00
|
|
|
|
bool init();
|
2025-04-06 03:14:06 +00:00
|
|
|
|
void update(float deltaTime, const float cameraPos[3]);
|
2025-04-06 02:03:23 +00:00
|
|
|
|
void render();
|
|
|
|
|
void debugUI();
|
2025-04-06 03:14:06 +00:00
|
|
|
|
|
2025-04-06 02:03:23 +00:00
|
|
|
|
private:
|
2025-04-06 03:14:06 +00:00
|
|
|
|
bool loadTextures();
|
|
|
|
|
Chunk generateChunk(int cx, int cz);
|
|
|
|
|
void updateChunks(const float cameraPos[3]);
|
|
|
|
|
void drawCube(float x, float y, float z, int voxelType);
|
|
|
|
|
|
|
|
|
|
std::unordered_map<ChunkKey, Chunk, ChunkKeyHash> chunks;
|
|
|
|
|
int totalChunksLoaded;
|
|
|
|
|
int totalChunksEverLoaded;
|
|
|
|
|
|
2025-04-06 02:03:23 +00:00
|
|
|
|
unsigned int textureGrass;
|
|
|
|
|
unsigned int textureDirt;
|
|
|
|
|
unsigned int textureWood;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // VOXELGAME_H
|