polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit a1c28cdfcf48983de4891b321d9f36ee15e28348
parent e7c279ca918af3493c045a632db24e0a8fb75733
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 21 Jul 2019 20:20:40 -0700

terrian grid debugging

Diffstat:
Msrc/terrain.c | 50+++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/terrain.h | 7+++++--
Msrc/update.c | 38++++++++++++++++++++++++++++++++++++--
3 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/src/terrain.c b/src/terrain.c @@ -105,6 +105,50 @@ void gen_terrain_samples(struct terrain *terrain, float scale, const double pdis } +static inline struct terrain_cell *index_terrain_cell(struct terrain *terrain, + int x, int y) +{ + if (x < 0 || y < 0 || x >= terrain->n_cells || y >= terrain->n_cells) + return NULL; + + return &terrain->grid[x * terrain->n_cells + y]; +} + +static inline struct terrain_cell *query_terrain_cell(struct terrain *terrain, + float x, float y, + int *grid_x, int *grid_y) +{ + assert(x < terrain->size); + assert(y < terrain->size); + *grid_x = x / terrain->cell_size; + *grid_y = y / terrain->cell_size; + + return index_terrain_cell(terrain, *grid_x, *grid_y); +} + +void query_terrain_grid(struct terrain *terrain, float x, float y, + struct terrain_cell *cells[9]) +{ + int grid_x, grid_y; + + // middle + cells[4] = query_terrain_cell(terrain, x, y, &grid_x, &grid_y); + + // top row + cells[0] = index_terrain_cell(terrain, grid_x - 1, grid_y - 1); + cells[1] = index_terrain_cell(terrain, grid_x, grid_y - 1); + cells[2] = index_terrain_cell(terrain, grid_x + 1, grid_y - 1); + + // left, right + cells[3] = index_terrain_cell(terrain, grid_x - 1, grid_y); + cells[5] = index_terrain_cell(terrain, grid_x + 1, grid_y); + + // bottom row + cells[6] = index_terrain_cell(terrain, grid_x - 1, grid_y + 1); + cells[7] = index_terrain_cell(terrain, grid_x , grid_y + 1); + cells[8] = index_terrain_cell(terrain, grid_x + 1, grid_y + 1); +} + void create_terrain(struct terrain *terrain, float scale, int seed) { u32 i; const double size = terrain->size; @@ -121,13 +165,17 @@ void create_terrain(struct terrain *terrain, float scale, int seed) { del_point2d_t *points = calloc(terrain->n_samples, sizeof(*points)); float *verts = calloc(terrain->n_samples * 3, sizeof(*verts)); + terrain->verts = verts; terrain->n_cells = round(size / pdist); + terrain->cell_size = pdist; debug("n_cells %d\n", terrain->n_cells); assert(terrain->n_cells == 417); struct terrain_cell *grid = calloc(terrain->n_cells * terrain->n_cells, sizeof(struct terrain_cell)); + terrain->grid = grid; + /* float *normals = calloc(terrain->n_samples * 3, sizeof(*verts)); */ terrain->fn = offset_fn; @@ -247,7 +295,7 @@ void create_terrain(struct terrain *terrain, float scale, int seed) { free(points); // needed for collision - free(verts); + /* free(verts); */ free(del_verts); // we might need norms in memory eventually as well ? diff --git a/src/terrain.h b/src/terrain.h @@ -25,29 +25,32 @@ struct perlin_settings { struct terrain_cell { u8 vert_count; u16 verts_index[MAX_CELL_VERTS]; + entity_id debug_ent[MAX_CELL_VERTS]; }; struct terrain { entity_id entity_id; struct terrain_cell *grid; int n_cells; // all cells = grid_cells^2 + float cell_size; float *verts; + int n_verts; struct perlin_settings settings; struct point *samples; double (*fn)(struct terrain *, double, double); int n_samples; + float pdist; double size; }; double old_noisy_boi(struct terrain *, double x, double y); - void update_terrain(struct terrain *terrain, const double pdist); void gen_terrain_samples(struct terrain *terrain, float scale, const double pdist); void init_terrain(struct terrain *terrain, float size); void reset_terrain(struct terrain *terrain, float size); void create_terrain(struct terrain *terrain, float scale, int seed); void destroy_terrain(struct terrain *terrain); - +void query_terrain_grid(struct terrain *terrain, float x, float y, struct terrain_cell *cells[9]); #endif /* POLYADVENT_TERRAIN_H */ diff --git a/src/update.c b/src/update.c @@ -307,7 +307,9 @@ static void camera_keep_above_ground(struct terrain *terrain, } } -static void player_update(struct game *game, struct entity *player) { +static void player_update(struct game *game, struct entity *player) +{ + struct terrain_cell *cells[9]; struct resources *res = &game->test_resources; struct orbit *camera = &res->orbit_camera; @@ -326,8 +328,40 @@ static void player_update(struct game *game, struct entity *player) { float yaw = game->test_resources.orbit_camera.coords.azimuth; quat_axis_angle(V3(0.0, 0.0, 1.0), -yaw - RAD(90), node->orientation); } - player_terrain_collision(&game->terrain, player); + + struct terrain *terrain = &game->terrain; + + player_terrain_collision(terrain, player); node_recalc(node); + + query_terrain_grid(terrain, node->pos[0], node->pos[1], cells); + + for (int i = 0; i < ARRAY_SIZE(cells); i++) { + struct terrain_cell *cell = cells[i]; + if (!cell) + continue; + + for (int j = 0; j < cell->vert_count; j++) { + entity_id *ent_id = &cell->debug_ent[j]; + + if (is_null_id(ent_id)) { + init_id(ent_id); + struct entity *ent = new_entity(ent_id); + ent->model_id = get_static_model(model_barrel, NULL); + struct node *enode = get_node(&ent->node_id); + node_set_label(enode, "grid_debug");; + assert(cell->verts_index[j] < terrain->n_samples); + float *vert = &terrain->verts[cell->verts_index[j] * 3]; + debug("creating new grid_debug entity at %f %f %f\n", vert[0], vert[1], vert[2]); + vec3_copy(vert, enode->pos); + node_scale(enode, 5.0); + node_mark_for_recalc(enode); + node_recalc(enode); + } + + } + + } }