refactor separate framework from game code, add demo3d
This commit is contained in:
parent
19ae869769
commit
40f5cdcaa5
92 changed files with 2665 additions and 6547 deletions
594
demo3d/client/bsp/demo3d_bsp.c
Normal file
594
demo3d/client/bsp/demo3d_bsp.c
Normal file
|
|
@ -0,0 +1,594 @@
|
|||
#include "demo3d_bsp.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "pxl8_color.h"
|
||||
#include "pxl8_io.h"
|
||||
#include "pxl8_log.h"
|
||||
#include "pxl8_mem.h"
|
||||
|
||||
#define BSP_VERSION 29
|
||||
|
||||
typedef enum {
|
||||
CHUNK_ENTITIES = 0,
|
||||
CHUNK_PLANES = 1,
|
||||
CHUNK_TEXTURES = 2,
|
||||
CHUNK_VERTICES = 3,
|
||||
CHUNK_VISIBILITY = 4,
|
||||
CHUNK_NODES = 5,
|
||||
CHUNK_TEXINFO = 6,
|
||||
CHUNK_FACES = 7,
|
||||
CHUNK_LIGHTING = 8,
|
||||
CHUNK_CLIPNODES = 9,
|
||||
CHUNK_LEAFS = 10,
|
||||
CHUNK_MARKSURFACES = 11,
|
||||
CHUNK_EDGES = 12,
|
||||
CHUNK_SURFEDGES = 13,
|
||||
CHUNK_MODELS = 14,
|
||||
CHUNK_COUNT = 15
|
||||
} demo3d_bsp_chunk_type;
|
||||
|
||||
typedef struct {
|
||||
u32 offset;
|
||||
u32 size;
|
||||
} demo3d_bsp_chunk;
|
||||
|
||||
typedef struct {
|
||||
u32 version;
|
||||
demo3d_bsp_chunk chunks[CHUNK_COUNT];
|
||||
} demo3d_bsp_header;
|
||||
|
||||
static inline pxl8_vec3 read_vec3(pxl8_stream* stream) {
|
||||
f32 x = pxl8_read_f32(stream);
|
||||
f32 y = pxl8_read_f32(stream);
|
||||
f32 z = pxl8_read_f32(stream);
|
||||
return (pxl8_vec3){x, z, y};
|
||||
}
|
||||
|
||||
static bool validate_chunk(const demo3d_bsp_chunk* chunk, u32 element_size, usize file_size) {
|
||||
if (chunk->size == 0) return true;
|
||||
if (chunk->offset >= file_size) return false;
|
||||
if (chunk->offset + chunk->size > file_size) return false;
|
||||
if (chunk->size % element_size != 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool demo3d_bsp_get_edge_vertex(const demo3d_bsp* bsp, i32 surfedge_idx, u32* out_vert_idx) {
|
||||
if (surfedge_idx >= (i32)bsp->num_surfedges) return false;
|
||||
|
||||
i32 edge_idx = bsp->surfedges[surfedge_idx];
|
||||
u32 vertex_index;
|
||||
|
||||
if (edge_idx >= 0) {
|
||||
if ((u32)edge_idx >= bsp->num_edges) return false;
|
||||
vertex_index = 0;
|
||||
} else {
|
||||
edge_idx = -edge_idx;
|
||||
if ((u32)edge_idx >= bsp->num_edges) return false;
|
||||
vertex_index = 1;
|
||||
}
|
||||
|
||||
*out_vert_idx = bsp->edges[edge_idx].vertex[vertex_index];
|
||||
return *out_vert_idx < bsp->num_vertices;
|
||||
}
|
||||
|
||||
pxl8_result demo3d_bsp_load(const char* path, demo3d_bsp* bsp) {
|
||||
if (!path || !bsp) return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
|
||||
memset(bsp, 0, sizeof(*bsp));
|
||||
|
||||
u8* file_data = NULL;
|
||||
usize file_size = 0;
|
||||
pxl8_result result = pxl8_io_read_binary_file(path, &file_data, &file_size);
|
||||
if (result != PXL8_OK) {
|
||||
pxl8_error("Failed to load BSP file: %s", path);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (file_size < sizeof(demo3d_bsp_header)) {
|
||||
pxl8_error("BSP file too small: %s", path);
|
||||
pxl8_free(file_data);
|
||||
return PXL8_ERROR_INVALID_FORMAT;
|
||||
}
|
||||
|
||||
pxl8_stream stream = pxl8_stream_create(file_data, (u32)file_size);
|
||||
|
||||
demo3d_bsp_header header;
|
||||
header.version = pxl8_read_u32(&stream);
|
||||
|
||||
if (header.version != BSP_VERSION) {
|
||||
pxl8_error("Invalid BSP version: %u (expected %d)", header.version, BSP_VERSION);
|
||||
pxl8_free(file_data);
|
||||
return PXL8_ERROR_INVALID_FORMAT;
|
||||
}
|
||||
|
||||
for (i32 i = 0; i < CHUNK_COUNT; i++) {
|
||||
header.chunks[i].offset = pxl8_read_u32(&stream);
|
||||
header.chunks[i].size = pxl8_read_u32(&stream);
|
||||
}
|
||||
|
||||
demo3d_bsp_chunk* chunk = &header.chunks[CHUNK_VERTICES];
|
||||
if (!validate_chunk(chunk, 12, file_size)) goto error_cleanup;
|
||||
bsp->num_vertices = chunk->size / 12;
|
||||
if (bsp->num_vertices > 0) {
|
||||
bsp->vertices = pxl8_calloc(bsp->num_vertices, sizeof(demo3d_bsp_vertex));
|
||||
if (!bsp->vertices) goto error_cleanup;
|
||||
pxl8_stream_seek(&stream, chunk->offset);
|
||||
for (u32 i = 0; i < bsp->num_vertices; i++) {
|
||||
bsp->vertices[i].position = read_vec3(&stream);
|
||||
}
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_EDGES];
|
||||
if (!validate_chunk(chunk, 4, file_size)) goto error_cleanup;
|
||||
bsp->num_edges = chunk->size / 4;
|
||||
if (bsp->num_edges > 0) {
|
||||
bsp->edges = pxl8_calloc(bsp->num_edges, sizeof(demo3d_bsp_edge));
|
||||
pxl8_stream_seek(&stream, chunk->offset);
|
||||
for (u32 i = 0; i < bsp->num_edges; i++) {
|
||||
bsp->edges[i].vertex[0] = pxl8_read_u16(&stream);
|
||||
bsp->edges[i].vertex[1] = pxl8_read_u16(&stream);
|
||||
}
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_SURFEDGES];
|
||||
if (!validate_chunk(chunk, 4, file_size)) goto error_cleanup;
|
||||
bsp->num_surfedges = chunk->size / 4;
|
||||
if (bsp->num_surfedges > 0) {
|
||||
bsp->surfedges = pxl8_calloc(bsp->num_surfedges, sizeof(i32));
|
||||
pxl8_stream_seek(&stream, chunk->offset);
|
||||
for (u32 i = 0; i < bsp->num_surfedges; i++) {
|
||||
bsp->surfedges[i] = pxl8_read_i32(&stream);
|
||||
}
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_PLANES];
|
||||
if (!validate_chunk(chunk, 20, file_size)) goto error_cleanup;
|
||||
bsp->num_planes = chunk->size / 20;
|
||||
if (bsp->num_planes > 0) {
|
||||
bsp->planes = pxl8_calloc(bsp->num_planes, sizeof(demo3d_bsp_plane));
|
||||
pxl8_stream_seek(&stream, chunk->offset);
|
||||
for (u32 i = 0; i < bsp->num_planes; i++) {
|
||||
bsp->planes[i].normal = read_vec3(&stream);
|
||||
bsp->planes[i].dist = pxl8_read_f32(&stream);
|
||||
bsp->planes[i].type = pxl8_read_i32(&stream);
|
||||
}
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_FACES];
|
||||
if (!validate_chunk(chunk, 20, file_size)) goto error_cleanup;
|
||||
bsp->num_faces = chunk->size / 20;
|
||||
if (bsp->num_faces > 0) {
|
||||
bsp->faces = pxl8_calloc(bsp->num_faces, sizeof(demo3d_bsp_face));
|
||||
pxl8_stream_seek(&stream, chunk->offset);
|
||||
for (u32 i = 0; i < bsp->num_faces; i++) {
|
||||
bsp->faces[i].plane_id = pxl8_read_u16(&stream);
|
||||
bsp->faces[i].side = pxl8_read_u16(&stream);
|
||||
bsp->faces[i].first_edge = pxl8_read_u32(&stream);
|
||||
bsp->faces[i].num_edges = pxl8_read_u16(&stream);
|
||||
bsp->faces[i].material_id = pxl8_read_u16(&stream);
|
||||
bsp->faces[i].styles[0] = pxl8_read_u8(&stream);
|
||||
bsp->faces[i].styles[1] = pxl8_read_u8(&stream);
|
||||
bsp->faces[i].styles[2] = pxl8_read_u8(&stream);
|
||||
bsp->faces[i].styles[3] = pxl8_read_u8(&stream);
|
||||
bsp->faces[i].lightmap_offset = pxl8_read_u32(&stream);
|
||||
|
||||
bsp->faces[i].aabb_min = (pxl8_vec3){1e30f, 1e30f, 1e30f};
|
||||
bsp->faces[i].aabb_max = (pxl8_vec3){-1e30f, -1e30f, -1e30f};
|
||||
}
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_NODES];
|
||||
if (!validate_chunk(chunk, 24, file_size)) goto error_cleanup;
|
||||
bsp->num_nodes = chunk->size / 24;
|
||||
if (bsp->num_nodes > 0) {
|
||||
bsp->nodes = pxl8_calloc(bsp->num_nodes, sizeof(demo3d_bsp_node));
|
||||
pxl8_stream_seek(&stream, chunk->offset);
|
||||
for (u32 i = 0; i < bsp->num_nodes; i++) {
|
||||
bsp->nodes[i].plane_id = pxl8_read_u32(&stream);
|
||||
bsp->nodes[i].children[0] = pxl8_read_i16(&stream);
|
||||
bsp->nodes[i].children[1] = pxl8_read_i16(&stream);
|
||||
i16 nx = pxl8_read_i16(&stream);
|
||||
i16 ny = pxl8_read_i16(&stream);
|
||||
i16 nz = pxl8_read_i16(&stream);
|
||||
bsp->nodes[i].mins[0] = nx;
|
||||
bsp->nodes[i].mins[1] = nz;
|
||||
bsp->nodes[i].mins[2] = ny;
|
||||
i16 mx = pxl8_read_i16(&stream);
|
||||
i16 my = pxl8_read_i16(&stream);
|
||||
i16 mz = pxl8_read_i16(&stream);
|
||||
bsp->nodes[i].maxs[0] = mx;
|
||||
bsp->nodes[i].maxs[1] = mz;
|
||||
bsp->nodes[i].maxs[2] = my;
|
||||
bsp->nodes[i].first_face = pxl8_read_u16(&stream);
|
||||
bsp->nodes[i].num_faces = pxl8_read_u16(&stream);
|
||||
}
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_LEAFS];
|
||||
if (!validate_chunk(chunk, 28, file_size)) goto error_cleanup;
|
||||
bsp->num_leafs = chunk->size / 28;
|
||||
if (bsp->num_leafs > 0) {
|
||||
bsp->leafs = pxl8_calloc(bsp->num_leafs, sizeof(demo3d_bsp_leaf));
|
||||
pxl8_stream_seek(&stream, chunk->offset);
|
||||
for (u32 i = 0; i < bsp->num_leafs; i++) {
|
||||
bsp->leafs[i].contents = pxl8_read_i32(&stream);
|
||||
bsp->leafs[i].visofs = pxl8_read_i32(&stream);
|
||||
i16 nx = pxl8_read_i16(&stream);
|
||||
i16 ny = pxl8_read_i16(&stream);
|
||||
i16 nz = pxl8_read_i16(&stream);
|
||||
bsp->leafs[i].mins[0] = nx;
|
||||
bsp->leafs[i].mins[1] = nz;
|
||||
bsp->leafs[i].mins[2] = ny;
|
||||
i16 mx = pxl8_read_i16(&stream);
|
||||
i16 my = pxl8_read_i16(&stream);
|
||||
i16 mz = pxl8_read_i16(&stream);
|
||||
bsp->leafs[i].maxs[0] = mx;
|
||||
bsp->leafs[i].maxs[1] = mz;
|
||||
bsp->leafs[i].maxs[2] = my;
|
||||
bsp->leafs[i].first_marksurface = pxl8_read_u16(&stream);
|
||||
bsp->leafs[i].num_marksurfaces = pxl8_read_u16(&stream);
|
||||
for (u32 j = 0; j < 4; j++) bsp->leafs[i].ambient_level[j] = pxl8_read_u8(&stream);
|
||||
}
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_MARKSURFACES];
|
||||
if (!validate_chunk(chunk, 2, file_size)) goto error_cleanup;
|
||||
bsp->num_marksurfaces = chunk->size / 2;
|
||||
if (bsp->num_marksurfaces > 0) {
|
||||
bsp->marksurfaces = pxl8_calloc(bsp->num_marksurfaces, sizeof(u16));
|
||||
pxl8_stream_seek(&stream, chunk->offset);
|
||||
for (u32 i = 0; i < bsp->num_marksurfaces; i++) {
|
||||
bsp->marksurfaces[i] = pxl8_read_u16(&stream);
|
||||
}
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_MODELS];
|
||||
if (!validate_chunk(chunk, 64, file_size)) goto error_cleanup;
|
||||
bsp->num_models = chunk->size / 64;
|
||||
if (bsp->num_models > 0) {
|
||||
bsp->models = pxl8_calloc(bsp->num_models, sizeof(demo3d_bsp_model));
|
||||
pxl8_stream_seek(&stream, chunk->offset);
|
||||
for (u32 i = 0; i < bsp->num_models; i++) {
|
||||
f32 minx = pxl8_read_f32(&stream);
|
||||
f32 miny = pxl8_read_f32(&stream);
|
||||
f32 minz = pxl8_read_f32(&stream);
|
||||
bsp->models[i].mins[0] = minx;
|
||||
bsp->models[i].mins[1] = minz;
|
||||
bsp->models[i].mins[2] = miny;
|
||||
f32 maxx = pxl8_read_f32(&stream);
|
||||
f32 maxy = pxl8_read_f32(&stream);
|
||||
f32 maxz = pxl8_read_f32(&stream);
|
||||
bsp->models[i].maxs[0] = maxx;
|
||||
bsp->models[i].maxs[1] = maxz;
|
||||
bsp->models[i].maxs[2] = maxy;
|
||||
bsp->models[i].origin = read_vec3(&stream);
|
||||
for (u32 j = 0; j < 4; j++) bsp->models[i].headnode[j] = pxl8_read_i32(&stream);
|
||||
bsp->models[i].visleafs = pxl8_read_i32(&stream);
|
||||
bsp->models[i].first_face = pxl8_read_i32(&stream);
|
||||
bsp->models[i].num_faces = pxl8_read_i32(&stream);
|
||||
}
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_VISIBILITY];
|
||||
if (!validate_chunk(chunk, 1, file_size)) goto error_cleanup;
|
||||
bsp->visdata_size = chunk->size;
|
||||
if (bsp->visdata_size > 0) {
|
||||
bsp->visdata = pxl8_malloc(bsp->visdata_size);
|
||||
memcpy(bsp->visdata, file_data + chunk->offset, bsp->visdata_size);
|
||||
}
|
||||
|
||||
chunk = &header.chunks[CHUNK_LIGHTING];
|
||||
if (!validate_chunk(chunk, 1, file_size)) goto error_cleanup;
|
||||
bsp->lightdata_size = chunk->size;
|
||||
if (bsp->lightdata_size > 0) {
|
||||
bsp->lightdata = pxl8_malloc(bsp->lightdata_size);
|
||||
memcpy(bsp->lightdata, file_data + chunk->offset, bsp->lightdata_size);
|
||||
}
|
||||
|
||||
pxl8_free(file_data);
|
||||
|
||||
for (u32 i = 0; i < bsp->num_faces; i++) {
|
||||
demo3d_bsp_face* face = &bsp->faces[i];
|
||||
f32 min_x = 1e30f, min_y = 1e30f, min_z = 1e30f;
|
||||
f32 max_x = -1e30f, max_y = -1e30f, max_z = -1e30f;
|
||||
|
||||
for (u32 j = 0; j < face->num_edges; j++) {
|
||||
i32 surfedge_idx = face->first_edge + j;
|
||||
u32 vert_idx;
|
||||
|
||||
if (!demo3d_bsp_get_edge_vertex(bsp, surfedge_idx, &vert_idx)) continue;
|
||||
|
||||
pxl8_vec3 v = bsp->vertices[vert_idx].position;
|
||||
|
||||
if (v.x < min_x) min_x = v.x;
|
||||
if (v.x > max_x) max_x = v.x;
|
||||
if (v.y < min_y) min_y = v.y;
|
||||
if (v.y > max_y) max_y = v.y;
|
||||
if (v.z < min_z) min_z = v.z;
|
||||
if (v.z > max_z) max_z = v.z;
|
||||
}
|
||||
|
||||
face->aabb_min = (pxl8_vec3){min_x, min_y, min_z};
|
||||
face->aabb_max = (pxl8_vec3){max_x, max_y, max_z};
|
||||
}
|
||||
|
||||
pxl8_debug("Loaded BSP: %u verts, %u faces, %u nodes, %u leafs",
|
||||
bsp->num_vertices, bsp->num_faces, bsp->num_nodes, bsp->num_leafs);
|
||||
|
||||
return PXL8_OK;
|
||||
|
||||
error_cleanup:
|
||||
pxl8_error("BSP chunk validation failed: %s", path);
|
||||
pxl8_free(file_data);
|
||||
demo3d_bsp_destroy(bsp);
|
||||
return PXL8_ERROR_INVALID_FORMAT;
|
||||
}
|
||||
|
||||
void demo3d_bsp_destroy(demo3d_bsp* bsp) {
|
||||
if (!bsp) return;
|
||||
|
||||
pxl8_free(bsp->cell_portals);
|
||||
pxl8_free(bsp->edges);
|
||||
pxl8_free(bsp->faces);
|
||||
pxl8_free(bsp->leafs);
|
||||
pxl8_free(bsp->lightdata);
|
||||
pxl8_free(bsp->lightmaps);
|
||||
pxl8_free(bsp->marksurfaces);
|
||||
pxl8_free(bsp->models);
|
||||
pxl8_free(bsp->nodes);
|
||||
pxl8_free(bsp->planes);
|
||||
pxl8_free(bsp->surfedges);
|
||||
pxl8_free(bsp->vertex_lights);
|
||||
pxl8_free(bsp->vertices);
|
||||
pxl8_free(bsp->visdata);
|
||||
pxl8_free(bsp->heightfield);
|
||||
|
||||
memset(bsp, 0, sizeof(*bsp));
|
||||
}
|
||||
|
||||
i32 demo3d_bsp_find_leaf(const demo3d_bsp* bsp, pxl8_vec3 pos) {
|
||||
if (!bsp || bsp->num_nodes == 0) return -1;
|
||||
|
||||
i32 node_id = 0;
|
||||
|
||||
while (node_id >= 0) {
|
||||
const demo3d_bsp_node* node = &bsp->nodes[node_id];
|
||||
const demo3d_bsp_plane* plane = &bsp->planes[node->plane_id];
|
||||
|
||||
f32 dist = pxl8_vec3_dot(pos, plane->normal) - plane->dist;
|
||||
node_id = node->children[dist < 0 ? 1 : 0];
|
||||
}
|
||||
|
||||
return -(node_id + 1);
|
||||
}
|
||||
|
||||
|
||||
bool demo3d_bsp_is_leaf_visible(const demo3d_bsp* bsp, i32 leaf_from, i32 leaf_to) {
|
||||
if (!bsp || !bsp->visdata || bsp->visdata_size == 0) return true;
|
||||
if (leaf_from < 0 || leaf_to < 0) return true;
|
||||
if ((u32)leaf_from >= bsp->num_leafs || (u32)leaf_to >= bsp->num_leafs) return true;
|
||||
|
||||
i32 visofs = bsp->leafs[leaf_from].visofs;
|
||||
if (visofs < 0) return true;
|
||||
|
||||
u32 row_size = (bsp->num_leafs + 7) >> 3;
|
||||
u32 byte_idx = (u32)leaf_to >> 3;
|
||||
u32 bit_idx = (u32)leaf_to & 7;
|
||||
|
||||
u8* vis = bsp->visdata + visofs;
|
||||
u8* vis_end = bsp->visdata + bsp->visdata_size;
|
||||
u32 out = 0;
|
||||
|
||||
while (out < row_size && vis < vis_end) {
|
||||
if (*vis) {
|
||||
if (out == byte_idx) {
|
||||
return (*vis & (1 << bit_idx)) != 0;
|
||||
}
|
||||
out++;
|
||||
vis++;
|
||||
} else {
|
||||
vis++;
|
||||
if (vis >= vis_end) break;
|
||||
u32 count = *vis++;
|
||||
if (out + count > byte_idx && byte_idx >= out) {
|
||||
return false;
|
||||
}
|
||||
out += count;
|
||||
}
|
||||
}
|
||||
|
||||
return out > byte_idx ? false : true;
|
||||
}
|
||||
|
||||
demo3d_bsp_pvs demo3d_bsp_decompress_pvs(const demo3d_bsp* bsp, i32 leaf) {
|
||||
demo3d_bsp_pvs pvs = {0};
|
||||
|
||||
u32 row = (bsp->num_leafs + 7) >> 3;
|
||||
pvs.data = pxl8_malloc(row);
|
||||
pvs.size = row;
|
||||
|
||||
if (!pvs.data) return pvs;
|
||||
|
||||
if (!bsp || leaf < 0 || (u32)leaf >= bsp->num_leafs) {
|
||||
memset(pvs.data, 0xFF, row);
|
||||
return pvs;
|
||||
}
|
||||
|
||||
i32 visofs = bsp->leafs[leaf].visofs;
|
||||
if (visofs < 0 || !bsp->visdata || bsp->visdata_size == 0) {
|
||||
memset(pvs.data, 0xFF, row);
|
||||
return pvs;
|
||||
}
|
||||
|
||||
u8* in = bsp->visdata + visofs;
|
||||
u8* out = pvs.data;
|
||||
u8* out_end = pvs.data + row;
|
||||
|
||||
do {
|
||||
if (*in) {
|
||||
*out++ = *in++;
|
||||
} else {
|
||||
in++;
|
||||
i32 c = *in++;
|
||||
while (c > 0 && out < out_end) {
|
||||
*out++ = 0;
|
||||
c--;
|
||||
}
|
||||
}
|
||||
} while (out < out_end);
|
||||
|
||||
return pvs;
|
||||
}
|
||||
|
||||
void demo3d_bsp_pvs_destroy(demo3d_bsp_pvs* pvs) {
|
||||
if (pvs) {
|
||||
pxl8_free(pvs->data);
|
||||
pvs->data = NULL;
|
||||
pvs->size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool demo3d_bsp_pvs_is_visible(const demo3d_bsp_pvs* pvs, i32 leaf) {
|
||||
if (!pvs || !pvs->data || leaf < 0) return true;
|
||||
u32 byte_idx = (u32)leaf >> 3;
|
||||
u32 bit_idx = (u32)leaf & 7;
|
||||
if (byte_idx >= pvs->size) return true;
|
||||
return (pvs->data[byte_idx] & (1 << bit_idx)) != 0;
|
||||
}
|
||||
|
||||
demo3d_bsp_lightmap demo3d_bsp_lightmap_uniform(u8 r, u8 g, u8 b) {
|
||||
return (demo3d_bsp_lightmap){
|
||||
.color = {r, g, b},
|
||||
.height = 0,
|
||||
.offset = 0,
|
||||
.width = 0,
|
||||
};
|
||||
}
|
||||
|
||||
demo3d_bsp_lightmap demo3d_bsp_lightmap_mapped(u8 width, u8 height, u32 offset) {
|
||||
return (demo3d_bsp_lightmap){
|
||||
.color = {0, 0, 0},
|
||||
.height = height,
|
||||
.offset = offset,
|
||||
.width = width,
|
||||
};
|
||||
}
|
||||
|
||||
u8 demo3d_bsp_light_at(const demo3d_bsp* bsp, f32 x, f32 y, f32 z, u8 ambient) {
|
||||
if (!bsp || !bsp->vertices || !bsp->vertex_lights) return 255;
|
||||
|
||||
f32 best_dist = FLT_MAX;
|
||||
u32 best_idx = 0;
|
||||
|
||||
for (u32 i = 0; i < bsp->num_vertices; i++) {
|
||||
f32 dx = bsp->vertices[i].position.x - x;
|
||||
f32 dy = bsp->vertices[i].position.y - y;
|
||||
f32 dz = bsp->vertices[i].position.z - z;
|
||||
f32 dist = dx * dx + dy * dy + dz * dz;
|
||||
if (dist < best_dist) {
|
||||
best_dist = dist;
|
||||
best_idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (best_idx >= bsp->num_vertex_lights) return 255;
|
||||
|
||||
u32 packed = bsp->vertex_lights[best_idx];
|
||||
u8 direct = (packed >> 24) & 0xFF;
|
||||
u8 ao = (packed >> 16) & 0xFF;
|
||||
f32 combined = (f32)direct + ((f32)ambient / 255.0f) * (f32)ao;
|
||||
return (u8)(combined > 255.0f ? 255.0f : combined);
|
||||
}
|
||||
|
||||
demo3d_bsp_lightmap_sample demo3d_bsp_sample_lightmap(const demo3d_bsp* bsp, u32 face_idx, f32 u, f32 v) {
|
||||
demo3d_bsp_lightmap_sample white = {255, 255, 255};
|
||||
|
||||
if (!bsp || !bsp->lightmaps || face_idx >= bsp->num_lightmaps) {
|
||||
return white;
|
||||
}
|
||||
|
||||
const demo3d_bsp_lightmap* lm = &bsp->lightmaps[face_idx];
|
||||
|
||||
if (lm->width == 0) {
|
||||
return (demo3d_bsp_lightmap_sample){lm->color[2], lm->color[1], lm->color[0]};
|
||||
}
|
||||
|
||||
if (!bsp->lightdata || bsp->lightdata_size == 0) {
|
||||
return white;
|
||||
}
|
||||
|
||||
f32 w = (f32)lm->width;
|
||||
f32 h = (f32)lm->height;
|
||||
f32 fx = u * w;
|
||||
f32 fy = v * h;
|
||||
|
||||
if (fx < 0) fx = 0;
|
||||
if (fx > w - 1.001f) fx = w - 1.001f;
|
||||
if (fy < 0) fy = 0;
|
||||
if (fy > h - 1.001f) fy = h - 1.001f;
|
||||
|
||||
u32 x0 = (u32)fx;
|
||||
u32 y0 = (u32)fy;
|
||||
u32 x1 = x0 + 1;
|
||||
u32 y1 = y0 + 1;
|
||||
if (x1 >= lm->width) x1 = lm->width - 1;
|
||||
if (y1 >= lm->height) y1 = lm->height - 1;
|
||||
|
||||
f32 frac_x = fx - (f32)x0;
|
||||
f32 frac_y = fy - (f32)y0;
|
||||
|
||||
u32 stride = lm->width;
|
||||
u32 base = lm->offset;
|
||||
|
||||
u32 idx00 = base + y0 * stride + x0;
|
||||
u32 idx10 = base + y0 * stride + x1;
|
||||
u32 idx01 = base + y1 * stride + x0;
|
||||
u32 idx11 = base + y1 * stride + x1;
|
||||
|
||||
u8 r00, g00, b00, r10, g10, b10, r01, g01, b01, r11, g11, b11;
|
||||
|
||||
if (idx00 < bsp->lightdata_size) pxl8_rgb332_unpack(bsp->lightdata[idx00], &r00, &g00, &b00);
|
||||
else { r00 = g00 = b00 = 255; }
|
||||
if (idx10 < bsp->lightdata_size) pxl8_rgb332_unpack(bsp->lightdata[idx10], &r10, &g10, &b10);
|
||||
else { r10 = g10 = b10 = 255; }
|
||||
if (idx01 < bsp->lightdata_size) pxl8_rgb332_unpack(bsp->lightdata[idx01], &r01, &g01, &b01);
|
||||
else { r01 = g01 = b01 = 255; }
|
||||
if (idx11 < bsp->lightdata_size) pxl8_rgb332_unpack(bsp->lightdata[idx11], &r11, &g11, &b11);
|
||||
else { r11 = g11 = b11 = 255; }
|
||||
|
||||
f32 inv_x = 1.0f - frac_x;
|
||||
f32 inv_y = 1.0f - frac_y;
|
||||
|
||||
u8 r = (u8)(r00 * inv_x * inv_y + r10 * frac_x * inv_y + r01 * inv_x * frac_y + r11 * frac_x * frac_y);
|
||||
u8 g = (u8)(g00 * inv_x * inv_y + g10 * frac_x * inv_y + g01 * inv_x * frac_y + g11 * frac_x * frac_y);
|
||||
u8 b = (u8)(b00 * inv_x * inv_y + b10 * frac_x * inv_y + b01 * inv_x * frac_y + b11 * frac_x * frac_y);
|
||||
|
||||
return (demo3d_bsp_lightmap_sample){b, g, r};
|
||||
}
|
||||
|
||||
u32 demo3d_bsp_face_count(const demo3d_bsp* bsp) {
|
||||
if (!bsp) return 0;
|
||||
return bsp->num_faces;
|
||||
}
|
||||
|
||||
pxl8_vec3 demo3d_bsp_face_normal(const demo3d_bsp* bsp, u32 face_id) {
|
||||
pxl8_vec3 up = {0, 1, 0};
|
||||
if (!bsp || face_id >= bsp->num_faces) return up;
|
||||
|
||||
const demo3d_bsp_face* face = &bsp->faces[face_id];
|
||||
if (face->plane_id >= bsp->num_planes) return up;
|
||||
|
||||
pxl8_vec3 normal = bsp->planes[face->plane_id].normal;
|
||||
if (face->side) {
|
||||
normal.x = -normal.x;
|
||||
normal.y = -normal.y;
|
||||
normal.z = -normal.z;
|
||||
}
|
||||
return normal;
|
||||
}
|
||||
|
||||
void demo3d_bsp_face_set_material(demo3d_bsp* bsp, u32 face_id, u16 material_id) {
|
||||
if (!bsp || face_id >= bsp->num_faces) return;
|
||||
bsp->faces[face_id].material_id = material_id;
|
||||
}
|
||||
162
demo3d/client/bsp/demo3d_bsp.h
Normal file
162
demo3d/client/bsp/demo3d_bsp.h
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
#pragma once
|
||||
|
||||
#include "pxl8_math.h"
|
||||
#include "pxl8_types.h"
|
||||
|
||||
typedef struct demo3d_bsp_edge {
|
||||
u16 vertex[2];
|
||||
} demo3d_bsp_edge;
|
||||
|
||||
typedef struct demo3d_bsp_face {
|
||||
u32 first_edge;
|
||||
u32 lightmap_offset;
|
||||
u16 num_edges;
|
||||
u16 plane_id;
|
||||
u16 side;
|
||||
|
||||
u8 styles[4];
|
||||
u16 material_id;
|
||||
|
||||
pxl8_vec3 aabb_min;
|
||||
pxl8_vec3 aabb_max;
|
||||
} demo3d_bsp_face;
|
||||
|
||||
typedef struct demo3d_bsp_leaf {
|
||||
u8 ambient_level[4];
|
||||
i32 contents;
|
||||
|
||||
u16 first_marksurface;
|
||||
i16 maxs[3];
|
||||
i16 mins[3];
|
||||
u16 num_marksurfaces;
|
||||
|
||||
i32 visofs;
|
||||
} demo3d_bsp_leaf;
|
||||
|
||||
typedef struct demo3d_bsp_model {
|
||||
i32 first_face;
|
||||
i32 headnode[4];
|
||||
f32 maxs[3];
|
||||
f32 mins[3];
|
||||
i32 num_faces;
|
||||
|
||||
pxl8_vec3 origin;
|
||||
i32 visleafs;
|
||||
} demo3d_bsp_model;
|
||||
|
||||
typedef struct demo3d_bsp_node {
|
||||
i32 children[2];
|
||||
|
||||
u16 first_face;
|
||||
i16 maxs[3];
|
||||
i16 mins[3];
|
||||
u16 num_faces;
|
||||
|
||||
u32 plane_id;
|
||||
} demo3d_bsp_node;
|
||||
|
||||
typedef struct demo3d_bsp_plane {
|
||||
f32 dist;
|
||||
pxl8_vec3 normal;
|
||||
i32 type;
|
||||
} demo3d_bsp_plane;
|
||||
|
||||
|
||||
typedef struct demo3d_bsp_vertex {
|
||||
pxl8_vec3 position;
|
||||
} demo3d_bsp_vertex;
|
||||
|
||||
typedef struct demo3d_bsp_lightmap {
|
||||
u8 color[3];
|
||||
u8 height;
|
||||
u32 offset;
|
||||
u8 width;
|
||||
} demo3d_bsp_lightmap;
|
||||
|
||||
typedef struct demo3d_bsp_lightmap_sample {
|
||||
u8 b;
|
||||
u8 g;
|
||||
u8 r;
|
||||
} demo3d_bsp_lightmap_sample;
|
||||
|
||||
typedef struct demo3d_bsp_pvs {
|
||||
u8* data;
|
||||
u32 size;
|
||||
} demo3d_bsp_pvs;
|
||||
|
||||
typedef struct demo3d_bsp_portal {
|
||||
f32 x0, z0;
|
||||
f32 x1, z1;
|
||||
u32 target_leaf;
|
||||
} demo3d_bsp_portal;
|
||||
|
||||
typedef struct demo3d_bsp_cell_portals {
|
||||
demo3d_bsp_portal portals[4];
|
||||
u8 num_portals;
|
||||
} demo3d_bsp_cell_portals;
|
||||
|
||||
typedef struct demo3d_bsp {
|
||||
demo3d_bsp_cell_portals* cell_portals;
|
||||
demo3d_bsp_edge* edges;
|
||||
demo3d_bsp_face* faces;
|
||||
demo3d_bsp_leaf* leafs;
|
||||
u8* lightdata;
|
||||
demo3d_bsp_lightmap* lightmaps;
|
||||
u16* marksurfaces;
|
||||
demo3d_bsp_model* models;
|
||||
demo3d_bsp_node* nodes;
|
||||
demo3d_bsp_plane* planes;
|
||||
i32* surfedges;
|
||||
u32* vertex_lights;
|
||||
demo3d_bsp_vertex* vertices;
|
||||
u8* visdata;
|
||||
f32* heightfield;
|
||||
|
||||
u32 lightdata_size;
|
||||
u32 num_cell_portals;
|
||||
u32 num_edges;
|
||||
u32 num_faces;
|
||||
u32 num_leafs;
|
||||
u32 num_lightmaps;
|
||||
u32 num_marksurfaces;
|
||||
u32 num_models;
|
||||
u32 num_nodes;
|
||||
u32 num_planes;
|
||||
u32 num_surfedges;
|
||||
u32 num_vertex_lights;
|
||||
u32 num_vertices;
|
||||
u32 num_heightfield;
|
||||
f32 heightfield_ox;
|
||||
f32 heightfield_oz;
|
||||
f32 heightfield_cell_size;
|
||||
u16 heightfield_w;
|
||||
u16 heightfield_h;
|
||||
u32 visdata_size;
|
||||
f32 bounds_min_x;
|
||||
f32 bounds_min_z;
|
||||
f32 bounds_max_x;
|
||||
f32 bounds_max_z;
|
||||
} demo3d_bsp;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
demo3d_bsp_pvs demo3d_bsp_decompress_pvs(const demo3d_bsp* bsp, i32 leaf);
|
||||
void demo3d_bsp_destroy(demo3d_bsp* bsp);
|
||||
u32 demo3d_bsp_face_count(const demo3d_bsp* bsp);
|
||||
pxl8_vec3 demo3d_bsp_face_normal(const demo3d_bsp* bsp, u32 face_id);
|
||||
void demo3d_bsp_face_set_material(demo3d_bsp* bsp, u32 face_id, u16 material_id);
|
||||
i32 demo3d_bsp_find_leaf(const demo3d_bsp* bsp, pxl8_vec3 pos);
|
||||
bool demo3d_bsp_is_leaf_visible(const demo3d_bsp* bsp, i32 leaf_from, i32 leaf_to);
|
||||
demo3d_bsp_lightmap demo3d_bsp_lightmap_mapped(u8 width, u8 height, u32 offset);
|
||||
demo3d_bsp_lightmap demo3d_bsp_lightmap_uniform(u8 r, u8 g, u8 b);
|
||||
pxl8_result demo3d_bsp_load(const char* path, demo3d_bsp* bsp);
|
||||
void demo3d_bsp_pvs_destroy(demo3d_bsp_pvs* pvs);
|
||||
bool demo3d_bsp_pvs_is_visible(const demo3d_bsp_pvs* pvs, i32 leaf);
|
||||
u8 demo3d_bsp_light_at(const demo3d_bsp* bsp, f32 x, f32 y, f32 z, u8 ambient);
|
||||
demo3d_bsp_lightmap_sample demo3d_bsp_sample_lightmap(const demo3d_bsp* bsp, u32 face_idx, f32 u, f32 v);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
213
demo3d/client/bsp/demo3d_bsp_render.c
Normal file
213
demo3d/client/bsp/demo3d_bsp_render.c
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
#include "demo3d_bsp_render.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "pxl8_gfx3d.h"
|
||||
#include "pxl8_log.h"
|
||||
#include "pxl8_mem.h"
|
||||
#include "pxl8_mesh.h"
|
||||
|
||||
static inline bool demo3d_bsp_get_edge_vertex(const demo3d_bsp* bsp, i32 surfedge_idx, u32* out_vert_idx) {
|
||||
if (surfedge_idx >= (i32)bsp->num_surfedges) return false;
|
||||
|
||||
i32 edge_idx = bsp->surfedges[surfedge_idx];
|
||||
u32 vertex_index;
|
||||
|
||||
if (edge_idx >= 0) {
|
||||
if ((u32)edge_idx >= bsp->num_edges) return false;
|
||||
vertex_index = 0;
|
||||
} else {
|
||||
edge_idx = -edge_idx;
|
||||
if ((u32)edge_idx >= bsp->num_edges) return false;
|
||||
vertex_index = 1;
|
||||
}
|
||||
|
||||
*out_vert_idx = bsp->edges[edge_idx].vertex[vertex_index];
|
||||
return *out_vert_idx < bsp->num_vertices;
|
||||
}
|
||||
|
||||
static inline bool face_in_frustum(const demo3d_bsp* bsp, u32 face_id, const pxl8_frustum* frustum) {
|
||||
const demo3d_bsp_face* face = &bsp->faces[face_id];
|
||||
return pxl8_frustum_test_aabb(frustum, face->aabb_min, face->aabb_max);
|
||||
}
|
||||
|
||||
static void collect_face_to_mesh(const demo3d_bsp* bsp, const demo3d_bsp_render_state* state,
|
||||
u32 face_id, pxl8_mesh* mesh, u8 ambient) {
|
||||
const demo3d_bsp_face* face = &bsp->faces[face_id];
|
||||
if (face->num_edges < 3) return;
|
||||
|
||||
pxl8_vec3 normal = {0, 1, 0};
|
||||
if (face->plane_id < bsp->num_planes) {
|
||||
normal = bsp->planes[face->plane_id].normal;
|
||||
if (face->side) {
|
||||
normal.x = -normal.x;
|
||||
normal.y = -normal.y;
|
||||
normal.z = -normal.z;
|
||||
}
|
||||
}
|
||||
|
||||
const pxl8_gfx_material* material = NULL;
|
||||
f32 tex_scale = 64.0f;
|
||||
if (state && face->material_id < state->num_materials) {
|
||||
material = &state->materials[face->material_id];
|
||||
}
|
||||
|
||||
pxl8_vec3 u_axis = {1.0f, 0.0f, 0.0f};
|
||||
pxl8_vec3 v_axis = {0.0f, 0.0f, 1.0f};
|
||||
f32 u_offset = 0.0f, v_offset = 0.0f;
|
||||
if (material) {
|
||||
u_offset = material->u_offset;
|
||||
v_offset = material->v_offset;
|
||||
}
|
||||
f32 abs_ny = normal.y < 0 ? -normal.y : normal.y;
|
||||
if (abs_ny > 0.7f) {
|
||||
u_axis = (pxl8_vec3){1.0f, 0.0f, 0.0f};
|
||||
v_axis = (pxl8_vec3){0.0f, 0.0f, 1.0f};
|
||||
} else {
|
||||
f32 abs_nx = normal.x < 0 ? -normal.x : normal.x;
|
||||
f32 abs_nz = normal.z < 0 ? -normal.z : normal.z;
|
||||
if (abs_nx > abs_nz) {
|
||||
u_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};
|
||||
}
|
||||
|
||||
u16 base_idx = (u16)mesh->vertex_count;
|
||||
u32 num_verts = 0;
|
||||
|
||||
for (u32 i = 0; i < face->num_edges && num_verts < 64; i++) {
|
||||
u32 edge_i = face->side ? (face->num_edges - 1 - i) : i;
|
||||
i32 surfedge_idx = face->first_edge + edge_i;
|
||||
u32 vert_idx;
|
||||
|
||||
if (!demo3d_bsp_get_edge_vertex(bsp, surfedge_idx, &vert_idx)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
pxl8_vec3 pos = bsp->vertices[vert_idx].position;
|
||||
|
||||
f32 u = (pxl8_vec3_dot(pos, u_axis) + u_offset) / tex_scale;
|
||||
f32 v = (pxl8_vec3_dot(pos, v_axis) + v_offset) / tex_scale;
|
||||
|
||||
u8 light = 255;
|
||||
if (bsp->vertex_lights && vert_idx < bsp->num_vertex_lights) {
|
||||
u32 packed = bsp->vertex_lights[vert_idx];
|
||||
u8 direct = (packed >> 24) & 0xFF;
|
||||
u8 ao = (packed >> 16) & 0xFF;
|
||||
f32 combined = (f32)direct + ((f32)ambient / 255.0f) * (f32)ao;
|
||||
light = (u8)(combined > 255.0f ? 255.0f : combined);
|
||||
}
|
||||
|
||||
pxl8_vertex vtx = {
|
||||
.position = pos,
|
||||
.normal = normal,
|
||||
.u = u,
|
||||
.v = v,
|
||||
.color = 15,
|
||||
.light = light,
|
||||
};
|
||||
pxl8_mesh_push_vertex(mesh, vtx);
|
||||
num_verts++;
|
||||
}
|
||||
|
||||
if (num_verts < 3) return;
|
||||
|
||||
for (u32 i = 1; i < num_verts - 1; i++) {
|
||||
pxl8_mesh_push_triangle(mesh, base_idx, base_idx + i, base_idx + i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
demo3d_bsp_render_state* demo3d_bsp_render_state_create(u32 num_faces) {
|
||||
demo3d_bsp_render_state* state = pxl8_calloc(1, sizeof(demo3d_bsp_render_state));
|
||||
if (!state) return NULL;
|
||||
|
||||
state->num_faces = num_faces;
|
||||
if (num_faces > 0) {
|
||||
state->render_face_flags = pxl8_calloc(num_faces, 1);
|
||||
if (!state->render_face_flags) {
|
||||
pxl8_free(state);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
void demo3d_bsp_render_state_destroy(demo3d_bsp_render_state* state) {
|
||||
if (!state) return;
|
||||
pxl8_free(state->materials);
|
||||
pxl8_free(state->render_face_flags);
|
||||
pxl8_free(state);
|
||||
}
|
||||
|
||||
void demo3d_bsp_render(pxl8_gfx* gfx, const demo3d_bsp* bsp,
|
||||
demo3d_bsp_render_state* state,
|
||||
const pxl8_gfx_draw_opts* opts) {
|
||||
if (!gfx || !bsp || !state || bsp->num_faces == 0) return;
|
||||
if (!state->materials || state->num_materials == 0) return;
|
||||
if (!state->render_face_flags) return;
|
||||
|
||||
const pxl8_frustum* frustum = pxl8_3d_get_frustum(gfx);
|
||||
if (!frustum) return;
|
||||
|
||||
pxl8_mesh* mesh = pxl8_mesh_create(8192, 16384);
|
||||
if (!mesh) return;
|
||||
|
||||
u8 ambient = pxl8_gfx_get_ambient(gfx);
|
||||
pxl8_mat4 identity = pxl8_mat4_identity();
|
||||
|
||||
for (u32 mat = 0; mat < state->num_materials; mat++) {
|
||||
for (u32 face_id = 0; face_id < bsp->num_faces; face_id++) {
|
||||
if (!state->render_face_flags[face_id]) continue;
|
||||
const demo3d_bsp_face* face = &bsp->faces[face_id];
|
||||
if (face->material_id != mat) continue;
|
||||
if (!face_in_frustum(bsp, face_id, frustum)) continue;
|
||||
collect_face_to_mesh(bsp, state, face_id, mesh, ambient);
|
||||
}
|
||||
if (mesh->index_count > 0) {
|
||||
pxl8_gfx_material mat_copy = state->materials[mat];
|
||||
if (state->exterior) mat_copy.double_sided = true;
|
||||
pxl8_3d_draw_mesh(gfx, mesh, &identity, &mat_copy, opts);
|
||||
pxl8_mesh_clear(mesh);
|
||||
}
|
||||
}
|
||||
|
||||
pxl8_mesh_destroy(mesh);
|
||||
}
|
||||
|
||||
void demo3d_bsp_set_material(demo3d_bsp_render_state* state, u16 material_id, const pxl8_gfx_material* material) {
|
||||
if (!state || !material) return;
|
||||
|
||||
if (material_id >= state->num_materials) {
|
||||
u32 new_count = material_id + 1;
|
||||
pxl8_gfx_material* new_materials = pxl8_realloc(state->materials, new_count * sizeof(pxl8_gfx_material));
|
||||
if (!new_materials) return;
|
||||
|
||||
for (u32 i = state->num_materials; i < new_count; i++) {
|
||||
memset(&new_materials[i], 0, sizeof(pxl8_gfx_material));
|
||||
new_materials[i].u_axis = (pxl8_vec3){1.0f, 0.0f, 0.0f};
|
||||
if (i == 0 || i == 2) {
|
||||
new_materials[i].v_axis = (pxl8_vec3){0.0f, 0.0f, 1.0f};
|
||||
} else {
|
||||
new_materials[i].v_axis = (pxl8_vec3){0.0f, 1.0f, 0.0f};
|
||||
}
|
||||
}
|
||||
|
||||
state->materials = new_materials;
|
||||
state->num_materials = new_count;
|
||||
}
|
||||
|
||||
pxl8_vec3 u_axis = state->materials[material_id].u_axis;
|
||||
pxl8_vec3 v_axis = state->materials[material_id].v_axis;
|
||||
f32 u_offset = state->materials[material_id].u_offset;
|
||||
f32 v_offset = state->materials[material_id].v_offset;
|
||||
|
||||
state->materials[material_id] = *material;
|
||||
|
||||
state->materials[material_id].u_axis = u_axis;
|
||||
state->materials[material_id].v_axis = v_axis;
|
||||
state->materials[material_id].u_offset = u_offset;
|
||||
state->materials[material_id].v_offset = v_offset;
|
||||
}
|
||||
29
demo3d/client/bsp/demo3d_bsp_render.h
Normal file
29
demo3d/client/bsp/demo3d_bsp_render.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include "demo3d_bsp.h"
|
||||
#include "pxl8_gfx.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct demo3d_bsp_render_state {
|
||||
pxl8_gfx_material* materials;
|
||||
u8* render_face_flags;
|
||||
u32 num_materials;
|
||||
u32 num_faces;
|
||||
bool exterior;
|
||||
} demo3d_bsp_render_state;
|
||||
|
||||
demo3d_bsp_render_state* demo3d_bsp_render_state_create(u32 num_faces);
|
||||
void demo3d_bsp_render_state_destroy(demo3d_bsp_render_state* state);
|
||||
|
||||
void demo3d_bsp_render(pxl8_gfx* gfx, const demo3d_bsp* bsp,
|
||||
demo3d_bsp_render_state* state,
|
||||
const pxl8_gfx_draw_opts* opts);
|
||||
void demo3d_bsp_set_material(demo3d_bsp_render_state* state, u16 material_id,
|
||||
const pxl8_gfx_material* material);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
218
demo3d/client/demo3d.c
Normal file
218
demo3d/client/demo3d.c
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
#include "pxl8_sys.h"
|
||||
#include "pxl8_log.h"
|
||||
#include "pxl8_mem.h"
|
||||
#include "pxl8_script.h"
|
||||
|
||||
#include "demo3d_net.h"
|
||||
#include "demo3d_world.h"
|
||||
|
||||
static const char* demo3d_ffi_cdefs =
|
||||
"typedef struct demo3d_bsp demo3d_bsp;\n"
|
||||
"\n"
|
||||
"u32 demo3d_bsp_face_count(const demo3d_bsp* bsp);\n"
|
||||
"pxl8_vec3 demo3d_bsp_face_normal(const demo3d_bsp* bsp, u32 face_id);\n"
|
||||
"void demo3d_bsp_face_set_material(demo3d_bsp* bsp, u32 face_id, u16 material_id);\n"
|
||||
"u8 demo3d_bsp_light_at(const demo3d_bsp* bsp, f32 x, f32 y, f32 z, u8 ambient);\n"
|
||||
"\n"
|
||||
"typedef struct demo3d_chunk {\n"
|
||||
" u32 id;\n"
|
||||
" u32 version;\n"
|
||||
" demo3d_bsp* bsp;\n"
|
||||
"} demo3d_chunk;\n"
|
||||
"\n"
|
||||
"typedef struct demo3d_world demo3d_world;\n"
|
||||
"\n"
|
||||
"typedef struct pxl8_ray {\n"
|
||||
" pxl8_vec3 normal;\n"
|
||||
" pxl8_vec3 point;\n"
|
||||
" float fraction;\n"
|
||||
" bool hit;\n"
|
||||
"} pxl8_ray;\n"
|
||||
"\n"
|
||||
"demo3d_world* demo3d_get_world(pxl8* sys);\n"
|
||||
"demo3d_chunk* demo3d_world_active_chunk(demo3d_world* world);\n"
|
||||
"bool demo3d_world_point_solid(const demo3d_world* world, float x, float y, float z);\n"
|
||||
"pxl8_ray demo3d_world_ray(const demo3d_world* world, pxl8_vec3 from, pxl8_vec3 to);\n"
|
||||
"pxl8_ray demo3d_world_sweep(const demo3d_world* world, pxl8_vec3 from, pxl8_vec3 to, float radius);\n"
|
||||
"void demo3d_world_render(demo3d_world* world, pxl8_gfx* gfx, pxl8_vec3 camera_pos);\n"
|
||||
"void demo3d_world_set_bsp_material(demo3d_world* world, u16 material_id, const pxl8_gfx_material* material);\n"
|
||||
"\n"
|
||||
"typedef struct demo3d_sim_entity {\n"
|
||||
" pxl8_vec3 pos;\n"
|
||||
" pxl8_vec3 vel;\n"
|
||||
" f32 yaw;\n"
|
||||
" f32 pitch;\n"
|
||||
" u32 flags;\n"
|
||||
" u16 kind;\n"
|
||||
" u16 _pad;\n"
|
||||
"} demo3d_sim_entity;\n"
|
||||
"\n"
|
||||
"void demo3d_world_init_local_player(demo3d_world* world, f32 x, f32 y, f32 z);\n"
|
||||
"void demo3d_world_set_look(demo3d_world* world, f32 yaw, f32 pitch);\n"
|
||||
"demo3d_sim_entity* demo3d_world_local_player(demo3d_world* world);\n"
|
||||
"\n"
|
||||
"typedef struct demo3d_input_msg {\n"
|
||||
" u32 buttons;\n"
|
||||
" f32 look_dx;\n"
|
||||
" f32 look_dy;\n"
|
||||
" f32 move_x;\n"
|
||||
" f32 move_y;\n"
|
||||
" f32 yaw;\n"
|
||||
" u64 tick;\n"
|
||||
" u64 timestamp;\n"
|
||||
"} demo3d_input_msg;\n"
|
||||
"\n"
|
||||
"typedef struct demo3d_sim_config {\n"
|
||||
" f32 move_speed;\n"
|
||||
" f32 ground_accel;\n"
|
||||
" f32 air_accel;\n"
|
||||
" f32 stop_speed;\n"
|
||||
" f32 friction;\n"
|
||||
" f32 gravity;\n"
|
||||
" f32 jump_velocity;\n"
|
||||
" f32 player_radius;\n"
|
||||
" f32 player_height;\n"
|
||||
" f32 max_pitch;\n"
|
||||
"} demo3d_sim_config;\n"
|
||||
"\n"
|
||||
"typedef struct demo3d_sim_world {\n"
|
||||
" const demo3d_bsp* chunks[9];\n"
|
||||
" i32 center_cx;\n"
|
||||
" i32 center_cz;\n"
|
||||
" f32 chunk_size;\n"
|
||||
"} demo3d_sim_world;\n"
|
||||
"\n"
|
||||
"void demo3d_sim_move_player(demo3d_sim_entity* ent, const demo3d_input_msg* input, const demo3d_sim_world* world, const demo3d_sim_config* cfg, f32 dt);\n"
|
||||
"void demo3d_sim_integrate(demo3d_sim_entity* ent, const demo3d_sim_world* world, const demo3d_sim_config* cfg, f32 dt);\n"
|
||||
"pxl8_vec3 demo3d_sim_trace(const demo3d_sim_world* world, pxl8_vec3 from, pxl8_vec3 to, f32 radius, f32 height);\n"
|
||||
"bool demo3d_sim_check_ground(const demo3d_sim_world* world, pxl8_vec3 pos, f32 radius);\n"
|
||||
"\n"
|
||||
"demo3d_sim_world demo3d_world_sim_world(const demo3d_world* world, pxl8_vec3 pos);\n"
|
||||
"void demo3d_world_set_sim_config(demo3d_world* world, const demo3d_sim_config* config);\n"
|
||||
"void demo3d_world_push_input(demo3d_world* world, const demo3d_input_msg* input);\n"
|
||||
"\n"
|
||||
"typedef struct demo3d_net demo3d_net;\n"
|
||||
"\n"
|
||||
"typedef struct demo3d_entity_state {\n"
|
||||
" u64 entity_id;\n"
|
||||
" u8 userdata[56];\n"
|
||||
"} demo3d_entity_state;\n"
|
||||
"\n"
|
||||
"typedef struct demo3d_snapshot_header {\n"
|
||||
" u16 entity_count;\n"
|
||||
" u16 event_count;\n"
|
||||
" u64 player_id;\n"
|
||||
" u64 tick;\n"
|
||||
" f32 time;\n"
|
||||
"} demo3d_snapshot_header;\n"
|
||||
"\n"
|
||||
"demo3d_net* demo3d_get_net(pxl8* sys);\n"
|
||||
"bool demo3d_net_connected(const demo3d_net* ng);\n"
|
||||
"i32 demo3d_net_spawn(demo3d_net* ng, f32 x, f32 y, f32 z, f32 yaw, f32 pitch);\n"
|
||||
"bool demo3d_net_has_chunk(const demo3d_net* ng);\n"
|
||||
"i32 demo3d_net_chunk_cx(const demo3d_net* ng);\n"
|
||||
"i32 demo3d_net_chunk_cz(const demo3d_net* ng);\n"
|
||||
"const demo3d_entity_state* demo3d_net_entities(const demo3d_net* ng);\n"
|
||||
"u64 demo3d_net_player_id(const demo3d_net* ng);\n"
|
||||
"const u8* demo3d_net_entity_userdata(const demo3d_net* ng, u64 entity_id);\n"
|
||||
"const u8* demo3d_net_entity_prev_userdata(const demo3d_net* ng, u64 entity_id);\n"
|
||||
"f32 demo3d_net_lerp_alpha(const demo3d_net* ng);\n"
|
||||
"const demo3d_snapshot_header* demo3d_net_snapshot(const demo3d_net* ng);\n"
|
||||
;
|
||||
|
||||
typedef struct demo3d_state {
|
||||
demo3d_net* net;
|
||||
demo3d_world* world;
|
||||
} demo3d_state;
|
||||
|
||||
static pxl8_result demo3d_init(pxl8_game* game, void* userdata) {
|
||||
(void)userdata;
|
||||
demo3d_state* state = pxl8_calloc(1, sizeof(demo3d_state));
|
||||
if (!state) return PXL8_ERROR_INITIALIZATION_FAILED;
|
||||
game->userdata = state;
|
||||
|
||||
pxl8_script_add_ffi(game->script, demo3d_ffi_cdefs);
|
||||
|
||||
state->world = demo3d_world_create();
|
||||
if (!state->world) {
|
||||
pxl8_error("failed to create world");
|
||||
return PXL8_ERROR_INITIALIZATION_FAILED;
|
||||
}
|
||||
|
||||
pxl8_net_config net_cfg = { .address = "127.0.0.1", .port = 7777 };
|
||||
state->net = demo3d_net_create(&net_cfg);
|
||||
if (state->net) {
|
||||
demo3d_net_set_chunk_cache(state->net, demo3d_world_get_chunk_cache(state->world));
|
||||
demo3d_net_set_world(state->net, state->world);
|
||||
demo3d_net_connect(state->net);
|
||||
}
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
if (state->net) {
|
||||
demo3d_net_start_thread(state->net);
|
||||
}
|
||||
if (state->world) {
|
||||
demo3d_world_start_sim_thread(state->world, state->net);
|
||||
}
|
||||
#endif
|
||||
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static void demo3d_update(pxl8_game* game, f32 dt, void* userdata) {
|
||||
(void)userdata;
|
||||
demo3d_state* state = (demo3d_state*)game->userdata;
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
demo3d_net_update(state->net, dt);
|
||||
#else
|
||||
if (state->net) {
|
||||
while (demo3d_net_poll(state->net)) {}
|
||||
demo3d_net_update(state->net, dt);
|
||||
demo3d_world_sync(state->world, state->net);
|
||||
}
|
||||
demo3d_world_update(state->world, dt);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void demo3d_quit(pxl8_game* game, void* userdata) {
|
||||
(void)userdata;
|
||||
demo3d_state* state = (demo3d_state*)game->userdata;
|
||||
if (!state) return;
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
if (state->world) {
|
||||
demo3d_world_stop_sim_thread(state->world);
|
||||
}
|
||||
if (state->net) {
|
||||
demo3d_net_stop_thread(state->net);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (state->net) demo3d_net_destroy(state->net);
|
||||
if (state->world) demo3d_world_destroy(state->world);
|
||||
|
||||
pxl8_free(state);
|
||||
game->userdata = NULL;
|
||||
}
|
||||
|
||||
demo3d_net* demo3d_get_net(pxl8* sys) {
|
||||
pxl8_game* game = pxl8_get_game(sys);
|
||||
if (!game || !game->userdata) return NULL;
|
||||
return ((demo3d_state*)game->userdata)->net;
|
||||
}
|
||||
|
||||
demo3d_world* demo3d_get_world(pxl8* sys) {
|
||||
pxl8_game* game = pxl8_get_game(sys);
|
||||
if (!game || !game->userdata) return NULL;
|
||||
return ((demo3d_state*)game->userdata)->world;
|
||||
}
|
||||
|
||||
void pxl8_register_game(pxl8* sys) {
|
||||
pxl8_game_hooks hooks = {
|
||||
.init = demo3d_init,
|
||||
.update = demo3d_update,
|
||||
.quit = demo3d_quit,
|
||||
};
|
||||
pxl8_set_game_hooks(sys, hooks);
|
||||
}
|
||||
297
demo3d/client/net/demo3d_net.c
Normal file
297
demo3d/client/net/demo3d_net.c
Normal file
|
|
@ -0,0 +1,297 @@
|
|||
#include "pxl8_platform.h"
|
||||
#include "demo3d_net.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "pxl8_bytes.h"
|
||||
#include "pxl8_log.h"
|
||||
#include "pxl8_mem.h"
|
||||
#include "demo3d_world.h"
|
||||
#include "demo3d_chunk_cache.h"
|
||||
|
||||
static const demo3d_entity_state* find_entity(const demo3d_entity_state* entities, u16 count, u64 id) {
|
||||
for (u16 i = 0; i < count; i++) {
|
||||
if (entities[i].entity_id == id) return &entities[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool dispatch_message(demo3d_net* ng, const u8* data, usize len) {
|
||||
if (len < sizeof(demo3d_msg_header)) return false;
|
||||
|
||||
demo3d_msg_header hdr;
|
||||
usize offset = demo3d_protocol_deserialize_header(data, len, &hdr);
|
||||
|
||||
if (hdr.type == DEMO3D_MSG_CHUNK) {
|
||||
if (!ng->chunk_cache) return false;
|
||||
demo3d_chunk_msg_header chunk_hdr;
|
||||
offset += demo3d_protocol_deserialize_chunk_msg_header(data + offset, len - offset, &chunk_hdr);
|
||||
const u8* payload = data + offset;
|
||||
usize payload_len = chunk_hdr.payload_size;
|
||||
if (payload_len > len - offset) payload_len = len - offset;
|
||||
demo3d_chunk_cache_receive(ng->chunk_cache, &chunk_hdr, payload, payload_len);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (hdr.type == DEMO3D_MSG_CHUNK_ENTER) {
|
||||
demo3d_chunk_enter_msg chunk_msg;
|
||||
demo3d_protocol_deserialize_chunk_enter(data + offset, len - offset, &chunk_msg);
|
||||
ng->chunk_cx = chunk_msg.cx;
|
||||
ng->chunk_cz = chunk_msg.cz;
|
||||
ng->has_chunk = true;
|
||||
pxl8_debug("Received CHUNK_ENTER cx=%d cz=%d", chunk_msg.cx, chunk_msg.cz);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (hdr.type == DEMO3D_MSG_CHUNK_EXIT) {
|
||||
ng->has_chunk = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (hdr.type != DEMO3D_MSG_SNAPSHOT) return false;
|
||||
|
||||
demo3d_snapshot_header snap;
|
||||
offset += demo3d_protocol_deserialize_snapshot_header(data + offset, len - offset, &snap);
|
||||
if (snap.tick <= ng->highest_tick) return false;
|
||||
|
||||
memcpy(ng->prev_entities, ng->entities, sizeof(ng->entities));
|
||||
ng->prev_snapshot = ng->snapshot;
|
||||
ng->highest_tick = snap.tick;
|
||||
ng->snapshot = snap;
|
||||
ng->interp_time = 0.0f;
|
||||
|
||||
u16 count = snap.entity_count;
|
||||
if (count > DEMO3D_MAX_SNAPSHOT_ENTITIES) count = DEMO3D_MAX_SNAPSHOT_ENTITIES;
|
||||
|
||||
for (u16 i = 0; i < count; i++) {
|
||||
offset += demo3d_protocol_deserialize_entity_state(data + offset, len - offset, &ng->entities[i]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
demo3d_net* demo3d_net_create(const pxl8_net_config* config) {
|
||||
demo3d_net* ng = pxl8_calloc(1, sizeof(demo3d_net));
|
||||
if (!ng) return NULL;
|
||||
ng->transport = pxl8_net_create(config);
|
||||
if (!ng->transport) {
|
||||
pxl8_free(ng);
|
||||
return NULL;
|
||||
}
|
||||
return ng;
|
||||
}
|
||||
|
||||
void demo3d_net_destroy(demo3d_net* ng) {
|
||||
if (!ng) return;
|
||||
pxl8_net_destroy(ng->transport);
|
||||
pxl8_free(ng);
|
||||
}
|
||||
|
||||
pxl8_result demo3d_net_connect(demo3d_net* ng) {
|
||||
if (!ng) return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
return pxl8_net_connect(ng->transport);
|
||||
}
|
||||
|
||||
bool demo3d_net_connected(const demo3d_net* ng) {
|
||||
return ng && pxl8_net_connected(ng->transport);
|
||||
}
|
||||
|
||||
void demo3d_net_disconnect(demo3d_net* ng) {
|
||||
if (!ng) return;
|
||||
pxl8_net_disconnect(ng->transport);
|
||||
}
|
||||
|
||||
const demo3d_entity_state* demo3d_net_entities(const demo3d_net* ng) {
|
||||
if (!ng) return NULL;
|
||||
return ng->entities;
|
||||
}
|
||||
|
||||
const u8* demo3d_net_entity_prev_userdata(const demo3d_net* ng, u64 entity_id) {
|
||||
if (!ng) return NULL;
|
||||
const demo3d_entity_state* e = find_entity(ng->prev_entities, ng->prev_snapshot.entity_count, entity_id);
|
||||
return e ? e->userdata : NULL;
|
||||
}
|
||||
|
||||
const u8* demo3d_net_entity_userdata(const demo3d_net* ng, u64 entity_id) {
|
||||
if (!ng) return NULL;
|
||||
const demo3d_entity_state* e = find_entity(ng->entities, ng->snapshot.entity_count, entity_id);
|
||||
return e ? e->userdata : NULL;
|
||||
}
|
||||
|
||||
const demo3d_input_msg* demo3d_net_input_at(const demo3d_net* ng, u64 tick) {
|
||||
if (!ng) return NULL;
|
||||
if (tick < ng->input_oldest_tick) return NULL;
|
||||
u64 age = tick - ng->input_oldest_tick;
|
||||
if (age >= DEMO3D_NET_INPUT_HISTORY_SIZE) return NULL;
|
||||
u64 idx = tick % DEMO3D_NET_INPUT_HISTORY_SIZE;
|
||||
const demo3d_input_msg* msg = &ng->input_history[idx];
|
||||
if (msg->tick != tick) return NULL;
|
||||
return msg;
|
||||
}
|
||||
|
||||
u64 demo3d_net_input_oldest_tick(const demo3d_net* ng) {
|
||||
if (!ng) return 0;
|
||||
return ng->input_oldest_tick;
|
||||
}
|
||||
|
||||
void demo3d_net_input_push(demo3d_net* ng, const demo3d_input_msg* input) {
|
||||
if (!ng || !input) return;
|
||||
u64 idx = input->tick % DEMO3D_NET_INPUT_HISTORY_SIZE;
|
||||
ng->input_history[idx] = *input;
|
||||
ng->input_head = input->tick;
|
||||
if (input->tick >= DEMO3D_NET_INPUT_HISTORY_SIZE) {
|
||||
ng->input_oldest_tick = input->tick - DEMO3D_NET_INPUT_HISTORY_SIZE + 1;
|
||||
}
|
||||
}
|
||||
|
||||
f32 demo3d_net_lerp_alpha(const demo3d_net* ng) {
|
||||
if (!ng) return 1.0f;
|
||||
return ng->interp_time / (1.0f / DEMO3D_NET_TICK_RATE);
|
||||
}
|
||||
|
||||
bool demo3d_net_needs_correction(const demo3d_net* ng) {
|
||||
if (!ng) return false;
|
||||
if (ng->snapshot.tick == 0) return false;
|
||||
if (ng->predicted_tick == 0) return false;
|
||||
|
||||
u64 player_id = ng->snapshot.player_id;
|
||||
const demo3d_entity_state* server = find_entity(ng->entities, ng->snapshot.entity_count, player_id);
|
||||
if (!server) return false;
|
||||
|
||||
return memcmp(server->userdata, ng->predicted_state, DEMO3D_NET_USERDATA_SIZE) != 0;
|
||||
}
|
||||
|
||||
u64 demo3d_net_player_id(const demo3d_net* ng) {
|
||||
if (!ng) return 0;
|
||||
return ng->snapshot.player_id;
|
||||
}
|
||||
|
||||
bool demo3d_net_poll(demo3d_net* ng) {
|
||||
if (!ng || !pxl8_net_connected(ng->transport)) return false;
|
||||
|
||||
u8 recv_buf[4096];
|
||||
usize len = pxl8_net_recv(ng->transport, recv_buf, sizeof(recv_buf));
|
||||
u64 prev_tick = ng->highest_tick;
|
||||
if (!dispatch_message(ng, recv_buf, len)) return false;
|
||||
|
||||
if (ng->highest_tick > prev_tick && ng->world) {
|
||||
demo3d_world_reconcile(ng->world, ng, 1.0f / 30.0f);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
u8* demo3d_net_predicted_state(demo3d_net* ng) {
|
||||
if (!ng) return NULL;
|
||||
return ng->predicted_state;
|
||||
}
|
||||
|
||||
void demo3d_net_predicted_tick_set(demo3d_net* ng, u64 tick) {
|
||||
if (!ng) return;
|
||||
ng->predicted_tick = tick;
|
||||
}
|
||||
|
||||
pxl8_result demo3d_net_send_command(demo3d_net* ng, const demo3d_command_msg* cmd) {
|
||||
if (!ng || !cmd) return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
if (!pxl8_net_connected(ng->transport)) return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
|
||||
u8 buf[512];
|
||||
demo3d_msg_header hdr = {.type = DEMO3D_MSG_COMMAND, .version = DEMO3D_PROTOCOL_VERSION};
|
||||
usize offset = demo3d_protocol_serialize_header(&hdr, buf, sizeof(buf));
|
||||
offset += demo3d_protocol_serialize_command(cmd, buf + offset, sizeof(buf) - offset);
|
||||
|
||||
return pxl8_net_send(ng->transport, buf, offset);
|
||||
}
|
||||
|
||||
pxl8_result demo3d_net_send_input(demo3d_net* ng, const demo3d_input_msg* input) {
|
||||
if (!ng || !input) return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
if (!pxl8_net_connected(ng->transport)) return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
|
||||
demo3d_net_input_push(ng, input);
|
||||
|
||||
u8 buf[512];
|
||||
demo3d_msg_header hdr = {.type = DEMO3D_MSG_INPUT, .version = DEMO3D_PROTOCOL_VERSION};
|
||||
usize offset = demo3d_protocol_serialize_header(&hdr, buf, sizeof(buf));
|
||||
offset += demo3d_protocol_serialize_input(input, buf + offset, sizeof(buf) - offset);
|
||||
|
||||
return pxl8_net_send(ng->transport, buf, offset);
|
||||
}
|
||||
|
||||
const demo3d_snapshot_header* demo3d_net_snapshot(const demo3d_net* ng) {
|
||||
if (!ng) return NULL;
|
||||
return &ng->snapshot;
|
||||
}
|
||||
|
||||
u64 demo3d_net_tick(const demo3d_net* ng) {
|
||||
if (!ng) return 0;
|
||||
return ng->snapshot.tick;
|
||||
}
|
||||
|
||||
void demo3d_net_update(demo3d_net* ng, f32 dt) {
|
||||
if (!ng) return;
|
||||
ng->interp_time += dt;
|
||||
}
|
||||
|
||||
void demo3d_net_set_chunk_cache(demo3d_net* ng, demo3d_chunk_cache* cache) {
|
||||
if (!ng) return;
|
||||
ng->chunk_cache = cache;
|
||||
}
|
||||
|
||||
demo3d_chunk_cache* demo3d_net_chunk_cache(demo3d_net* ng) {
|
||||
if (!ng) return NULL;
|
||||
return ng->chunk_cache;
|
||||
}
|
||||
|
||||
void demo3d_net_set_world(demo3d_net* ng, demo3d_world* world) {
|
||||
if (!ng) return;
|
||||
ng->world = world;
|
||||
}
|
||||
|
||||
i32 demo3d_net_chunk_cx(const demo3d_net* ng) {
|
||||
if (!ng) return 0;
|
||||
return ng->chunk_cx;
|
||||
}
|
||||
|
||||
i32 demo3d_net_chunk_cz(const demo3d_net* ng) {
|
||||
if (!ng) return 0;
|
||||
return ng->chunk_cz;
|
||||
}
|
||||
|
||||
bool demo3d_net_has_chunk(const demo3d_net* ng) {
|
||||
if (!ng) return false;
|
||||
return ng->has_chunk;
|
||||
}
|
||||
|
||||
pxl8_result demo3d_net_spawn(demo3d_net* ng, f32 x, f32 y, f32 z, f32 yaw, f32 pitch) {
|
||||
if (!ng) return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
|
||||
demo3d_command_msg cmd = {0};
|
||||
cmd.cmd_type = DEMO3D_CMD_SPAWN_ENTITY;
|
||||
|
||||
u8* p = cmd.payload;
|
||||
memcpy(p, &x, 4); p += 4;
|
||||
memcpy(p, &y, 4); p += 4;
|
||||
memcpy(p, &z, 4); p += 4;
|
||||
memcpy(p, &yaw, 4); p += 4;
|
||||
memcpy(p, &pitch, 4);
|
||||
cmd.payload_size = 20;
|
||||
|
||||
return demo3d_net_send_command(ng, &cmd);
|
||||
}
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
void demo3d_net_start_thread(demo3d_net* ng) {
|
||||
if (!ng) return;
|
||||
pxl8_net_start_thread(ng->transport);
|
||||
}
|
||||
|
||||
void demo3d_net_stop_thread(demo3d_net* ng) {
|
||||
if (!ng) return;
|
||||
pxl8_net_stop_thread(ng->transport);
|
||||
}
|
||||
|
||||
bool demo3d_net_dispatch_packet(demo3d_net* ng, const pxl8_packet* pkt) {
|
||||
if (!ng || !pkt) return false;
|
||||
return dispatch_message(ng, pkt->data, pkt->len);
|
||||
}
|
||||
#endif
|
||||
74
demo3d/client/net/demo3d_net.h
Normal file
74
demo3d/client/net/demo3d_net.h
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#pragma once
|
||||
|
||||
#include "pxl8_net.h"
|
||||
#include "demo3d_protocol.h"
|
||||
#include "pxl8_types.h"
|
||||
|
||||
#define DEMO3D_NET_INPUT_HISTORY_SIZE 64
|
||||
#define DEMO3D_NET_USERDATA_SIZE 56
|
||||
#define DEMO3D_NET_TICK_RATE 30.0f
|
||||
|
||||
typedef struct demo3d_world demo3d_world;
|
||||
typedef struct demo3d_chunk_cache demo3d_chunk_cache;
|
||||
|
||||
typedef struct demo3d_net {
|
||||
pxl8_net* transport;
|
||||
|
||||
demo3d_chunk_cache* chunk_cache;
|
||||
i32 chunk_cx;
|
||||
i32 chunk_cz;
|
||||
bool has_chunk;
|
||||
demo3d_world* world;
|
||||
|
||||
u64 highest_tick;
|
||||
f32 interp_time;
|
||||
|
||||
demo3d_entity_state entities[DEMO3D_MAX_SNAPSHOT_ENTITIES];
|
||||
demo3d_entity_state prev_entities[DEMO3D_MAX_SNAPSHOT_ENTITIES];
|
||||
demo3d_snapshot_header prev_snapshot;
|
||||
demo3d_snapshot_header snapshot;
|
||||
|
||||
u64 input_head;
|
||||
demo3d_input_msg input_history[DEMO3D_NET_INPUT_HISTORY_SIZE];
|
||||
u64 input_oldest_tick;
|
||||
|
||||
u8 predicted_state[DEMO3D_NET_USERDATA_SIZE];
|
||||
u64 predicted_tick;
|
||||
} demo3d_net;
|
||||
|
||||
demo3d_net* demo3d_net_create(const pxl8_net_config* config);
|
||||
void demo3d_net_destroy(demo3d_net* ng);
|
||||
pxl8_result demo3d_net_connect(demo3d_net* ng);
|
||||
bool demo3d_net_connected(const demo3d_net* ng);
|
||||
void demo3d_net_disconnect(demo3d_net* ng);
|
||||
|
||||
const demo3d_entity_state* demo3d_net_entities(const demo3d_net* ng);
|
||||
const u8* demo3d_net_entity_prev_userdata(const demo3d_net* ng, u64 entity_id);
|
||||
const u8* demo3d_net_entity_userdata(const demo3d_net* ng, u64 entity_id);
|
||||
const demo3d_input_msg* demo3d_net_input_at(const demo3d_net* ng, u64 tick);
|
||||
u64 demo3d_net_input_oldest_tick(const demo3d_net* ng);
|
||||
void demo3d_net_input_push(demo3d_net* ng, const demo3d_input_msg* input);
|
||||
f32 demo3d_net_lerp_alpha(const demo3d_net* ng);
|
||||
bool demo3d_net_needs_correction(const demo3d_net* ng);
|
||||
u64 demo3d_net_player_id(const demo3d_net* ng);
|
||||
bool demo3d_net_poll(demo3d_net* ng);
|
||||
u8* demo3d_net_predicted_state(demo3d_net* ng);
|
||||
void demo3d_net_predicted_tick_set(demo3d_net* ng, u64 tick);
|
||||
pxl8_result demo3d_net_send_command(demo3d_net* ng, const demo3d_command_msg* cmd);
|
||||
pxl8_result demo3d_net_send_input(demo3d_net* ng, const demo3d_input_msg* input);
|
||||
const demo3d_snapshot_header* demo3d_net_snapshot(const demo3d_net* ng);
|
||||
u64 demo3d_net_tick(const demo3d_net* ng);
|
||||
void demo3d_net_update(demo3d_net* ng, f32 dt);
|
||||
void demo3d_net_set_chunk_cache(demo3d_net* ng, demo3d_chunk_cache* cache);
|
||||
demo3d_chunk_cache* demo3d_net_chunk_cache(demo3d_net* ng);
|
||||
void demo3d_net_set_world(demo3d_net* ng, demo3d_world* world);
|
||||
i32 demo3d_net_chunk_cx(const demo3d_net* ng);
|
||||
i32 demo3d_net_chunk_cz(const demo3d_net* ng);
|
||||
bool demo3d_net_has_chunk(const demo3d_net* ng);
|
||||
pxl8_result demo3d_net_spawn(demo3d_net* ng, f32 x, f32 y, f32 z, f32 yaw, f32 pitch);
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
void demo3d_net_start_thread(demo3d_net* ng);
|
||||
void demo3d_net_stop_thread(demo3d_net* ng);
|
||||
bool demo3d_net_dispatch_packet(demo3d_net* ng, const pxl8_packet* pkt);
|
||||
#endif
|
||||
197
demo3d/client/net/demo3d_protocol.c
Normal file
197
demo3d/client/net/demo3d_protocol.c
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
#include "demo3d_protocol.h"
|
||||
#include "pxl8_bytes.h"
|
||||
|
||||
usize demo3d_protocol_serialize_header(const demo3d_msg_header* msg, u8* buf, usize len) {
|
||||
if (len < sizeof(demo3d_msg_header)) return 0;
|
||||
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
||||
pxl8_write_u32_be(&s, msg->sequence);
|
||||
pxl8_write_u16_be(&s, msg->size);
|
||||
pxl8_write_u8(&s, msg->type);
|
||||
pxl8_write_u8(&s, msg->version);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_deserialize_header(const u8* buf, usize len, demo3d_msg_header* msg) {
|
||||
if (len < sizeof(demo3d_msg_header)) return 0;
|
||||
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
||||
msg->sequence = pxl8_read_u32_be(&s);
|
||||
msg->size = pxl8_read_u16_be(&s);
|
||||
msg->type = pxl8_read_u8(&s);
|
||||
msg->version = pxl8_read_u8(&s);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_serialize_input(const demo3d_input_msg* msg, u8* buf, usize len) {
|
||||
if (len < sizeof(demo3d_input_msg)) return 0;
|
||||
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
||||
pxl8_write_u32_be(&s, msg->buttons);
|
||||
pxl8_write_f32_be(&s, msg->look_dx);
|
||||
pxl8_write_f32_be(&s, msg->look_dy);
|
||||
pxl8_write_f32_be(&s, msg->move_x);
|
||||
pxl8_write_f32_be(&s, msg->move_y);
|
||||
pxl8_write_f32_be(&s, msg->yaw);
|
||||
pxl8_write_u64_be(&s, msg->tick);
|
||||
pxl8_write_u64_be(&s, msg->timestamp);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_deserialize_input(const u8* buf, usize len, demo3d_input_msg* msg) {
|
||||
if (len < sizeof(demo3d_input_msg)) return 0;
|
||||
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
||||
msg->buttons = pxl8_read_u32_be(&s);
|
||||
msg->look_dx = pxl8_read_f32_be(&s);
|
||||
msg->look_dy = pxl8_read_f32_be(&s);
|
||||
msg->move_x = pxl8_read_f32_be(&s);
|
||||
msg->move_y = pxl8_read_f32_be(&s);
|
||||
msg->yaw = pxl8_read_f32_be(&s);
|
||||
msg->tick = pxl8_read_u64_be(&s);
|
||||
msg->timestamp = pxl8_read_u64_be(&s);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_serialize_command(const demo3d_command_msg* msg, u8* buf, usize len) {
|
||||
if (len < sizeof(demo3d_command_msg)) return 0;
|
||||
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
||||
pxl8_write_u16_be(&s, msg->cmd_type);
|
||||
pxl8_write_bytes(&s, msg->payload, DEMO3D_COMMAND_PAYLOAD_SIZE);
|
||||
pxl8_write_u16_be(&s, msg->payload_size);
|
||||
pxl8_write_u64_be(&s, msg->tick);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_deserialize_command(const u8* buf, usize len, demo3d_command_msg* msg) {
|
||||
if (len < sizeof(demo3d_command_msg)) return 0;
|
||||
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
||||
msg->cmd_type = pxl8_read_u16_be(&s);
|
||||
pxl8_read_bytes(&s, msg->payload, DEMO3D_COMMAND_PAYLOAD_SIZE);
|
||||
msg->payload_size = pxl8_read_u16_be(&s);
|
||||
msg->tick = pxl8_read_u64_be(&s);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_serialize_entity_state(const demo3d_entity_state* state, u8* buf, usize len) {
|
||||
if (len < sizeof(demo3d_entity_state)) return 0;
|
||||
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
||||
pxl8_write_u64_be(&s, state->entity_id);
|
||||
pxl8_write_bytes(&s, state->userdata, 56);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_deserialize_entity_state(const u8* buf, usize len, demo3d_entity_state* state) {
|
||||
if (len < sizeof(demo3d_entity_state)) return 0;
|
||||
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
||||
state->entity_id = pxl8_read_u64_be(&s);
|
||||
pxl8_read_bytes(&s, state->userdata, 56);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_serialize_event(const demo3d_event_msg* msg, u8* buf, usize len) {
|
||||
if (len < sizeof(demo3d_event_msg)) return 0;
|
||||
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
||||
pxl8_write_u8(&s, msg->event_type);
|
||||
pxl8_write_bytes(&s, msg->payload, DEMO3D_EVENT_PAYLOAD_SIZE);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_deserialize_event(const u8* buf, usize len, demo3d_event_msg* msg) {
|
||||
if (len < sizeof(demo3d_event_msg)) return 0;
|
||||
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
||||
msg->event_type = pxl8_read_u8(&s);
|
||||
pxl8_read_bytes(&s, msg->payload, DEMO3D_EVENT_PAYLOAD_SIZE);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_serialize_snapshot_header(const demo3d_snapshot_header* hdr, u8* buf, usize len) {
|
||||
if (len < sizeof(demo3d_snapshot_header)) return 0;
|
||||
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
||||
pxl8_write_u16_be(&s, hdr->entity_count);
|
||||
pxl8_write_u16_be(&s, hdr->event_count);
|
||||
pxl8_write_u64_be(&s, hdr->player_id);
|
||||
pxl8_write_u64_be(&s, hdr->tick);
|
||||
pxl8_write_f32_be(&s, hdr->time);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_deserialize_snapshot_header(const u8* buf, usize len, demo3d_snapshot_header* hdr) {
|
||||
if (len < sizeof(demo3d_snapshot_header)) return 0;
|
||||
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
||||
hdr->entity_count = pxl8_read_u16_be(&s);
|
||||
hdr->event_count = pxl8_read_u16_be(&s);
|
||||
hdr->player_id = pxl8_read_u64_be(&s);
|
||||
hdr->tick = pxl8_read_u64_be(&s);
|
||||
hdr->time = pxl8_read_f32_be(&s);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_serialize_chunk_msg_header(const demo3d_chunk_msg_header* hdr, u8* buf, usize len) {
|
||||
if (len < 24) return 0;
|
||||
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
||||
pxl8_write_u8(&s, hdr->chunk_type);
|
||||
pxl8_write_u8(&s, hdr->flags);
|
||||
pxl8_write_u8(&s, hdr->fragment_idx);
|
||||
pxl8_write_u8(&s, hdr->fragment_count);
|
||||
pxl8_write_u32_be(&s, hdr->id);
|
||||
pxl8_write_u32_be(&s, (u32)hdr->cx);
|
||||
pxl8_write_u32_be(&s, (u32)hdr->cy);
|
||||
pxl8_write_u32_be(&s, (u32)hdr->cz);
|
||||
pxl8_write_u32_be(&s, hdr->version);
|
||||
pxl8_write_u16_be(&s, hdr->payload_size);
|
||||
pxl8_write_u16_be(&s, hdr->reserved);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_deserialize_chunk_msg_header(const u8* buf, usize len, demo3d_chunk_msg_header* hdr) {
|
||||
if (len < 24) return 0;
|
||||
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
||||
hdr->chunk_type = pxl8_read_u8(&s);
|
||||
hdr->flags = pxl8_read_u8(&s);
|
||||
hdr->fragment_idx = pxl8_read_u8(&s);
|
||||
hdr->fragment_count = pxl8_read_u8(&s);
|
||||
hdr->id = pxl8_read_u32_be(&s);
|
||||
hdr->cx = (i32)pxl8_read_u32_be(&s);
|
||||
hdr->cy = (i32)pxl8_read_u32_be(&s);
|
||||
hdr->cz = (i32)pxl8_read_u32_be(&s);
|
||||
hdr->version = pxl8_read_u32_be(&s);
|
||||
hdr->payload_size = pxl8_read_u16_be(&s);
|
||||
hdr->reserved = pxl8_read_u16_be(&s);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_deserialize_bsp_wire_header(const u8* buf, usize len, demo3d_bsp_wire_header* hdr) {
|
||||
if (len < 48) return 0;
|
||||
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
||||
hdr->num_vertices = pxl8_read_u32_be(&s);
|
||||
hdr->num_edges = pxl8_read_u32_be(&s);
|
||||
hdr->num_faces = pxl8_read_u32_be(&s);
|
||||
hdr->num_planes = pxl8_read_u32_be(&s);
|
||||
hdr->num_nodes = pxl8_read_u32_be(&s);
|
||||
hdr->num_leafs = pxl8_read_u32_be(&s);
|
||||
hdr->num_surfedges = pxl8_read_u32_be(&s);
|
||||
hdr->num_marksurfaces = pxl8_read_u32_be(&s);
|
||||
hdr->num_cell_portals = pxl8_read_u32_be(&s);
|
||||
hdr->visdata_size = pxl8_read_u32_be(&s);
|
||||
hdr->num_vertex_lights = pxl8_read_u32_be(&s);
|
||||
hdr->num_heightfield = pxl8_read_u32_be(&s);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_serialize_chunk_enter(const demo3d_chunk_enter_msg* msg, u8* buf, usize len) {
|
||||
if (len < 8) return 0;
|
||||
pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
|
||||
pxl8_write_u32_be(&s, (u32)msg->cx);
|
||||
pxl8_write_u32_be(&s, (u32)msg->cz);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
usize demo3d_protocol_deserialize_chunk_enter(const u8* buf, usize len, demo3d_chunk_enter_msg* msg) {
|
||||
if (len < 8) return 0;
|
||||
pxl8_stream s = pxl8_stream_create(buf, (u32)len);
|
||||
msg->cx = (i32)pxl8_read_u32_be(&s);
|
||||
msg->cz = (i32)pxl8_read_u32_be(&s);
|
||||
return s.offset;
|
||||
}
|
||||
|
||||
u32 demo3d_chunk_hash(i32 cx, i32 cz) {
|
||||
u32 h = (u32)cx * 374761393u + (u32)cz * 668265263u;
|
||||
return h ^ (h >> 16);
|
||||
}
|
||||
143
demo3d/client/net/demo3d_protocol.h
Normal file
143
demo3d/client/net/demo3d_protocol.h
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
#pragma once
|
||||
|
||||
#include "pxl8_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DEMO3D_PROTOCOL_VERSION 1
|
||||
#define DEMO3D_MAX_SNAPSHOT_ENTITIES 256
|
||||
#define DEMO3D_MAX_SNAPSHOT_EVENTS 32
|
||||
#define DEMO3D_COMMAND_PAYLOAD_SIZE 64
|
||||
#define DEMO3D_EVENT_PAYLOAD_SIZE 15
|
||||
|
||||
typedef enum demo3d_msg_type {
|
||||
DEMO3D_MSG_NONE = 0,
|
||||
DEMO3D_MSG_CHUNK,
|
||||
DEMO3D_MSG_CHUNK_ENTER,
|
||||
DEMO3D_MSG_CHUNK_EXIT,
|
||||
DEMO3D_MSG_COMMAND,
|
||||
DEMO3D_MSG_CONNECT,
|
||||
DEMO3D_MSG_DISCONNECT,
|
||||
DEMO3D_MSG_EVENT,
|
||||
DEMO3D_MSG_INPUT,
|
||||
DEMO3D_MSG_SNAPSHOT
|
||||
} demo3d_msg_type;
|
||||
|
||||
typedef struct demo3d_msg_header {
|
||||
u32 sequence;
|
||||
u16 size;
|
||||
u8 type;
|
||||
u8 version;
|
||||
} demo3d_msg_header;
|
||||
|
||||
typedef enum demo3d_cmd_type {
|
||||
DEMO3D_CMD_NONE = 0,
|
||||
DEMO3D_CMD_SPAWN_ENTITY,
|
||||
} demo3d_cmd_type;
|
||||
|
||||
typedef struct demo3d_input_msg {
|
||||
u32 buttons;
|
||||
f32 look_dx;
|
||||
f32 look_dy;
|
||||
f32 move_x;
|
||||
f32 move_y;
|
||||
f32 yaw;
|
||||
u64 tick;
|
||||
u64 timestamp;
|
||||
} demo3d_input_msg;
|
||||
|
||||
typedef struct demo3d_command_msg {
|
||||
u16 cmd_type;
|
||||
u8 payload[DEMO3D_COMMAND_PAYLOAD_SIZE];
|
||||
u16 payload_size;
|
||||
u64 tick;
|
||||
} demo3d_command_msg;
|
||||
|
||||
typedef struct demo3d_entity_state {
|
||||
u64 entity_id;
|
||||
u8 userdata[56];
|
||||
} demo3d_entity_state;
|
||||
|
||||
typedef struct demo3d_event_msg {
|
||||
u8 event_type;
|
||||
u8 payload[DEMO3D_EVENT_PAYLOAD_SIZE];
|
||||
} demo3d_event_msg;
|
||||
|
||||
typedef struct demo3d_snapshot_header {
|
||||
u16 entity_count;
|
||||
u16 event_count;
|
||||
u64 player_id;
|
||||
u64 tick;
|
||||
f32 time;
|
||||
} demo3d_snapshot_header;
|
||||
|
||||
#define DEMO3D_CHUNK_TYPE_BSP 1
|
||||
|
||||
#define DEMO3D_CHUNK_FLAG_FINAL 0x04
|
||||
#define DEMO3D_CHUNK_MAX_PAYLOAD 1400
|
||||
|
||||
typedef struct demo3d_chunk_msg_header {
|
||||
u8 chunk_type;
|
||||
u8 flags;
|
||||
u8 fragment_idx;
|
||||
u8 fragment_count;
|
||||
u32 id;
|
||||
i32 cx, cy, cz;
|
||||
u32 version;
|
||||
u16 payload_size;
|
||||
u16 reserved;
|
||||
} demo3d_chunk_msg_header;
|
||||
|
||||
typedef struct demo3d_bsp_wire_header {
|
||||
u32 num_vertices;
|
||||
u32 num_edges;
|
||||
u32 num_faces;
|
||||
u32 num_planes;
|
||||
u32 num_nodes;
|
||||
u32 num_leafs;
|
||||
u32 num_surfedges;
|
||||
u32 num_marksurfaces;
|
||||
u32 num_cell_portals;
|
||||
u32 visdata_size;
|
||||
u32 num_vertex_lights;
|
||||
u32 num_heightfield;
|
||||
} demo3d_bsp_wire_header;
|
||||
|
||||
usize demo3d_protocol_serialize_header(const demo3d_msg_header* msg, u8* buf, usize len);
|
||||
usize demo3d_protocol_deserialize_header(const u8* buf, usize len, demo3d_msg_header* msg);
|
||||
|
||||
usize demo3d_protocol_serialize_input(const demo3d_input_msg* msg, u8* buf, usize len);
|
||||
usize demo3d_protocol_deserialize_input(const u8* buf, usize len, demo3d_input_msg* msg);
|
||||
|
||||
usize demo3d_protocol_serialize_command(const demo3d_command_msg* msg, u8* buf, usize len);
|
||||
usize demo3d_protocol_deserialize_command(const u8* buf, usize len, demo3d_command_msg* msg);
|
||||
|
||||
usize demo3d_protocol_serialize_entity_state(const demo3d_entity_state* state, u8* buf, usize len);
|
||||
usize demo3d_protocol_deserialize_entity_state(const u8* buf, usize len, demo3d_entity_state* state);
|
||||
|
||||
usize demo3d_protocol_serialize_event(const demo3d_event_msg* msg, u8* buf, usize len);
|
||||
usize demo3d_protocol_deserialize_event(const u8* buf, usize len, demo3d_event_msg* msg);
|
||||
|
||||
usize demo3d_protocol_serialize_snapshot_header(const demo3d_snapshot_header* hdr, u8* buf, usize len);
|
||||
usize demo3d_protocol_deserialize_snapshot_header(const u8* buf, usize len, demo3d_snapshot_header* hdr);
|
||||
|
||||
usize demo3d_protocol_serialize_chunk_msg_header(const demo3d_chunk_msg_header* hdr, u8* buf, usize len);
|
||||
usize demo3d_protocol_deserialize_chunk_msg_header(const u8* buf, usize len, demo3d_chunk_msg_header* hdr);
|
||||
|
||||
usize demo3d_protocol_deserialize_bsp_wire_header(const u8* buf, usize len, demo3d_bsp_wire_header* hdr);
|
||||
|
||||
typedef struct demo3d_chunk_enter_msg {
|
||||
i32 cx;
|
||||
i32 cz;
|
||||
} demo3d_chunk_enter_msg;
|
||||
|
||||
usize demo3d_protocol_serialize_chunk_enter(const demo3d_chunk_enter_msg* msg, u8* buf, usize len);
|
||||
usize demo3d_protocol_deserialize_chunk_enter(const u8* buf, usize len, demo3d_chunk_enter_msg* msg);
|
||||
|
||||
u32 demo3d_chunk_hash(i32 cx, i32 cz);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
448
demo3d/client/sim/demo3d_sim.c
Normal file
448
demo3d/client/sim/demo3d_sim.c
Normal file
|
|
@ -0,0 +1,448 @@
|
|||
#include "demo3d_sim.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#define DIST_EPSILON 0.03125f
|
||||
|
||||
typedef struct {
|
||||
f32 fraction;
|
||||
pxl8_vec3 normal;
|
||||
bool all_solid;
|
||||
bool start_solid;
|
||||
} trace_result;
|
||||
|
||||
static i32 bsp_find_leaf(const demo3d_bsp* bsp, pxl8_vec3 pos) {
|
||||
if (!bsp || bsp->num_nodes == 0) return -1;
|
||||
|
||||
i32 node_id = 0;
|
||||
while (node_id >= 0) {
|
||||
const demo3d_bsp_node* node = &bsp->nodes[node_id];
|
||||
const demo3d_bsp_plane* plane = &bsp->planes[node->plane_id];
|
||||
f32 dist = pxl8_vec3_dot(pos, plane->normal) - plane->dist;
|
||||
node_id = node->children[dist < 0 ? 1 : 0];
|
||||
}
|
||||
|
||||
return -(node_id + 1);
|
||||
}
|
||||
|
||||
static i32 bsp_contents_from(const demo3d_bsp* bsp, i32 node_id, pxl8_vec3 pos) {
|
||||
while (node_id >= 0) {
|
||||
const demo3d_bsp_node* node = &bsp->nodes[node_id];
|
||||
const demo3d_bsp_plane* plane = &bsp->planes[node->plane_id];
|
||||
f32 d = pxl8_vec3_dot(pos, plane->normal) - plane->dist;
|
||||
node_id = node->children[d < 0 ? 1 : 0];
|
||||
}
|
||||
i32 leaf_idx = -(node_id + 1);
|
||||
if (leaf_idx < 0 || (u32)leaf_idx >= bsp->num_leafs) return -1;
|
||||
return bsp->leafs[leaf_idx].contents;
|
||||
}
|
||||
|
||||
static bool bsp_recursive_trace(const demo3d_bsp* bsp, i32 node_id,
|
||||
f32 p1f, f32 p2f,
|
||||
pxl8_vec3 p1, pxl8_vec3 p2,
|
||||
trace_result* tr) {
|
||||
if (node_id < 0) {
|
||||
i32 leaf_idx = -(node_id + 1);
|
||||
if (leaf_idx >= 0 && (u32)leaf_idx < bsp->num_leafs &&
|
||||
bsp->leafs[leaf_idx].contents == -1) {
|
||||
tr->start_solid = true;
|
||||
} else {
|
||||
tr->all_solid = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const demo3d_bsp_node* node = &bsp->nodes[node_id];
|
||||
const demo3d_bsp_plane* plane = &bsp->planes[node->plane_id];
|
||||
|
||||
f32 t1 = pxl8_vec3_dot(p1, plane->normal) - plane->dist;
|
||||
f32 t2 = pxl8_vec3_dot(p2, plane->normal) - plane->dist;
|
||||
|
||||
if (t1 >= 0 && t2 >= 0)
|
||||
return bsp_recursive_trace(bsp, node->children[0], p1f, p2f, p1, p2, tr);
|
||||
if (t1 < 0 && t2 < 0)
|
||||
return bsp_recursive_trace(bsp, node->children[1], p1f, p2f, p1, p2, tr);
|
||||
|
||||
i32 side;
|
||||
f32 frac;
|
||||
if (t1 < 0) {
|
||||
frac = (t1 + DIST_EPSILON) / (t1 - t2);
|
||||
side = 1;
|
||||
} else {
|
||||
frac = (t1 - DIST_EPSILON) / (t1 - t2);
|
||||
side = 0;
|
||||
}
|
||||
if (frac < 0) frac = 0;
|
||||
if (frac > 1) frac = 1;
|
||||
|
||||
f32 midf = p1f + (p2f - p1f) * frac;
|
||||
pxl8_vec3 mid = {
|
||||
p1.x + frac * (p2.x - p1.x),
|
||||
p1.y + frac * (p2.y - p1.y),
|
||||
p1.z + frac * (p2.z - p1.z),
|
||||
};
|
||||
|
||||
if (!bsp_recursive_trace(bsp, node->children[side], p1f, midf, p1, mid, tr))
|
||||
return false;
|
||||
|
||||
if (bsp_contents_from(bsp, node->children[side ^ 1], mid) != -1)
|
||||
return bsp_recursive_trace(bsp, node->children[side ^ 1], midf, p2f, mid, p2, tr);
|
||||
|
||||
if (tr->all_solid)
|
||||
return false;
|
||||
|
||||
if (midf < tr->fraction) {
|
||||
tr->fraction = midf;
|
||||
if (side == 0) {
|
||||
tr->normal = plane->normal;
|
||||
} else {
|
||||
tr->normal.x = -plane->normal.x;
|
||||
tr->normal.y = -plane->normal.y;
|
||||
tr->normal.z = -plane->normal.z;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static trace_result bsp_trace_line(const demo3d_bsp* bsp, pxl8_vec3 from, pxl8_vec3 to) {
|
||||
trace_result tr = { .fraction = 1.0f, .all_solid = true };
|
||||
if (!bsp || bsp->num_nodes == 0) {
|
||||
tr.all_solid = false;
|
||||
return tr;
|
||||
}
|
||||
bsp_recursive_trace(bsp, 0, 0.0f, 1.0f, from, to, &tr);
|
||||
if (tr.all_solid) {
|
||||
tr.fraction = 0.0f;
|
||||
tr.start_solid = true;
|
||||
}
|
||||
return tr;
|
||||
}
|
||||
|
||||
bool demo3d_bsp_point_solid(const demo3d_bsp* bsp, pxl8_vec3 pos) {
|
||||
if (!bsp) return false;
|
||||
if (bsp->bounds_max_x > bsp->bounds_min_x &&
|
||||
(pos.x < bsp->bounds_min_x || pos.x >= bsp->bounds_max_x ||
|
||||
pos.z < bsp->bounds_min_z || pos.z >= bsp->bounds_max_z))
|
||||
return false;
|
||||
i32 leaf = bsp_find_leaf(bsp, pos);
|
||||
if (leaf < 0 || (u32)leaf >= bsp->num_leafs) return true;
|
||||
return bsp->leafs[leaf].contents == -1;
|
||||
}
|
||||
|
||||
static void trace_offsets(const demo3d_bsp* bsp, pxl8_vec3 from, pxl8_vec3 to,
|
||||
f32 radius, f32* out_frac, pxl8_vec3* out_normal) {
|
||||
f32 d = radius * 0.7071f;
|
||||
pxl8_vec3 offsets[9] = {
|
||||
{0, 0, 0},
|
||||
{radius, 0, 0}, {-radius, 0, 0},
|
||||
{0, 0, radius}, {0, 0, -radius},
|
||||
{d, 0, d}, {d, 0, -d},
|
||||
{-d, 0, d}, {-d, 0, -d},
|
||||
};
|
||||
|
||||
*out_frac = 1.0f;
|
||||
*out_normal = (pxl8_vec3){0, 0, 0};
|
||||
|
||||
for (i32 i = 0; i < 9; i++) {
|
||||
pxl8_vec3 s = { from.x + offsets[i].x, from.y + offsets[i].y, from.z + offsets[i].z };
|
||||
pxl8_vec3 e = { to.x + offsets[i].x, to.y + offsets[i].y, to.z + offsets[i].z };
|
||||
trace_result tr = bsp_trace_line(bsp, s, e);
|
||||
if (tr.fraction < *out_frac && !tr.start_solid) {
|
||||
*out_frac = tr.fraction;
|
||||
*out_normal = tr.normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pxl8_vec3 demo3d_bsp_trace(const demo3d_bsp* bsp, pxl8_vec3 from, pxl8_vec3 to, f32 radius) {
|
||||
if (!bsp || bsp->num_nodes == 0) return to;
|
||||
|
||||
f32 frac;
|
||||
pxl8_vec3 normal;
|
||||
trace_offsets(bsp, from, to, radius, &frac, &normal);
|
||||
if (frac >= 1.0f) return to;
|
||||
|
||||
pxl8_vec3 delta = { to.x - from.x, to.y - from.y, to.z - from.z };
|
||||
pxl8_vec3 hit = {
|
||||
from.x + delta.x * frac,
|
||||
from.y + delta.y * frac,
|
||||
from.z + delta.z * frac,
|
||||
};
|
||||
|
||||
f32 remaining = 1.0f - frac;
|
||||
if (remaining <= 0.0f) return hit;
|
||||
|
||||
f32 backoff = pxl8_vec3_dot(delta, normal) * remaining;
|
||||
pxl8_vec3 slide_target = {
|
||||
hit.x + delta.x * remaining - normal.x * backoff,
|
||||
hit.y + delta.y * remaining - normal.y * backoff,
|
||||
hit.z + delta.z * remaining - normal.z * backoff,
|
||||
};
|
||||
|
||||
f32 slide_frac;
|
||||
pxl8_vec3 slide_normal;
|
||||
trace_offsets(bsp, hit, slide_target, radius, &slide_frac, &slide_normal);
|
||||
if (slide_frac >= 1.0f) return slide_target;
|
||||
|
||||
pxl8_vec3 slide_delta = {
|
||||
slide_target.x - hit.x,
|
||||
slide_target.y - hit.y,
|
||||
slide_target.z - hit.z,
|
||||
};
|
||||
return (pxl8_vec3){
|
||||
hit.x + slide_delta.x * slide_frac,
|
||||
hit.y + slide_delta.y * slide_frac,
|
||||
hit.z + slide_delta.z * slide_frac,
|
||||
};
|
||||
}
|
||||
|
||||
static const demo3d_bsp* sim_bsp_at(const demo3d_sim_world* world, f32 x, f32 z) {
|
||||
i32 cx = (i32)floorf(x / world->chunk_size);
|
||||
i32 cz = (i32)floorf(z / world->chunk_size);
|
||||
i32 dx = cx - world->center_cx + 1;
|
||||
i32 dz = cz - world->center_cz + 1;
|
||||
if (dx < 0 || dx > 2 || dz < 0 || dz > 2) return NULL;
|
||||
return world->chunks[dz * 3 + dx];
|
||||
}
|
||||
|
||||
static f32 bsp_terrain_height(const demo3d_bsp* bsp, f32 x, f32 z) {
|
||||
if (!bsp || !bsp->heightfield || bsp->heightfield_cell_size <= 0) return -1e9f;
|
||||
f32 lx = (x - bsp->heightfield_ox) / bsp->heightfield_cell_size;
|
||||
f32 lz = (z - bsp->heightfield_oz) / bsp->heightfield_cell_size;
|
||||
i32 ix = (i32)floorf(lx);
|
||||
i32 iz = (i32)floorf(lz);
|
||||
if (ix < 0 || ix >= bsp->heightfield_w - 1 || iz < 0 || iz >= bsp->heightfield_h - 1) return -1e9f;
|
||||
f32 fx = lx - ix;
|
||||
f32 fz = lz - iz;
|
||||
i32 w = bsp->heightfield_w;
|
||||
f32 h00 = bsp->heightfield[iz * w + ix];
|
||||
f32 h10 = bsp->heightfield[iz * w + ix + 1];
|
||||
f32 h01 = bsp->heightfield[(iz + 1) * w + ix];
|
||||
f32 h11 = bsp->heightfield[(iz + 1) * w + ix + 1];
|
||||
f32 h0 = h00 + (h10 - h00) * fx;
|
||||
f32 h1 = h01 + (h11 - h01) * fx;
|
||||
return h0 + (h1 - h0) * fz;
|
||||
}
|
||||
|
||||
static f32 sim_terrain_height(const demo3d_sim_world* world, f32 x, f32 z) {
|
||||
const demo3d_bsp* bsp = sim_bsp_at(world, x, z);
|
||||
return bsp_terrain_height(bsp, x, z);
|
||||
}
|
||||
|
||||
static void sim_trace_offsets(const demo3d_sim_world* world, pxl8_vec3 from, pxl8_vec3 to,
|
||||
f32 radius, f32* out_frac, pxl8_vec3* out_normal) {
|
||||
f32 d = radius * 0.7071f;
|
||||
pxl8_vec3 offsets[9] = {
|
||||
{0, 0, 0},
|
||||
{radius, 0, 0}, {-radius, 0, 0},
|
||||
{0, 0, radius}, {0, 0, -radius},
|
||||
{d, 0, d}, {d, 0, -d},
|
||||
{-d, 0, d}, {-d, 0, -d},
|
||||
};
|
||||
|
||||
*out_frac = 1.0f;
|
||||
*out_normal = (pxl8_vec3){0, 0, 0};
|
||||
|
||||
for (i32 i = 0; i < 9; i++) {
|
||||
pxl8_vec3 s = { from.x + offsets[i].x, from.y + offsets[i].y, from.z + offsets[i].z };
|
||||
pxl8_vec3 e = { to.x + offsets[i].x, to.y + offsets[i].y, to.z + offsets[i].z };
|
||||
const demo3d_bsp* bsp_s = sim_bsp_at(world, s.x, s.z);
|
||||
const demo3d_bsp* bsp_e = sim_bsp_at(world, e.x, e.z);
|
||||
if (bsp_s) {
|
||||
trace_result tr = bsp_trace_line(bsp_s, s, e);
|
||||
if (tr.fraction < *out_frac && !tr.start_solid) {
|
||||
*out_frac = tr.fraction;
|
||||
*out_normal = tr.normal;
|
||||
}
|
||||
}
|
||||
if (bsp_e && bsp_e != bsp_s) {
|
||||
bool inside_bounds = !(bsp_e->bounds_max_x > bsp_e->bounds_min_x) ||
|
||||
(s.x >= bsp_e->bounds_min_x && s.x < bsp_e->bounds_max_x &&
|
||||
s.z >= bsp_e->bounds_min_z && s.z < bsp_e->bounds_max_z);
|
||||
if (inside_bounds) {
|
||||
trace_result tr = bsp_trace_line(bsp_e, s, e);
|
||||
if (tr.fraction < *out_frac && !tr.start_solid) {
|
||||
*out_frac = tr.fraction;
|
||||
*out_normal = tr.normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pxl8_vec3 demo3d_sim_trace(const demo3d_sim_world* world, pxl8_vec3 from, pxl8_vec3 to, f32 radius, f32 height) {
|
||||
(void)height;
|
||||
if (!world || world->chunk_size <= 0) return to;
|
||||
|
||||
f32 frac;
|
||||
pxl8_vec3 normal;
|
||||
sim_trace_offsets(world, from, to, radius, &frac, &normal);
|
||||
if (frac >= 1.0f) return to;
|
||||
|
||||
pxl8_vec3 delta = { to.x - from.x, to.y - from.y, to.z - from.z };
|
||||
pxl8_vec3 hit = {
|
||||
from.x + delta.x * frac,
|
||||
from.y + delta.y * frac,
|
||||
from.z + delta.z * frac,
|
||||
};
|
||||
|
||||
f32 remaining = 1.0f - frac;
|
||||
if (remaining <= 0.0f) return hit;
|
||||
|
||||
f32 backoff = pxl8_vec3_dot(delta, normal) * remaining;
|
||||
pxl8_vec3 slide_target = {
|
||||
hit.x + delta.x * remaining - normal.x * backoff,
|
||||
hit.y + delta.y * remaining - normal.y * backoff,
|
||||
hit.z + delta.z * remaining - normal.z * backoff,
|
||||
};
|
||||
|
||||
f32 slide_frac;
|
||||
pxl8_vec3 slide_normal;
|
||||
sim_trace_offsets(world, hit, slide_target, radius, &slide_frac, &slide_normal);
|
||||
if (slide_frac >= 1.0f) return slide_target;
|
||||
|
||||
pxl8_vec3 slide_delta = {
|
||||
slide_target.x - hit.x,
|
||||
slide_target.y - hit.y,
|
||||
slide_target.z - hit.z,
|
||||
};
|
||||
return (pxl8_vec3){
|
||||
hit.x + slide_delta.x * slide_frac,
|
||||
hit.y + slide_delta.y * slide_frac,
|
||||
hit.z + slide_delta.z * slide_frac,
|
||||
};
|
||||
}
|
||||
|
||||
bool demo3d_sim_check_ground(const demo3d_sim_world* world, pxl8_vec3 pos, f32 radius) {
|
||||
if (!world || world->chunk_size <= 0) return true;
|
||||
|
||||
f32 th = sim_terrain_height(world, pos.x, pos.z);
|
||||
if (th > -1e8f && pos.y - th < 2.0f) return true;
|
||||
|
||||
pxl8_vec3 down = {pos.x, pos.y - 2.0f, pos.z};
|
||||
pxl8_vec3 result = demo3d_sim_trace(world, pos, down, radius, 0.0f);
|
||||
|
||||
return result.y > down.y;
|
||||
}
|
||||
|
||||
void demo3d_sim_move_player(demo3d_sim_entity* ent, const demo3d_input_msg* input,
|
||||
const demo3d_sim_world* world, const demo3d_sim_config* cfg, f32 dt) {
|
||||
if (!ent || !input || !cfg) return;
|
||||
if (!(ent->flags & DEMO3D_SIM_FLAG_ALIVE)) return;
|
||||
|
||||
ent->yaw -= input->look_dx * 0.008f;
|
||||
f32 new_pitch = ent->pitch - input->look_dy * 0.008f;
|
||||
if (new_pitch > cfg->max_pitch) new_pitch = cfg->max_pitch;
|
||||
if (new_pitch < -cfg->max_pitch) new_pitch = -cfg->max_pitch;
|
||||
ent->pitch = new_pitch;
|
||||
|
||||
f32 sin_yaw = sinf(ent->yaw);
|
||||
f32 cos_yaw = cosf(ent->yaw);
|
||||
|
||||
f32 input_len = sqrtf(input->move_x * input->move_x + input->move_y * input->move_y);
|
||||
|
||||
pxl8_vec3 move_dir = {0, 0, 0};
|
||||
f32 target_speed = 0.0f;
|
||||
|
||||
if (input_len > 0.0f) {
|
||||
f32 nx = input->move_x / input_len;
|
||||
f32 ny = input->move_y / input_len;
|
||||
move_dir.x = cos_yaw * nx - sin_yaw * ny;
|
||||
move_dir.z = -sin_yaw * nx - cos_yaw * ny;
|
||||
target_speed = cfg->move_speed;
|
||||
}
|
||||
|
||||
ent->vel.x = move_dir.x * target_speed;
|
||||
ent->vel.z = move_dir.z * target_speed;
|
||||
|
||||
bool grounded = (ent->flags & DEMO3D_SIM_FLAG_GROUNDED) != 0;
|
||||
|
||||
if (grounded && (input->buttons & 1)) {
|
||||
ent->vel.y = cfg->jump_velocity;
|
||||
ent->flags &= ~DEMO3D_SIM_FLAG_GROUNDED;
|
||||
grounded = false;
|
||||
}
|
||||
|
||||
if (!grounded) {
|
||||
ent->vel.y -= cfg->gravity * dt;
|
||||
}
|
||||
|
||||
pxl8_vec3 target = {
|
||||
ent->pos.x + ent->vel.x * dt,
|
||||
ent->pos.y + ent->vel.y * dt,
|
||||
ent->pos.z + ent->vel.z * dt
|
||||
};
|
||||
|
||||
if (grounded) {
|
||||
f32 th_dest = sim_terrain_height(world, target.x, target.z);
|
||||
if (th_dest > -1e8f) target.y = th_dest;
|
||||
}
|
||||
|
||||
pxl8_vec3 new_pos = demo3d_sim_trace(world, ent->pos, target, cfg->player_radius, cfg->player_height);
|
||||
|
||||
f32 th = sim_terrain_height(world, new_pos.x, new_pos.z);
|
||||
if (th > -1e8f && new_pos.y < th) {
|
||||
new_pos.y = th;
|
||||
if (ent->vel.y < 0) ent->vel.y = 0;
|
||||
}
|
||||
|
||||
ent->pos = new_pos;
|
||||
|
||||
if (demo3d_sim_check_ground(world, ent->pos, cfg->player_radius)) {
|
||||
ent->flags |= DEMO3D_SIM_FLAG_GROUNDED;
|
||||
if (ent->vel.y < 0) ent->vel.y = 0;
|
||||
} else {
|
||||
ent->flags &= ~DEMO3D_SIM_FLAG_GROUNDED;
|
||||
}
|
||||
}
|
||||
|
||||
void demo3d_sim_integrate(demo3d_sim_entity* ent, const demo3d_sim_world* world,
|
||||
const demo3d_sim_config* cfg, f32 dt) {
|
||||
if (!ent || !cfg) return;
|
||||
if (!(ent->flags & DEMO3D_SIM_FLAG_ALIVE)) return;
|
||||
if (ent->flags & DEMO3D_SIM_FLAG_PLAYER) return;
|
||||
|
||||
bool grounded = (ent->flags & DEMO3D_SIM_FLAG_GROUNDED) != 0;
|
||||
|
||||
ent->vel.y -= cfg->gravity * dt;
|
||||
|
||||
if (grounded) {
|
||||
f32 speed = sqrtf(ent->vel.x * ent->vel.x + ent->vel.z * ent->vel.z);
|
||||
if (speed > 0.0f) {
|
||||
f32 drop = speed * cfg->friction * dt;
|
||||
f32 new_speed = speed - drop;
|
||||
if (new_speed < 0.0f) new_speed = 0.0f;
|
||||
f32 scale = new_speed / speed;
|
||||
ent->vel.x *= scale;
|
||||
ent->vel.z *= scale;
|
||||
}
|
||||
}
|
||||
|
||||
pxl8_vec3 target = {
|
||||
ent->pos.x + ent->vel.x * dt,
|
||||
ent->pos.y + ent->vel.y * dt,
|
||||
ent->pos.z + ent->vel.z * dt
|
||||
};
|
||||
|
||||
if (grounded) {
|
||||
f32 th_dest = sim_terrain_height(world, target.x, target.z);
|
||||
if (th_dest > -1e8f) target.y = th_dest;
|
||||
}
|
||||
|
||||
pxl8_vec3 new_pos = demo3d_sim_trace(world, ent->pos, target, cfg->player_radius, cfg->player_height);
|
||||
|
||||
f32 th2 = sim_terrain_height(world, new_pos.x, new_pos.z);
|
||||
if (th2 > -1e8f && new_pos.y < th2) {
|
||||
new_pos.y = th2;
|
||||
if (ent->vel.y < 0.0f) ent->vel.y = 0.0f;
|
||||
}
|
||||
|
||||
ent->pos = new_pos;
|
||||
|
||||
if (demo3d_sim_check_ground(world, ent->pos, cfg->player_radius)) {
|
||||
ent->flags |= DEMO3D_SIM_FLAG_GROUNDED;
|
||||
if (ent->vel.y < 0.0f) ent->vel.y = 0.0f;
|
||||
} else {
|
||||
ent->flags &= ~DEMO3D_SIM_FLAG_GROUNDED;
|
||||
}
|
||||
}
|
||||
56
demo3d/client/sim/demo3d_sim.h
Normal file
56
demo3d/client/sim/demo3d_sim.h
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#pragma once
|
||||
|
||||
#include "demo3d_bsp.h"
|
||||
#include "pxl8_math.h"
|
||||
#include "demo3d_protocol.h"
|
||||
#include "pxl8_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DEMO3D_SIM_FLAG_ALIVE (1 << 0)
|
||||
#define DEMO3D_SIM_FLAG_PLAYER (1 << 1)
|
||||
#define DEMO3D_SIM_FLAG_GROUNDED (1 << 2)
|
||||
|
||||
typedef struct demo3d_sim_config {
|
||||
f32 move_speed;
|
||||
f32 ground_accel;
|
||||
f32 air_accel;
|
||||
f32 stop_speed;
|
||||
f32 friction;
|
||||
f32 gravity;
|
||||
f32 jump_velocity;
|
||||
f32 player_radius;
|
||||
f32 player_height;
|
||||
f32 max_pitch;
|
||||
} demo3d_sim_config;
|
||||
|
||||
typedef struct demo3d_sim_entity {
|
||||
pxl8_vec3 pos;
|
||||
pxl8_vec3 vel;
|
||||
f32 yaw;
|
||||
f32 pitch;
|
||||
u32 flags;
|
||||
u16 kind;
|
||||
u16 _pad;
|
||||
} demo3d_sim_entity;
|
||||
|
||||
typedef struct demo3d_sim_world {
|
||||
const demo3d_bsp* chunks[9];
|
||||
i32 center_cx;
|
||||
i32 center_cz;
|
||||
f32 chunk_size;
|
||||
} demo3d_sim_world;
|
||||
|
||||
bool demo3d_bsp_point_solid(const demo3d_bsp* bsp, pxl8_vec3 pos);
|
||||
pxl8_vec3 demo3d_bsp_trace(const demo3d_bsp* bsp, pxl8_vec3 from, pxl8_vec3 to, f32 radius);
|
||||
|
||||
pxl8_vec3 demo3d_sim_trace(const demo3d_sim_world* world, pxl8_vec3 from, pxl8_vec3 to, f32 radius, f32 height);
|
||||
bool demo3d_sim_check_ground(const demo3d_sim_world* world, pxl8_vec3 pos, f32 radius);
|
||||
void demo3d_sim_move_player(demo3d_sim_entity* ent, const demo3d_input_msg* input, const demo3d_sim_world* world, const demo3d_sim_config* cfg, f32 dt);
|
||||
void demo3d_sim_integrate(demo3d_sim_entity* ent, const demo3d_sim_world* world, const demo3d_sim_config* cfg, f32 dt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
17
demo3d/client/world/demo3d_chunk.c
Normal file
17
demo3d/client/world/demo3d_chunk.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include "demo3d_chunk.h"
|
||||
|
||||
#include "demo3d_bsp.h"
|
||||
#include "pxl8_mem.h"
|
||||
|
||||
demo3d_chunk* demo3d_chunk_create_bsp(u32 id) {
|
||||
demo3d_chunk* chunk = pxl8_calloc(1, sizeof(demo3d_chunk));
|
||||
if (!chunk) return NULL;
|
||||
chunk->id = id;
|
||||
return chunk;
|
||||
}
|
||||
|
||||
void demo3d_chunk_destroy(demo3d_chunk* chunk) {
|
||||
if (!chunk) return;
|
||||
if (chunk->bsp) demo3d_bsp_destroy(chunk->bsp);
|
||||
pxl8_free(chunk);
|
||||
}
|
||||
21
demo3d/client/world/demo3d_chunk.h
Normal file
21
demo3d/client/world/demo3d_chunk.h
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#include "demo3d_bsp.h"
|
||||
#include "pxl8_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct demo3d_chunk {
|
||||
u32 id;
|
||||
u32 version;
|
||||
demo3d_bsp* bsp;
|
||||
} demo3d_chunk;
|
||||
|
||||
demo3d_chunk* demo3d_chunk_create_bsp(u32 id);
|
||||
void demo3d_chunk_destroy(demo3d_chunk* chunk);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
401
demo3d/client/world/demo3d_chunk_cache.c
Normal file
401
demo3d/client/world/demo3d_chunk_cache.c
Normal file
|
|
@ -0,0 +1,401 @@
|
|||
#include "demo3d_chunk_cache.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "pxl8_bytes.h"
|
||||
#include "pxl8_log.h"
|
||||
#include "pxl8_mem.h"
|
||||
|
||||
static demo3d_chunk_cache_entry* find_entry_bsp(demo3d_chunk_cache* cache, u32 id) {
|
||||
for (u32 i = 0; i < cache->entry_count; i++) {
|
||||
demo3d_chunk_cache_entry* e = &cache->entries[i];
|
||||
if (e->valid && e->chunk && e->chunk->id == id) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static demo3d_chunk_cache_entry* alloc_entry(demo3d_chunk_cache* cache) {
|
||||
if (cache->entry_count < DEMO3D_CHUNK_CACHE_SIZE) {
|
||||
demo3d_chunk_cache_entry* e = &cache->entries[cache->entry_count++];
|
||||
memset(e, 0, sizeof(*e));
|
||||
return e;
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < DEMO3D_CHUNK_CACHE_SIZE; i++) {
|
||||
if (!cache->entries[i].valid) {
|
||||
demo3d_chunk_cache_entry* e = &cache->entries[i];
|
||||
memset(e, 0, sizeof(*e));
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
u64 oldest = cache->entries[0].last_used;
|
||||
u32 slot = 0;
|
||||
for (u32 i = 1; i < DEMO3D_CHUNK_CACHE_SIZE; i++) {
|
||||
if (cache->entries[i].last_used < oldest) {
|
||||
oldest = cache->entries[i].last_used;
|
||||
slot = i;
|
||||
}
|
||||
}
|
||||
|
||||
demo3d_chunk_cache_entry* e = &cache->entries[slot];
|
||||
if (e->chunk) {
|
||||
demo3d_chunk_destroy(e->chunk);
|
||||
}
|
||||
memset(e, 0, sizeof(*e));
|
||||
return e;
|
||||
}
|
||||
|
||||
static void assembly_reset(demo3d_chunk_assembly* a) {
|
||||
a->id = 0;
|
||||
a->cx = 0;
|
||||
a->cy = 0;
|
||||
a->cz = 0;
|
||||
a->version = 0;
|
||||
a->fragment_count = 0;
|
||||
a->fragments_received = 0;
|
||||
a->data_size = 0;
|
||||
a->active = false;
|
||||
a->complete = false;
|
||||
}
|
||||
|
||||
static void assembly_init(demo3d_chunk_assembly* a, const demo3d_chunk_msg_header* hdr) {
|
||||
assembly_reset(a);
|
||||
a->id = hdr->id;
|
||||
a->cx = hdr->cx;
|
||||
a->cy = hdr->cy;
|
||||
a->cz = hdr->cz;
|
||||
a->version = hdr->version;
|
||||
a->fragment_count = hdr->fragment_count;
|
||||
a->active = true;
|
||||
|
||||
u32 needed = DEMO3D_CHUNK_MAX_PAYLOAD * hdr->fragment_count;
|
||||
if (a->data_capacity < needed) {
|
||||
a->data_capacity = needed;
|
||||
a->data = pxl8_realloc(a->data, a->data_capacity);
|
||||
}
|
||||
}
|
||||
|
||||
static pxl8_result deserialize_vertex(pxl8_stream* s, demo3d_bsp_vertex* v) {
|
||||
v->position.x = pxl8_read_f32_be(s);
|
||||
v->position.y = pxl8_read_f32_be(s);
|
||||
v->position.z = pxl8_read_f32_be(s);
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result deserialize_edge(pxl8_stream* s, demo3d_bsp_edge* e) {
|
||||
e->vertex[0] = pxl8_read_u16_be(s);
|
||||
e->vertex[1] = pxl8_read_u16_be(s);
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result deserialize_plane(pxl8_stream* s, demo3d_bsp_plane* p) {
|
||||
p->normal.x = pxl8_read_f32_be(s);
|
||||
p->normal.y = pxl8_read_f32_be(s);
|
||||
p->normal.z = pxl8_read_f32_be(s);
|
||||
p->dist = pxl8_read_f32_be(s);
|
||||
p->type = (i32)pxl8_read_u32_be(s);
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result deserialize_face(pxl8_stream* s, demo3d_bsp_face* f) {
|
||||
f->first_edge = pxl8_read_u32_be(s);
|
||||
f->lightmap_offset = pxl8_read_u32_be(s);
|
||||
f->num_edges = pxl8_read_u16_be(s);
|
||||
f->plane_id = pxl8_read_u16_be(s);
|
||||
f->side = pxl8_read_u16_be(s);
|
||||
pxl8_read_bytes(s, f->styles, 4);
|
||||
f->material_id = pxl8_read_u16_be(s);
|
||||
f->aabb_min.x = pxl8_read_f32_be(s);
|
||||
f->aabb_min.y = pxl8_read_f32_be(s);
|
||||
f->aabb_min.z = pxl8_read_f32_be(s);
|
||||
f->aabb_max.x = pxl8_read_f32_be(s);
|
||||
f->aabb_max.y = pxl8_read_f32_be(s);
|
||||
f->aabb_max.z = pxl8_read_f32_be(s);
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result deserialize_node(pxl8_stream* s, demo3d_bsp_node* n) {
|
||||
n->children[0] = (i32)pxl8_read_u32_be(s);
|
||||
n->children[1] = (i32)pxl8_read_u32_be(s);
|
||||
n->first_face = pxl8_read_u16_be(s);
|
||||
n->maxs[0] = (i16)pxl8_read_u16_be(s);
|
||||
n->maxs[1] = (i16)pxl8_read_u16_be(s);
|
||||
n->maxs[2] = (i16)pxl8_read_u16_be(s);
|
||||
n->mins[0] = (i16)pxl8_read_u16_be(s);
|
||||
n->mins[1] = (i16)pxl8_read_u16_be(s);
|
||||
n->mins[2] = (i16)pxl8_read_u16_be(s);
|
||||
n->num_faces = pxl8_read_u16_be(s);
|
||||
n->plane_id = pxl8_read_u32_be(s);
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result deserialize_leaf(pxl8_stream* s, demo3d_bsp_leaf* l) {
|
||||
pxl8_read_bytes(s, l->ambient_level, 4);
|
||||
l->contents = (i32)pxl8_read_u32_be(s);
|
||||
l->first_marksurface = pxl8_read_u16_be(s);
|
||||
l->maxs[0] = (i16)pxl8_read_u16_be(s);
|
||||
l->maxs[1] = (i16)pxl8_read_u16_be(s);
|
||||
l->maxs[2] = (i16)pxl8_read_u16_be(s);
|
||||
l->mins[0] = (i16)pxl8_read_u16_be(s);
|
||||
l->mins[1] = (i16)pxl8_read_u16_be(s);
|
||||
l->mins[2] = (i16)pxl8_read_u16_be(s);
|
||||
l->num_marksurfaces = pxl8_read_u16_be(s);
|
||||
l->visofs = (i32)pxl8_read_u32_be(s);
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result deserialize_portal(pxl8_stream* s, demo3d_bsp_portal* p) {
|
||||
p->x0 = pxl8_read_f32_be(s);
|
||||
p->z0 = pxl8_read_f32_be(s);
|
||||
p->x1 = pxl8_read_f32_be(s);
|
||||
p->z1 = pxl8_read_f32_be(s);
|
||||
p->target_leaf = pxl8_read_u32_be(s);
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static pxl8_result deserialize_cell_portals(pxl8_stream* s, demo3d_bsp_cell_portals* cp) {
|
||||
cp->num_portals = pxl8_read_u8(s);
|
||||
pxl8_read_u8(s);
|
||||
pxl8_read_u8(s);
|
||||
pxl8_read_u8(s);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
deserialize_portal(s, &cp->portals[i]);
|
||||
}
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
static demo3d_bsp* assembly_to_bsp(demo3d_chunk_assembly* a) {
|
||||
if (!a->complete || a->data_size < 48) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
demo3d_bsp* bsp = pxl8_calloc(1, sizeof(demo3d_bsp));
|
||||
if (!bsp) return NULL;
|
||||
|
||||
pxl8_stream s = pxl8_stream_create(a->data, (u32)a->data_size);
|
||||
|
||||
demo3d_bsp_wire_header wire_hdr;
|
||||
demo3d_protocol_deserialize_bsp_wire_header(a->data, 48, &wire_hdr);
|
||||
s.offset = 48;
|
||||
|
||||
if (wire_hdr.num_vertices > 0) {
|
||||
bsp->vertices = pxl8_calloc(wire_hdr.num_vertices, sizeof(demo3d_bsp_vertex));
|
||||
bsp->num_vertices = wire_hdr.num_vertices;
|
||||
for (u32 i = 0; i < wire_hdr.num_vertices; i++) {
|
||||
deserialize_vertex(&s, &bsp->vertices[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.num_edges > 0) {
|
||||
bsp->edges = pxl8_calloc(wire_hdr.num_edges, sizeof(demo3d_bsp_edge));
|
||||
bsp->num_edges = wire_hdr.num_edges;
|
||||
for (u32 i = 0; i < wire_hdr.num_edges; i++) {
|
||||
deserialize_edge(&s, &bsp->edges[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.num_surfedges > 0) {
|
||||
bsp->surfedges = pxl8_calloc(wire_hdr.num_surfedges, sizeof(i32));
|
||||
bsp->num_surfedges = wire_hdr.num_surfedges;
|
||||
for (u32 i = 0; i < wire_hdr.num_surfedges; i++) {
|
||||
bsp->surfedges[i] = (i32)pxl8_read_u32_be(&s);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.num_planes > 0) {
|
||||
bsp->planes = pxl8_calloc(wire_hdr.num_planes, sizeof(demo3d_bsp_plane));
|
||||
bsp->num_planes = wire_hdr.num_planes;
|
||||
for (u32 i = 0; i < wire_hdr.num_planes; i++) {
|
||||
deserialize_plane(&s, &bsp->planes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.num_faces > 0) {
|
||||
bsp->faces = pxl8_calloc(wire_hdr.num_faces, sizeof(demo3d_bsp_face));
|
||||
bsp->num_faces = wire_hdr.num_faces;
|
||||
for (u32 i = 0; i < wire_hdr.num_faces; i++) {
|
||||
deserialize_face(&s, &bsp->faces[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.num_nodes > 0) {
|
||||
bsp->nodes = pxl8_calloc(wire_hdr.num_nodes, sizeof(demo3d_bsp_node));
|
||||
bsp->num_nodes = wire_hdr.num_nodes;
|
||||
for (u32 i = 0; i < wire_hdr.num_nodes; i++) {
|
||||
deserialize_node(&s, &bsp->nodes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.num_leafs > 0) {
|
||||
bsp->leafs = pxl8_calloc(wire_hdr.num_leafs, sizeof(demo3d_bsp_leaf));
|
||||
bsp->num_leafs = wire_hdr.num_leafs;
|
||||
for (u32 i = 0; i < wire_hdr.num_leafs; i++) {
|
||||
deserialize_leaf(&s, &bsp->leafs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.num_marksurfaces > 0) {
|
||||
bsp->marksurfaces = pxl8_calloc(wire_hdr.num_marksurfaces, sizeof(u16));
|
||||
bsp->num_marksurfaces = wire_hdr.num_marksurfaces;
|
||||
for (u32 i = 0; i < wire_hdr.num_marksurfaces; i++) {
|
||||
bsp->marksurfaces[i] = pxl8_read_u16_be(&s);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.num_cell_portals > 0) {
|
||||
bsp->cell_portals = pxl8_calloc(wire_hdr.num_cell_portals, sizeof(demo3d_bsp_cell_portals));
|
||||
bsp->num_cell_portals = wire_hdr.num_cell_portals;
|
||||
for (u32 i = 0; i < wire_hdr.num_cell_portals; i++) {
|
||||
deserialize_cell_portals(&s, &bsp->cell_portals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.visdata_size > 0) {
|
||||
bsp->visdata = pxl8_malloc(wire_hdr.visdata_size);
|
||||
bsp->visdata_size = wire_hdr.visdata_size;
|
||||
pxl8_read_bytes(&s, bsp->visdata, wire_hdr.visdata_size);
|
||||
}
|
||||
|
||||
if (wire_hdr.num_vertex_lights > 0) {
|
||||
bsp->vertex_lights = pxl8_calloc(wire_hdr.num_vertex_lights, sizeof(u32));
|
||||
bsp->num_vertex_lights = wire_hdr.num_vertex_lights;
|
||||
for (u32 i = 0; i < wire_hdr.num_vertex_lights; i++) {
|
||||
bsp->vertex_lights[i] = pxl8_read_u32_be(&s);
|
||||
}
|
||||
}
|
||||
|
||||
if (wire_hdr.num_heightfield > 0) {
|
||||
bsp->heightfield = pxl8_calloc(wire_hdr.num_heightfield, sizeof(f32));
|
||||
bsp->num_heightfield = wire_hdr.num_heightfield;
|
||||
for (u32 i = 0; i < wire_hdr.num_heightfield; i++) {
|
||||
u32 raw = pxl8_read_u32_be(&s);
|
||||
memcpy(&bsp->heightfield[i], &raw, sizeof(f32));
|
||||
}
|
||||
bsp->heightfield_w = pxl8_read_u16_be(&s);
|
||||
bsp->heightfield_h = pxl8_read_u16_be(&s);
|
||||
u32 ox_raw = pxl8_read_u32_be(&s);
|
||||
memcpy(&bsp->heightfield_ox, &ox_raw, sizeof(f32));
|
||||
u32 oz_raw = pxl8_read_u32_be(&s);
|
||||
memcpy(&bsp->heightfield_oz, &oz_raw, sizeof(f32));
|
||||
u32 cs_raw = pxl8_read_u32_be(&s);
|
||||
memcpy(&bsp->heightfield_cell_size, &cs_raw, sizeof(f32));
|
||||
}
|
||||
|
||||
u32 raw;
|
||||
raw = pxl8_read_u32_be(&s);
|
||||
memcpy(&bsp->bounds_min_x, &raw, sizeof(f32));
|
||||
raw = pxl8_read_u32_be(&s);
|
||||
memcpy(&bsp->bounds_min_z, &raw, sizeof(f32));
|
||||
raw = pxl8_read_u32_be(&s);
|
||||
memcpy(&bsp->bounds_max_x, &raw, sizeof(f32));
|
||||
raw = pxl8_read_u32_be(&s);
|
||||
memcpy(&bsp->bounds_max_z, &raw, sizeof(f32));
|
||||
|
||||
return bsp;
|
||||
}
|
||||
|
||||
static pxl8_result assemble_bsp(demo3d_chunk_cache* cache, demo3d_chunk_assembly* a) {
|
||||
demo3d_bsp* bsp = assembly_to_bsp(a);
|
||||
if (!bsp) {
|
||||
assembly_reset(a);
|
||||
return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
demo3d_chunk_cache_entry* entry = find_entry_bsp(cache, a->id);
|
||||
if (entry) {
|
||||
if (entry->chunk && entry->chunk->bsp) {
|
||||
demo3d_bsp_destroy(entry->chunk->bsp);
|
||||
}
|
||||
} else {
|
||||
entry = alloc_entry(cache);
|
||||
entry->chunk = demo3d_chunk_create_bsp(a->id);
|
||||
entry->valid = true;
|
||||
}
|
||||
|
||||
entry->chunk->bsp = bsp;
|
||||
entry->chunk->version = a->version;
|
||||
entry->last_used = cache->frame_counter;
|
||||
|
||||
assembly_reset(a);
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
demo3d_chunk_cache* demo3d_chunk_cache_create(void) {
|
||||
demo3d_chunk_cache* cache = pxl8_calloc(1, sizeof(demo3d_chunk_cache));
|
||||
if (!cache) return NULL;
|
||||
assembly_reset(&cache->assembly);
|
||||
return cache;
|
||||
}
|
||||
|
||||
void demo3d_chunk_cache_destroy(demo3d_chunk_cache* cache) {
|
||||
if (!cache) return;
|
||||
|
||||
for (u32 i = 0; i < cache->entry_count; i++) {
|
||||
demo3d_chunk_cache_entry* e = &cache->entries[i];
|
||||
if (e->chunk) demo3d_chunk_destroy(e->chunk);
|
||||
}
|
||||
|
||||
pxl8_free(cache->assembly.data);
|
||||
pxl8_free(cache);
|
||||
}
|
||||
|
||||
pxl8_result demo3d_chunk_cache_receive(demo3d_chunk_cache* cache,
|
||||
const demo3d_chunk_msg_header* hdr,
|
||||
const u8* payload, usize len) {
|
||||
if (!cache || !hdr || !payload) return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
|
||||
demo3d_chunk_assembly* a = &cache->assembly;
|
||||
|
||||
bool new_assembly = !a->active ||
|
||||
a->id != hdr->id ||
|
||||
a->version != hdr->version;
|
||||
|
||||
if (new_assembly) {
|
||||
assembly_init(a, hdr);
|
||||
}
|
||||
|
||||
if (hdr->fragment_idx >= DEMO3D_CHUNK_MAX_FRAGMENTS) {
|
||||
return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
u32 offset = (u32)hdr->fragment_idx * DEMO3D_CHUNK_MAX_PAYLOAD;
|
||||
u32 required = offset + (u32)len;
|
||||
|
||||
if (required > a->data_capacity) {
|
||||
return PXL8_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
memcpy(a->data + offset, payload, len);
|
||||
|
||||
if (required > a->data_size) {
|
||||
a->data_size = required;
|
||||
}
|
||||
|
||||
a->fragments_received++;
|
||||
|
||||
if (hdr->flags & DEMO3D_CHUNK_FLAG_FINAL) {
|
||||
a->complete = true;
|
||||
return assemble_bsp(cache, a);
|
||||
}
|
||||
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
demo3d_chunk* demo3d_chunk_cache_get_bsp(demo3d_chunk_cache* cache, u32 id) {
|
||||
if (!cache) return NULL;
|
||||
demo3d_chunk_cache_entry* e = find_entry_bsp(cache, id);
|
||||
if (e) {
|
||||
e->last_used = cache->frame_counter;
|
||||
return e->chunk;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void demo3d_chunk_cache_tick(demo3d_chunk_cache* cache) {
|
||||
if (!cache) return;
|
||||
cache->frame_counter++;
|
||||
}
|
||||
55
demo3d/client/world/demo3d_chunk_cache.h
Normal file
55
demo3d/client/world/demo3d_chunk_cache.h
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#pragma once
|
||||
|
||||
#include "pxl8_mesh.h"
|
||||
#include "demo3d_protocol.h"
|
||||
#include "pxl8_types.h"
|
||||
#include "demo3d_chunk.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DEMO3D_CHUNK_CACHE_SIZE 512
|
||||
#define DEMO3D_CHUNK_MAX_FRAGMENTS 255
|
||||
#define DEMO3D_CHUNK_MAX_DATA_SIZE 131072
|
||||
|
||||
typedef struct demo3d_chunk_cache_entry {
|
||||
demo3d_chunk* chunk;
|
||||
u64 last_used;
|
||||
bool valid;
|
||||
} demo3d_chunk_cache_entry;
|
||||
|
||||
typedef struct demo3d_chunk_assembly {
|
||||
u32 id;
|
||||
i32 cx, cy, cz;
|
||||
u32 version;
|
||||
u8 fragment_count;
|
||||
u8 fragments_received;
|
||||
u8* data;
|
||||
u32 data_size;
|
||||
u32 data_capacity;
|
||||
bool active;
|
||||
bool complete;
|
||||
} demo3d_chunk_assembly;
|
||||
|
||||
typedef struct demo3d_chunk_cache {
|
||||
demo3d_chunk_cache_entry entries[DEMO3D_CHUNK_CACHE_SIZE];
|
||||
demo3d_chunk_assembly assembly;
|
||||
u32 entry_count;
|
||||
u64 frame_counter;
|
||||
} demo3d_chunk_cache;
|
||||
|
||||
demo3d_chunk_cache* demo3d_chunk_cache_create(void);
|
||||
void demo3d_chunk_cache_destroy(demo3d_chunk_cache* cache);
|
||||
|
||||
pxl8_result demo3d_chunk_cache_receive(demo3d_chunk_cache* cache,
|
||||
const demo3d_chunk_msg_header* hdr,
|
||||
const u8* payload, usize len);
|
||||
|
||||
demo3d_chunk* demo3d_chunk_cache_get_bsp(demo3d_chunk_cache* cache, u32 id);
|
||||
|
||||
void demo3d_chunk_cache_tick(demo3d_chunk_cache* cache);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
496
demo3d/client/world/demo3d_entity.c
Normal file
496
demo3d/client/world/demo3d_entity.c
Normal file
|
|
@ -0,0 +1,496 @@
|
|||
#include "demo3d_entity.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "pxl8_mem.h"
|
||||
|
||||
#define DEMO3D_ENTITY_COMPONENT_NAME_MAX 32
|
||||
#define DEMO3D_ENTITY_RELATIONSHIP_NAME_MAX 32
|
||||
|
||||
typedef struct demo3d_component_type {
|
||||
char name[DEMO3D_ENTITY_COMPONENT_NAME_MAX];
|
||||
u32 size;
|
||||
} demo3d_component_type;
|
||||
|
||||
typedef struct demo3d_component_storage {
|
||||
u32* sparse;
|
||||
void* dense_data;
|
||||
demo3d_entity* dense_entities;
|
||||
u32 count;
|
||||
} demo3d_component_storage;
|
||||
|
||||
typedef struct demo3d_relationship_type {
|
||||
char name[DEMO3D_ENTITY_RELATIONSHIP_NAME_MAX];
|
||||
} demo3d_relationship_type;
|
||||
|
||||
typedef struct demo3d_relationship_entry {
|
||||
demo3d_entity subject;
|
||||
demo3d_entity object;
|
||||
demo3d_entity_relationship rel;
|
||||
u32 next_by_subject;
|
||||
u32 next_by_object;
|
||||
} demo3d_relationship_entry;
|
||||
|
||||
struct demo3d_entity_pool {
|
||||
u32* generations;
|
||||
u32* free_list;
|
||||
u32 free_count;
|
||||
u32 capacity;
|
||||
u32 alive_count;
|
||||
|
||||
demo3d_component_type* component_types;
|
||||
demo3d_component_storage* component_storage;
|
||||
u32 component_type_count;
|
||||
u32 component_type_capacity;
|
||||
|
||||
demo3d_relationship_type* relationship_types;
|
||||
u32 relationship_type_count;
|
||||
u32 relationship_type_capacity;
|
||||
|
||||
demo3d_relationship_entry* relationships;
|
||||
u32* rel_by_subject;
|
||||
u32* rel_by_object;
|
||||
u32 relationship_count;
|
||||
u32 relationship_capacity;
|
||||
};
|
||||
|
||||
demo3d_entity_pool* demo3d_entity_pool_create(u32 capacity) {
|
||||
demo3d_entity_pool* pool = pxl8_calloc(1, sizeof(demo3d_entity_pool));
|
||||
if (!pool) return NULL;
|
||||
|
||||
pool->capacity = capacity;
|
||||
pool->generations = pxl8_calloc(capacity, sizeof(u32));
|
||||
pool->free_list = pxl8_malloc(capacity * sizeof(u32));
|
||||
|
||||
if (!pool->generations || !pool->free_list) {
|
||||
demo3d_entity_pool_destroy(pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < capacity; i++) {
|
||||
pool->free_list[i] = capacity - 1 - i;
|
||||
}
|
||||
pool->free_count = capacity;
|
||||
|
||||
pool->component_type_capacity = 16;
|
||||
pool->component_types = pxl8_calloc(pool->component_type_capacity, sizeof(demo3d_component_type));
|
||||
pool->component_storage = pxl8_calloc(pool->component_type_capacity, sizeof(demo3d_component_storage));
|
||||
|
||||
pool->relationship_type_capacity = 16;
|
||||
pool->relationship_types = pxl8_calloc(pool->relationship_type_capacity, sizeof(demo3d_relationship_type));
|
||||
|
||||
pool->relationship_capacity = 256;
|
||||
pool->relationships = pxl8_malloc(pool->relationship_capacity * sizeof(demo3d_relationship_entry));
|
||||
pool->rel_by_subject = pxl8_malloc(capacity * sizeof(u32));
|
||||
pool->rel_by_object = pxl8_malloc(capacity * sizeof(u32));
|
||||
|
||||
for (u32 i = 0; i < capacity; i++) {
|
||||
pool->rel_by_subject[i] = UINT32_MAX;
|
||||
pool->rel_by_object[i] = UINT32_MAX;
|
||||
}
|
||||
|
||||
return pool;
|
||||
}
|
||||
|
||||
void demo3d_entity_pool_clear(demo3d_entity_pool* pool) {
|
||||
if (!pool) return;
|
||||
|
||||
for (u32 i = 0; i < pool->capacity; i++) {
|
||||
pool->generations[i] = 0;
|
||||
pool->free_list[i] = pool->capacity - 1 - i;
|
||||
pool->rel_by_subject[i] = UINT32_MAX;
|
||||
pool->rel_by_object[i] = UINT32_MAX;
|
||||
}
|
||||
pool->free_count = pool->capacity;
|
||||
pool->alive_count = 0;
|
||||
|
||||
for (u32 i = 0; i < pool->component_type_count; i++) {
|
||||
pool->component_storage[i].count = 0;
|
||||
}
|
||||
|
||||
pool->relationship_count = 0;
|
||||
}
|
||||
|
||||
void demo3d_entity_pool_destroy(demo3d_entity_pool* pool) {
|
||||
if (!pool) return;
|
||||
|
||||
for (u32 i = 0; i < pool->component_type_count; i++) {
|
||||
pxl8_free(pool->component_storage[i].sparse);
|
||||
pxl8_free(pool->component_storage[i].dense_data);
|
||||
pxl8_free(pool->component_storage[i].dense_entities);
|
||||
}
|
||||
|
||||
pxl8_free(pool->component_types);
|
||||
pxl8_free(pool->component_storage);
|
||||
pxl8_free(pool->relationship_types);
|
||||
pxl8_free(pool->relationships);
|
||||
pxl8_free(pool->rel_by_subject);
|
||||
pxl8_free(pool->rel_by_object);
|
||||
pxl8_free(pool->generations);
|
||||
pxl8_free(pool->free_list);
|
||||
pxl8_free(pool);
|
||||
}
|
||||
|
||||
demo3d_entity demo3d_entity_spawn(demo3d_entity_pool* pool) {
|
||||
if (!pool || pool->free_count == 0) return DEMO3D_ENTITY_INVALID;
|
||||
|
||||
u32 idx = pool->free_list[--pool->free_count];
|
||||
pool->generations[idx]++;
|
||||
pool->alive_count++;
|
||||
|
||||
return (demo3d_entity){ .idx = idx, .gen = pool->generations[idx] };
|
||||
}
|
||||
|
||||
void demo3d_entity_despawn(demo3d_entity_pool* pool, demo3d_entity e) {
|
||||
if (!pool || !demo3d_entity_alive(pool, e)) return;
|
||||
|
||||
for (u32 i = 0; i < pool->component_type_count; i++) {
|
||||
demo3d_entity_component_remove(pool, e, i + 1);
|
||||
}
|
||||
|
||||
pool->free_list[pool->free_count++] = e.idx;
|
||||
pool->generations[e.idx]++;
|
||||
pool->alive_count--;
|
||||
}
|
||||
|
||||
bool demo3d_entity_alive(const demo3d_entity_pool* pool, demo3d_entity e) {
|
||||
if (!pool || e.idx >= pool->capacity) return false;
|
||||
return pool->generations[e.idx] == e.gen && e.gen != 0;
|
||||
}
|
||||
|
||||
u32 demo3d_entity_count(const demo3d_entity_pool* pool) {
|
||||
return pool ? pool->alive_count : 0;
|
||||
}
|
||||
|
||||
demo3d_entity_component demo3d_entity_component_register(demo3d_entity_pool* pool, const char* name, u32 size) {
|
||||
if (!pool || !name || size == 0) return DEMO3D_ENTITY_COMPONENT_INVALID;
|
||||
|
||||
demo3d_entity_component existing = demo3d_entity_component_find(pool, name);
|
||||
if (existing != DEMO3D_ENTITY_COMPONENT_INVALID) return existing;
|
||||
|
||||
if (pool->component_type_count >= pool->component_type_capacity) {
|
||||
u32 new_capacity = pool->component_type_capacity * 2;
|
||||
demo3d_component_type* new_types = pxl8_realloc(pool->component_types, new_capacity * sizeof(demo3d_component_type));
|
||||
demo3d_component_storage* new_storage = pxl8_realloc(pool->component_storage, new_capacity * sizeof(demo3d_component_storage));
|
||||
if (!new_types || !new_storage) return DEMO3D_ENTITY_COMPONENT_INVALID;
|
||||
|
||||
pool->component_types = new_types;
|
||||
pool->component_storage = new_storage;
|
||||
memset(&pool->component_types[pool->component_type_capacity], 0, (new_capacity - pool->component_type_capacity) * sizeof(demo3d_component_type));
|
||||
memset(&pool->component_storage[pool->component_type_capacity], 0, (new_capacity - pool->component_type_capacity) * sizeof(demo3d_component_storage));
|
||||
pool->component_type_capacity = new_capacity;
|
||||
}
|
||||
|
||||
u32 type_idx = pool->component_type_count++;
|
||||
strncpy(pool->component_types[type_idx].name, name, DEMO3D_ENTITY_COMPONENT_NAME_MAX - 1);
|
||||
pool->component_types[type_idx].size = size;
|
||||
|
||||
demo3d_component_storage* storage = &pool->component_storage[type_idx];
|
||||
storage->sparse = pxl8_malloc(pool->capacity * sizeof(u32));
|
||||
storage->dense_data = pxl8_malloc(pool->capacity * size);
|
||||
storage->dense_entities = pxl8_malloc(pool->capacity * sizeof(demo3d_entity));
|
||||
storage->count = 0;
|
||||
|
||||
for (u32 i = 0; i < pool->capacity; i++) {
|
||||
storage->sparse[i] = UINT32_MAX;
|
||||
}
|
||||
|
||||
return type_idx + 1;
|
||||
}
|
||||
|
||||
demo3d_entity_component demo3d_entity_component_find(const demo3d_entity_pool* pool, const char* name) {
|
||||
if (!pool || !name) return DEMO3D_ENTITY_COMPONENT_INVALID;
|
||||
|
||||
for (u32 i = 0; i < pool->component_type_count; i++) {
|
||||
if (strcmp(pool->component_types[i].name, name) == 0) {
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
return DEMO3D_ENTITY_COMPONENT_INVALID;
|
||||
}
|
||||
|
||||
const char* demo3d_entity_component_name(const demo3d_entity_pool* pool, demo3d_entity_component comp) {
|
||||
if (!pool || comp == 0 || comp > pool->component_type_count) return NULL;
|
||||
return pool->component_types[comp - 1].name;
|
||||
}
|
||||
|
||||
void* demo3d_entity_component_add(demo3d_entity_pool* pool, demo3d_entity e, demo3d_entity_component comp) {
|
||||
if (!pool || !demo3d_entity_alive(pool, e)) return NULL;
|
||||
if (comp == 0 || comp > pool->component_type_count) return NULL;
|
||||
|
||||
u32 type_idx = comp - 1;
|
||||
demo3d_component_storage* storage = &pool->component_storage[type_idx];
|
||||
u32 size = pool->component_types[type_idx].size;
|
||||
|
||||
if (storage->sparse[e.idx] != UINT32_MAX) {
|
||||
return (u8*)storage->dense_data + storage->sparse[e.idx] * size;
|
||||
}
|
||||
|
||||
u32 dense_idx = storage->count++;
|
||||
storage->sparse[e.idx] = dense_idx;
|
||||
storage->dense_entities[dense_idx] = e;
|
||||
|
||||
void* data = (u8*)storage->dense_data + dense_idx * size;
|
||||
memset(data, 0, size);
|
||||
return data;
|
||||
}
|
||||
|
||||
void* demo3d_entity_component_get(const demo3d_entity_pool* pool, demo3d_entity e, demo3d_entity_component comp) {
|
||||
if (!pool || !demo3d_entity_alive(pool, e)) return NULL;
|
||||
if (comp == 0 || comp > pool->component_type_count) return NULL;
|
||||
|
||||
u32 type_idx = comp - 1;
|
||||
const demo3d_component_storage* storage = &pool->component_storage[type_idx];
|
||||
|
||||
if (storage->sparse[e.idx] == UINT32_MAX) return NULL;
|
||||
|
||||
u32 size = pool->component_types[type_idx].size;
|
||||
return (u8*)storage->dense_data + storage->sparse[e.idx] * size;
|
||||
}
|
||||
|
||||
void demo3d_entity_component_remove(demo3d_entity_pool* pool, demo3d_entity e, demo3d_entity_component comp) {
|
||||
if (!pool || !demo3d_entity_alive(pool, e)) return;
|
||||
if (comp == 0 || comp > pool->component_type_count) return;
|
||||
|
||||
u32 type_idx = comp - 1;
|
||||
demo3d_component_storage* storage = &pool->component_storage[type_idx];
|
||||
u32 size = pool->component_types[type_idx].size;
|
||||
|
||||
u32 dense_idx = storage->sparse[e.idx];
|
||||
if (dense_idx == UINT32_MAX) return;
|
||||
|
||||
u32 last_idx = storage->count - 1;
|
||||
if (dense_idx != last_idx) {
|
||||
demo3d_entity last_entity = storage->dense_entities[last_idx];
|
||||
memcpy((u8*)storage->dense_data + dense_idx * size,
|
||||
(u8*)storage->dense_data + last_idx * size, size);
|
||||
storage->dense_entities[dense_idx] = last_entity;
|
||||
storage->sparse[last_entity.idx] = dense_idx;
|
||||
}
|
||||
|
||||
storage->sparse[e.idx] = UINT32_MAX;
|
||||
storage->count--;
|
||||
}
|
||||
|
||||
bool demo3d_entity_component_has(const demo3d_entity_pool* pool, demo3d_entity e, demo3d_entity_component comp) {
|
||||
if (!pool || !demo3d_entity_alive(pool, e)) return false;
|
||||
if (comp == 0 || comp > pool->component_type_count) return false;
|
||||
|
||||
return pool->component_storage[comp - 1].sparse[e.idx] != UINT32_MAX;
|
||||
}
|
||||
|
||||
demo3d_entity_relationship demo3d_entity_relationship_register(demo3d_entity_pool* pool, const char* name) {
|
||||
if (!pool || !name) return DEMO3D_ENTITY_RELATIONSHIP_INVALID;
|
||||
|
||||
demo3d_entity_relationship existing = demo3d_entity_relationship_find(pool, name);
|
||||
if (existing != DEMO3D_ENTITY_RELATIONSHIP_INVALID) return existing;
|
||||
|
||||
if (pool->relationship_type_count >= pool->relationship_type_capacity) {
|
||||
u32 new_capacity = pool->relationship_type_capacity * 2;
|
||||
demo3d_relationship_type* new_types = pxl8_realloc(pool->relationship_types, new_capacity * sizeof(demo3d_relationship_type));
|
||||
if (!new_types) return DEMO3D_ENTITY_RELATIONSHIP_INVALID;
|
||||
|
||||
pool->relationship_types = new_types;
|
||||
memset(&pool->relationship_types[pool->relationship_type_capacity], 0, (new_capacity - pool->relationship_type_capacity) * sizeof(demo3d_relationship_type));
|
||||
pool->relationship_type_capacity = new_capacity;
|
||||
}
|
||||
|
||||
u32 type_idx = pool->relationship_type_count++;
|
||||
strncpy(pool->relationship_types[type_idx].name, name, DEMO3D_ENTITY_RELATIONSHIP_NAME_MAX - 1);
|
||||
|
||||
return type_idx + 1;
|
||||
}
|
||||
|
||||
demo3d_entity_relationship demo3d_entity_relationship_find(const demo3d_entity_pool* pool, const char* name) {
|
||||
if (!pool || !name) return DEMO3D_ENTITY_RELATIONSHIP_INVALID;
|
||||
|
||||
for (u32 i = 0; i < pool->relationship_type_count; i++) {
|
||||
if (strcmp(pool->relationship_types[i].name, name) == 0) {
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
return DEMO3D_ENTITY_RELATIONSHIP_INVALID;
|
||||
}
|
||||
|
||||
const char* demo3d_entity_relationship_name(const demo3d_entity_pool* pool, demo3d_entity_relationship rel) {
|
||||
if (!pool || rel == 0 || rel > pool->relationship_type_count) return NULL;
|
||||
return pool->relationship_types[rel - 1].name;
|
||||
}
|
||||
|
||||
void demo3d_entity_relationship_add(demo3d_entity_pool* pool, demo3d_entity subject, demo3d_entity_relationship rel, demo3d_entity object) {
|
||||
if (!pool) return;
|
||||
if (!demo3d_entity_alive(pool, subject) || !demo3d_entity_alive(pool, object)) return;
|
||||
if (rel == 0 || rel > pool->relationship_type_count) return;
|
||||
if (demo3d_entity_relationship_has(pool, subject, rel, object)) return;
|
||||
|
||||
if (pool->relationship_count >= pool->relationship_capacity) {
|
||||
u32 new_capacity = pool->relationship_capacity * 2;
|
||||
demo3d_relationship_entry* new_rels = pxl8_realloc(pool->relationships, new_capacity * sizeof(demo3d_relationship_entry));
|
||||
if (!new_rels) return;
|
||||
pool->relationships = new_rels;
|
||||
pool->relationship_capacity = new_capacity;
|
||||
}
|
||||
|
||||
u32 entry_idx = pool->relationship_count++;
|
||||
demo3d_relationship_entry* entry = &pool->relationships[entry_idx];
|
||||
entry->subject = subject;
|
||||
entry->object = object;
|
||||
entry->rel = rel;
|
||||
|
||||
entry->next_by_subject = pool->rel_by_subject[subject.idx];
|
||||
pool->rel_by_subject[subject.idx] = entry_idx;
|
||||
|
||||
entry->next_by_object = pool->rel_by_object[object.idx];
|
||||
pool->rel_by_object[object.idx] = entry_idx;
|
||||
}
|
||||
|
||||
void demo3d_entity_relationship_remove(demo3d_entity_pool* pool, demo3d_entity subject, demo3d_entity_relationship rel, demo3d_entity object) {
|
||||
if (!pool) return;
|
||||
if (rel == 0 || rel > pool->relationship_type_count) return;
|
||||
|
||||
u32* prev_ptr = &pool->rel_by_subject[subject.idx];
|
||||
u32 idx = *prev_ptr;
|
||||
|
||||
while (idx != UINT32_MAX) {
|
||||
demo3d_relationship_entry* entry = &pool->relationships[idx];
|
||||
if (demo3d_entity_eq(entry->subject, subject) &&
|
||||
demo3d_entity_eq(entry->object, object) &&
|
||||
entry->rel == rel) {
|
||||
|
||||
*prev_ptr = entry->next_by_subject;
|
||||
|
||||
u32* obj_prev = &pool->rel_by_object[object.idx];
|
||||
while (*obj_prev != UINT32_MAX) {
|
||||
if (*obj_prev == idx) {
|
||||
*obj_prev = entry->next_by_object;
|
||||
break;
|
||||
}
|
||||
obj_prev = &pool->relationships[*obj_prev].next_by_object;
|
||||
}
|
||||
|
||||
if (idx != pool->relationship_count - 1) {
|
||||
u32 last_idx = pool->relationship_count - 1;
|
||||
demo3d_relationship_entry* last = &pool->relationships[last_idx];
|
||||
|
||||
u32* last_subj_prev = &pool->rel_by_subject[last->subject.idx];
|
||||
while (*last_subj_prev != UINT32_MAX) {
|
||||
if (*last_subj_prev == last_idx) {
|
||||
*last_subj_prev = idx;
|
||||
break;
|
||||
}
|
||||
last_subj_prev = &pool->relationships[*last_subj_prev].next_by_subject;
|
||||
}
|
||||
|
||||
u32* last_obj_prev = &pool->rel_by_object[last->object.idx];
|
||||
while (*last_obj_prev != UINT32_MAX) {
|
||||
if (*last_obj_prev == last_idx) {
|
||||
*last_obj_prev = idx;
|
||||
break;
|
||||
}
|
||||
last_obj_prev = &pool->relationships[*last_obj_prev].next_by_object;
|
||||
}
|
||||
|
||||
*entry = *last;
|
||||
}
|
||||
pool->relationship_count--;
|
||||
return;
|
||||
}
|
||||
prev_ptr = &entry->next_by_subject;
|
||||
idx = *prev_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool demo3d_entity_relationship_has(const demo3d_entity_pool* pool, demo3d_entity subject, demo3d_entity_relationship rel, demo3d_entity object) {
|
||||
if (!pool) return false;
|
||||
if (rel == 0 || rel > pool->relationship_type_count) return false;
|
||||
|
||||
u32 idx = pool->rel_by_subject[subject.idx];
|
||||
while (idx != UINT32_MAX) {
|
||||
const demo3d_relationship_entry* entry = &pool->relationships[idx];
|
||||
if (demo3d_entity_eq(entry->subject, subject) &&
|
||||
demo3d_entity_eq(entry->object, object) &&
|
||||
entry->rel == rel) {
|
||||
return true;
|
||||
}
|
||||
idx = entry->next_by_subject;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
u32 demo3d_entity_relationship_subjects(const demo3d_entity_pool* pool, demo3d_entity object, demo3d_entity_relationship rel, demo3d_entity* out, u32 max) {
|
||||
if (!pool || !out || max == 0) return 0;
|
||||
if (rel == 0 || rel > pool->relationship_type_count) return 0;
|
||||
|
||||
u32 count = 0;
|
||||
u32 idx = pool->rel_by_object[object.idx];
|
||||
while (idx != UINT32_MAX && count < max) {
|
||||
const demo3d_relationship_entry* entry = &pool->relationships[idx];
|
||||
if (demo3d_entity_eq(entry->object, object) && entry->rel == rel) {
|
||||
out[count++] = entry->subject;
|
||||
}
|
||||
idx = entry->next_by_object;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
u32 demo3d_entity_relationship_objects(const demo3d_entity_pool* pool, demo3d_entity subject, demo3d_entity_relationship rel, demo3d_entity* out, u32 max) {
|
||||
if (!pool || !out || max == 0) return 0;
|
||||
if (rel == 0 || rel > pool->relationship_type_count) return 0;
|
||||
|
||||
u32 count = 0;
|
||||
u32 idx = pool->rel_by_subject[subject.idx];
|
||||
while (idx != UINT32_MAX && count < max) {
|
||||
const demo3d_relationship_entry* entry = &pool->relationships[idx];
|
||||
if (demo3d_entity_eq(entry->subject, subject) && entry->rel == rel) {
|
||||
out[count++] = entry->object;
|
||||
}
|
||||
idx = entry->next_by_subject;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void demo3d_entity_each(demo3d_entity_pool* pool, demo3d_entity_component comp, demo3d_entity_each_fn fn, void* ctx) {
|
||||
if (!pool || !fn) return;
|
||||
if (comp == 0 || comp > pool->component_type_count) return;
|
||||
|
||||
demo3d_component_storage* storage = &pool->component_storage[comp - 1];
|
||||
for (u32 i = 0; i < storage->count; i++) {
|
||||
demo3d_entity e = storage->dense_entities[i];
|
||||
if (demo3d_entity_alive(pool, e)) {
|
||||
fn(pool, e, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void demo3d_entity_each_with(demo3d_entity_pool* pool, const demo3d_entity_component* comps, u32 count, demo3d_entity_each_fn fn, void* ctx) {
|
||||
if (!pool || !comps || !fn || count == 0) return;
|
||||
|
||||
u32 smallest_idx = 0;
|
||||
u32 smallest_count = UINT32_MAX;
|
||||
|
||||
for (u32 i = 0; i < count; i++) {
|
||||
if (comps[i] == 0 || comps[i] > pool->component_type_count) return;
|
||||
u32 storage_count = pool->component_storage[comps[i] - 1].count;
|
||||
if (storage_count < smallest_count) {
|
||||
smallest_count = storage_count;
|
||||
smallest_idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
demo3d_component_storage* storage = &pool->component_storage[comps[smallest_idx] - 1];
|
||||
for (u32 i = 0; i < storage->count; i++) {
|
||||
demo3d_entity e = storage->dense_entities[i];
|
||||
if (!demo3d_entity_alive(pool, e)) continue;
|
||||
|
||||
bool has_all = true;
|
||||
for (u32 j = 0; j < count && has_all; j++) {
|
||||
if (j != smallest_idx) {
|
||||
has_all = demo3d_entity_component_has(pool, e, comps[j]);
|
||||
}
|
||||
}
|
||||
|
||||
if (has_all) {
|
||||
fn(pool, e, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
67
demo3d/client/world/demo3d_entity.h
Normal file
67
demo3d/client/world/demo3d_entity.h
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#pragma once
|
||||
|
||||
#include "pxl8_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct demo3d_entity_pool demo3d_entity_pool;
|
||||
|
||||
typedef struct demo3d_entity {
|
||||
u32 idx;
|
||||
u32 gen;
|
||||
} demo3d_entity;
|
||||
|
||||
#define DEMO3D_ENTITY_INVALID ((demo3d_entity){0, 0})
|
||||
|
||||
typedef u32 demo3d_entity_component;
|
||||
typedef u32 demo3d_entity_relationship;
|
||||
|
||||
#define DEMO3D_ENTITY_COMPONENT_INVALID 0
|
||||
#define DEMO3D_ENTITY_RELATIONSHIP_INVALID 0
|
||||
|
||||
demo3d_entity_pool* demo3d_entity_pool_create(u32 capacity);
|
||||
void demo3d_entity_pool_clear(demo3d_entity_pool* pool);
|
||||
void demo3d_entity_pool_destroy(demo3d_entity_pool* pool);
|
||||
|
||||
demo3d_entity demo3d_entity_spawn(demo3d_entity_pool* pool);
|
||||
void demo3d_entity_despawn(demo3d_entity_pool* pool, demo3d_entity e);
|
||||
bool demo3d_entity_alive(const demo3d_entity_pool* pool, demo3d_entity e);
|
||||
u32 demo3d_entity_count(const demo3d_entity_pool* pool);
|
||||
|
||||
demo3d_entity_component demo3d_entity_component_register(demo3d_entity_pool* pool, const char* name, u32 size);
|
||||
demo3d_entity_component demo3d_entity_component_find(const demo3d_entity_pool* pool, const char* name);
|
||||
const char* demo3d_entity_component_name(const demo3d_entity_pool* pool, demo3d_entity_component comp);
|
||||
|
||||
void* demo3d_entity_component_add(demo3d_entity_pool* pool, demo3d_entity e, demo3d_entity_component comp);
|
||||
void* demo3d_entity_component_get(const demo3d_entity_pool* pool, demo3d_entity e, demo3d_entity_component comp);
|
||||
void demo3d_entity_component_remove(demo3d_entity_pool* pool, demo3d_entity e, demo3d_entity_component comp);
|
||||
bool demo3d_entity_component_has(const demo3d_entity_pool* pool, demo3d_entity e, demo3d_entity_component comp);
|
||||
|
||||
demo3d_entity_relationship demo3d_entity_relationship_register(demo3d_entity_pool* pool, const char* name);
|
||||
demo3d_entity_relationship demo3d_entity_relationship_find(const demo3d_entity_pool* pool, const char* name);
|
||||
const char* demo3d_entity_relationship_name(const demo3d_entity_pool* pool, demo3d_entity_relationship rel);
|
||||
|
||||
void demo3d_entity_relationship_add(demo3d_entity_pool* pool, demo3d_entity subject, demo3d_entity_relationship rel, demo3d_entity object);
|
||||
void demo3d_entity_relationship_remove(demo3d_entity_pool* pool, demo3d_entity subject, demo3d_entity_relationship rel, demo3d_entity object);
|
||||
bool demo3d_entity_relationship_has(const demo3d_entity_pool* pool, demo3d_entity subject, demo3d_entity_relationship rel, demo3d_entity object);
|
||||
|
||||
u32 demo3d_entity_relationship_subjects(const demo3d_entity_pool* pool, demo3d_entity object, demo3d_entity_relationship rel, demo3d_entity* out, u32 max);
|
||||
u32 demo3d_entity_relationship_objects(const demo3d_entity_pool* pool, demo3d_entity subject, demo3d_entity_relationship rel, demo3d_entity* out, u32 max);
|
||||
|
||||
typedef void (*demo3d_entity_each_fn)(demo3d_entity_pool* pool, demo3d_entity e, void* ctx);
|
||||
void demo3d_entity_each(demo3d_entity_pool* pool, demo3d_entity_component comp, demo3d_entity_each_fn fn, void* ctx);
|
||||
void demo3d_entity_each_with(demo3d_entity_pool* pool, const demo3d_entity_component* comps, u32 count, demo3d_entity_each_fn fn, void* ctx);
|
||||
|
||||
static inline bool demo3d_entity_valid(demo3d_entity e) {
|
||||
return e.idx != 0 || e.gen != 0;
|
||||
}
|
||||
|
||||
static inline bool demo3d_entity_eq(demo3d_entity a, demo3d_entity b) {
|
||||
return a.idx == b.idx && a.gen == b.gen;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
981
demo3d/client/world/demo3d_world.c
Normal file
981
demo3d/client/world/demo3d_world.c
Normal file
|
|
@ -0,0 +1,981 @@
|
|||
#include "demo3d_world.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "pxl8_platform.h"
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
#include <stdatomic.h>
|
||||
#include "pxl8_queue.h"
|
||||
#endif
|
||||
|
||||
#include "demo3d_bsp_render.h"
|
||||
#include "pxl8_io.h"
|
||||
#include "pxl8_gfx3d.h"
|
||||
#include "pxl8_log.h"
|
||||
#include "pxl8_mem.h"
|
||||
#include "demo3d_protocol.h"
|
||||
#include "demo3d_sim.h"
|
||||
|
||||
#define DEMO3D_VIS_MAX_NODES (DEMO3D_WORLD_MAX_LOADED_CHUNKS * 512)
|
||||
#define DEMO3D_VIS_MAX_QUEUE (DEMO3D_VIS_MAX_NODES * 4)
|
||||
#define DEMO3D_VIS_BYTES ((DEMO3D_VIS_MAX_NODES + 7) / 8)
|
||||
#define DEMO3D_WORLD_ENTITY_CAPACITY 256
|
||||
|
||||
typedef struct {
|
||||
u16 chunk_idx;
|
||||
u16 leaf_idx;
|
||||
pxl8_rect window;
|
||||
} world_vis_node;
|
||||
|
||||
struct demo3d_world {
|
||||
demo3d_loaded_chunk loaded[DEMO3D_WORLD_MAX_LOADED_CHUNKS];
|
||||
u32 loaded_count;
|
||||
demo3d_chunk* active_chunk;
|
||||
demo3d_bsp_render_state* active_render_state;
|
||||
|
||||
pxl8_gfx_material shared_materials[16];
|
||||
bool shared_material_set[16];
|
||||
|
||||
demo3d_chunk_cache* chunk_cache;
|
||||
demo3d_entity_pool* entities;
|
||||
|
||||
demo3d_sim_entity local_player;
|
||||
u64 client_tick;
|
||||
|
||||
pxl8_vec2 pointer_motion;
|
||||
demo3d_sim_config sim_config;
|
||||
|
||||
u8 vis_bits[DEMO3D_VIS_BYTES];
|
||||
u8* vis_ptrs[DEMO3D_WORLD_MAX_LOADED_CHUNKS];
|
||||
pxl8_rect vis_windows[DEMO3D_VIS_MAX_NODES];
|
||||
pxl8_rect* vis_win_ptrs[DEMO3D_WORLD_MAX_LOADED_CHUNKS];
|
||||
world_vis_node vis_queue[DEMO3D_VIS_MAX_QUEUE];
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
demo3d_sim_entity render_state[2];
|
||||
atomic_uint active_buffer;
|
||||
pxl8_thread* sim_thread;
|
||||
atomic_bool sim_running;
|
||||
atomic_bool sim_paused;
|
||||
demo3d_net* net;
|
||||
pxl8_queue input_queue;
|
||||
f32 sim_accumulator;
|
||||
#endif
|
||||
};
|
||||
|
||||
demo3d_world* demo3d_world_create(void) {
|
||||
demo3d_world* world = pxl8_calloc(1, sizeof(demo3d_world));
|
||||
if (!world) return NULL;
|
||||
|
||||
world->chunk_cache = demo3d_chunk_cache_create();
|
||||
world->entities = demo3d_entity_pool_create(DEMO3D_WORLD_ENTITY_CAPACITY);
|
||||
world->sim_config = (demo3d_sim_config){
|
||||
.move_speed = 180.0f,
|
||||
.ground_accel = 10.0f,
|
||||
.air_accel = 1.0f,
|
||||
.stop_speed = 100.0f,
|
||||
.friction = 6.0f,
|
||||
.gravity = 800.0f,
|
||||
.jump_velocity = 200.0f,
|
||||
.player_radius = 16.0f,
|
||||
.player_height = 72.0f,
|
||||
.max_pitch = 1.5f,
|
||||
};
|
||||
|
||||
if (!world->chunk_cache || !world->entities) {
|
||||
demo3d_world_destroy(world);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return world;
|
||||
}
|
||||
|
||||
void demo3d_world_destroy(demo3d_world* world) {
|
||||
if (!world) return;
|
||||
|
||||
for (u32 i = 0; i < world->loaded_count; i++) {
|
||||
demo3d_bsp_render_state_destroy(world->loaded[i].render_state);
|
||||
}
|
||||
demo3d_chunk_cache_destroy(world->chunk_cache);
|
||||
demo3d_entity_pool_destroy(world->entities);
|
||||
pxl8_free(world);
|
||||
}
|
||||
|
||||
demo3d_chunk_cache* demo3d_world_get_chunk_cache(demo3d_world* world) {
|
||||
if (!world) return NULL;
|
||||
return world->chunk_cache;
|
||||
}
|
||||
|
||||
demo3d_chunk* demo3d_world_active_chunk(demo3d_world* world) {
|
||||
if (!world) return NULL;
|
||||
return world->active_chunk;
|
||||
}
|
||||
|
||||
demo3d_sim_world demo3d_world_sim_world(const demo3d_world* world, pxl8_vec3 pos) {
|
||||
demo3d_sim_world sim = {0};
|
||||
const f32 chunk_size = 16.0f * 64.0f;
|
||||
sim.chunk_size = chunk_size;
|
||||
|
||||
i32 pcx = (i32)floorf(pos.x / chunk_size);
|
||||
i32 pcz = (i32)floorf(pos.z / chunk_size);
|
||||
sim.center_cx = pcx;
|
||||
sim.center_cz = pcz;
|
||||
|
||||
for (u32 i = 0; i < world->loaded_count; i++) {
|
||||
const demo3d_loaded_chunk* lc = &world->loaded[i];
|
||||
if (!lc->chunk || !lc->chunk->bsp) continue;
|
||||
i32 dx = lc->cx - pcx + 1;
|
||||
i32 dz = lc->cz - pcz + 1;
|
||||
if (dx >= 0 && dx <= 2 && dz >= 0 && dz <= 2) {
|
||||
sim.chunks[dz * 3 + dx] = lc->chunk->bsp;
|
||||
}
|
||||
}
|
||||
|
||||
return sim;
|
||||
}
|
||||
|
||||
static void entity_to_userdata(const demo3d_sim_entity* ent, u8* userdata) {
|
||||
u8* p = userdata;
|
||||
memcpy(p, &ent->pos.x, 4); p += 4;
|
||||
memcpy(p, &ent->pos.y, 4); p += 4;
|
||||
memcpy(p, &ent->pos.z, 4); p += 4;
|
||||
memcpy(p, &ent->yaw, 4); p += 4;
|
||||
memcpy(p, &ent->pitch, 4); p += 4;
|
||||
memcpy(p, &ent->vel.x, 4); p += 4;
|
||||
memcpy(p, &ent->vel.y, 4); p += 4;
|
||||
memcpy(p, &ent->vel.z, 4); p += 4;
|
||||
memcpy(p, &ent->flags, 4); p += 4;
|
||||
memcpy(p, &ent->kind, 2);
|
||||
}
|
||||
|
||||
static void userdata_to_entity(const u8* userdata, demo3d_sim_entity* ent) {
|
||||
const u8* p = userdata;
|
||||
memcpy(&ent->pos.x, p, 4); p += 4;
|
||||
memcpy(&ent->pos.y, p, 4); p += 4;
|
||||
memcpy(&ent->pos.z, p, 4); p += 4;
|
||||
memcpy(&ent->yaw, p, 4); p += 4;
|
||||
memcpy(&ent->pitch, p, 4); p += 4;
|
||||
memcpy(&ent->vel.x, p, 4); p += 4;
|
||||
memcpy(&ent->vel.y, p, 4); p += 4;
|
||||
memcpy(&ent->vel.z, p, 4); p += 4;
|
||||
memcpy(&ent->flags, p, 4); p += 4;
|
||||
memcpy(&ent->kind, p, 2);
|
||||
}
|
||||
|
||||
bool demo3d_world_point_solid(const demo3d_world* world, f32 x, f32 y, f32 z) {
|
||||
if (!world) return false;
|
||||
|
||||
if (world->active_chunk && world->active_chunk->bsp) {
|
||||
return demo3d_bsp_point_solid(world->active_chunk->bsp, (pxl8_vec3){x, y, z});
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
pxl8_ray demo3d_world_ray(const demo3d_world* world, pxl8_vec3 from, pxl8_vec3 to) {
|
||||
pxl8_ray result = {0};
|
||||
|
||||
if (!world) return result;
|
||||
|
||||
pxl8_vec3 dir = pxl8_vec3_sub(to, from);
|
||||
f32 length = pxl8_vec3_length(dir);
|
||||
if (length < 0.001f) return result;
|
||||
|
||||
f32 step_size = 1.0f;
|
||||
f32 traveled = 0.0f;
|
||||
|
||||
while (traveled < length) {
|
||||
f32 t = traveled / length;
|
||||
pxl8_vec3 pos = {
|
||||
from.x + dir.x * t,
|
||||
from.y + dir.y * t,
|
||||
from.z + dir.z * t
|
||||
};
|
||||
|
||||
if (demo3d_world_point_solid(world, pos.x, pos.y, pos.z)) {
|
||||
result.hit = true;
|
||||
result.fraction = t;
|
||||
result.point = pos;
|
||||
|
||||
f32 eps = 0.1f;
|
||||
bool sx_neg = demo3d_world_point_solid(world, pos.x - eps, pos.y, pos.z);
|
||||
bool sx_pos = demo3d_world_point_solid(world, pos.x + eps, pos.y, pos.z);
|
||||
bool sy_neg = demo3d_world_point_solid(world, pos.x, pos.y - eps, pos.z);
|
||||
bool sy_pos = demo3d_world_point_solid(world, pos.x, pos.y + eps, pos.z);
|
||||
bool sz_neg = demo3d_world_point_solid(world, pos.x, pos.y, pos.z - eps);
|
||||
bool sz_pos = demo3d_world_point_solid(world, pos.x, pos.y, pos.z + eps);
|
||||
|
||||
result.normal = (pxl8_vec3){
|
||||
(sx_neg && !sx_pos) ? 1.0f : (!sx_neg && sx_pos) ? -1.0f : 0.0f,
|
||||
(sy_neg && !sy_pos) ? 1.0f : (!sy_neg && sy_pos) ? -1.0f : 0.0f,
|
||||
(sz_neg && !sz_pos) ? 1.0f : (!sz_neg && sz_pos) ? -1.0f : 0.0f
|
||||
};
|
||||
|
||||
f32 nl = pxl8_vec3_length(result.normal);
|
||||
if (nl > 0.001f) {
|
||||
result.normal = pxl8_vec3_scale(result.normal, 1.0f / nl);
|
||||
} else {
|
||||
result.normal = (pxl8_vec3){0, 1, 0};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
traveled += step_size;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
pxl8_ray demo3d_world_sweep(const demo3d_world* world, pxl8_vec3 from, pxl8_vec3 to, f32 radius) {
|
||||
pxl8_ray result = {0};
|
||||
|
||||
if (!world) return result;
|
||||
|
||||
f32 diag = radius * 0.707f;
|
||||
|
||||
bool dest_blocked = demo3d_world_point_solid(world, to.x, to.y, to.z) ||
|
||||
demo3d_world_point_solid(world, to.x + radius, to.y, to.z) ||
|
||||
demo3d_world_point_solid(world, to.x - radius, to.y, to.z) ||
|
||||
demo3d_world_point_solid(world, to.x, to.y, to.z + radius) ||
|
||||
demo3d_world_point_solid(world, to.x, to.y, to.z - radius) ||
|
||||
demo3d_world_point_solid(world, to.x + diag, to.y, to.z + diag) ||
|
||||
demo3d_world_point_solid(world, to.x + diag, to.y, to.z - diag) ||
|
||||
demo3d_world_point_solid(world, to.x - diag, to.y, to.z + diag) ||
|
||||
demo3d_world_point_solid(world, to.x - diag, to.y, to.z - diag);
|
||||
|
||||
if (dest_blocked) {
|
||||
result.hit = true;
|
||||
result.fraction = 0;
|
||||
result.point = from;
|
||||
pxl8_vec3 dir = pxl8_vec3_sub(to, from);
|
||||
f32 length = pxl8_vec3_length(dir);
|
||||
if (length > 0.001f) {
|
||||
result.normal = pxl8_vec3_scale(dir, -1.0f / length);
|
||||
} else {
|
||||
result.normal = (pxl8_vec3){0, 1, 0};
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void demo3d_world_update(demo3d_world* world, f32 dt) {
|
||||
(void)dt;
|
||||
if (!world) return;
|
||||
demo3d_chunk_cache_tick(world->chunk_cache);
|
||||
}
|
||||
|
||||
static inline bool vr_valid(pxl8_rect r) {
|
||||
return r.x0 < r.x1 && r.y0 < r.y1;
|
||||
}
|
||||
|
||||
static inline pxl8_rect vr_intersect(pxl8_rect a, pxl8_rect b) {
|
||||
return (pxl8_rect){
|
||||
.x0 = a.x0 > b.x0 ? a.x0 : b.x0,
|
||||
.y0 = a.y0 > b.y0 ? a.y0 : b.y0,
|
||||
.x1 = a.x1 < b.x1 ? a.x1 : b.x1,
|
||||
.y1 = a.y1 < b.y1 ? a.y1 : b.y1,
|
||||
};
|
||||
}
|
||||
|
||||
static pxl8_rect project_portal(f32 px0, f32 pz0, f32 px1, f32 pz1,
|
||||
f32 y_lo, f32 y_hi, const pxl8_mat4* vp) {
|
||||
pxl8_vec3 corners[4] = {
|
||||
{px0, y_lo, pz0}, {px1, y_lo, pz1},
|
||||
{px1, y_hi, pz1}, {px0, y_hi, pz0},
|
||||
};
|
||||
|
||||
const f32 NEAR_W = 0.001f;
|
||||
pxl8_vec4 clip[4];
|
||||
bool front[4];
|
||||
i32 fc = 0;
|
||||
|
||||
for (i32 i = 0; i < 4; i++) {
|
||||
clip[i] = pxl8_mat4_multiply_vec4(*vp, (pxl8_vec4){
|
||||
corners[i].x, corners[i].y, corners[i].z, 1.0f});
|
||||
front[i] = clip[i].w > NEAR_W;
|
||||
if (front[i]) fc++;
|
||||
}
|
||||
|
||||
if (fc == 0) return (pxl8_rect){0, 0, 0, 0};
|
||||
if (fc < 4) return (pxl8_rect){-1.0f, -1.0f, 1.0f, 1.0f};
|
||||
|
||||
pxl8_rect r = {1e30f, 1e30f, -1e30f, -1e30f};
|
||||
|
||||
for (i32 i = 0; i < 4; i++) {
|
||||
f32 iw = 1.0f / clip[i].w;
|
||||
f32 nx = clip[i].x * iw, ny = clip[i].y * iw;
|
||||
if (nx < r.x0) r.x0 = nx; if (nx > r.x1) r.x1 = nx;
|
||||
if (ny < r.y0) r.y0 = ny; if (ny > r.y1) r.y1 = ny;
|
||||
}
|
||||
|
||||
if (r.x0 < -1.0f) r.x0 = -1.0f;
|
||||
if (r.y0 < -1.0f) r.y0 = -1.0f;
|
||||
if (r.x1 > 1.0f) r.x1 = 1.0f;
|
||||
if (r.y1 > 1.0f) r.y1 = 1.0f;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static void compute_edge_leafs(demo3d_loaded_chunk* lc) {
|
||||
const f32 CHUNK_SIZE = 16.0f * 64.0f;
|
||||
const demo3d_bsp* bsp = lc->chunk->bsp;
|
||||
f32 cx0 = lc->cx * CHUNK_SIZE;
|
||||
f32 cz0 = lc->cz * CHUNK_SIZE;
|
||||
f32 cx1 = cx0 + CHUNK_SIZE;
|
||||
f32 cz1 = cz0 + CHUNK_SIZE;
|
||||
|
||||
memset(lc->edges, 0, sizeof(lc->edges));
|
||||
|
||||
for (u32 i = 0; i < bsp->num_leafs; i++) {
|
||||
const demo3d_bsp_leaf* leaf = &bsp->leafs[i];
|
||||
if (bsp->leafs[i].contents == -1) continue;
|
||||
|
||||
if ((f32)leaf->mins[2] <= (f32)((i16)cz0) + 1.0f && lc->edges[0].count < 16)
|
||||
lc->edges[0].leafs[lc->edges[0].count++] = (u16)i;
|
||||
if ((f32)leaf->maxs[2] >= (f32)((i16)cz1) - 1.0f && lc->edges[1].count < 16)
|
||||
lc->edges[1].leafs[lc->edges[1].count++] = (u16)i;
|
||||
if ((f32)leaf->mins[0] <= (f32)((i16)cx0) + 1.0f && lc->edges[2].count < 16)
|
||||
lc->edges[2].leafs[lc->edges[2].count++] = (u16)i;
|
||||
if ((f32)leaf->maxs[0] >= (f32)((i16)cx1) - 1.0f && lc->edges[3].count < 16)
|
||||
lc->edges[3].leafs[lc->edges[3].count++] = (u16)i;
|
||||
}
|
||||
}
|
||||
|
||||
static i32 world_find_chunk(const demo3d_world* world, i32 cx, i32 cz) {
|
||||
for (u32 i = 0; i < world->loaded_count; i++) {
|
||||
if (world->loaded[i].cx == cx && world->loaded[i].cz == cz &&
|
||||
world->loaded[i].chunk && world->loaded[i].chunk->bsp)
|
||||
return (i32)i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void world_mark_leaf_faces(const demo3d_bsp* bsp, demo3d_bsp_render_state* rs, u32 leaf_idx) {
|
||||
const demo3d_bsp_leaf* leaf = &bsp->leafs[leaf_idx];
|
||||
for (u32 i = 0; i < leaf->num_marksurfaces; i++) {
|
||||
u32 si = leaf->first_marksurface + i;
|
||||
if (si < bsp->num_marksurfaces) {
|
||||
u32 fi = bsp->marksurfaces[si];
|
||||
if (fi < bsp->num_faces && rs)
|
||||
rs->render_face_flags[fi] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool vis_try_enqueue(u8** vis, pxl8_rect** windows, world_vis_node* queue,
|
||||
u32* tail, u32 max_queue,
|
||||
u16 ci, u16 li, pxl8_rect nw) {
|
||||
if (!vis[ci] || !windows[ci]) return false;
|
||||
|
||||
u32 byte = li >> 3;
|
||||
u32 bit = 1 << (li & 7);
|
||||
|
||||
if (vis[ci][byte] & bit) {
|
||||
pxl8_rect* ex = &windows[ci][li];
|
||||
bool expanded = false;
|
||||
if (nw.x0 < ex->x0) { ex->x0 = nw.x0; expanded = true; }
|
||||
if (nw.y0 < ex->y0) { ex->y0 = nw.y0; expanded = true; }
|
||||
if (nw.x1 > ex->x1) { ex->x1 = nw.x1; expanded = true; }
|
||||
if (nw.y1 > ex->y1) { ex->y1 = nw.y1; expanded = true; }
|
||||
if (expanded && *tail < max_queue)
|
||||
queue[(*tail)++] = (world_vis_node){ci, li, *ex};
|
||||
return expanded;
|
||||
}
|
||||
|
||||
vis[ci][byte] |= bit;
|
||||
windows[ci][li] = nw;
|
||||
if (*tail < max_queue)
|
||||
queue[(*tail)++] = (world_vis_node){ci, li, nw};
|
||||
return true;
|
||||
}
|
||||
|
||||
static void world_compute_visibility(demo3d_world* world, pxl8_gfx* gfx, pxl8_vec3 camera_pos) {
|
||||
const f32 CHUNK_SIZE = 16.0f * 64.0f;
|
||||
const f32 PORTAL_Y_HI = 192.0f;
|
||||
|
||||
const pxl8_mat4* vp = pxl8_3d_get_view_proj(gfx);
|
||||
|
||||
i32 cam_ci = -1, cam_li = -1;
|
||||
for (u32 i = 0; i < world->loaded_count; i++) {
|
||||
demo3d_loaded_chunk* lc = &world->loaded[i];
|
||||
if (!lc->chunk || !lc->chunk->bsp) continue;
|
||||
const demo3d_bsp* bsp = lc->chunk->bsp;
|
||||
f32 cx0 = lc->cx * CHUNK_SIZE;
|
||||
f32 cz0 = lc->cz * CHUNK_SIZE;
|
||||
if (camera_pos.x < cx0 || camera_pos.x >= cx0 + CHUNK_SIZE ||
|
||||
camera_pos.z < cz0 || camera_pos.z >= cz0 + CHUNK_SIZE) continue;
|
||||
if (bsp->bounds_max_x > bsp->bounds_min_x &&
|
||||
(camera_pos.x < bsp->bounds_min_x || camera_pos.x >= bsp->bounds_max_x ||
|
||||
camera_pos.z < bsp->bounds_min_z || camera_pos.z >= bsp->bounds_max_z))
|
||||
continue;
|
||||
i32 leaf = demo3d_bsp_find_leaf(bsp, camera_pos);
|
||||
if (leaf >= 0 && (u32)leaf < bsp->num_leafs &&
|
||||
bsp->leafs[leaf].contents == -2) {
|
||||
cam_ci = (i32)i;
|
||||
cam_li = leaf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (cam_ci < 0 || !vp) {
|
||||
for (u32 i = 0; i < world->loaded_count; i++) {
|
||||
demo3d_bsp_render_state* rs = world->loaded[i].render_state;
|
||||
if (!rs) continue;
|
||||
if (rs->render_face_flags)
|
||||
memset(rs->render_face_flags, 1, rs->num_faces);
|
||||
rs->exterior = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
memset(world->vis_bits, 0, sizeof(world->vis_bits));
|
||||
memset(world->vis_ptrs, 0, sizeof(world->vis_ptrs));
|
||||
memset(world->vis_win_ptrs, 0, sizeof(world->vis_win_ptrs));
|
||||
|
||||
u32 voff = 0, woff = 0;
|
||||
for (u32 i = 0; i < world->loaded_count; i++) {
|
||||
if (world->loaded[i].chunk && world->loaded[i].chunk->bsp) {
|
||||
u32 nl = world->loaded[i].chunk->bsp->num_leafs;
|
||||
u32 vbytes = (nl + 7) / 8;
|
||||
if (voff + vbytes > DEMO3D_VIS_BYTES || woff + nl > DEMO3D_VIS_MAX_NODES)
|
||||
continue;
|
||||
world->vis_ptrs[i] = world->vis_bits + voff;
|
||||
voff += vbytes;
|
||||
world->vis_win_ptrs[i] = world->vis_windows + woff;
|
||||
woff += nl;
|
||||
}
|
||||
}
|
||||
|
||||
if (!world->vis_ptrs[cam_ci] || !world->vis_win_ptrs[cam_ci]) return;
|
||||
|
||||
for (u32 i = 0; i < world->loaded_count; i++) {
|
||||
demo3d_bsp_render_state* rs = world->loaded[i].render_state;
|
||||
if (!rs) continue;
|
||||
if (rs->render_face_flags)
|
||||
memset(rs->render_face_flags, 0, rs->num_faces);
|
||||
rs->exterior = false;
|
||||
}
|
||||
|
||||
u32 head = 0, tail = 0;
|
||||
|
||||
pxl8_rect full_screen = {-1.0f, -1.0f, 1.0f, 1.0f};
|
||||
const demo3d_bsp* cam_bsp = world->loaded[cam_ci].chunk->bsp;
|
||||
bool cam_exterior = cam_bsp->leafs[cam_li].contents == 0 &&
|
||||
!(cam_bsp->bounds_max_x > cam_bsp->bounds_min_x);
|
||||
|
||||
world->vis_ptrs[cam_ci][cam_li >> 3] |= (1 << (cam_li & 7));
|
||||
world->vis_win_ptrs[cam_ci][cam_li] = full_screen;
|
||||
world->vis_queue[tail++] = (world_vis_node){(u16)cam_ci, (u16)cam_li, full_screen};
|
||||
|
||||
while (head < tail) {
|
||||
world_vis_node cur = world->vis_queue[head++];
|
||||
demo3d_loaded_chunk* lc = &world->loaded[cur.chunk_idx];
|
||||
const demo3d_bsp* bsp = lc->chunk->bsp;
|
||||
|
||||
world_mark_leaf_faces(bsp, lc->render_state, cur.leaf_idx);
|
||||
|
||||
if (bsp->cell_portals && cur.leaf_idx < bsp->num_cell_portals) {
|
||||
bool is_cam_leaf = (cur.chunk_idx == (u16)cam_ci && cur.leaf_idx == (u16)cam_li);
|
||||
bool is_cam_chunk = (cur.chunk_idx == (u16)cam_ci);
|
||||
const demo3d_bsp_cell_portals* cp = &bsp->cell_portals[cur.leaf_idx];
|
||||
for (u8 pi = 0; pi < cp->num_portals; pi++) {
|
||||
const demo3d_bsp_portal* p = &cp->portals[pi];
|
||||
u32 target = p->target_leaf;
|
||||
if (target >= bsp->num_leafs) continue;
|
||||
if (bsp->leafs[target].contents == -1) continue;
|
||||
if (is_cam_chunk && !demo3d_bsp_is_leaf_visible(bsp, cam_li, (i32)target)) continue;
|
||||
|
||||
if (is_cam_leaf || cam_exterior) {
|
||||
vis_try_enqueue(world->vis_ptrs, world->vis_win_ptrs,
|
||||
world->vis_queue, &tail, DEMO3D_VIS_MAX_QUEUE,
|
||||
cur.chunk_idx, (u16)target, full_screen);
|
||||
continue;
|
||||
}
|
||||
|
||||
pxl8_rect psr = project_portal(p->x0, p->z0, p->x1, p->z1,
|
||||
0.0f, PORTAL_Y_HI, vp);
|
||||
if (!vr_valid(psr)) continue;
|
||||
pxl8_rect nw = vr_intersect(cur.window, psr);
|
||||
if (!vr_valid(nw)) continue;
|
||||
|
||||
vis_try_enqueue(world->vis_ptrs, world->vis_win_ptrs,
|
||||
world->vis_queue, &tail, DEMO3D_VIS_MAX_QUEUE,
|
||||
cur.chunk_idx, (u16)target, nw);
|
||||
}
|
||||
}
|
||||
|
||||
const demo3d_bsp_leaf* leaf = &bsp->leafs[cur.leaf_idx];
|
||||
f32 chunk_x0 = lc->cx * CHUNK_SIZE;
|
||||
f32 chunk_z0 = lc->cz * CHUNK_SIZE;
|
||||
f32 chunk_x1 = chunk_x0 + CHUNK_SIZE;
|
||||
f32 chunk_z1 = chunk_z0 + CHUNK_SIZE;
|
||||
|
||||
struct { i32 dx, dz; bool at_edge; f32 bnd; } dirs[4] = {
|
||||
{ 0, -1, (f32)leaf->mins[2] <= (f32)((i16)chunk_z0) + 1.0f, chunk_z0 },
|
||||
{ 0, 1, (f32)leaf->maxs[2] >= (f32)((i16)chunk_z1) - 1.0f, chunk_z1 },
|
||||
{-1, 0, (f32)leaf->mins[0] <= (f32)((i16)chunk_x0) + 1.0f, chunk_x0 },
|
||||
{ 1, 0, (f32)leaf->maxs[0] >= (f32)((i16)chunk_x1) - 1.0f, chunk_x1 },
|
||||
};
|
||||
|
||||
static const u8 opposite_edge[4] = {1, 0, 3, 2};
|
||||
|
||||
for (u32 d = 0; d < 4; d++) {
|
||||
if (!dirs[d].at_edge) continue;
|
||||
i32 nci = world_find_chunk(world, lc->cx + dirs[d].dx, lc->cz + dirs[d].dz);
|
||||
if (nci < 0) continue;
|
||||
|
||||
const demo3d_bsp* nbsp = world->loaded[nci].chunk->bsp;
|
||||
const demo3d_edge_leafs* nedge = &world->loaded[nci].edges[opposite_edge[d]];
|
||||
|
||||
for (u8 k = 0; k < nedge->count; k++) {
|
||||
u16 nl = nedge->leafs[k];
|
||||
const demo3d_bsp_leaf* nleaf =  ->leafs[nl];
|
||||
|
||||
bool overlaps = false;
|
||||
if (dirs[d].dx != 0) {
|
||||
overlaps = leaf->maxs[2] > nleaf->mins[2] && leaf->mins[2] < nleaf->maxs[2];
|
||||
} else {
|
||||
overlaps = leaf->maxs[0] > nleaf->mins[0] && leaf->mins[0] < nleaf->maxs[0];
|
||||
}
|
||||
if (!overlaps) continue;
|
||||
|
||||
f32 bpx0, bpz0, bpx1, bpz1;
|
||||
if (dirs[d].dx != 0) {
|
||||
bpx0 = dirs[d].bnd; bpx1 = dirs[d].bnd;
|
||||
i16 zlo = leaf->mins[2] > nleaf->mins[2] ? leaf->mins[2] : nleaf->mins[2];
|
||||
i16 zhi = leaf->maxs[2] < nleaf->maxs[2] ? leaf->maxs[2] : nleaf->maxs[2];
|
||||
bpz0 = (f32)zlo; bpz1 = (f32)zhi;
|
||||
} else {
|
||||
bpz0 = dirs[d].bnd; bpz1 = dirs[d].bnd;
|
||||
i16 xlo = leaf->mins[0] > nleaf->mins[0] ? leaf->mins[0] : nleaf->mins[0];
|
||||
i16 xhi = leaf->maxs[0] < nleaf->maxs[0] ? leaf->maxs[0] : nleaf->maxs[0];
|
||||
bpx0 = (f32)xlo; bpx1 = (f32)xhi;
|
||||
}
|
||||
|
||||
if (cam_exterior) {
|
||||
vis_try_enqueue(world->vis_ptrs, world->vis_win_ptrs,
|
||||
world->vis_queue, &tail, DEMO3D_VIS_MAX_QUEUE,
|
||||
(u16)nci, (u16)nl, full_screen);
|
||||
continue;
|
||||
}
|
||||
|
||||
pxl8_rect psr = project_portal(bpx0, bpz0, bpx1, bpz1,
|
||||
0.0f, PORTAL_Y_HI, vp);
|
||||
if (!vr_valid(psr)) continue;
|
||||
pxl8_rect nw = vr_intersect(cur.window, psr);
|
||||
if (!vr_valid(nw)) continue;
|
||||
|
||||
vis_try_enqueue(world->vis_ptrs, world->vis_win_ptrs,
|
||||
world->vis_queue, &tail, DEMO3D_VIS_MAX_QUEUE,
|
||||
(u16)nci, (u16)nl, nw);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void demo3d_world_render(demo3d_world* world, pxl8_gfx* gfx, pxl8_vec3 camera_pos) {
|
||||
if (!world || !gfx) return;
|
||||
|
||||
world_compute_visibility(world, gfx, camera_pos);
|
||||
|
||||
for (u32 i = 0; i < world->loaded_count; i++) {
|
||||
demo3d_loaded_chunk* lc = &world->loaded[i];
|
||||
if (!lc->chunk || !lc->chunk->bsp || !lc->render_state) continue;
|
||||
demo3d_bsp_render(gfx, lc->chunk->bsp, lc->render_state, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void apply_shared_materials(demo3d_world* world, demo3d_bsp_render_state* rs) {
|
||||
for (u16 i = 0; i < 16; i++) {
|
||||
if (world->shared_material_set[i]) {
|
||||
demo3d_bsp_set_material(rs, i, &world->shared_materials[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void demo3d_world_sync(demo3d_world* world, demo3d_net* net) {
|
||||
if (!world || !net) return;
|
||||
|
||||
if (!demo3d_net_has_chunk(net)) {
|
||||
u32 old_count = world->loaded_count;
|
||||
world->loaded_count = 0;
|
||||
world->active_chunk = NULL;
|
||||
world->active_render_state = NULL;
|
||||
for (u32 i = 0; i < old_count; i++) {
|
||||
demo3d_bsp_render_state_destroy(world->loaded[i].render_state);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
i32 center_cx = demo3d_net_chunk_cx(net);
|
||||
i32 center_cz = demo3d_net_chunk_cz(net);
|
||||
|
||||
demo3d_loaded_chunk old_loaded[DEMO3D_WORLD_MAX_LOADED_CHUNKS];
|
||||
u32 old_count = world->loaded_count;
|
||||
memcpy(old_loaded, world->loaded, sizeof(old_loaded));
|
||||
|
||||
demo3d_loaded_chunk new_loaded[DEMO3D_WORLD_MAX_LOADED_CHUNKS];
|
||||
u32 new_count = 0;
|
||||
demo3d_chunk* new_active_chunk = NULL;
|
||||
demo3d_bsp_render_state* new_active_rs = NULL;
|
||||
|
||||
for (i32 dz = -2; dz <= 2; dz++) {
|
||||
for (i32 dx = -2; dx <= 2; dx++) {
|
||||
i32 cx = center_cx + dx;
|
||||
i32 cz = center_cz + dz;
|
||||
u32 id = demo3d_chunk_hash(cx, cz);
|
||||
|
||||
demo3d_chunk* chunk = demo3d_chunk_cache_get_bsp(world->chunk_cache, id);
|
||||
if (!chunk || !chunk->bsp) {
|
||||
for (u32 j = 0; j < old_count; j++) {
|
||||
if (old_loaded[j].active && old_loaded[j].cx == cx && old_loaded[j].cz == cz) {
|
||||
u32 idx = new_count++;
|
||||
new_loaded[idx] = old_loaded[j];
|
||||
old_loaded[j].active = false;
|
||||
if (dx == 0 && dz == 0) {
|
||||
new_active_chunk = new_loaded[idx].chunk;
|
||||
new_active_rs = new_loaded[idx].render_state;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
demo3d_bsp_render_state* rs = NULL;
|
||||
for (u32 j = 0; j < old_count; j++) {
|
||||
if (old_loaded[j].active && old_loaded[j].cx == cx && old_loaded[j].cz == cz &&
|
||||
old_loaded[j].chunk == chunk) {
|
||||
rs = old_loaded[j].render_state;
|
||||
old_loaded[j].active = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!rs) {
|
||||
rs = demo3d_bsp_render_state_create(chunk->bsp->num_faces);
|
||||
if (rs && rs->render_face_flags)
|
||||
memset(rs->render_face_flags, 1, rs->num_faces);
|
||||
apply_shared_materials(world, rs);
|
||||
}
|
||||
|
||||
u32 idx = new_count++;
|
||||
new_loaded[idx] = (demo3d_loaded_chunk){
|
||||
.chunk = chunk,
|
||||
.render_state = rs,
|
||||
.cx = cx,
|
||||
.cz = cz,
|
||||
.active = true,
|
||||
};
|
||||
compute_edge_leafs(&new_loaded[idx]);
|
||||
|
||||
if (dx == 0 && dz == 0) {
|
||||
new_active_chunk = chunk;
|
||||
new_active_rs = rs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (u32 j = 0; j < old_count; j++) {
|
||||
if (!old_loaded[j].active) continue;
|
||||
if (new_count < DEMO3D_WORLD_MAX_LOADED_CHUNKS &&
|
||||
old_loaded[j].chunk && old_loaded[j].chunk->bsp &&
|
||||
demo3d_chunk_cache_get_bsp(world->chunk_cache, old_loaded[j].chunk->id)) {
|
||||
new_loaded[new_count++] = old_loaded[j];
|
||||
} else {
|
||||
demo3d_bsp_render_state_destroy(old_loaded[j].render_state);
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(world->loaded, new_loaded, sizeof(new_loaded));
|
||||
world->active_chunk = new_active_chunk;
|
||||
world->active_render_state = new_active_rs;
|
||||
world->loaded_count = new_count;
|
||||
}
|
||||
|
||||
void demo3d_world_set_bsp_material(demo3d_world* world, u16 material_id, const pxl8_gfx_material* material) {
|
||||
if (!world || !material || material_id >= 16) return;
|
||||
|
||||
world->shared_materials[material_id] = *material;
|
||||
world->shared_material_set[material_id] = true;
|
||||
|
||||
for (u32 i = 0; i < world->loaded_count; i++) {
|
||||
if (world->loaded[i].render_state) {
|
||||
demo3d_bsp_set_material(world->loaded[i].render_state, material_id, material);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void demo3d_world_set_sim_config(demo3d_world* world, const demo3d_sim_config* config) {
|
||||
if (!world || !config) return;
|
||||
world->sim_config = *config;
|
||||
}
|
||||
|
||||
void demo3d_world_init_local_player(demo3d_world* world, f32 x, f32 y, f32 z) {
|
||||
if (!world) return;
|
||||
world->local_player.pos = (pxl8_vec3){x, y, z};
|
||||
world->local_player.vel = (pxl8_vec3){0, 0, 0};
|
||||
world->local_player.yaw = 0;
|
||||
world->local_player.pitch = 0;
|
||||
world->local_player.flags = DEMO3D_SIM_FLAG_ALIVE | DEMO3D_SIM_FLAG_PLAYER | DEMO3D_SIM_FLAG_GROUNDED;
|
||||
world->local_player.kind = 0;
|
||||
world->client_tick = 0;
|
||||
world->pointer_motion = (pxl8_vec2){0};
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
world->render_state[0] = world->local_player;
|
||||
world->render_state[1] = world->local_player;
|
||||
#endif
|
||||
}
|
||||
|
||||
void demo3d_world_set_look(demo3d_world* world, f32 yaw, f32 pitch) {
|
||||
if (!world) return;
|
||||
world->local_player.yaw = yaw;
|
||||
world->local_player.pitch = pitch;
|
||||
world->pointer_motion = (pxl8_vec2){.yaw = yaw, .pitch = pitch};
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
world->render_state[0].yaw = yaw;
|
||||
world->render_state[0].pitch = pitch;
|
||||
world->render_state[1].yaw = yaw;
|
||||
world->render_state[1].pitch = pitch;
|
||||
#endif
|
||||
}
|
||||
|
||||
demo3d_sim_entity* demo3d_world_local_player(demo3d_world* world) {
|
||||
if (!world) return NULL;
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
const demo3d_sim_entity* state = demo3d_world_get_render_state(world);
|
||||
if (!state) return NULL;
|
||||
if (!(state->flags & DEMO3D_SIM_FLAG_ALIVE)) return NULL;
|
||||
return (demo3d_sim_entity*)state;
|
||||
#else
|
||||
if (!(world->local_player.flags & DEMO3D_SIM_FLAG_ALIVE)) return NULL;
|
||||
return &world->local_player;
|
||||
#endif
|
||||
}
|
||||
|
||||
void demo3d_world_predict(demo3d_world* world, demo3d_net* net, const demo3d_input_msg* input, f32 dt) {
|
||||
if (!world || !net || !input) return;
|
||||
if (!(world->local_player.flags & DEMO3D_SIM_FLAG_ALIVE)) return;
|
||||
|
||||
demo3d_sim_world sim = demo3d_world_sim_world(world, world->local_player.pos);
|
||||
demo3d_sim_move_player(&world->local_player, input, &sim, &world->sim_config, dt);
|
||||
|
||||
world->client_tick++;
|
||||
|
||||
entity_to_userdata(&world->local_player, demo3d_net_predicted_state(net));
|
||||
demo3d_net_predicted_tick_set(net, world->client_tick);
|
||||
}
|
||||
|
||||
void demo3d_world_reconcile(demo3d_world* world, demo3d_net* net, f32 dt) {
|
||||
if (!world || !net) return;
|
||||
if (!(world->local_player.flags & DEMO3D_SIM_FLAG_ALIVE)) return;
|
||||
if (!demo3d_net_needs_correction(net)) return;
|
||||
|
||||
u64 player_id = demo3d_net_player_id(net);
|
||||
const u8* server_state = demo3d_net_entity_userdata(net, player_id);
|
||||
if (!server_state) return;
|
||||
|
||||
demo3d_sim_entity server_player = {0};
|
||||
userdata_to_entity(server_state, &server_player);
|
||||
|
||||
if (!(server_player.flags & DEMO3D_SIM_FLAG_ALIVE)) {
|
||||
return;
|
||||
}
|
||||
|
||||
world->local_player = server_player;
|
||||
|
||||
const demo3d_snapshot_header* snap = demo3d_net_snapshot(net);
|
||||
u64 server_tick = snap ? snap->tick : 0;
|
||||
|
||||
for (u64 tick = server_tick + 1; tick <= world->client_tick; tick++) {
|
||||
const demo3d_input_msg* input = demo3d_net_input_at(net, tick);
|
||||
if (!input) continue;
|
||||
|
||||
demo3d_sim_world sim = demo3d_world_sim_world(world, world->local_player.pos);
|
||||
demo3d_sim_move_player(&world->local_player, input, &sim, &world->sim_config, dt);
|
||||
}
|
||||
|
||||
entity_to_userdata(&world->local_player, demo3d_net_predicted_state(net));
|
||||
}
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
|
||||
#define SIM_TIMESTEP (1.0f / 60.0f)
|
||||
|
||||
static void demo3d_world_sim_tick(demo3d_world* world, f32 dt) {
|
||||
bool alive = (world->local_player.flags & DEMO3D_SIM_FLAG_ALIVE) != 0;
|
||||
demo3d_input_msg merged = {0};
|
||||
demo3d_input_msg* input = NULL;
|
||||
|
||||
while ((input = pxl8_queue_pop(&world->input_queue))) {
|
||||
merged.look_dx += input->look_dx;
|
||||
merged.look_dy += input->look_dy;
|
||||
merged.move_x = input->move_x;
|
||||
merged.move_y = input->move_y;
|
||||
merged.buttons |= input->buttons;
|
||||
pxl8_free(input);
|
||||
}
|
||||
|
||||
if (!alive) {
|
||||
return;
|
||||
}
|
||||
|
||||
const f32 MAX_LOOK_DELTA = 100.0f;
|
||||
if (merged.look_dx > MAX_LOOK_DELTA) merged.look_dx = MAX_LOOK_DELTA;
|
||||
if (merged.look_dx < -MAX_LOOK_DELTA) merged.look_dx = -MAX_LOOK_DELTA;
|
||||
if (merged.look_dy > MAX_LOOK_DELTA) merged.look_dy = MAX_LOOK_DELTA;
|
||||
if (merged.look_dy < -MAX_LOOK_DELTA) merged.look_dy = -MAX_LOOK_DELTA;
|
||||
|
||||
merged.tick = world->client_tick;
|
||||
merged.timestamp = pxl8_get_ticks_ns();
|
||||
merged.yaw = world->pointer_motion.yaw;
|
||||
if (world->net) {
|
||||
demo3d_net_send_input(world->net, &merged);
|
||||
}
|
||||
|
||||
world->local_player.yaw = world->pointer_motion.yaw;
|
||||
world->local_player.pitch = world->pointer_motion.pitch;
|
||||
merged.look_dx = 0;
|
||||
merged.look_dy = 0;
|
||||
|
||||
demo3d_sim_world sim = demo3d_world_sim_world(world, world->local_player.pos);
|
||||
demo3d_sim_move_player(&world->local_player, &merged, &sim, &world->sim_config, dt);
|
||||
|
||||
if (world->net) {
|
||||
entity_to_userdata(&world->local_player, demo3d_net_predicted_state(world->net));
|
||||
demo3d_net_predicted_tick_set(world->net, world->client_tick);
|
||||
}
|
||||
|
||||
world->client_tick++;
|
||||
}
|
||||
|
||||
static void demo3d_world_swap_buffers(demo3d_world* world) {
|
||||
u32 back = atomic_load(&world->active_buffer) ^ 1;
|
||||
world->render_state[back] = world->local_player;
|
||||
atomic_store(&world->active_buffer, back);
|
||||
}
|
||||
|
||||
static int demo3d_world_sim_thread(void* data) {
|
||||
demo3d_world* world = (demo3d_world*)data;
|
||||
u64 last_time = pxl8_get_ticks_ns();
|
||||
|
||||
while (atomic_load(&world->sim_running)) {
|
||||
if (atomic_load(&world->sim_paused)) {
|
||||
last_time = pxl8_get_ticks_ns();
|
||||
pxl8_sleep_ms(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
u64 now = pxl8_get_ticks_ns();
|
||||
f32 dt = (f32)(now - last_time) / 1e9f;
|
||||
last_time = now;
|
||||
|
||||
if (dt > 0.1f) dt = 0.1f;
|
||||
if (dt < 0.0001f) dt = 0.0001f;
|
||||
|
||||
world->sim_accumulator += dt;
|
||||
|
||||
while (world->sim_accumulator >= SIM_TIMESTEP) {
|
||||
demo3d_chunk_cache_tick(world->chunk_cache);
|
||||
|
||||
if (world->net) {
|
||||
pxl8_packet* pkt;
|
||||
while ((pkt = pxl8_net_pop_packet(world->net->transport))) {
|
||||
demo3d_net_dispatch_packet(world->net, pkt);
|
||||
pxl8_net_packet_free(pkt);
|
||||
}
|
||||
|
||||
demo3d_world_sync(world, world->net);
|
||||
demo3d_world_reconcile(world, world->net, SIM_TIMESTEP);
|
||||
}
|
||||
|
||||
demo3d_world_sim_tick(world, SIM_TIMESTEP);
|
||||
world->sim_accumulator -= SIM_TIMESTEP;
|
||||
}
|
||||
|
||||
demo3d_world_swap_buffers(world);
|
||||
pxl8_sleep_ms(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void demo3d_world_start_sim_thread(demo3d_world* world, demo3d_net* net) {
|
||||
if (!world || world->sim_thread) return;
|
||||
|
||||
world->net = net;
|
||||
pxl8_queue_init(&world->input_queue);
|
||||
atomic_store(&world->active_buffer, 0);
|
||||
atomic_store(&world->sim_running, true);
|
||||
world->sim_accumulator = 0.0f;
|
||||
|
||||
world->render_state[0] = world->local_player;
|
||||
world->render_state[1] = world->local_player;
|
||||
|
||||
world->sim_thread = pxl8_thread_create(demo3d_world_sim_thread, "pxl8_sim", world);
|
||||
}
|
||||
|
||||
void demo3d_world_stop_sim_thread(demo3d_world* world) {
|
||||
if (!world || !world->sim_thread) return;
|
||||
|
||||
atomic_store(&world->sim_running, false);
|
||||
pxl8_thread_wait(world->sim_thread, NULL);
|
||||
world->sim_thread = NULL;
|
||||
|
||||
demo3d_input_msg* input;
|
||||
while ((input = pxl8_queue_pop(&world->input_queue))) {
|
||||
pxl8_free(input);
|
||||
}
|
||||
}
|
||||
|
||||
void demo3d_world_pause_sim(demo3d_world* world, bool paused) {
|
||||
if (!world) return;
|
||||
|
||||
if (paused) {
|
||||
atomic_store(&world->sim_paused, true);
|
||||
} else {
|
||||
demo3d_input_msg* input;
|
||||
while ((input = pxl8_queue_pop(&world->input_queue))) {
|
||||
pxl8_free(input);
|
||||
}
|
||||
world->sim_accumulator = 0.0f;
|
||||
atomic_store(&world->sim_paused, false);
|
||||
}
|
||||
}
|
||||
|
||||
void demo3d_world_push_input(demo3d_world* world, const demo3d_input_msg* input) {
|
||||
if (!world || !input) return;
|
||||
|
||||
world->pointer_motion.yaw -= input->look_dx * 0.008f;
|
||||
f32 pitch = world->pointer_motion.pitch - input->look_dy * 0.008f;
|
||||
if (pitch > world->sim_config.max_pitch) pitch = world->sim_config.max_pitch;
|
||||
if (pitch < -world->sim_config.max_pitch) pitch = -world->sim_config.max_pitch;
|
||||
world->pointer_motion.pitch = pitch;
|
||||
|
||||
demo3d_input_msg* copy = pxl8_malloc(sizeof(demo3d_input_msg));
|
||||
if (copy) {
|
||||
*copy = *input;
|
||||
if (!pxl8_queue_push(&world->input_queue, copy)) {
|
||||
pxl8_free(copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const demo3d_sim_entity* demo3d_world_get_render_state(const demo3d_world* world) {
|
||||
if (!world) return NULL;
|
||||
u32 front = atomic_load(&((demo3d_world*)world)->active_buffer);
|
||||
return &world->render_state[front];
|
||||
}
|
||||
|
||||
f32 demo3d_world_get_interp_alpha(const demo3d_world* world) {
|
||||
if (!world) return 1.0f;
|
||||
return world->sim_accumulator / SIM_TIMESTEP;
|
||||
}
|
||||
|
||||
#endif
|
||||
71
demo3d/client/world/demo3d_world.h
Normal file
71
demo3d/client/world/demo3d_world.h
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#pragma once
|
||||
|
||||
#include "demo3d_entity.h"
|
||||
#include "pxl8_gfx.h"
|
||||
#include "pxl8_gfx3d.h"
|
||||
#include "pxl8_math.h"
|
||||
#include "demo3d_net.h"
|
||||
#include "demo3d_sim.h"
|
||||
#include "pxl8_types.h"
|
||||
#include "demo3d_chunk.h"
|
||||
#include "demo3d_chunk_cache.h"
|
||||
#include "demo3d_bsp_render.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DEMO3D_WORLD_MAX_LOADED_CHUNKS 64
|
||||
|
||||
typedef struct {
|
||||
u16 leafs[16];
|
||||
u8 count;
|
||||
} demo3d_edge_leafs;
|
||||
|
||||
typedef struct demo3d_loaded_chunk {
|
||||
demo3d_chunk* chunk;
|
||||
demo3d_bsp_render_state* render_state;
|
||||
demo3d_edge_leafs edges[4];
|
||||
i32 cx;
|
||||
i32 cz;
|
||||
bool active;
|
||||
} demo3d_loaded_chunk;
|
||||
|
||||
typedef struct demo3d_world demo3d_world;
|
||||
|
||||
demo3d_world* demo3d_world_create(void);
|
||||
void demo3d_world_destroy(demo3d_world* world);
|
||||
|
||||
demo3d_chunk_cache* demo3d_world_get_chunk_cache(demo3d_world* world);
|
||||
demo3d_chunk* demo3d_world_active_chunk(demo3d_world* world);
|
||||
|
||||
bool demo3d_world_point_solid(const demo3d_world* world, f32 x, f32 y, f32 z);
|
||||
pxl8_ray demo3d_world_ray(const demo3d_world* world, pxl8_vec3 from, pxl8_vec3 to);
|
||||
pxl8_ray demo3d_world_sweep(const demo3d_world* world, pxl8_vec3 from, pxl8_vec3 to, f32 radius);
|
||||
|
||||
void demo3d_world_update(demo3d_world* world, f32 dt);
|
||||
void demo3d_world_render(demo3d_world* world, pxl8_gfx* gfx, pxl8_vec3 camera_pos);
|
||||
void demo3d_world_sync(demo3d_world* world, demo3d_net* net);
|
||||
|
||||
void demo3d_world_set_bsp_material(demo3d_world* world, u16 material_id, const pxl8_gfx_material* material);
|
||||
|
||||
void demo3d_world_set_sim_config(demo3d_world* world, const demo3d_sim_config* config);
|
||||
void demo3d_world_init_local_player(demo3d_world* world, f32 x, f32 y, f32 z);
|
||||
void demo3d_world_set_look(demo3d_world* world, f32 yaw, f32 pitch);
|
||||
demo3d_sim_entity* demo3d_world_local_player(demo3d_world* world);
|
||||
demo3d_sim_world demo3d_world_sim_world(const demo3d_world* world, pxl8_vec3 pos);
|
||||
void demo3d_world_predict(demo3d_world* world, demo3d_net* net, const demo3d_input_msg* input, f32 dt);
|
||||
void demo3d_world_reconcile(demo3d_world* world, demo3d_net* net, f32 dt);
|
||||
|
||||
#ifdef PXL8_ASYNC_THREADS
|
||||
void demo3d_world_start_sim_thread(demo3d_world* world, demo3d_net* net);
|
||||
void demo3d_world_stop_sim_thread(demo3d_world* world);
|
||||
void demo3d_world_pause_sim(demo3d_world* world, bool paused);
|
||||
void demo3d_world_push_input(demo3d_world* world, const demo3d_input_msg* input);
|
||||
const demo3d_sim_entity* demo3d_world_get_render_state(const demo3d_world* world);
|
||||
f32 demo3d_world_get_interp_alpha(const demo3d_world* world);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue