clean up worldgen and remove verbose logging

This commit is contained in:
asrael 2025-11-11 21:24:53 -06:00
parent 4d84122ef3
commit 34e534b6f2
11 changed files with 359 additions and 174 deletions

View file

@ -78,19 +78,6 @@ static i32 cave_grid_count_neighbors(const cave_grid* grid, i32 x, i32 y) {
return count;
}
static void calculate_texture_axes(const pxl8_vec3 normal, pxl8_vec3* u_axis, pxl8_vec3* v_axis) {
if (fabsf(normal.y) > 0.9f) {
*u_axis = (pxl8_vec3){1.0f, 0.0f, 0.0f};
*v_axis = (pxl8_vec3){0.0f, 0.0f, 1.0f};
} else if (fabsf(normal.x) > 0.7f) {
*u_axis = (pxl8_vec3){0.0f, 1.0f, 0.0f};
*v_axis = (pxl8_vec3){0.0f, 0.0f, 1.0f};
} else {
*u_axis = (pxl8_vec3){1.0f, 0.0f, 0.0f};
*v_axis = (pxl8_vec3){0.0f, 1.0f, 0.0f};
}
}
static inline void compute_face_aabb(pxl8_bsp_face* face, const pxl8_bsp_vertex* verts, u32 vert_idx) {
face->aabb_min = (pxl8_vec3){1e30f, 1e30f, 1e30f};
face->aabb_max = (pxl8_vec3){-1e30f, -1e30f, -1e30f};
@ -159,12 +146,14 @@ static pxl8_result cave_to_bsp(pxl8_bsp* bsp, const cave_grid* grid) {
bsp->planes = calloc(face_count, sizeof(pxl8_bsp_plane));
bsp->edges = calloc(vertex_count, sizeof(pxl8_bsp_edge));
bsp->surfedges = calloc(vertex_count, sizeof(i32));
bsp->texinfo = calloc(face_count, sizeof(pxl8_bsp_texinfo));
if (!bsp->vertices || !bsp->faces || !bsp->planes || !bsp->edges || !bsp->surfedges || !bsp->texinfo) {
if (!bsp->vertices || !bsp->faces || !bsp->planes || !bsp->edges || !bsp->surfedges) {
return PXL8_ERROR_OUT_OF_MEMORY;
}
bsp->texinfo = NULL;
bsp->num_texinfo = 0;
i32 vert_idx = 0;
i32 face_idx = 0;
i32 edge_idx = 0;
@ -190,13 +179,7 @@ static pxl8_result cave_to_bsp(pxl8_bsp* bsp, const cave_grid* grid) {
bsp->faces[face_idx].plane_id = face_idx;
bsp->faces[face_idx].num_edges = 4;
bsp->faces[face_idx].first_edge = edge_idx;
bsp->faces[face_idx].texinfo_id = face_idx;
calculate_texture_axes(bsp->planes[face_idx].normal,
&bsp->texinfo[face_idx].u_axis,
&bsp->texinfo[face_idx].v_axis);
bsp->texinfo[face_idx].u_offset = 0.0f;
bsp->texinfo[face_idx].v_offset = 0.0f;
bsp->faces[face_idx].texinfo_id = 0;
for (i32 i = 0; i < 4; i++) {
bsp->edges[edge_idx + i].vertex[0] = vert_idx + i;
@ -223,13 +206,7 @@ static pxl8_result cave_to_bsp(pxl8_bsp* bsp, const cave_grid* grid) {
bsp->faces[face_idx].plane_id = face_idx;
bsp->faces[face_idx].num_edges = 4;
bsp->faces[face_idx].first_edge = edge_idx;
bsp->faces[face_idx].texinfo_id = face_idx;
calculate_texture_axes(bsp->planes[face_idx].normal,
&bsp->texinfo[face_idx].u_axis,
&bsp->texinfo[face_idx].v_axis);
bsp->texinfo[face_idx].u_offset = 0.0f;
bsp->texinfo[face_idx].v_offset = 0.0f;
bsp->faces[face_idx].texinfo_id = 0;
for (i32 i = 0; i < 4; i++) {
bsp->edges[edge_idx + i].vertex[0] = vert_idx + i;
@ -256,13 +233,7 @@ static pxl8_result cave_to_bsp(pxl8_bsp* bsp, const cave_grid* grid) {
bsp->faces[face_idx].plane_id = face_idx;
bsp->faces[face_idx].num_edges = 4;
bsp->faces[face_idx].first_edge = edge_idx;
bsp->faces[face_idx].texinfo_id = face_idx;
calculate_texture_axes(bsp->planes[face_idx].normal,
&bsp->texinfo[face_idx].u_axis,
&bsp->texinfo[face_idx].v_axis);
bsp->texinfo[face_idx].u_offset = 0.0f;
bsp->texinfo[face_idx].v_offset = 0.0f;
bsp->faces[face_idx].texinfo_id = 0;
for (i32 i = 0; i < 4; i++) {
bsp->edges[edge_idx + i].vertex[0] = vert_idx + i;
@ -289,13 +260,7 @@ static pxl8_result cave_to_bsp(pxl8_bsp* bsp, const cave_grid* grid) {
bsp->faces[face_idx].plane_id = face_idx;
bsp->faces[face_idx].num_edges = 4;
bsp->faces[face_idx].first_edge = edge_idx;
bsp->faces[face_idx].texinfo_id = face_idx;
calculate_texture_axes(bsp->planes[face_idx].normal,
&bsp->texinfo[face_idx].u_axis,
&bsp->texinfo[face_idx].v_axis);
bsp->texinfo[face_idx].u_offset = 0.0f;
bsp->texinfo[face_idx].v_offset = 0.0f;
bsp->faces[face_idx].texinfo_id = 0;
for (i32 i = 0; i < 4; i++) {
bsp->edges[edge_idx + i].vertex[0] = vert_idx + i;
@ -330,13 +295,7 @@ static pxl8_result cave_to_bsp(pxl8_bsp* bsp, const cave_grid* grid) {
bsp->faces[face_idx].plane_id = face_idx;
bsp->faces[face_idx].num_edges = 4;
bsp->faces[face_idx].first_edge = edge_idx;
bsp->faces[face_idx].texinfo_id = face_idx;
calculate_texture_axes(bsp->planes[face_idx].normal,
&bsp->texinfo[face_idx].u_axis,
&bsp->texinfo[face_idx].v_axis);
bsp->texinfo[face_idx].u_offset = 0.0f;
bsp->texinfo[face_idx].v_offset = 0.0f;
bsp->faces[face_idx].texinfo_id = 0;
for (i32 i = 0; i < 4; i++) {
bsp->edges[edge_idx + i].vertex[0] = vert_idx + i;
@ -361,13 +320,7 @@ static pxl8_result cave_to_bsp(pxl8_bsp* bsp, const cave_grid* grid) {
bsp->faces[face_idx].plane_id = face_idx;
bsp->faces[face_idx].num_edges = 4;
bsp->faces[face_idx].first_edge = edge_idx;
bsp->faces[face_idx].texinfo_id = face_idx;
calculate_texture_axes(bsp->planes[face_idx].normal,
&bsp->texinfo[face_idx].u_axis,
&bsp->texinfo[face_idx].v_axis);
bsp->texinfo[face_idx].u_offset = 0.0f;
bsp->texinfo[face_idx].v_offset = 0.0f;
bsp->faces[face_idx].texinfo_id = 0;
for (i32 i = 0; i < 4; i++) {
bsp->edges[edge_idx + i].vertex[0] = vert_idx + i;
@ -389,7 +342,6 @@ static pxl8_result cave_to_bsp(pxl8_bsp* bsp, const cave_grid* grid) {
bsp->num_planes = face_count;
bsp->num_edges = vertex_count;
bsp->num_surfedges = vertex_count;
bsp->num_texinfo = face_count;
bsp->leafs = calloc(1, sizeof(pxl8_bsp_leaf));
bsp->marksurfaces = calloc(face_count, sizeof(u16));
@ -455,45 +407,99 @@ pxl8_result pxl8_procgen(pxl8_bsp* bsp, const pxl8_procgen_params* params) {
}
}
static u32 hash2d(i32 x, i32 y) {
u32 h = ((u32)x * 374761393u) + ((u32)y * 668265263u);
h ^= h >> 13;
h ^= h << 17;
h ^= h >> 5;
return h;
}
void pxl8_procgen_tex(u8* buffer, const pxl8_procgen_tex_params* params) {
if (!buffer || !params) return;
prng_seed(params->seed);
u8 min_val = 255, max_val = 0;
for (i32 y = 0; y < params->height; y++) {
for (i32 x = 0; x < params->width; x++) {
i32 ix = (i32)((f32)x * params->scale);
i32 iy = (i32)((f32)y * params->scale);
f32 u = (f32)x / (f32)params->width;
f32 v = (f32)y / (f32)params->height;
u32 block_hash = ((u32)ix * 374761393u + (u32)iy * 668265263u) ^ params->seed;
block_hash ^= block_hash >> 13;
block_hash ^= block_hash << 17;
block_hash ^= block_hash >> 5;
u8 color = params->base_color;
u32 pixel_hash = ((u32)x * 1597334677u + (u32)y * 3812015801u) ^ params->seed;
pixel_hash ^= pixel_hash >> 13;
pixel_hash ^= pixel_hash << 17;
pixel_hash ^= pixel_hash >> 5;
// Tile-based pattern (floor style)
if (params->seed == 11111) {
i32 tile_x = (i32)floorf(u * 8.0f);
i32 tile_y = (i32)floorf(v * 8.0f);
u32 h = hash2d(tile_x, tile_y);
u32 combined = (block_hash * 3 + pixel_hash) / 4;
f32 pattern = (f32)(h & 0xFF) / 255.0f;
i32 quantized = (pattern < 0.3f) ? 0 : (pattern < 0.7f) ? 1 : 2;
u32 value_range = params->variation + 1;
i32 value = params->base_color + (combined % value_range);
color = params->base_color + quantized;
if (value < 0) value = 0;
if (value > 15) value = 15;
// Checkerboard dither
if (((tile_x + tile_y) & 1) == 0 && (h & 0x100)) {
color = (color < 15) ? color + 1 : color;
}
}
// Large tile pattern (ceiling style)
else if (params->seed == 22222) {
i32 coarse_x = (i32)floorf(u * 2.0f);
i32 coarse_y = (i32)floorf(v * 2.0f);
u32 coarse_h = hash2d(coarse_x, coarse_y);
u8 final_value = (u8)value;
buffer[y * params->width + x] = final_value;
i32 subdivision = (coarse_h >> 8) & 0x3;
i32 tile_x, tile_y;
if (final_value < min_val) min_val = final_value;
if (final_value > max_val) max_val = final_value;
switch (subdivision) {
case 0: tile_x = (i32)floorf(u * 3.0f); tile_y = (i32)floorf(v * 3.0f); break;
case 1: tile_x = (i32)floorf(u * 5.0f); tile_y = (i32)floorf(v * 5.0f); break;
case 2: tile_x = (i32)floorf(u * 2.0f); tile_y = (i32)floorf(v * 4.0f); break;
default: tile_x = (i32)floorf(u * 4.0f); tile_y = (i32)floorf(v * 2.0f); break;
}
u32 h = hash2d(tile_x, tile_y);
f32 pattern = (f32)(h & 0xFF) / 255.0f;
if (pattern < 0.25f) color = params->base_color;
else if (pattern < 0.50f) color = params->base_color + 1;
else if (pattern < 0.75f) color = params->base_color + 2;
else color = params->base_color + 1;
}
// Brick pattern (wall style)
else {
f32 brick_y = floorf(v * 4.0f);
f32 offset = ((i32)brick_y & 1) ? 0.5f : 0.0f;
i32 brick_x = (i32)floorf(u * 4.0f + offset);
brick_y = (i32)brick_y;
f32 brick_u = fabsf((u * 4.0f + offset) - floorf(u * 4.0f + offset) - 0.5f);
f32 brick_v = fabsf((v * 4.0f) - floorf(v * 4.0f) - 0.5f);
u32 h = hash2d(brick_x, (i32)brick_y);
f32 noise = (f32)(h & 0xFF) / 255.0f;
// Mortar lines
if (brick_u > 0.47f || brick_v > 0.47f) {
color = params->base_color - 2;
} else {
i32 shade = (i32)(noise * 3.0f);
color = params->base_color + shade;
}
}
if (color > 31) color = 31;
buffer[y * params->width + x] = color;
}
}
pxl8_debug("Generated texture %dx%d: values range %u-%u (base=%u, variation=%u)",
params->width, params->height, min_val, max_val,
params->base_color, params->variation);
u8 min_color = 255, max_color = 0;
u32 color_counts[256] = {0};
for (i32 i = 0; i < params->width * params->height; i++) {
if (buffer[i] < min_color) min_color = buffer[i];
if (buffer[i] > max_color) max_color = buffer[i];
color_counts[buffer[i]]++;
}
}