Added chunk loading

This commit is contained in:
Eric-Paul Ickhorn 2023-10-14 10:47:21 +02:00
parent afde681ce9
commit 1e7aaadb5d
6 changed files with 60 additions and 58 deletions

View File

@ -102,8 +102,11 @@ void tc_reload_chunk(tc_world_s *world, tc_vec3i_s coord)
void tc_remove_loaders_chunk(tc_chunkloader_s *loader, uint32_t index)
{
tc_vec3i_s position = loader->chunks[index]->position;
tc_free_chunk(loader->world, loader->chunks[index]);
if(loader->num_chunks != 0)
{
// This is is NOT last element
if((index+1) < loader->num_chunks)
{
@ -111,6 +114,7 @@ void tc_remove_loaders_chunk(tc_chunkloader_s *loader, uint32_t index)
}
--loader->num_chunks;
}
}
void tc_remove_unnecessary_chunks_of_loader(tc_chunkloader_s *loader)
@ -120,7 +124,6 @@ void tc_remove_unnecessary_chunks_of_loader(tc_chunkloader_s *loader)
tc_chunk_s *chunk = loader->chunks[chunk_index];
if(!tc_coord_is_within_loaders_range(loader, chunk->position))
{
printf("Chunk at %d %d %d isn't in range\n", chunk->position.x, chunk->position.y, chunk->position.z);
tc_remove_loaders_chunk(loader, chunk_index);
}
}
@ -128,21 +131,16 @@ void tc_remove_unnecessary_chunks_of_loader(tc_chunkloader_s *loader)
void tc_load_all_chunks_of_chunkloader(tc_chunkloader_s *loader)
{
for(uint32_t x = 0; x < loader->extent.x; ++x)
for(int32_t x = 0; x < loader->extent.x; ++x)
{
for(uint32_t y = 0; y < loader->extent.y; ++y)
for(int32_t y = 0; y < loader->extent.y; ++y)
{
for(uint32_t z = 0; z < loader->extent.z; ++z)
for(int32_t z = 0; z < loader->extent.z; ++z)
{
tc_vec3i_s coords;
/*
coords.x = ((x + loader->center.x) - (loader->extent.x / 2)) + loader->extent.x;
coords.y = ((y + loader->center.y) - (loader->extent.y / 2)) + loader->extent.y;
coords.z = ((z + loader->center.z) - (loader->extent.z / 2)) + loader->extent.z;
*/
coords.x = loader->center.x + (x - (loader->extent.x / 2));
coords.y = loader->center.y + (y - (loader->extent.y / 2));
coords.z = loader->center.z + (z - (loader->extent.z / 2));
coords.x = (loader->center.x - (x - ((int32_t) loader->extent.x)))-1;
coords.y = (loader->center.y - (y - ((int32_t) loader->extent.y)))-1;
coords.z = (loader->center.z - (z - ((int32_t) loader->extent.z)))-1;
tc_load_chunk_of_loader(loader, coords);
}
@ -160,12 +158,8 @@ uint64_t world_update_tick = 0;
void tc_update_world(tc_world_s *world)
{
if((world_update_tick == 0))// % 512) == 0)
if((world_update_tick % 512) == 0)
{
world->loaders[0].center.x = world->loading_center.x;
world->loaders[0].center.y = world->loading_center.y;
world->loaders[0].center.z = world->loading_center.z;
uint32_t chunkloader_index = 0;
while(chunkloader_index < world->num_loaders)
{

View File

@ -45,7 +45,7 @@ tc_chunk_pool_entry_s * tc_allocate_chunk_pool_entry(tc_chunk_pool_s *pool)
tc_chunk_s * tc_allocate_chunk(tc_world_s *world)
{
tc_chunk_pool_entry_s *entry = tc_allocate_chunk_pool_entry(world->pool);
entry->chunk.superstructure = entry;
entry->chunk.pool_entry = entry;
return &entry->chunk;
}
@ -54,19 +54,36 @@ void tc_free_chunk(tc_world_s *world, tc_chunk_s *chunk)
{
tc_chunk_pool_s *pool = world->pool;
if(chunk->vao != 0) glDeleteVertexArrays(1, &chunk->vertex_data);
if(chunk->vao != 0)
{
glDeleteVertexArrays(1, &chunk->vertex_data);
chunk->vao = 0;
}
if(chunk->vertex_data != 0) glDeleteBuffers(1, &chunk->vertex_data);
if(chunk->vertex_data != 0)
{
glDeleteBuffers(1, &chunk->vertex_data);
chunk->vertex_data = 0;
}
if(chunk->vertex_positions != NULL) free(chunk->vertex_positions);
if(chunk->vertex_positions != NULL)
{
free(chunk->vertex_positions);
chunk->vertex_positions = NULL;
}
if(chunk->vertex_uvs != NULL) free(chunk->vertex_uvs);
if(chunk->vertex_uvs != NULL)
{
free(chunk->vertex_uvs);
chunk->vertex_uvs = NULL;
}
chunk->superstructure->next = pool->first_free;
pool->first_free = chunk->superstructure;
chunk->num_vertices = 0;
chunk->position.x = 0;
chunk->position.y = 0;
chunk->position.z = 0;
memset(&chunk->blocks, 0x00, sizeof(uint32_t) * 32 * 32 * 32);
chunk->pool_entry->next = pool->first_free;
pool->first_free = chunk->pool_entry;
}

View File

@ -155,8 +155,6 @@ void tc_adjust_cube_uvs_for_image(tc_image_s *image, float *face_uvs)
uv_per_pixel_w = 1.0f / image->superimage->width;
uv_per_pixel_h = 1.0f / image->superimage->height;
}
printf("UV Per Pixel: %f|%f\n", uv_per_pixel_w, uv_per_pixel_h);
float lower_x = uv_per_pixel_w * ((float) image->x);
float lower_y = uv_per_pixel_h * ((float) image->y);
float higher_x = 1.0f;
@ -165,8 +163,6 @@ void tc_adjust_cube_uvs_for_image(tc_image_s *image, float *face_uvs)
higher_x = lower_x + uv_per_pixel_w * 8; // image->width;
higher_y = lower_y + uv_per_pixel_h * 8; // image->height;
printf("Lower Coordinates: %f|%f, Higher Coordinates: %f %f\n", lower_x, lower_y, higher_x, higher_y);
uint32_t num_floats = 2 * 6;
uint32_t index = 0;

View File

@ -67,8 +67,6 @@ float triangle_vertices[108] =
tc_vec3_s tc_camera_position = { 0.0f, 0.0f, 0.0f };
// tc_vec3_s tc_camera_rotation = { 0.0f, 0.0f, 0.0f };
tc_block_s block;
void render_block(tc_block_s block)
{
mat4x4 model_matrix;
@ -135,7 +133,6 @@ void render()
glUniformMatrix4fv(view_uniform_location, 1, GL_FALSE, &view_matrix[0][0]);
// printf("%d\n", view_uniform_location);
render_block(block);
tc_draw_world(tc_game_state_g.main_world);
SDL_GL_SwapWindow(tc_game_state_g.renderer.window);
@ -327,9 +324,9 @@ bool update()
tc_camera_position.x += raw_difference[0];
tc_camera_position.z += raw_difference[2];
tc_game_state_g.main_world->loading_center.x = ((int32_t) tc_camera_position.x) / 32;
tc_game_state_g.main_world->loading_center.y = ((int32_t) tc_camera_position.y) / 32;
tc_game_state_g.main_world->loading_center.z = ((int32_t) tc_camera_position.z) / 32;
tc_game_state_g.main_world->spawn_loader->center.x = ((int32_t) tc_camera_position.x) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.y = ((int32_t) tc_camera_position.y) / 32 - UPDATE_DISTANCE;
tc_game_state_g.main_world->spawn_loader->center.z = ((int32_t) tc_camera_position.z) / 32 - UPDATE_DISTANCE;
tc_update_world(tc_game_state_g.main_world);
@ -339,11 +336,6 @@ bool update()
int main(int argc, char **argv)
{
tc_init();
// tc_new_chunk(tc_game_state_g.main_world, 1.0f, 1.0f, 1.0f);
block = tc_new_block_at_3f(0.0, 0.0, 0.0);
// tc_new_cube_block();
int64_t frame_index = 1;
while(frame_index > 0)
@ -359,8 +351,6 @@ int main(int argc, char **argv)
SDL_ShowWindow(tc_game_state_g.renderer.window);
}
block.position.x += 0.005f;
++frame_index;
SDL_Delay(1000/60);
}

View File

@ -115,12 +115,13 @@ void tc_setup_terrain(tc_world_s *world)
}
tc_chunkloader_s tc_create_chunkloader(tc_world_s *world)
{
tc_chunkloader_s loader;
loader.extent.x = world->load_radius * 2 + 1;
loader.extent.y = world->load_radius * 2 + 1;
loader.extent.z = world->load_radius * 2 + 1;
loader.extent.x = UPDATE_DISTANCE * 2 + 1;
loader.extent.y = UPDATE_DISTANCE * 2 + 1;
loader.extent.z = UPDATE_DISTANCE * 2 + 1;
loader.center.x = 0;
loader.center.y = 0;
loader.center.z = 0;
@ -155,13 +156,14 @@ tc_chunkloader_s tc_create_spawn_loader(tc_world_s *world)
tc_world_s * tc_new_world(tc_worldgen_s *generator)
{
tc_world_s *world = calloc(sizeof(tc_world_s), 1);
world->pool = tc_new_chunk_pool(128);
world->pool = tc_new_chunk_pool(512);
world->worldgen = &tc_default_terrain_generator_g;
// world->center_chunk = tc_new_chunk(world, 0, 0, 0);
world->load_radius = 3;
// world->load_radius = 3;
world->num_loaders = 1;
world->loaders = malloc(sizeof(tc_chunkloader_s) * world->num_loaders);
world->loaders[0] = tc_create_spawn_loader(world);
world->spawn_loader = &world->loaders[0];
// tc_upload_chunk(world->center_chunk);

View File

@ -8,6 +8,10 @@
#include "blocks.h"
#include "utility.h"
#define UPDATE_DISTANCE 2
typedef struct tc_worldgen tc_worldgen_s;
typedef struct tc_block tc_block_s;
typedef struct tc_chunk tc_chunk_s;
@ -34,7 +38,7 @@ struct tc_block
struct tc_chunk
{
tc_chunk_pool_entry_s *superstructure;
tc_chunk_pool_entry_s *pool_entry;
tc_vec3i_s position;
uint32_t blocks[32][32][32];
@ -90,8 +94,7 @@ struct tc_world
tc_chunkloader_s *loaders;
// chunk_loading_center: The center of loading ON THE CHUNK GRID COORDINATES
tc_vec3_s loading_center;
uint32_t load_radius;
tc_chunkloader_s *spawn_loader;
};
extern tc_worldgen_s tc_default_terrain_generator_g;