Techneck/code/source-c/meshize.c

215 lines
5.1 KiB
C
Raw Normal View History

2023-10-11 08:10:06 +00:00
#include "world.h"
#include <stdlib.h>
#include <string.h>
float tc_block_vertices[108] =
{
// Front
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
2023-10-11 08:10:06 +00:00
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
2023-10-11 08:10:06 +00:00
// Back
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
2023-10-11 08:10:06 +00:00
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
2023-10-11 08:10:06 +00:00
// Left
-0.5f, 0.5f, -0.5f,
2023-10-11 08:10:06 +00:00
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
2023-10-11 08:10:06 +00:00
-0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
2023-10-11 08:10:06 +00:00
// Right
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
2023-10-11 08:10:06 +00:00
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
2023-10-11 08:10:06 +00:00
// Top
-0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
2023-10-11 08:10:06 +00:00
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
2023-10-11 08:10:06 +00:00
// Bottom
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
2023-10-11 08:10:06 +00:00
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f
2023-10-11 08:10:06 +00:00
};
float test_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, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
// Left
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
// Right
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
// Top
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
1.0f, 0.0f,
// Back
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f
};
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)
{
if(chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk] > 0)
2023-10-11 08:10:06 +00:00
{
num_vertices += 36;
}
++z_in_chunk;
}
++y_in_chunk;
}
++x_in_chunk;
}
return num_vertices;
}
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);
2023-10-11 08:10:06 +00:00
uint32_t vertex_index = 0;
float block_positions[36*3];
float block_uvs[36*2];
memcpy(block_uvs, test_uvs, sizeof(float) * 36 * 2);
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)
{
if(chunk->blocks[x_in_chunk][y_in_chunk][z_in_chunk] > 0)
2023-10-11 08:10:06 +00:00
{
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(
2023-10-11 08:41:58 +00:00
&chunk->vertex_positions[vertex_index*3],
2023-10-11 08:10:06 +00:00
block_positions,
sizeof(float) * 36 * 3
);
memcpy(
&chunk->vertex_uvs[vertex_index*2],
block_uvs,
sizeof(float) * 36 * 2
);
2023-10-11 08:10:06 +00:00
vertex_index += 36;
}
++z_in_chunk;
}
++y_in_chunk;
}
++x_in_chunk;
}
}