#include "world.h" #include "assets.h" #include #include float tc_block_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, // 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, // 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, // 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, // 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, // 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 }; float tc_generic_cube_uvs[72] = { // Front 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Back 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, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Right 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Top 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Bottom 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f }; uint32_t tc_count_chunk_vertices(tc_chunk_s *chunk) { uint32_t num_vertices = 0; uint32_t x_in_chunk = 0; uint32_t y_in_chunk = 0; uint32_t z_in_chunk = 0; while(x_in_chunk < 32) { y_in_chunk = 0; while(y_in_chunk < 32) { z_in_chunk = 0; while(z_in_chunk < 32) { if(chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk] > 0) { num_vertices += 36; } ++z_in_chunk; } ++y_in_chunk; } ++x_in_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); chunk->vertex_positions = malloc(sizeof(float) * (3) * chunk->num_vertices); chunk->vertex_uvs = malloc(sizeof(float) * (2) * chunk->num_vertices); uint32_t vertex_index = 0; float block_positions[36*3]; float block_uvs[36*2]; uint32_t x_in_chunk = 0; uint32_t y_in_chunk = 0; uint32_t z_in_chunk = 0; while(x_in_chunk < 32) { y_in_chunk = 0; while(y_in_chunk < 32) { z_in_chunk = 0; while(z_in_chunk < 32) { 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, tc_block_vertices, sizeof(float) * 36 * 3 ); // Move the current block to where it is supposed to be uint32_t vertex_of_block = 0; while(vertex_of_block < 36) { uint32_t vertex_start = vertex_of_block * 3; block_positions[vertex_start] += x_in_chunk; block_positions[vertex_start + 1] += y_in_chunk; block_positions[vertex_start + 2] += z_in_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, sizeof(float) * 36 * 3 ); memcpy( &chunk->vertex_uvs[vertex_index*2], block_uvs, sizeof(float) * 36 * 2 ); vertex_index += 36; } ++z_in_chunk; } ++y_in_chunk; } ++x_in_chunk; } }