terrain.h (1988B)
1 2 3 #ifndef POLYADVENT_TERRAIN_H 4 #define POLYADVENT_TERRAIN_H 5 6 #include "entity.h" 7 #include "model.h" 8 #include "debug.h" 9 10 #define MAX_CELL_VERTS 4 11 #define MAX_VERT_TRIS 20 12 13 struct point; 14 15 struct perlin_settings { 16 double freq; 17 int depth; 18 int seed; 19 double amplitude; 20 double ox, oy; 21 double o1, o1s; 22 double o2, o2s; 23 double scale; 24 double exp; 25 }; 26 27 struct tri { 28 #ifdef DEBUG 29 struct entity_id debug_id; 30 #endif 31 int vert_indices[3]; 32 }; 33 34 struct vert_tris { 35 u8 tri_count; 36 struct tri tris[MAX_VERT_TRIS]; 37 }; 38 39 struct terrain_cell { 40 u8 vert_count; 41 int verts_index[MAX_CELL_VERTS]; 42 #ifdef DEBUG 43 struct entity_id debug_ent[MAX_CELL_VERTS]; 44 #endif 45 }; 46 47 struct terrain { 48 struct entity_id entity_id; 49 struct terrain_cell *grid; 50 struct vert_tris *vtris; 51 int n_cells; // all cells = grid_cells^2 52 float cell_size; 53 float *verts; 54 int n_verts; 55 struct perlin_settings settings; 56 struct point *samples; 57 double (*fn)(struct terrain *, double, double); 58 int n_samples; 59 float pdist; 60 double size; 61 }; 62 63 double old_noisy_boi(struct terrain *, double x, double y); 64 65 66 void update_terrain(struct terrain *terrain, const double pdist); 67 void gen_terrain_samples(struct terrain *terrain, float scale, const double pdist); 68 void init_terrain(struct terrain *terrain, float size); 69 void reset_terrain(struct terrain *terrain, float size); 70 void create_terrain(struct terrain *terrain, float scale, int seed); 71 void destroy_terrain(struct terrain *terrain); 72 void query_terrain_grid(struct terrain *terrain, float x, float y, struct terrain_cell *cells[9]); 73 74 static inline int grid_index(struct terrain *terrain, float x) { 75 return x / terrain->cell_size; 76 } 77 78 static inline void grid_pos_debug(const char *thing, struct terrain *terrain, float *pos) { 79 int gx = grid_index(terrain, pos[0]); 80 int gy = grid_index(terrain, pos[1]); 81 debug("%s grid pos (%d, %d)\n", thing, gx, gy); 82 } 83 84 #endif /* POLYADVENT_TERRAIN_H */