summaryrefslogtreecommitdiff
path: root/src/chunk.h
diff options
context:
space:
mode:
authorAaditya Dhruv <[email protected]>2026-01-31 03:22:27 -0600
committerAaditya Dhruv <[email protected]>2026-01-31 03:22:27 -0600
commit7f13b0abaa76a5e90674d5733f8162f02ceab693 (patch)
treee280ad2a4a881e79d2519f2558dcab6a36d5b389 /src/chunk.h
parent0c3e1f450f591d871c2779504b4113daf891fd1b (diff)
Rework chunk rendering to get a 100x performance boost!
- Switching to semi-chunk mesh rendering just 100x the framerate, it was running at around 100fps for 3 CHUNK_DISTANCE, and pushing it above would make it drop to 30. Now it runs at 9000 frames per second with CHUNK_DISTANCE of 8, probably can push it even more - What is bizarre is this is just from the reduction in draw calls, I still need to implement face culling for invisible blocks (simple) and frustrum culling (using AABB) or maybe octrees - Block is way way more simplifed, it's just metadata about a coordinate in the chunk block array - All rendering and mesh generation code is handled by chunks. There is a VAO, single VBO and EBO for each chunk. The data buffer is loaded into the GPU with a chunk_load, and it stays like that until it is loaded again. chunk_load is called if we move chunks, in engine_update. Here we unload existing chunks, then load the new ones
Diffstat (limited to 'src/chunk.h')
-rw-r--r--src/chunk.h27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/chunk.h b/src/chunk.h
index d49ba80..12fa111 100644
--- a/src/chunk.h
+++ b/src/chunk.h
@@ -1,8 +1,11 @@
#pragma once
#include "block.h"
+#include "cglm/types.h"
+#include "shader.h"
#include "texture.h"
#include "world.h"
#include <stdint.h>
+#include <junk/vector.h>
#define CHUNK_WIDTH 16
#define CHUNK_LENGTH 16
#define CHUNK_HEIGHT 128
@@ -17,11 +20,21 @@ enum biome {
// World.c compiles -> includes world.h -> first line is include chunk.h -> struct world
// is used here, which messes it up. Forward declare to avoid errors
struct world;
+/**
+ * A chunk is the "basic" rendering unit used - it will allocate buffers for all blocks in a chunk, hide covered faces of blocks, generate a chunk mesh and dispatch that buffer data to the GPU
+ *
+ */
struct chunk {
struct block* blocks[CHUNK_WIDTH][CHUNK_LENGTH][CHUNK_HEIGHT];
+ GLuint _vao;
+ GLuint _vbo;
+ GLuint _ebo;
+ int vertex_count;
+ mat4 model;
enum biome biome;
vec2 coord;
int loaded;
+ int dirty;
};
/**
@@ -35,19 +48,27 @@ int chunk_gen(struct world* wld, vec2 coord, struct chunk** chunk);
* it's local coordinate system. We want to load this particular chunk to a
* location in WORLD coordinates, which is what coord is. This vec2 will be
* used to translate the blocks that constitute the chunk
+ *
+ * The chunk will allocate a VAO/VBO/EBO buffer to render the chunk mesh. This GPU data is usually not updated in the loop, unless a chunk_update is called
* @param chunk Chunk to load
* @param coord coordinates in world space
*/
void chunk_load(struct chunk* chunk, int coord[2]);
/**
- * Unload a chunk. Delete block GPU and memory data, not the chunk data itself
+ * Chunk updates are performed on already loaded chunks. It will redraw the
+ * chunk mesh as needed based on block updates and whatnot.
+ * @param chunk Chunk to load
+ * @param coord coordinates in world space
+ */
+void chunk_update(struct chunk* chunk);
+/**
+ * Unload a chunk. Delete GPU and memory data, not the chunk data itself
*
* @param chunk Chunk to load
*/
void chunk_unload(struct chunk* chunk);
/*
- * Similar to block_draw, this dispatches calls to OpenGL to draw the chunk.
- * Technically this wraps block_draw, so block_draw is the one doing all the work
+ * This dispatches calls to OpenGL to draw the chunk.
* @param chunk Chunk to draw
* @param shader Shader to pass to block_draw
* @param texture Textures that block_draw will use