diff --git a/terrain.c b/terrain.c index d526d96..dd82b87 100644 --- a/terrain.c +++ b/terrain.c @@ -4,6 +4,7 @@ #include "haloo3d/haloo3dex_obj.h" #include "haloo3d/haloo3dex_print.h" +#include "haloo3d/lib/FastNoiseLite.h" #include "haloo3d/lib/mathc.h" #include "unigi/main.h" @@ -25,28 +26,35 @@ // Lookvec for objects which all face forward along with the player #define DEFAULTLOOK \ { .x = 0, .y = 0, .z = -1 } -// { .x = 1.0, .y = 1.0, .z = 1.0 } // How big each chunk is (x and y, square). MUST BE POWER OF 2 - 1 -#define CHUNKSIZE 7 +#define CHUNKSIZE 7 // This is the width in tiles #define CHUNKSCALE 1.0 / CHUNKSIZE +// This is how many vertices across a chunk is +#define CHUNKVSIZE (CHUNKSIZE + 1) #define VIEWDISTANCE 10 #define PLBOXEDGE (VIEWDISTANCE * 2 + 1) -#define MAXCHUNKPERFRAME 1 - -// These are initial values but there may be ways to change it -#define INIT_CAMPITCH MPI_2 // MPI_2 * 1.6 // 1.575 #define INIT_NEARCLIP 0.01 #define INIT_FARCLIP 100.0 -#define INIT_FOV 90.0 -#define INIT_DITHERSTART 100 -#define INIT_DITHEREND 100 -#define INIT_LIGHTPITCH MPI_2 * 1.75 -#define INIT_LIGHTYAW 0 -#define INIT_MINLIGHT 0.3 -#define INIT_PLAYERHEIGHT 0.5 +#define MAXCHUNKPERFRAME 1 + +// Generation consts / things for looks +#define LANDCOL 0xF4E1 +#define SEACOL 0xF26C +#define LANDHEIGHT 4.0 +#define SKYCOL 0xF77F +#define INIT_LIGHTPITCH MPI_2 * 1.65 +#define INIT_LIGHTYAW MPI_2 * 0.65 +#define INIT_MINLIGHT 0.2 +#define INIT_DITHERSTART 3 +#define INIT_DITHEREND 10 + +// Player things +#define INIT_FOV 90.0 +#define INIT_CAMPITCH MPI_2 * 1.2 // MPI_2 * 1.6 // 1.575 +#define INIT_PLAYERHEIGHT 0.65 #define INIT_PLAYERSPEED \ - { .x = 0, .y = 0, .z = -1.5 } + { .x = 0, .y = 0, .z = -9.5 } #define RANDF() ((float)rand() / RAND_MAX) @@ -65,27 +73,39 @@ void fastface(haloo3d_facei face, uint16_t tex, uint16_t v0, uint16_t v1, face[2].posi = v2; } +static int gen_terrain_seed = 0; + void gen_terrain(struct vec3i pos, haloo3d_obj *model) { eprintf("Generating terrain at %d,%d\n", pos.x, pos.z); // Don't allow the model to have more than some amount of faces/vertices. - haloo3d_obj_resetfixed(model, (CHUNKSIZE + 1) * (CHUNKSIZE + 1) * 4, - (CHUNKSIZE + 1) * (CHUNKSIZE + 1) * 4); - struct vec3 landcol = haloo3d_gen_paletteuv(0xF2F0); + haloo3d_obj_resetfixed(model, CHUNKVSIZE * CHUNKVSIZE * 4, + CHUNKVSIZE * CHUNKVSIZE * 4); + struct vec3 landcol = haloo3d_gen_paletteuv(LANDCOL); // 0xF2C0); int landuv = haloo3d_obj_addvtexture(model, landcol); - for (int z = 0; z <= CHUNKSIZE; z++) { - for (int x = 0; x <= CHUNKSIZE; x++) { - haloo3d_obj_addvertex( - model, (struct vec4){.x = x, .y = 2 * RANDF() - 1, .z = -z}); + fnl_state ns = fnlCreateState(); + ns.noise_type = FNL_NOISE_OPENSIMPLEX2; + ns.seed = gen_terrain_seed; + ns.frequency = 0.025; + ns.fractal_type = FNL_FRACTAL_RIDGED; + float noisex = pos.x * CHUNKSIZE; + float noisey = -pos.z * CHUNKSIZE; + for (int z = 0; z < CHUNKVSIZE; z++) { + for (int x = 0; x < CHUNKVSIZE; x++) { + // clang-format off + haloo3d_obj_addvertex(model, + (struct vec4){.x = x, .y = fnlGetNoise2D(&ns, noisex + x, noisey + z), .z = -z }); + //(struct vec4){.x = x, .y = 2 * RANDF() - 1, .z = -z}); + // clang-format on } } - for (int i = 0; i < (CHUNKSIZE + 1) * CHUNKSIZE; i++) { + for (int i = 0; i < CHUNKVSIZE * (CHUNKVSIZE - 1); i++) { if ((i & CHUNKSIZE) == CHUNKSIZE) { continue; } uint16_t bl = i; uint16_t br = i + 1; - uint16_t tl = i + CHUNKSIZE + 1; - uint16_t tr = i + CHUNKSIZE + 2; + uint16_t tl = i + CHUNKVSIZE; + uint16_t tr = i + CHUNKVSIZE + 1; haloo3d_facei face; fastface(face, landuv, bl, br, tl); haloo3d_obj_addface(model, face); @@ -117,7 +137,7 @@ void gen_chunk(render_context *ctx, tecs *ecs, struct vec3i pos) { // eprintf("TEX: %p\n", haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY)); ECS_SETCOMPONENT(ecs, id, ecs_object){ .texture = haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY), - .scale = {.x = CHUNKSCALE, .y = CHUNKSCALE, .z = CHUNKSCALE}, + .scale = {.x = CHUNKSCALE, .y = CHUNKSCALE * LANDHEIGHT, .z = CHUNKSCALE}, .lighting = &ecs->chunklight, .model = model, .cullbackface = 1, @@ -405,7 +425,7 @@ ecs_eid setup_sea(tecs *ecs, render_context *ctx, ecs_placement *playerpos, }; haloo3d_obj *model = haloo3d_easystore_addobj(storage, "sea"); haloo3d_obj_resetfixed(model, 2, 4); - struct vec3 seacol = haloo3d_gen_paletteuv(0xF26F); + struct vec3 seacol = haloo3d_gen_paletteuv(SEACOL); // 0xF26F); int seauv = haloo3d_obj_addvtexture(model, seacol); // struct vec3 seacol2 = haloo3d_gen_paletteuv(0xF008); // haloo3d_obj_addvtexture(model, seacol2); @@ -424,9 +444,9 @@ ecs_eid setup_sea(tecs *ecs, render_context *ctx, ecs_placement *playerpos, haloo3d_obj_addface(model, face); ECS_SETCOMPONENT(ecs, seaid, ecs_object){ .texture = haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY), - .scale = {.x = VIEWDISTANCE * 1.5, - .y = VIEWDISTANCE * 1.5, - .z = VIEWDISTANCE * 1.5}, + .scale = {.x = VIEWDISTANCE * 0.5, + .y = VIEWDISTANCE * 0.5, + .z = VIEWDISTANCE * 0.5}, .lighting = NULL, .model = model, .cullbackface = 1, @@ -441,6 +461,7 @@ ecs_eid setup_sea(tecs *ecs, render_context *ctx, ecs_placement *playerpos, int main() { // int argc, char **argv) { srand(clock()); + gen_terrain_seed = rand(); // Init unigi system. Can use anything here that can render to screen unigi_type_resolution res; @@ -467,7 +488,7 @@ int main() { // int argc, char **argv) { haloo3d_easytimer_init(&sdltimer, AVGWEIGHT); render_context context; - context.windowclear = 0xF333; + context.windowclear = SKYCOL; context.nearclip = INIT_NEARCLIP; context.farclip = INIT_FARCLIP; context.fov = INIT_FOV;