ChunkManager key changes

This commit is contained in:
2025-06-02 22:55:18 +02:00
parent cee2f480d5
commit 6b37822480
7 changed files with 76 additions and 55 deletions
+4 -1
View File
@@ -8,6 +8,8 @@ varying float f_height;
uniform sampler2D materials[5];
/* uniform int max_chunk_height; */
void main ()
{
vec4 result = texture2D (materials[0], f_uv);
@@ -15,6 +17,7 @@ void main ()
result = mix (result, texture2D (materials[2], f_uv), f_material[1]);
result = mix (result, texture2D (materials[3], f_uv), f_material[2]);
result = mix (result, texture2D (materials[4], f_uv), f_material[3]);
result = result * (f_height / 5.0);
gl_FragColor = result;
}
+7 -3
View File
@@ -12,14 +12,18 @@ Chunk_new ()
}
void
Chunk_random (Chunk *chunk)
Chunk_random (Chunk *chunk, int xoffset, int yoffset)
{
for (int y = 0; y < CHUNK_DIM; ++y)
{
for (int x = 0; x < CHUNK_DIM; ++x)
{
chunk->heightmap[INDEX (x, y)] = 0;
chunk->materialmap[INDEX (x, y)] = rand () % 5;
chunk->heightmap[INDEX (x, y)] =
(glm_perlin_vec2
((vec2) { (x + xoffset + 0.5) / 10.0, (y + yoffset + 0.5) / 10.0 }) + 0.6) * 5;
chunk->materialmap[INDEX (x, y)] = (int)
(glm_perlin_vec2 ((vec2) { (x + xoffset + 0.5) / 20.0, (y + yoffset + 0.5) / 20.0 }) + 0.6 * 5) % 5;
}
}
}
+3 -1
View File
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <cglm/noise.h>
#include "../util.h"
#define CHUNK_DIM 16
@@ -16,6 +18,6 @@ typedef struct Chunk
} Chunk;
Chunk *Chunk_new ();
void Chunk_random (Chunk *chunk);
void Chunk_random (Chunk *chunk, int x, int y);
void Chunk_free (Chunk *chunk);
void Chunk_heightmap_print (Chunk *chunk);
+53 -41
View File
@@ -5,42 +5,58 @@
ChunkManager chunk_manager;
guint
cm_ivec2_hash (gconstpointer key)
{
const ivec2 *v = key;
return g_int_hash(&(*v)[0]) ^ g_int_hash(&(*v)[1]);
}
gboolean
cm_ivec2_equal (gconstpointer a, gconstpointer b)
{
const ivec2 *av = a;
const ivec2 *bv = b;
return (*av)[0] == (*bv)[0] && (*av)[1] == (*bv)[1];
}
ivec2 *
cm_make_key(int x, int y)
{
ivec2 *key = malloc(sizeof(ivec2));
(*key)[0] = x;
(*key)[1] = y;
return key;
}
void
ChunkManager_init ()
{
chunk_manager.chunks = g_hash_table_new (g_str_hash, g_str_equal);
chunk_manager.chunk_meshes = g_hash_table_new (g_str_hash, g_str_equal);
chunk_manager.chunks = g_hash_table_new_full (cm_ivec2_hash, cm_ivec2_equal, free, (GDestroyNotify) Chunk_free);
chunk_manager.chunk_meshes = g_hash_table_new_full (cm_ivec2_hash, cm_ivec2_equal, free, (GDestroyNotify) Chunk_free);
}
void
cm_get_chunk_pos (float x, float y, int *ox, int *oy)
cm_world_to_chunk (float x, float y, int *ox, int *oy)
{
*ox = floor (x / (float) CHUNK_DIM);
*oy = floor (y / (float) CHUNK_DIM);
}
char *
cm_coords_to_string (int ox, int oy)
{
char *buffer = malloc (sizeof (char) * 128);
snprintf (buffer, 128, "%d %d", ox, oy);
return buffer;
}
void
ChunkManager_update (float px, float py)
/* x = camera's X position, y = camera's Y position */
{
int cx = 0, cy = 0;
cm_get_chunk_pos (px, py, &cx, &cy);
cm_world_to_chunk (px, py, &cx, &cy);
for (int y = cy - RENDER_DISTANCE - 1; y <= cy + RENDER_DISTANCE + 1; ++y)
{
for (int x = cx - RENDER_DISTANCE - 1; x <= cx + RENDER_DISTANCE + 1; ++x)
{
if (ChunkManager_get_chunk_at (x, y) == NULL)
if (ChunkManager_get_Chunk_at (x, y) == NULL)
{
ChunkManager_generate_chunk_at (x, y);
ChunkManager_generate_Chunk_at (x, y);
}
}
}
@@ -49,9 +65,9 @@ ChunkManager_update (float px, float py)
{
for (int x = cx - RENDER_DISTANCE; x <= cx + RENDER_DISTANCE; ++x)
{
if (ChunkManager_get_chunk_mesh_at (x, y) == NULL)
if (ChunkManager_get_ChunkMesh_at (x, y) == NULL)
{
ChunkManager_generate_chunk_mesh_at (x, y);
ChunkManager_generate_ChunkMesh_at (x, y);
}
}
}
@@ -61,13 +77,13 @@ void
ChunkManager_render (float px, float py, int program)
{
int cx = 0, cy = 0;
cm_get_chunk_pos (px, py, &cx, &cy);
cm_world_to_chunk (px, py, &cx, &cy);
for (int y = cy - RENDER_DISTANCE; y <= cy + RENDER_DISTANCE; ++y)
{
for (int x = cx - RENDER_DISTANCE; x <= cx + RENDER_DISTANCE; ++x)
{
ChunkMesh *mesh = ChunkManager_get_chunk_mesh_at (x, y);
ChunkMesh *mesh = ChunkManager_get_ChunkMesh_at (x, y);
if (mesh != NULL)
{
program_set_vec3 (program, "chunk_position", x * CHUNK_DIM, 0.0, y * CHUNK_DIM);
@@ -78,54 +94,50 @@ ChunkManager_render (float px, float py, int program)
}
Chunk *
ChunkManager_get_chunk_at (int x, int y)
ChunkManager_get_Chunk_at (int x, int y)
{
char *key = cm_coords_to_string (x, y);
return (Chunk *) g_hash_table_lookup (chunk_manager.chunks, key);
return (Chunk *) g_hash_table_lookup (chunk_manager.chunks, (ivec2) { x, y });
}
void
ChunkManager_set_chunk_at (int x, int y, Chunk *chunk)
ChunkManager_set_Chunk_at (int x, int y, Chunk *chunk)
{
char *key = cm_coords_to_string (x, y);
g_hash_table_insert (chunk_manager.chunks, key, chunk);
g_hash_table_insert (chunk_manager.chunks, cm_make_key (x, y), chunk);
}
void
ChunkManager_generate_chunk_at (int x, int y)
ChunkManager_generate_Chunk_at (int x, int y)
{
Chunk *chunk = Chunk_new ();
Chunk_random (chunk);
ChunkManager_set_chunk_at (x, y, chunk);
Chunk_random (chunk, x * CHUNK_DIM, y * CHUNK_DIM);
ChunkManager_set_Chunk_at (x, y, chunk);
}
void
ChunkManager_get_chunk_neighbors_at (int x, int y, Chunk **nr, Chunk **nd, Chunk **nc)
ChunkManager_get_Chunk_neighbors_at (int x, int y, Chunk **nr, Chunk **nd, Chunk **nc)
{
*nr = ChunkManager_get_chunk_at (x + 1, y);
*nd = ChunkManager_get_chunk_at (x, y + 1);
*nc = ChunkManager_get_chunk_at (x + 1, y + 1);
*nr = ChunkManager_get_Chunk_at (x + 1, y);
*nd = ChunkManager_get_Chunk_at (x, y + 1);
*nc = ChunkManager_get_Chunk_at (x + 1, y + 1);
}
ChunkMesh *
ChunkManager_get_chunk_mesh_at (int x, int y)
ChunkManager_get_ChunkMesh_at (int x, int y)
{
char *key = cm_coords_to_string (x, y);
return (ChunkMesh *) g_hash_table_lookup (chunk_manager.chunk_meshes, key);
return (ChunkMesh *) g_hash_table_lookup (chunk_manager.chunk_meshes, (ivec2) { x, y });
}
void
ChunkManager_set_chunk_mesh_at (int x, int y, ChunkMesh *mesh)
ChunkManager_set_ChunkMesh_at (int x, int y, ChunkMesh *mesh)
{
char *key = cm_coords_to_string (x, y);
g_hash_table_insert (chunk_manager.chunk_meshes, key, mesh);
g_hash_table_insert (chunk_manager.chunk_meshes, cm_make_key (x, y), mesh);
}
void
ChunkManager_generate_chunk_mesh_at (int x, int y)
ChunkManager_generate_ChunkMesh_at (int x, int y)
{
Chunk *nr, *nd, *nc;
ChunkManager_get_chunk_neighbors_at (x, y, &nr, &nd, &nc);
ChunkManager_set_chunk_mesh_at
(x, y, ChunkMesh_from_Chunk_and_neighbors (ChunkManager_get_chunk_at (x, y), nr, nd, nc));
ChunkManager_get_Chunk_neighbors_at (x, y, &nr, &nd, &nc);
ChunkManager_set_ChunkMesh_at
(x, y, ChunkMesh_build (ChunkManager_get_Chunk_at (x, y), nr, nd, nc));
}
+7 -7
View File
@@ -22,12 +22,12 @@ void ChunkManager_init ();
void ChunkManager_update (float px, float py);
void ChunkManager_render (float px, float py, int program);
Chunk *ChunkManager_get_chunk_at (int x, int y);
void ChunkManager_get_chunk_neighbors_at (int x, int y, Chunk **nr, Chunk **nd, Chunk **nc);
void ChunkManager_set_chunk_at (int x, int y, Chunk *chunk);
Chunk *ChunkManager_get_Chunk_at (int x, int y);
void ChunkManager_get_Chunk_neighbors_at (int x, int y, Chunk **nr, Chunk **nd, Chunk **nc);
void ChunkManager_set_Chunk_at (int x, int y, Chunk *chunk);
ChunkMesh *ChunkManager_get_chunk_mesh_at (int x, int y);
void ChunkManager_set_chunk_mesh_at (int x, int y, ChunkMesh *mesh);
ChunkMesh *ChunkManager_get_ChunkMesh_at (int x, int y);
void ChunkManager_set_ChunkMesh_at (int x, int y, ChunkMesh *mesh);
void ChunkManager_generate_chunk_at (int x, int y);
void ChunkManager_generate_chunk_mesh_at (int x, int y);
void ChunkManager_generate_Chunk_at (int x, int y);
void ChunkManager_generate_ChunkMesh_at (int x, int y);
+1 -1
View File
@@ -3,7 +3,7 @@
extern GameFlags game_flags;
ChunkMesh *
ChunkMesh_from_Chunk_and_neighbors (Chunk *chunk, Chunk *nr, Chunk *nd, Chunk *nc)
ChunkMesh_build (Chunk *chunk, Chunk *nr, Chunk *nd, Chunk *nc)
/* nd --- down neighbor; nr --- right neighbor; nc --- corner neighbor */
{
vec3 *positions = malloc (sizeof (vec3) * CHUNKMESH_SIZE);
+1 -1
View File
@@ -18,5 +18,5 @@ typedef struct ChunkMesh
} ChunkMesh;
/* Rename to ChunkMesh_build */
ChunkMesh *ChunkMesh_from_Chunk_and_neighbors (Chunk *chunk, Chunk *nr, Chunk *nd, Chunk *nc);
ChunkMesh *ChunkMesh_build (Chunk *chunk, Chunk *nr, Chunk *nd, Chunk *nc);
void ChunkMesh_render (ChunkMesh *mesh);