finish changing voxel storage to a 1d aray

This commit is contained in:
0x01FE 2025-11-19 21:53:07 -06:00
parent 4925849747
commit 55d01e86c1
5 changed files with 26 additions and 13 deletions

View File

@ -13,10 +13,10 @@ void main()
// You can now use v_VoxelKind to modify the color // You can now use v_VoxelKind to modify the color
// Example: Different colors for different voxel types // Example: Different colors for different voxel types
vec3 color = u_Color; vec3 color = u_Color;
if (v_VoxelKind == 0.0) { if (v_VoxelKind == 1.0) {
// Dirt - brownish // Dirt - brownish
color *= vec3(0.6, 0.4, 0.2); color *= vec3(0.6, 0.4, 0.2);
} else if (v_VoxelKind == 1.0) { } else if (v_VoxelKind == 2.0) {
// Stone - grayish (keep as is) // Stone - grayish (keep as is)
} }

View File

@ -119,7 +119,7 @@ void Chunk::generateMesh() {
bool neighbour_is_solid = false; bool neighbour_is_solid = false;
// in-bounds neighbor -> check data // in-bounds neighbor -> check data
if (nx >= 0 && nx < SIZE && ny >= 0 && ny < SIZE && nz >= 0 && nz < SIZE) { if (nx >= 0 && nx < SIZE && ny >= 0 && ny < SIZE && nz >= 0 && nz < SIZE) {
std::cout << "Accessing " << nx << ", " << ny << ", " << nz << std::endl; // std::cout << "Accessing " << nx << ", " << ny << ", " << nz << std::endl;
neighbour_is_solid = (data[index(nx, ny, nz)] != VoxelKind::Air); neighbour_is_solid = (data[index(nx, ny, nz)] != VoxelKind::Air);
} else { } else {
// neighbour out of this chunk: treat as air (exposed face) // neighbour out of this chunk: treat as air (exposed face)

View File

@ -52,7 +52,7 @@ class Chunk {
return data[index(x, y, z)]; return data[index(x, y, z)];
} }
inline VoxelKind get(glm::ivec3 pos) const { inline VoxelKind get(glm::ivec3 pos) const {
std::cout << "Getting Pos (" << pos.x << ", " << pos.y << ", " << pos.z << ")" << std::endl; // std::cout << "Getting Pos (" << pos.x << ", " << pos.y << ", " << pos.z << ")" << std::endl;
return data[index(pos.x, pos.y, pos.z)]; return data[index(pos.x, pos.y, pos.z)];
} }

View File

@ -169,5 +169,5 @@ VoxelKind World::get_voxel(glm::ivec3 position) {
Chunk* chunk = this->get_or_create_chunk(chunk_position); Chunk* chunk = this->get_or_create_chunk(chunk_position);
return chunk->get(position); return chunk->get(local_position);
} }

View File

@ -30,6 +30,8 @@ const float Z_NEAR = 0.1f;
bool WIREFRAME_MODE = !true; bool WIREFRAME_MODE = !true;
const float FOV = 110.0f; const float FOV = 110.0f;
const std::string VERSION = "0.8.0";
const int VIEW_DISTANCE = 4; // In Chunks const int VIEW_DISTANCE = 4; // In Chunks
bool W_pressed, S_pressed, A_pressed, D_pressed = false; bool W_pressed, S_pressed, A_pressed, D_pressed = false;
@ -269,7 +271,7 @@ int main() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// 2. --- Create a Window --- // 2. --- Create a Window ---
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Voxel Engine 0.7.0", NULL, NULL); GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, ("Voxel Engine " + VERSION).c_str(), NULL, NULL);
if (window == NULL) { if (window == NULL) {
std::cerr << "Failed to create GLFW window" << std::endl; std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate(); glfwTerminate();
@ -391,8 +393,24 @@ int main() {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
bool debug_printed = false; bool debug_printed = false;
// FPS counter variables
double last_fps_update = glfwGetTime();
int frame_count = 0;
double fps = 0.0;
// 6. --- The Render Loop --- // 6. --- The Render Loop ---
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
// Update FPS counter
double current_time = glfwGetTime();
frame_count++;
if (current_time - last_fps_update >= 1.0) {
fps = frame_count / (current_time - last_fps_update);
std::string title = "Voxel Engine " + VERSION + " | FPS: " + std::to_string((int)fps);
glfwSetWindowTitle(window, title.c_str());
frame_count = 0;
last_fps_update = current_time;
}
// Input (e.g., close window on ESC) // Input (e.g., close window on ESC)
processInput(window); processInput(window);
@ -403,13 +421,6 @@ int main() {
shader.use(); shader.use();
// if (!debug_printed) {
// std::cout << "\n=== RENDER DEBUG ===" << std::endl;
// std::cout << "Camera pos: (" << camera.position.x << ", " << camera.position.y << ", " << camera.position.z << ")" << std::endl;
// std::cout << "Camera target: (" << camera.target.x << ", " << camera.target.y << ", " << camera.target.z << ")" << std::endl;
// // std::cout << "Number of chunks to draw: " << world.chunks.size() << std::endl;
// }
// Generate chunks around the camera // Generate chunks around the camera
glm::ivec3 camera_chunk = glm::ivec3( glm::ivec3 camera_chunk = glm::ivec3(
std::floor(camera.position.x / CHUNK_SIZE), std::floor(camera.position.x / CHUNK_SIZE),
@ -437,6 +448,8 @@ int main() {
Chunk* chunk = world.get_or_create_chunk(chunk_pos); Chunk* chunk = world.get_or_create_chunk(chunk_pos);
Mesh* chunk_mesh = chunk->getMesh(); Mesh* chunk_mesh = chunk->getMesh();
if (chunk_mesh)
chunk_mesh->draw();
} }
} }
} }