diff --git a/terrain.c b/terrain.c index b119345..dda9409 100644 --- a/terrain.c +++ b/terrain.c @@ -59,7 +59,7 @@ #define CHUNKSCALE (1.0 / CHUNKSIZE) // This is how many vertices across a chunk is #define CHUNKVSIZE (CHUNKSIZE + 1) -#define VIEWDISTANCE 7 +#define VIEWDISTANCE 10 #define PLBOXEDGE (VIEWDISTANCE * 2 + 1) #define INIT_NEARCLIP 0.01 #define INIT_FARCLIP 100.0 @@ -76,7 +76,8 @@ #define TREETRUNKCOL 0xF950 #define TREEBOTTOM -0.333 #define TREETRUNKWIDTH 0.2 -#define REDFLOWERCOL 0xFF36 +#define REDFLOWERCOL 0xFF24 +#define YELLOWFLOWERCOL 0xFFF0 #define SEATRANS 0.75 #define CLOUDTRANSMIN 0.1 #define CLOUDTRANSMAX 0.2 @@ -108,6 +109,7 @@ int fps = 60; #define TREEKEY "tree" #define CLOUDKEY "cloud" #define REDFLOWERKEY "redflower" +#define YELLOWFLOWERKEY "yellowflower" #include "commonobj.c" @@ -130,11 +132,13 @@ static inline mfloat_t calc_barycentric(mfloat_t *point, mfloat_t *values, return 0; } -void gen_terrain(struct vec3i pos, haloo3d_obj *model, - haloo3d_easystore *storage) { +uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model, + haloo3d_easystore *storage) { eprintf("Generating terrain at %d,%d\n", pos.x, pos.z); haloo3d_obj *tree = haloo3d_easystore_getobj(storage, TREEKEY); - haloo3d_obj *redflower = haloo3d_easystore_getobj(storage, REDFLOWERKEY); + haloo3d_obj *flowers[2]; + flowers[0] = haloo3d_easystore_getobj(storage, REDFLOWERKEY); + 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); @@ -154,6 +158,14 @@ void gen_terrain(struct vec3i pos, haloo3d_obj *model, // clang-format on } } + struct vec2 triedge[6]; + mfloat_t heights[6]; + uint16_t faces[2]; + // A funny little thing... we're going to add flowers to this alternative + // model, then add them to the end of the real model with a pointer saying + // "don't do lighting past this point" + haloo3d_obj nl_model; + haloo3d_obj_resetfixed(&nl_model, H3D_OBJ_MAXFACES, H3D_OBJ_MAXVERTICES); for (int i = 0; i < CHUNKVSIZE * (CHUNKVSIZE - 1); i++) { if ((i & CHUNKSIZE) == CHUNKSIZE) { continue; @@ -162,14 +174,8 @@ void gen_terrain(struct vec3i pos, haloo3d_obj *model, uint16_t br = i + 1; uint16_t tl = i + CHUNKVSIZE; uint16_t tr = i + CHUNKVSIZE + 1; - haloo3d_facei face; - uint16_t faces[2]; - fastface(face, landuv, bl, br, tl); - faces[0] = haloo3d_obj_addface(model, face); - fastface(face, landuv, br, tr, tl); - faces[1] = haloo3d_obj_addface(model, face); - struct vec2 triedge[6]; - mfloat_t heights[6]; + faces[0] = fastface2(model, landuv, bl, br, tl); + faces[1] = fastface2(model, landuv, br, tr, tl); for (int t = 0; t < 2; t++) { for (int v = 0; v < 3; v++) { int ofs = t * 3; @@ -183,27 +189,30 @@ void gen_terrain(struct vec3i pos, haloo3d_obj *model, // ns.seed = 0; ns.frequency = 0.02; ns.fractal_type = FNL_FRACTAL_NONE; - // Pick a random place for a flower - struct vec2 flowerpos = {.x = triedge[0].x + 0.8 * RANDF(), - .y = triedge[0].y - 0.8 * RANDF()}; - float y = calc_barycentric(flowerpos.v, heights, triedge, 2); - if (y < 0.4 && y > 0) { - float nlayer = - fnlGetNoise2D(&ns, noisex + flowerpos.x, noisey + flowerpos.y); - float scale = 0.04; - float vertscale = scale; // * CHUNKSCALE * 4; - if (RANDF() < -nlayer) { - haloo3d_obj_addobj( - model, redflower, - (struct vec3){ - .x = flowerpos.x, .y = y + vertscale * 0.5, .z = flowerpos.y}, - (struct vec3)DEFAULTLOOK, - //(struct vec3){.x = -sin(MPI * 0.25), .y = 0, .z = -cos(MPI * - // 0.25)}, - (struct vec3)DEFAULTUP, - (struct vec3){.x = scale, .y = vertscale, .z = scale}); - } - } + // for (int fi = 0; fi < 2; fi++) { + // // Pick a random place for a flower + // struct vec2 flowerpos = {.x = triedge[0].x + 0.8 * RANDF(), + // .y = triedge[0].y - 0.8 * RANDF()}; + // float y = calc_barycentric(flowerpos.v, heights, triedge, 2); + // if (y > 0.2 * fi && y < 0.2 * (fi + 1)) { + // float nlayer = + // fnlGetNoise2D(&ns, noisex + flowerpos.x, noisey + flowerpos.y); + // float scale = 0.04; + // float vertscale = scale * (1 - 0.5 * RANDF()); // * CHUNKSCALE * 4; + // if (RANDF() < -nlayer) { + // haloo3d_obj_addobj( + // &nl_model, flowers[fi], + // (struct vec3){ + // .x = flowerpos.x, .y = y + vertscale * 0.5, .z = + // flowerpos.y}, + // (struct vec3)DEFAULTLOOK, + // //(struct vec3){.x = -sin(MPI * 0.25), .y = 0, .z = -cos(MPI * + // // 0.25)}, + // (struct vec3)DEFAULTUP, + // (struct vec3){.x = scale, .y = vertscale, .z = scale}); + // } + // } + // } for (int t = 0; t < MAXTREEPERCELL; t++) { // We know that the first vec in the first triangle is bl, which is our // base to start generating trees @@ -226,18 +235,28 @@ void gen_terrain(struct vec3i pos, haloo3d_obj *model, .x = treepos.x, .y = y + scale * height, .z = treepos.y}, (struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP, (struct vec3){.x = scale, .y = scale * height, .z = scale}); + } else if ((rand() & 1) && y < 0.4 && y > 0 && RANDF() < -nlayer) { + // if (y > 0.2 * fi && y < 0.2 * (fi + 1)) { + float scale = 0.04; + float vertscale = scale * (1 - 0.5 * RANDF()); // * CHUNKSCALE * 4; + haloo3d_obj_addobj( + &nl_model, flowers[y < 0.2], + (struct vec3){ + .x = treepos.x, .y = y + vertscale * 0.5, .z = treepos.y}, + (struct vec3)DEFAULTLOOK, + //(struct vec3){.x = -sin(MPI * 0.25), .y = 0, .z = -cos(MPI * + // 0.25)}, + (struct vec3)DEFAULTUP, + (struct vec3){.x = scale, .y = vertscale, .z = scale}); } } } - // uint16_t stoplighting = model->numfaces; - // for (int i = 0; i < CHUNKVSIZE * (CHUNKVSIZE - 1); i++) { - // if ((i & CHUNKSIZE) == CHUNKSIZE) { - // continue; - // } - // uint16_t bl = i; - // } - // haloo3d_obj_shrinktofit(model); - // return stoplighting; + uint16_t stoplighting = model->numfaces; + haloo3d_obj_addobj(model, &nl_model, (struct vec3){.x = 0, .y = 0, .z = 0}, + (struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP, + (struct vec3){.x = 1, .y = 1, .z = 1}); + haloo3d_obj_free(&nl_model); + return stoplighting; } void gen_cloud(render_context *ctx, tecs *ecs, struct vec3i pos) { @@ -254,7 +273,7 @@ void gen_cloud(render_context *ctx, tecs *ecs, struct vec3i pos) { .texture = haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY), .scale = {.x = 1 + RANDF(), .y = 1.0 / CLOUDSTRETCH, .z = 1}, .lighting = NULL, //&ecs->chunklight, - //.stoplighting = 0, + .stoplighting = 0, .model = cloud, .cullbackface = 1, .context = {ctx}, @@ -272,7 +291,7 @@ void gen_chunk(render_context *ctx, tecs *ecs, struct vec3i pos) { sprintf(garbage.dynmodel, "e%d", id); haloo3d_obj *model = haloo3d_easystore_addobj(&ecs->storage, garbage.dynmodel); - gen_terrain(pos, model, &ecs->storage); + uint16_t stoplighting = gen_terrain(pos, model, &ecs->storage); ECS_SETCOMPONENT(ecs, id, ecs_playergarbage) garbage; ECS_SETCOMPONENT(ecs, id, ecs_chunk){.pos = pos}; ECS_SETCOMPONENT(ecs, id, @@ -286,7 +305,7 @@ void gen_chunk(render_context *ctx, tecs *ecs, struct vec3i pos) { .texture = haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY), .scale = {.x = CHUNKSCALE, .y = CHUNKSCALE * LANDHEIGHT, .z = CHUNKSCALE}, .lighting = &ecs->chunklight, - //.stoplighting = stoplighting, + .stoplighting = stoplighting, .model = model, .cullbackface = 1, .context = {ctx}, @@ -464,7 +483,7 @@ void sys_renderobject(ecs_placement *p, ecs_object *o, tecs **ecs) { int tris = haloo3d_facef_clip(face, outfaces); if (tris > 0) { uint8_t oflags = rsettings.flags; - if (o->lighting) { + if (o->lighting && facei < o->stoplighting) { haloo3d_obj_facef(o->model, o->model->faces[facei], baseface); rsettings.intensity = haloo3d_calc_light(lighting.v, o->lighting->minlight, baseface); @@ -681,6 +700,9 @@ int main() { // int argc, char **argv) { gen_circle_model(cloudmodel, CLOUDCOL, 32); haloo3d_obj *redflower = haloo3d_easystore_addobj(&ecs.storage, REDFLOWERKEY); gen_flower(redflower, REDFLOWERCOL); + haloo3d_obj *yellowflower = + haloo3d_easystore_addobj(&ecs.storage, YELLOWFLOWERKEY); + gen_flower(yellowflower, YELLOWFLOWERCOL); eprintf("Setup ECS system + obj/tex storage (%d bytes)\n", tecs_size); diff --git a/terrain_ecstypes.h b/terrain_ecstypes.h index e204aa7..6fa7880 100644 --- a/terrain_ecstypes.h +++ b/terrain_ecstypes.h @@ -51,7 +51,7 @@ typedef struct { object_lighting *lighting; // a pointer to lighting, null for none haloo3d_obj *model; haloo3d_fb *texture; - // uint16_t stoplighting; + uint16_t stoplighting; uint8_t cullbackface; // Whether to cull backfaces (you probably should) uint8_t contextcount; // How many contexts we render into float flatdither; // A flat dither amount. Set to -1 to disable