Techneck/code/source-c/meshize.c

247 lines
9.0 KiB
C
Raw Normal View History

2023-10-13 10:23:26 +00:00
#include "blocks.h"
2023-10-11 08:10:06 +00:00
#include "world.h"
2023-10-12 06:18:43 +00:00
#include "assets.h"
2023-10-11 08:10:06 +00:00
#include <stdlib.h>
#include <string.h>
2023-10-13 10:23:26 +00:00
bool tc_block_is_opaque_by_id(uint32_t id)
2023-10-11 08:10:06 +00:00
{
2023-10-13 10:23:26 +00:00
return !!(id & TC_BLOCK_OPAQUE_BIT);
}
2023-10-13 10:23:26 +00:00
uint32_t tc_count_vertices_of_block(tc_chunk_s *chunk, uint8_t x, uint8_t y, uint8_t z)
{
2023-10-13 10:23:26 +00:00
uint32_t num_vertices = 0;
uint32_t top_neighbour = 0;
uint32_t bottom_neighbour = 0;
uint32_t north_neighbour = 0;
uint32_t south_neighbour = 0;
uint32_t east_neighbour = 0;
uint32_t west_neighbour = 0;
if(x < 31) east_neighbour = chunk->blocks[x+1] [y] [z];
if(x > 0) west_neighbour = chunk->blocks[x-1] [y] [z];
if(y < 31) top_neighbour = chunk->blocks[x] [y+1] [z];
if(y > 0) bottom_neighbour = chunk->blocks[x] [y-1] [z];
if(z < 31) north_neighbour = chunk->blocks[x] [y] [z+1];
if(z > 0) south_neighbour = chunk->blocks[x] [y] [z-1];
if(!tc_block_is_opaque_by_id(top_neighbour)) num_vertices += 6;
if(!tc_block_is_opaque_by_id(bottom_neighbour)) num_vertices += 6;
if(!tc_block_is_opaque_by_id(north_neighbour)) num_vertices += 6;
if(!tc_block_is_opaque_by_id(south_neighbour)) num_vertices += 6;
if(!tc_block_is_opaque_by_id(east_neighbour)) num_vertices += 6;
if(!tc_block_is_opaque_by_id(west_neighbour)) num_vertices += 6;
2023-10-13 10:23:26 +00:00
return num_vertices;
}
2023-10-11 08:10:06 +00:00
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)
{
2023-10-13 10:23:26 +00:00
if(chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk] != 0)
2023-10-11 08:10:06 +00:00
{
2023-10-13 10:23:26 +00:00
num_vertices += tc_count_vertices_of_block(chunk, x_in_chunk, y_in_chunk, z_in_chunk);
2023-10-11 08:10:06 +00:00
}
++z_in_chunk;
}
++y_in_chunk;
}
++x_in_chunk;
}
return num_vertices;
}
2023-10-13 10:23:26 +00:00
void printfloatarr(float *arr, uint32_t num)
2023-10-12 06:18:43 +00:00
{
2023-10-13 10:23:26 +00:00
for(int i = 0; i < num; ++i)
printf("%f ", arr[i]);
}
uint32_t tc_meshize_block(tc_chunk_s *chunk,
uint8_t x, uint8_t y, uint8_t z,
float *xyz, float *uv)
{
tc_block_entry_s *entry =
tc_resolve_id_to_block_entry(
chunk->blocks[x][y][z]
);
2023-10-12 06:18:43 +00:00
2023-10-13 10:23:26 +00:00
if(entry == NULL)
2023-10-12 06:18:43 +00:00
{
2023-10-13 10:23:26 +00:00
return 0;
2023-10-12 06:18:43 +00:00
}
2023-10-13 10:23:26 +00:00
uint32_t num_vertices = 0;
uint32_t top_neighbour = 0;
uint32_t bottom_neighbour = 0;
uint32_t north_neighbour = 0;
uint32_t south_neighbour = 0;
uint32_t east_neighbour = 0;
uint32_t west_neighbour = 0;
if(x < 31) east_neighbour = chunk->blocks[x+1] [y] [z];
if(x > 0) west_neighbour = chunk->blocks[x-1] [y] [z];
if(y < 31) top_neighbour = chunk->blocks[x] [y+1] [z];
if(y > 0) bottom_neighbour = chunk->blocks[x] [y-1] [z];
if(z < 31) north_neighbour = chunk->blocks[x] [y] [z+1];
if(z > 0) south_neighbour = chunk->blocks[x] [y] [z-1];
if(!tc_block_is_opaque_by_id(top_neighbour))
2023-10-12 06:18:43 +00:00
{
2023-10-13 10:23:26 +00:00
tc_cube_side_mesh_s *side_mesh =
tc_block_resolve_name_to_attribute(entry, "top_mesh")->value.pointer;
memcpy(&xyz[num_vertices*3], side_mesh->xyz, sizeof(float) * side_mesh->num_vertices * 3);
memcpy(&uv[num_vertices*2], side_mesh->uv, sizeof(float) * side_mesh->num_vertices * 2);
num_vertices += side_mesh->num_vertices;
2023-10-12 06:18:43 +00:00
}
2023-10-13 10:23:26 +00:00
if(!tc_block_is_opaque_by_id(bottom_neighbour))
{
tc_cube_side_mesh_s *side_mesh =
tc_block_resolve_name_to_attribute(entry, "bottom_mesh")->value.pointer;
memcpy(&xyz[num_vertices*3], side_mesh->xyz, sizeof(float) * side_mesh->num_vertices * 3);
memcpy(&uv[num_vertices*2], side_mesh->uv, sizeof(float) * side_mesh->num_vertices * 2);
num_vertices += side_mesh->num_vertices;
}
2023-10-12 06:18:43 +00:00
2023-10-13 10:23:26 +00:00
if(!tc_block_is_opaque_by_id(south_neighbour))
2023-10-12 06:18:43 +00:00
{
2023-10-13 10:23:26 +00:00
tc_cube_side_mesh_s *side_mesh =
tc_block_resolve_name_to_attribute(entry, "south_mesh")->value.pointer;
2023-10-12 06:18:43 +00:00
2023-10-13 10:23:26 +00:00
memcpy(&xyz[num_vertices*3], side_mesh->xyz, sizeof(float) * side_mesh->num_vertices * 3);
memcpy(&uv[num_vertices*2], side_mesh->uv, sizeof(float) * side_mesh->num_vertices * 2);
num_vertices += side_mesh->num_vertices;
}
if(!tc_block_is_opaque_by_id(north_neighbour))
{
tc_cube_side_mesh_s *side_mesh =
tc_block_resolve_name_to_attribute(entry, "north_mesh")->value.pointer;
2023-10-12 06:18:43 +00:00
2023-10-13 10:23:26 +00:00
memcpy(&xyz[num_vertices*3], side_mesh->xyz, sizeof(float) * side_mesh->num_vertices * 3);
memcpy(&uv[num_vertices*2], side_mesh->uv, sizeof(float) * side_mesh->num_vertices * 2);
num_vertices += side_mesh->num_vertices;
}
if(!tc_block_is_opaque_by_id(east_neighbour))
{
tc_cube_side_mesh_s *side_mesh =
tc_block_resolve_name_to_attribute(entry, "east_mesh")->value.pointer;
memcpy(&xyz[num_vertices*3], side_mesh->xyz, sizeof(float) * side_mesh->num_vertices * 3);
memcpy(&uv[num_vertices*2], side_mesh->uv, sizeof(float) * side_mesh->num_vertices * 2);
num_vertices += side_mesh->num_vertices;
}
if(!tc_block_is_opaque_by_id(west_neighbour))
{
tc_cube_side_mesh_s *side_mesh =
tc_block_resolve_name_to_attribute(entry, "west_mesh")->value.pointer;
memcpy(&xyz[num_vertices*3], side_mesh->xyz, sizeof(float) * side_mesh->num_vertices * 3);
memcpy(&uv[num_vertices*2], side_mesh->uv, sizeof(float) * side_mesh->num_vertices * 2);
num_vertices += side_mesh->num_vertices;
2023-10-12 06:18:43 +00:00
}
2023-10-13 10:23:26 +00:00
uint32_t vertex_of_block = 0;
vertex_of_block = 0;
while(vertex_of_block < num_vertices)
{
xyz[vertex_of_block*3 + 0] += (float) x;
xyz[vertex_of_block*3 + 1] += (float) y;
xyz[vertex_of_block*3 + 2] += (float) z;
++vertex_of_block;
}
2023-10-12 06:18:43 +00:00
2023-10-13 10:23:26 +00:00
return num_vertices;
2023-10-12 06:18:43 +00:00
}
2023-10-11 08:10:06 +00:00
void tc_meshize_chunk(tc_chunk_s *chunk)
{
chunk->num_vertices = tc_count_chunk_vertices(chunk);
2023-10-13 10:23:26 +00:00
chunk->vertex_positions = calloc(sizeof(float), 3 * chunk->num_vertices);
chunk->vertex_uvs = calloc(sizeof(float), 2 * chunk->num_vertices);
2023-10-11 08:10:06 +00:00
uint32_t vertex_index = 0;
2023-10-13 10:23:26 +00:00
float block_positions[1024];
float block_uvs[1024];
2023-10-11 08:10:06 +00:00
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)
{
2023-10-12 06:18:43 +00:00
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"))
2023-10-11 08:10:06 +00:00
{
2023-10-13 10:23:26 +00:00
memset(block_positions, 0x00, sizeof(float) * 1024);
memset(block_uvs, 0x00, sizeof(float) * 1024);
uint32_t made_vertices = tc_meshize_block(chunk, x_in_chunk, y_in_chunk, z_in_chunk, block_positions, block_uvs);
2023-10-12 06:18:43 +00:00
2023-10-11 08:10:06 +00:00
memcpy(
2023-10-11 08:41:58 +00:00
&chunk->vertex_positions[vertex_index*3],
2023-10-11 08:10:06 +00:00
block_positions,
2023-10-13 10:23:26 +00:00
sizeof(float) * made_vertices * 3
2023-10-11 08:10:06 +00:00
);
memcpy(
&chunk->vertex_uvs[vertex_index*2],
block_uvs,
2023-10-13 10:23:26 +00:00
sizeof(float) * made_vertices * 2
);
2023-10-13 10:23:26 +00:00
vertex_index += made_vertices;
2023-10-11 08:10:06 +00:00
}
++z_in_chunk;
}
++y_in_chunk;
}
++x_in_chunk;
}
}