diff --git a/build.bash b/build.bash index a61b912..df33d46 100644 --- a/build.bash +++ b/build.bash @@ -4,9 +4,9 @@ mkdir -p dependencies/build/ gcc -g -o dependencies/build/glad.o \ -c dependencies/sources/glad/glad.c \ - -I dependencies/include/ + -I dependencies/include/ -Wall gcc -g -o techneck.elf \ code/source-c/*.c dependencies/build/glad.o \ - -I dependencies/include -lGL -lSDL2 -lm + -I dependencies/include -lGL -lSDL2 -lm -Wall diff --git a/code/source-c/assets.c b/code/source-c/assets.c index 5cdd1bf..0244892 100644 --- a/code/source-c/assets.c +++ b/code/source-c/assets.c @@ -6,6 +6,9 @@ #define STB_IMAGE_IMPLEMENTATION #include +// TODO: This can be done better. +uint32_t tc_image_id_counter = 1; + tc_asset_storage_s tc_init_asset_storage(char *asset_folder_path) { tc_asset_storage_s assets; @@ -35,17 +38,26 @@ void tc_upload_image(tc_image_s *image) GL_UNSIGNED_BYTE, image->pixels ); + + image->is_uploaded = true; } tc_image_s * tc_load_image_from_disk(char *path) { tc_image_s *image = malloc(sizeof(tc_image_s)); image->attributes_capacity = 8; - image->attributes = calloc(sizeof(tc_asset_attribute_s), image->attributes_capacity); + image->attributes = + calloc(sizeof(tc_asset_attribute_s), image->attributes_capacity); image->num_attributes = 0; image->pixels = stbi_load(path, &image->width, &image->height, NULL, 4); image->len_name = 0; image->name = NULL; + image->x = 0; + image->y = 0; + image->is_subimage = false; + image->superimage = NULL; + image->image_id = tc_image_id_counter; + ++tc_image_id_counter; if(tc_game_state_g.asset_storage.num_images >= tc_game_state_g.asset_storage.images_capacity) { @@ -57,7 +69,79 @@ tc_image_s * tc_load_image_from_disk(char *path) } tc_game_state_g.asset_storage.images[tc_game_state_g.asset_storage.num_images] = image; + ++tc_game_state_g.asset_storage.num_images; return image; } +tc_image_s * tc_create_subimage(tc_image_s *superimage, + uint32_t x, uint32_t y, + uint32_t width, uint32_t height +) { + if(!superimage->is_uploaded) + { + puts("Superimages must have been uploaded before creating a subimage!"); + return NULL; + } + + tc_image_s *image = malloc(sizeof(tc_image_s)); + image->attributes_capacity = 8; + image->attributes = + calloc(sizeof(tc_asset_attribute_s), image->attributes_capacity); + image->num_attributes = 0; + image->pixels = NULL; + image->len_name = 0; + image->name = NULL; + image->x = x; + image->y = y; + image->is_subimage = true; + image->width = width; + image->height = height; + image->superimage = superimage; + image->image_id = tc_image_id_counter; + ++tc_image_id_counter; + + if(tc_game_state_g.asset_storage.num_images >= tc_game_state_g.asset_storage.images_capacity) + { + tc_game_state_g.asset_storage.images_capacity *= 2; + tc_game_state_g.asset_storage.images = + realloc(tc_game_state_g.asset_storage.images, + sizeof(tc_image_s *) * tc_game_state_g.asset_storage.images_capacity + ); + } + + tc_game_state_g.asset_storage.images[tc_game_state_g.asset_storage.num_images] = image; + ++tc_game_state_g.asset_storage.num_images; + + return tc_game_state_g.asset_storage.images[tc_game_state_g.asset_storage.num_images-1]; +} + +tc_image_s * tc_resolve_id_to_image(uint32_t id) +{ + uint32_t image_index = 0; + while(image_index < tc_game_state_g.asset_storage.num_images) + { + if(tc_game_state_g.asset_storage.images[image_index]->image_id == id) + return tc_game_state_g.asset_storage.images[image_index]; + ++image_index; + } + return NULL; +} + +tc_image_s * tc_resolve_name_to_image(char *name) +{ + uint32_t image_index = 0; + while(image_index < tc_game_state_g.asset_storage.num_images) + { + if(tc_game_state_g.asset_storage.images[image_index]->name == NULL) + { + ++image_index; + continue; + } + if(!strcmp(tc_game_state_g.asset_storage.images[image_index]->name, name)) + return tc_game_state_g.asset_storage.images[image_index]; + ++image_index; + } + return NULL; +} + diff --git a/code/source-c/assets.h b/code/source-c/assets.h index 87574e0..65ee357 100644 --- a/code/source-c/assets.h +++ b/code/source-c/assets.h @@ -6,6 +6,8 @@ #include #include +typedef struct tc_image tc_image_s; + typedef struct tc_asset_attribute { char *issuer; @@ -21,9 +23,15 @@ typedef struct tc_asset_attribute } tc_asset_attribute_s; -typedef struct tc_image -{ +struct tc_image +{ + bool is_subimage; + bool is_uploaded; + + tc_image_s *superimage; + uint32_t gl_identifier; + uint32_t image_id; uint32_t len_name; char *name; @@ -32,18 +40,20 @@ typedef struct tc_image uint32_t num_attributes; tc_asset_attribute_s *attributes; + uint32_t x; // Only used if this is a subimage + uint32_t y; // Only used if this is a subimage uint32_t width; uint32_t height; void *pixels; -} tc_image_s; +}; typedef struct tc_asset_storage { uint32_t images_capacity; uint32_t num_images; tc_image_s **images; - + // todo: meshes } tc_asset_storage_s; @@ -52,5 +62,12 @@ tc_asset_storage_s tc_init_asset_storage (char *asset_folder_ tc_image_s * tc_load_image_from_disk (char *path); void tc_upload_image (tc_image_s *image); +tc_image_s * tc_resolve_id_to_image (uint32_t id); +tc_image_s * tc_resolve_name_to_image (char *name); + +tc_image_s * tc_create_subimage(tc_image_s *superimage, + uint32_t x, uint32_t y, + uint32_t width, uint32_t height); + #endif diff --git a/code/source-c/blocks.c b/code/source-c/blocks.c index 1a6442c..071783e 100644 --- a/code/source-c/blocks.c +++ b/code/source-c/blocks.c @@ -3,18 +3,34 @@ #include -tc_block_entry_s tc_new_block_entry(char *name) + +tc_block_entry_s * tc_new_block_entry(char *name) { - tc_block_entry_s entry; - entry.name = name; - entry.attributes_capacity = 16; - entry.num_attributes = 0; - entry.attributes = calloc(sizeof(tc_block_attribute_s), entry.attributes_capacity); + tc_block_entry_s *entry = malloc(sizeof(tc_block_entry_s)); + entry->name = name; + entry->attributes_capacity = 16; + entry->num_attributes = 0; + entry->attributes = calloc( + sizeof(tc_block_attribute_s), entry->attributes_capacity + ); + + if(tc_game_state_g.block_registry.num_blocks >= tc_game_state_g.block_registry.blocks_capacity) + { + tc_game_state_g.block_registry.blocks = + realloc(tc_game_state_g.block_registry.blocks, + sizeof(tc_block_entry_s) + * + tc_game_state_g.block_registry.blocks_capacity + ); + } + + tc_game_state_g.block_registry.blocks[tc_game_state_g.block_registry.num_blocks] = entry; + ++tc_game_state_g.block_registry.num_blocks; return entry; } -void tc_add_attribute_to_block(tc_block_entry_s *entry, tc_block_attribute_s attribute) +void tc_add_attribute_to_block_entry(tc_block_entry_s *entry, tc_block_attribute_s attribute) { if(entry->num_attributes >= entry->attributes_capacity) { @@ -24,6 +40,21 @@ void tc_add_attribute_to_block(tc_block_entry_s *entry, tc_block_attribute_s att } entry->attributes[entry->num_attributes] = attribute; + ++entry->num_attributes; +} + + +tc_block_attribute_s * tc_block_resolve_name_to_attribute(tc_block_entry_s *block, char *name) +{ + uint32_t attribute_index = 0; + while(attribute_index < block->num_attributes) + { + if(!strcmp(block->attributes[attribute_index].name, name)) + return &block->attributes[attribute_index]; + + ++attribute_index; + } + return NULL; } tc_block_entry_s * tc_get_block(char *name) @@ -31,20 +62,109 @@ tc_block_entry_s * tc_get_block(char *name) uint32_t index = 0; while(index < tc_game_state_g.block_registry.num_blocks) { - if(!strcmp(tc_game_state_g.block_registry.blocks[index].name, name)) - return &tc_game_state_g.block_registry.blocks[index]; + if(!strcmp(tc_game_state_g.block_registry.blocks[index]->name, name)) + return tc_game_state_g.block_registry.blocks[index]; ++index; } return NULL; } +tc_block_entry_s * tc_resolve_id_to_block_entry(uint32_t id) +{ + uint32_t index = 0; + while(index < tc_game_state_g.block_registry.num_blocks) + { + if(tc_game_state_g.block_registry.blocks[index]->identifier == id) + { + return tc_game_state_g.block_registry.blocks[index]; + } + ++index; + } + return NULL; +} + tc_block_registry_s tc_init_blocks() { tc_block_registry_s registry; registry.blocks_capacity = 256; + registry.num_blocks = 0; registry.blocks = calloc(sizeof(tc_block_entry_s), registry.blocks_capacity); return registry; } +tc_block_entry_s * tc_new_cube_block(tc_cube_info_s info) +{ + tc_block_entry_s *entry = tc_new_block_entry(info.name); + entry->identifier = info.identifier; + + tc_block_attribute_s block_type_attrib; + block_type_attrib.name = "block_type"; + block_type_attrib.value.string = "basic_cube"; + tc_add_attribute_to_block_entry(entry, block_type_attrib); + + tc_block_attribute_s top_attrib; + top_attrib.name = "top_texture"; + top_attrib.value.pointer = tc_resolve_id_to_image(info.texture_top); + tc_add_attribute_to_block_entry(entry, top_attrib); + + tc_block_attribute_s bottom_attrib; + bottom_attrib.name = "bottom_texture"; + bottom_attrib.value.pointer = tc_resolve_id_to_image(info.texture_bottom); + tc_add_attribute_to_block_entry(entry, bottom_attrib); + + tc_block_attribute_s east_attrib; + east_attrib.name = "east_texture"; + east_attrib.value.pointer = tc_resolve_id_to_image(info.texture_east); + tc_add_attribute_to_block_entry(entry, east_attrib); + + tc_block_attribute_s west_attrib; + west_attrib.name = "west_texture"; + west_attrib.value.pointer = tc_resolve_id_to_image(info.texture_west); + tc_add_attribute_to_block_entry(entry, west_attrib); + + tc_block_attribute_s back_attrib; + back_attrib.name = "back_texture"; + back_attrib.value.pointer = tc_resolve_id_to_image(info.texture_back); + tc_add_attribute_to_block_entry(entry, back_attrib); + + tc_block_attribute_s front_attrib; + front_attrib.name = "front_texture"; + front_attrib.value.pointer = tc_resolve_id_to_image(info.texture_front); + tc_add_attribute_to_block_entry(entry, front_attrib); + + return entry; +} + +tc_cube_info_s tc_gen_unitexture_cube_info + (char *name, uint32_t identifier, uint32_t texture) +{ + tc_cube_info_s cube_info; + cube_info.name = name; + cube_info.identifier = identifier; + cube_info.texture_top = texture; + cube_info.texture_bottom = texture; + cube_info.texture_east = texture; + cube_info.texture_west = texture; + cube_info.texture_back = texture; + cube_info.texture_front = texture; + + return cube_info; +} + +void tc_create_blocks() +{ + tc_image_s *dirt_texture = tc_resolve_name_to_image("dirt"); + tc_image_s *grass_texture = tc_resolve_name_to_image("grass"); + tc_image_s *stone_texture = tc_resolve_name_to_image("stone"); + + tc_cube_info_s dirt = tc_gen_unitexture_cube_info("Dirt", 1, dirt_texture->image_id); + tc_cube_info_s grass = tc_gen_unitexture_cube_info("Grass", 2, grass_texture->image_id); + tc_cube_info_s stone = tc_gen_unitexture_cube_info("Stone", 3, stone_texture->image_id); + + tc_new_cube_block(dirt); + tc_new_cube_block(grass); + tc_new_cube_block(stone); +} + diff --git a/code/source-c/blocks.h b/code/source-c/blocks.h index 251c92a..a70431b 100644 --- a/code/source-c/blocks.h +++ b/code/source-c/blocks.h @@ -9,11 +9,13 @@ typedef struct tc_block_attribute { - uint32_t len_name; - uint32_t len_content; - char *name; - void *content; + + union { + void *pointer; + char *string; + int64_t integer; + } value; } tc_block_attribute_s; @@ -32,16 +34,44 @@ typedef struct tc_block_registry { uint32_t blocks_capacity; uint32_t num_blocks; - tc_block_entry_s *blocks; + tc_block_entry_s **blocks; } tc_block_registry_s; -tc_block_entry_s tc_new_block_entry (char *name); -void tc_add_attribute_to_block + + +tc_block_entry_s * tc_new_block_entry (char *name); +void tc_add_attribute_to_block_entry (tc_block_entry_s *entry, tc_block_attribute_s attribute); +tc_block_attribute_s * tc_block_resolve_name_to_attribute (tc_block_entry_s *block, char *name); + tc_block_entry_s * tc_get_block (char *name); +tc_block_entry_s * tc_resolve_id_to_block_entry (uint32_t id); tc_block_registry_s tc_init_blocks (); + + +typedef struct tc_cube_info +{ + char *name; + + uint32_t identifier; + + uint32_t texture_top; + uint32_t texture_bottom; + uint32_t texture_east; + uint32_t texture_west; + uint32_t texture_back; + uint32_t texture_front; + +} tc_cube_info_s; + +tc_cube_info_s tc_gen_unitexture_cube_info + (char *name, uint32_t identifier, uint32_t texture); + +tc_block_entry_s * tc_new_cube_block (tc_cube_info_s info); +void tc_create_blocks (); + #endif diff --git a/code/source-c/default_terrain_generator.c b/code/source-c/default_terrain_generator.c index 646e46a..6801c5a 100644 --- a/code/source-c/default_terrain_generator.c +++ b/code/source-c/default_terrain_generator.c @@ -28,4 +28,5 @@ bool tc_generate_default_terrain_chunk(tc_worldgen_s *gen, tc_chunk_s *chunk) } return true; -} +} + diff --git a/code/source-c/initialization.c b/code/source-c/initialization.c index 3a292dc..0ef411f 100644 --- a/code/source-c/initialization.c +++ b/code/source-c/initialization.c @@ -3,7 +3,8 @@ #include -const char *tc_window_title_g = "Techneck"; +const char *tc_window_title_g = "Techneck"; +techneck_s tc_game_state_g; void tc_init_renderer(techneck_s *techneck) { @@ -29,24 +30,41 @@ void tc_init_renderer(techneck_s *techneck) techneck->renderer.draw_shader = tc_make_shader_program("vertex_shader.glsl", "fragment_shader.glsl"); glEnable(GL_DEPTH_TEST); - + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); } void tc_load_textures() { tc_game_state_g.block_texture_atlas = tc_load_image_from_disk("assets/block_atlas.png"); + tc_game_state_g.block_texture_atlas->name = "Atlas"; tc_upload_image(tc_game_state_g.block_texture_atlas); + + tc_image_s *dirt = + tc_create_subimage(tc_game_state_g.block_texture_atlas, 0, 0, 8, 8); + dirt->name = "dirt"; + printf("POiNTA: %p!\n", dirt); + printf("Resolving from array: %p\n", tc_game_state_g.asset_storage.images); + + tc_image_s *grass = + tc_create_subimage(tc_game_state_g.block_texture_atlas, 8, 0, 8, 8); + grass->name = "grass"; + + tc_image_s *stone = + tc_create_subimage(tc_game_state_g.block_texture_atlas, 16, 0, 8, 8); + stone->name = "stone"; } -techneck_s tc_init() +void tc_init() { - techneck_s techneck; - tc_init_renderer(&techneck); - techneck.main_world = tc_init_worlds(); - techneck.asset_storage = tc_init_asset_storage("assets/"); - techneck.block_registry = tc_init_blocks(); + tc_init_renderer(&tc_game_state_g); + tc_game_state_g.asset_storage = tc_init_asset_storage("assets/"); + tc_load_textures(); + tc_game_state_g.block_registry = tc_init_blocks(); + tc_create_blocks(); + tc_game_state_g.main_world = tc_init_worlds(); - return techneck; + puts("Finished initializing!"); } void tc_cleanup() diff --git a/code/source-c/main.c b/code/source-c/main.c index 0161af5..200a81f 100644 --- a/code/source-c/main.c +++ b/code/source-c/main.c @@ -1,7 +1,5 @@ #include "state.h" -techneck_s tc_game_state_g; - #include #include #include @@ -13,57 +11,57 @@ float triangle_vertices[108] = { // Front -0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, // Back - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, // Left - -0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, -0.5f, // Right - 0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, // Top - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, -0.5f, // Bottom + -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, -0.5f + 0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, -0.5f }; tc_vec3_s tc_camera_position = { 0.0f, 0.0f, 0.0f }; @@ -300,12 +298,12 @@ bool update() int main(int argc, char **argv) { - tc_game_state_g = tc_init(); + tc_init(); tc_new_chunk(&tc_game_state_g.main_world, 1.0f, 1.0f, 1.0f); - tc_load_textures(); 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) diff --git a/code/source-c/meshize.c b/code/source-c/meshize.c index e7bd728..1f16979 100644 --- a/code/source-c/meshize.c +++ b/code/source-c/meshize.c @@ -1,4 +1,5 @@ #include "world.h" +#include "assets.h" #include #include @@ -15,27 +16,27 @@ float tc_block_vertices[108] = -0.5f, 0.5f, -0.5f, // Back + 0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, // Left -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, -0.5f, // Right - 0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, @@ -46,13 +47,13 @@ float tc_block_vertices[108] = -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, // Bottom - -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, @@ -60,7 +61,7 @@ float tc_block_vertices[108] = 0.5f, -0.5f, -0.5f }; -float test_uvs[72] = +float tc_generic_cube_uvs[72] = { // Front 0.0f, 0.0f, @@ -72,13 +73,13 @@ float test_uvs[72] = 0.0f, 1.0f, // Back - 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, - 0.0f, 0.0f, + 0.0f, 1.0f, 1.0f, 0.0f, + 1.0f, 1.0f, // Left 1.0f, 0.0f, @@ -107,7 +108,7 @@ float test_uvs[72] = 0.0f, 1.0f, 1.0f, 0.0f, - // Back + // Bottom 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, @@ -145,6 +146,74 @@ uint32_t tc_count_chunk_vertices(tc_chunk_s *chunk) return num_vertices; } +void tc_modify_basic_cube_face_uvs_to_fit(tc_image_s *image, float *face_uvs) +{ + float lower_x = ((float) image->x) / ((float) image->width); + float lower_y = ((float) image->y) / ((float) image->height); + float higher_x; + float higher_y; + + if(image->superimage != NULL) + { + higher_x = (((float) (image->x + image->width)) / ((float) image->superimage->width)); + higher_y = (((float) (image->y + image->height)) / ((float) image->superimage->height)); + } + else + { + higher_x = 1.0f; + higher_y = 1.0f; + } + + uint32_t num_floats = 2 * 6; + uint32_t index = 0; + + while(index < num_floats) + { + if(face_uvs[index] == 1.0f) + { + face_uvs[index] = higher_x; + } + else + { + face_uvs[index] = lower_x; + } + + ++index; + + if(face_uvs[index] == 1.0f) + { + face_uvs[index] = higher_y; + } + else + { + face_uvs[index] = lower_y; + } + ++index; + } +} + +void tc_generate_basic_cube_uvs(tc_block_entry_s *entry, float *block_uvs) +{ + uint32_t num_floats = 2 * 36; + memcpy(block_uvs, tc_generic_cube_uvs, sizeof(float) * num_floats); + + tc_block_attribute_s *front = tc_block_resolve_name_to_attribute(entry, "front_texture"); + tc_block_attribute_s *back = tc_block_resolve_name_to_attribute(entry, "back_texture"); + tc_block_attribute_s *west = tc_block_resolve_name_to_attribute(entry, "west_texture"); + tc_block_attribute_s *east = tc_block_resolve_name_to_attribute(entry, "east_texture"); + tc_block_attribute_s *top = tc_block_resolve_name_to_attribute(entry, "top_texture"); + tc_block_attribute_s *bottom = tc_block_resolve_name_to_attribute(entry, "bottom_texture"); + + uint32_t num_floats_per_face = 2 * 6; + + tc_modify_basic_cube_face_uvs_to_fit(front->value.pointer, &block_uvs[0]); + tc_modify_basic_cube_face_uvs_to_fit(back->value.pointer, &block_uvs[num_floats_per_face]); + tc_modify_basic_cube_face_uvs_to_fit(west->value.pointer, &block_uvs[num_floats_per_face*2]); + tc_modify_basic_cube_face_uvs_to_fit(east->value.pointer, &block_uvs[num_floats_per_face*3]); + tc_modify_basic_cube_face_uvs_to_fit(top->value.pointer, &block_uvs[num_floats_per_face*4]); + tc_modify_basic_cube_face_uvs_to_fit(bottom->value.pointer, &block_uvs[num_floats_per_face*5]); +} + void tc_meshize_chunk(tc_chunk_s *chunk) { chunk->num_vertices = tc_count_chunk_vertices(chunk); @@ -155,8 +224,6 @@ void tc_meshize_chunk(tc_chunk_s *chunk) float block_positions[36*3]; float block_uvs[36*2]; - memcpy(block_uvs, test_uvs, sizeof(float) * 36 * 2); - uint32_t x_in_chunk = 0; uint32_t y_in_chunk = 0; uint32_t z_in_chunk = 0; @@ -168,7 +235,26 @@ void tc_meshize_chunk(tc_chunk_s *chunk) z_in_chunk = 0; while(z_in_chunk < 32) { - if(chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk] > 0) + tc_block_entry_s *entry = + tc_resolve_id_to_block_entry( + chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk] + ); + if(entry == NULL) + { + ++z_in_chunk; + continue; + } + + tc_block_attribute_s *block = + tc_block_resolve_name_to_attribute(entry, "block_type"); + + if(block == NULL) + { + ++z_in_chunk; + continue; + } + + if(!strcmp(block->value.string, "basic_cube")) { memcpy( block_positions, @@ -191,6 +277,9 @@ void tc_meshize_chunk(tc_chunk_s *chunk) ++vertex_of_block; } + memcpy(block_uvs, tc_generic_cube_uvs, sizeof(float) * 36 * 2); + tc_generate_basic_cube_uvs(entry, block_uvs); + memcpy( &chunk->vertex_positions[vertex_index*3], block_positions, diff --git a/code/source-c/state.h b/code/source-c/state.h index 1ad1c64..dd1aa7e 100644 --- a/code/source-c/state.h +++ b/code/source-c/state.h @@ -43,8 +43,10 @@ typedef struct extern techneck_s tc_game_state_g; -techneck_s tc_init (); -void tc_cleanup (); +void tc_init (); +void tc_cleanup (); + +void tc_load_textures (); #endif diff --git a/code/source-c/world.c b/code/source-c/world.c index f52cb5e..9c3447d 100644 --- a/code/source-c/world.c +++ b/code/source-c/world.c @@ -100,7 +100,9 @@ tc_chunk_s * tc_new_chunk(tc_world_s *world, float x, float y, float z) chunk->position.y = y; chunk->position.z = z; world->worldgen->fn_generate_chunk(world->worldgen, chunk); + puts("Meshizing"); fflush(stdout); tc_meshize_chunk(chunk); + puts("Meshized"); fflush(stdout); glGenVertexArrays(1, &chunk->vao); glBindVertexArray(chunk->vao); diff --git a/code/source-c/world.h b/code/source-c/world.h index a28ed30..747252d 100644 --- a/code/source-c/world.h +++ b/code/source-c/world.h @@ -56,7 +56,7 @@ struct tc_world tc_world_s tc_init_worlds (); void tc_draw_world (tc_world_s *world); -tc_chunk_s * tc_new_chunk (tc_world_s *world, float x, float y, float z); +tc_chunk_s * tc_new_chunk (tc_world_s *world, float x, float y, float z); // must be integers void tc_set_block_in_chunk( tc_chunk_s *chunk, diff --git a/vertex_shader.glsl b/vertex_shader.glsl index f26a910..67f00bc 100644 --- a/vertex_shader.glsl +++ b/vertex_shader.glsl @@ -12,6 +12,7 @@ uniform mat4 model_matrix; void main() { gl_Position = projection_matrix * view_matrix * model_matrix * vec4(pos, 1.0); + uv_coords = uv; }