Crazy flowers

This commit is contained in:
Carlos Sanchez 2024-09-21 09:26:52 -04:00
parent 20a8c65266
commit 8a1534004c
2 changed files with 70 additions and 48 deletions

114
terrain.c
View File

@ -59,7 +59,7 @@
#define CHUNKSCALE (1.0 / CHUNKSIZE) #define CHUNKSCALE (1.0 / CHUNKSIZE)
// This is how many vertices across a chunk is // This is how many vertices across a chunk is
#define CHUNKVSIZE (CHUNKSIZE + 1) #define CHUNKVSIZE (CHUNKSIZE + 1)
#define VIEWDISTANCE 7 #define VIEWDISTANCE 10
#define PLBOXEDGE (VIEWDISTANCE * 2 + 1) #define PLBOXEDGE (VIEWDISTANCE * 2 + 1)
#define INIT_NEARCLIP 0.01 #define INIT_NEARCLIP 0.01
#define INIT_FARCLIP 100.0 #define INIT_FARCLIP 100.0
@ -76,7 +76,8 @@
#define TREETRUNKCOL 0xF950 #define TREETRUNKCOL 0xF950
#define TREEBOTTOM -0.333 #define TREEBOTTOM -0.333
#define TREETRUNKWIDTH 0.2 #define TREETRUNKWIDTH 0.2
#define REDFLOWERCOL 0xFF36 #define REDFLOWERCOL 0xFF24
#define YELLOWFLOWERCOL 0xFFF0
#define SEATRANS 0.75 #define SEATRANS 0.75
#define CLOUDTRANSMIN 0.1 #define CLOUDTRANSMIN 0.1
#define CLOUDTRANSMAX 0.2 #define CLOUDTRANSMAX 0.2
@ -108,6 +109,7 @@ int fps = 60;
#define TREEKEY "tree" #define TREEKEY "tree"
#define CLOUDKEY "cloud" #define CLOUDKEY "cloud"
#define REDFLOWERKEY "redflower" #define REDFLOWERKEY "redflower"
#define YELLOWFLOWERKEY "yellowflower"
#include "commonobj.c" #include "commonobj.c"
@ -130,11 +132,13 @@ static inline mfloat_t calc_barycentric(mfloat_t *point, mfloat_t *values,
return 0; return 0;
} }
void gen_terrain(struct vec3i pos, haloo3d_obj *model, uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model,
haloo3d_easystore *storage) { haloo3d_easystore *storage) {
eprintf("Generating terrain at %d,%d\n", pos.x, pos.z); eprintf("Generating terrain at %d,%d\n", pos.x, pos.z);
haloo3d_obj *tree = haloo3d_easystore_getobj(storage, TREEKEY); 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. // Don't allow the model to have more than some amount of faces/vertices.
haloo3d_obj_resetfixed(model, H3D_OBJ_MAXFACES, H3D_OBJ_MAXVERTICES); haloo3d_obj_resetfixed(model, H3D_OBJ_MAXFACES, H3D_OBJ_MAXVERTICES);
struct vec3 landcol = haloo3d_gen_paletteuv(LANDCOL); // 0xF2C0); struct vec3 landcol = haloo3d_gen_paletteuv(LANDCOL); // 0xF2C0);
@ -154,6 +158,14 @@ void gen_terrain(struct vec3i pos, haloo3d_obj *model,
// clang-format on // 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++) { for (int i = 0; i < CHUNKVSIZE * (CHUNKVSIZE - 1); i++) {
if ((i & CHUNKSIZE) == CHUNKSIZE) { if ((i & CHUNKSIZE) == CHUNKSIZE) {
continue; continue;
@ -162,14 +174,8 @@ void gen_terrain(struct vec3i pos, haloo3d_obj *model,
uint16_t br = i + 1; uint16_t br = i + 1;
uint16_t tl = i + CHUNKVSIZE; uint16_t tl = i + CHUNKVSIZE;
uint16_t tr = i + CHUNKVSIZE + 1; uint16_t tr = i + CHUNKVSIZE + 1;
haloo3d_facei face; faces[0] = fastface2(model, landuv, bl, br, tl);
uint16_t faces[2]; faces[1] = fastface2(model, landuv, br, tr, tl);
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];
for (int t = 0; t < 2; t++) { for (int t = 0; t < 2; t++) {
for (int v = 0; v < 3; v++) { for (int v = 0; v < 3; v++) {
int ofs = t * 3; int ofs = t * 3;
@ -183,27 +189,30 @@ void gen_terrain(struct vec3i pos, haloo3d_obj *model,
// ns.seed = 0; // ns.seed = 0;
ns.frequency = 0.02; ns.frequency = 0.02;
ns.fractal_type = FNL_FRACTAL_NONE; ns.fractal_type = FNL_FRACTAL_NONE;
// Pick a random place for a flower // for (int fi = 0; fi < 2; fi++) {
struct vec2 flowerpos = {.x = triedge[0].x + 0.8 * RANDF(), // // Pick a random place for a flower
.y = triedge[0].y - 0.8 * RANDF()}; // struct vec2 flowerpos = {.x = triedge[0].x + 0.8 * RANDF(),
float y = calc_barycentric(flowerpos.v, heights, triedge, 2); // .y = triedge[0].y - 0.8 * RANDF()};
if (y < 0.4 && y > 0) { // float y = calc_barycentric(flowerpos.v, heights, triedge, 2);
float nlayer = // if (y > 0.2 * fi && y < 0.2 * (fi + 1)) {
fnlGetNoise2D(&ns, noisex + flowerpos.x, noisey + flowerpos.y); // float nlayer =
float scale = 0.04; // fnlGetNoise2D(&ns, noisex + flowerpos.x, noisey + flowerpos.y);
float vertscale = scale; // * CHUNKSCALE * 4; // float scale = 0.04;
if (RANDF() < -nlayer) { // float vertscale = scale * (1 - 0.5 * RANDF()); // * CHUNKSCALE * 4;
haloo3d_obj_addobj( // if (RANDF() < -nlayer) {
model, redflower, // haloo3d_obj_addobj(
(struct vec3){ // &nl_model, flowers[fi],
.x = flowerpos.x, .y = y + vertscale * 0.5, .z = flowerpos.y}, // (struct vec3){
(struct vec3)DEFAULTLOOK, // .x = flowerpos.x, .y = y + vertscale * 0.5, .z =
//(struct vec3){.x = -sin(MPI * 0.25), .y = 0, .z = -cos(MPI * // flowerpos.y},
// 0.25)}, // (struct vec3)DEFAULTLOOK,
(struct vec3)DEFAULTUP, // //(struct vec3){.x = -sin(MPI * 0.25), .y = 0, .z = -cos(MPI *
(struct vec3){.x = scale, .y = vertscale, .z = scale}); // // 0.25)},
} // (struct vec3)DEFAULTUP,
} // (struct vec3){.x = scale, .y = vertscale, .z = scale});
// }
// }
// }
for (int t = 0; t < MAXTREEPERCELL; t++) { for (int t = 0; t < MAXTREEPERCELL; t++) {
// We know that the first vec in the first triangle is bl, which is our // We know that the first vec in the first triangle is bl, which is our
// base to start generating trees // 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}, .x = treepos.x, .y = y + scale * height, .z = treepos.y},
(struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP, (struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP,
(struct vec3){.x = scale, .y = scale * height, .z = scale}); (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; uint16_t stoplighting = model->numfaces;
// for (int i = 0; i < CHUNKVSIZE * (CHUNKVSIZE - 1); i++) { haloo3d_obj_addobj(model, &nl_model, (struct vec3){.x = 0, .y = 0, .z = 0},
// if ((i & CHUNKSIZE) == CHUNKSIZE) { (struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP,
// continue; (struct vec3){.x = 1, .y = 1, .z = 1});
// } haloo3d_obj_free(&nl_model);
// uint16_t bl = i; return stoplighting;
// }
// haloo3d_obj_shrinktofit(model);
// return stoplighting;
} }
void gen_cloud(render_context *ctx, tecs *ecs, struct vec3i pos) { 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), .texture = haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY),
.scale = {.x = 1 + RANDF(), .y = 1.0 / CLOUDSTRETCH, .z = 1}, .scale = {.x = 1 + RANDF(), .y = 1.0 / CLOUDSTRETCH, .z = 1},
.lighting = NULL, //&ecs->chunklight, .lighting = NULL, //&ecs->chunklight,
//.stoplighting = 0, .stoplighting = 0,
.model = cloud, .model = cloud,
.cullbackface = 1, .cullbackface = 1,
.context = {ctx}, .context = {ctx},
@ -272,7 +291,7 @@ void gen_chunk(render_context *ctx, tecs *ecs, struct vec3i pos) {
sprintf(garbage.dynmodel, "e%d", id); sprintf(garbage.dynmodel, "e%d", id);
haloo3d_obj *model = haloo3d_obj *model =
haloo3d_easystore_addobj(&ecs->storage, garbage.dynmodel); 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_playergarbage) garbage;
ECS_SETCOMPONENT(ecs, id, ecs_chunk){.pos = pos}; ECS_SETCOMPONENT(ecs, id, ecs_chunk){.pos = pos};
ECS_SETCOMPONENT(ecs, id, 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), .texture = haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY),
.scale = {.x = CHUNKSCALE, .y = CHUNKSCALE * LANDHEIGHT, .z = CHUNKSCALE}, .scale = {.x = CHUNKSCALE, .y = CHUNKSCALE * LANDHEIGHT, .z = CHUNKSCALE},
.lighting = &ecs->chunklight, .lighting = &ecs->chunklight,
//.stoplighting = stoplighting, .stoplighting = stoplighting,
.model = model, .model = model,
.cullbackface = 1, .cullbackface = 1,
.context = {ctx}, .context = {ctx},
@ -464,7 +483,7 @@ void sys_renderobject(ecs_placement *p, ecs_object *o, tecs **ecs) {
int tris = haloo3d_facef_clip(face, outfaces); int tris = haloo3d_facef_clip(face, outfaces);
if (tris > 0) { if (tris > 0) {
uint8_t oflags = rsettings.flags; uint8_t oflags = rsettings.flags;
if (o->lighting) { if (o->lighting && facei < o->stoplighting) {
haloo3d_obj_facef(o->model, o->model->faces[facei], baseface); haloo3d_obj_facef(o->model, o->model->faces[facei], baseface);
rsettings.intensity = rsettings.intensity =
haloo3d_calc_light(lighting.v, o->lighting->minlight, baseface); 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); gen_circle_model(cloudmodel, CLOUDCOL, 32);
haloo3d_obj *redflower = haloo3d_easystore_addobj(&ecs.storage, REDFLOWERKEY); haloo3d_obj *redflower = haloo3d_easystore_addobj(&ecs.storage, REDFLOWERKEY);
gen_flower(redflower, REDFLOWERCOL); 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); eprintf("Setup ECS system + obj/tex storage (%d bytes)\n", tecs_size);

View File

@ -51,7 +51,7 @@ typedef struct {
object_lighting *lighting; // a pointer to lighting, null for none object_lighting *lighting; // a pointer to lighting, null for none
haloo3d_obj *model; haloo3d_obj *model;
haloo3d_fb *texture; haloo3d_fb *texture;
// uint16_t stoplighting; uint16_t stoplighting;
uint8_t cullbackface; // Whether to cull backfaces (you probably should) uint8_t cullbackface; // Whether to cull backfaces (you probably should)
uint8_t contextcount; // How many contexts we render into uint8_t contextcount; // How many contexts we render into
float flatdither; // A flat dither amount. Set to -1 to disable float flatdither; // A flat dither amount. Set to -1 to disable