diff --git a/terrain.c b/terrain.c index 0decc6b..3401e94 100644 --- a/terrain.c +++ b/terrain.c @@ -67,10 +67,13 @@ #define MAXCHUNKPERFRAME PLBOXEDGE // Generation consts / things for looks +#define COLOREDTERRAIN // Some cost, not much #define LANDHEIGHT 4.0 -#define LANDCOL 0xF6D2 // 0xF4E1 -#define SEACOL 0xF28D // 0xF26C -#define SKYCOL 0xFA7F // 0xF77F +#define LANDCOL 0xF6D2 // 0xF4E1 +#define FLANDCOL 0xF260 // 0xF3A0 // 0xF4E1 +#define UWLANDCOL 0xF026 +#define SEACOL 0xF28D // 0xF26C +#define SKYCOL 0xFA7F // 0xF77F #define CLOUDCOL 0xFFFF #define TREECOL 0xF070 #define TREETRUNKCOL 0xF950 @@ -210,8 +213,16 @@ uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model, tecs *ecs, flowers[1] = haloo3d_easystore_getobj(storage, YELLOWFLOWERKEY); // Don't allow the model to have more than some amount of faces/vertices. haloo3d_obj_resetfixed(model, H3D_OBJ_MAXFACES, H3D_OBJ_MAXVERTICES); - struct vec3 landcol = haloo3d_gen_paletteuv(LANDCOL); // 0xF2C0); - int landuv = haloo3d_obj_addvtexture(model, landcol); +#define LCOLSTEP 8 + uint16_t landcols[(LCOLSTEP + 1) * 2]; + for (int i = 0; i <= LCOLSTEP; i++) { + landcols[i] = + COMCOL(model, haloo3d_col_lerp(LANDCOL, FLANDCOL, + (float)i / LCOLSTEP)); // 0xF2C0); + landcols[i + LCOLSTEP + 1] = + COMCOL(model, haloo3d_col_lerp(LANDCOL, UWLANDCOL, + (float)i / LCOLSTEP)); // 0xF2C0); + } fnl_state ns = fnlCreateState(); ns.noise_type = FNL_NOISE_OPENSIMPLEX2; ns.seed = gen_terrain_seed; @@ -222,8 +233,8 @@ uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model, tecs *ecs, 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, .w = 1 }); + haloo3d_obj_addvertex(model, (struct vec4){ + .x = x, .y = fnlGetNoise2D(&ns, noisex + x, noisey + z), .z = -z, .w = 1 }); // clang-format on } } @@ -240,28 +251,64 @@ uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model, tecs *ecs, if ((i & CHUNKSIZE) == CHUNKSIZE) { continue; } + // Change frequency for subthings + // ns.seed = 0; + ns.frequency = 0.01; // 0.02; + ns.fractal_type = FNL_FRACTAL_NONE; uint16_t bl = i; uint16_t br = i + 1; uint16_t tl = i + CHUNKVSIZE; uint16_t tr = i + CHUNKVSIZE + 1; - faces[0] = fastface2(model, landuv, bl, br, tl); - faces[1] = fastface2(model, landuv, br, tr, tl); - float lowy = 999; + // float lowy = MIN(model->vertices[bl].y, model->vertices[br].y); + // lowy = MIN(lowy, model->vertices[tl].y); + // lowy = MIN(lowy, model->vertices[tr].y); + // float highy = MAX(model->vertices[bl].y, model->vertices[br].y); + // highy = MAX(highy, model->vertices[tl].y); + // highy = MAX(highy, model->vertices[tr].y); + // float nlayer = fnlGetNoise2D(&ns, noisex + model->vertices[bl].x, + // noisey + model->vertices[bl].z); + // // int landcol = nlayer > 0.0 ? 4 * fabs(lowy) : 0; + // int landcol; + // if (highy < 0) { + // landcol = LCOLSTEP + 1 + LCOLSTEP * fabs(highy); + // } else { + // landcol = LCOLSTEP * fabs(lowy) * (1 + nlayer) * 0.5; + // } + faces[0] = fastface2(model, landcols[0], bl, br, tl); + faces[1] = fastface2(model, landcols[0], br, tr, tl); + float lowys[2]; + float highys[2]; for (int t = 0; t < 2; t++) { + lowys[t] = 999; + highys[t] = -999; for (int v = 0; v < 3; v++) { int ofs = t * 3; // NOTE: WE SWTICH Y AND Z TO MAKE THE EDGE FUNCTION WORK triedge[ofs + v].x = model->vertices[model->faces[faces[t]][v].posi].x; triedge[ofs + v].y = model->vertices[model->faces[faces[t]][v].posi].z; heights[ofs + v] = model->vertices[model->faces[faces[t]][v].posi].y; - if (heights[ofs + v] < lowy) - lowy = heights[ofs + v]; + if (heights[ofs + v] < lowys[t]) + lowys[t] = heights[ofs + v]; + if (heights[ofs + v] > highys[t]) + highys[t] = heights[ofs + v]; } +#ifdef COLOREDTERRAIN + uint16_t tfvi = model->faces[faces[t]][0].posi; + float nlayer = fnlGetNoise2D(&ns, noisex + model->vertices[tfvi].x, + noisey + model->vertices[tfvi].z); + // int landcol = nlayer > 0.0 ? 4 * fabs(lowy) : 0; + int landcol; + if (highys[t] < 0) { + landcol = LCOLSTEP + 1 + LCOLSTEP * fabs(highys[t]); + } else { + landcol = LCOLSTEP * fabs(lowys[t]) * (1 + nlayer) * 0.5; + } + for (int v = 0; v < 3; v++) { + model->faces[faces[t]][v].texi = landcols[landcol]; + } +#endif } - // Change frequency for subthings - // ns.seed = 0; - ns.frequency = 0.02; - ns.fractal_type = FNL_FRACTAL_NONE; + float lowy = MIN(lowys[0], lowys[1]); int treecount = 0; for (int t = 0; t < MAXTREEPERCELL; t++) { // We know that the first vec in the first triangle is bl, which is our