From a1182995ae24cea61b216748cc8ade33c41611cb Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Fri, 20 Sep 2024 19:33:53 -0400 Subject: [PATCH] Better tree spawning (getting mem error) --- commonobj.c | 70 +++++++++++++++++++++++++++ terrain.c | 135 ++++++++++++++++++++++++++-------------------------- 2 files changed, 137 insertions(+), 68 deletions(-) create mode 100644 commonobj.c diff --git a/commonobj.c b/commonobj.c new file mode 100644 index 0000000..350058d --- /dev/null +++ b/commonobj.c @@ -0,0 +1,70 @@ +#include "haloo3d/haloo3d.h" +#include "haloo3d/haloo3dex_gen.h" +#include "haloo3d/haloo3dex_obj.h" + +#ifndef TREECOL +#define TREECOL 0xF070 +#endif +#ifndef TREETRUNKCOL +#define TREETRUNKCOL 0xF950 +#endif +#ifndef TREEBOTTOM +#define TREEBOTTOM -0.333 +#endif +#ifndef TREETOPWIDTH +#define TREETOPWIDTH 1.0 +#endif +#ifndef TREETRUNKWIDTH +#define TREETRUNKWIDTH 0.2 +#endif + +// Fill a face with a solid color given by the tex +void fastface(haloo3d_facei face, uint16_t tex, uint16_t v0, uint16_t v1, + uint16_t v2) { + face[0].texi = tex; + face[1].texi = tex; + face[2].texi = tex; + face[0].posi = v0; + face[1].posi = v1; + face[2].posi = v2; +} + +// Generate a basic tree model +void gen_tree_model(haloo3d_obj *tree) { + haloo3d_obj_resetfixed(tree, 12, 24); + struct vec3 treecol = haloo3d_gen_paletteuv(TREECOL); + int treeuv = haloo3d_obj_addvtexture(tree, treecol); + struct vec3 trunkcol = haloo3d_gen_paletteuv(TREETRUNKCOL); + int trunkuv = haloo3d_obj_addvtexture(tree, trunkcol); + // clang-format off + uint16_t top = haloo3d_obj_addvertex(tree, + (struct vec4){.x = 0, .y = 1, .z = 0, .w = 1}); + uint16_t tree1 = haloo3d_obj_addvertex(tree, + (struct vec4){.x = -TREETOPWIDTH, .y = TREEBOTTOM, .z = -TREETOPWIDTH, .w = 1}); + uint16_t tree2 = haloo3d_obj_addvertex(tree, + (struct vec4){.x = TREETOPWIDTH, .y = TREEBOTTOM, .z = -TREETOPWIDTH, .w = 1}); + uint16_t tree3 = haloo3d_obj_addvertex(tree, + (struct vec4){.x = 0, .y = TREEBOTTOM, .z = TREETOPWIDTH, .w = 1}); + uint16_t trunk1 = haloo3d_obj_addvertex(tree, + (struct vec4){ .x = -TREETRUNKWIDTH, .y = -1, .z = -TREETRUNKWIDTH, .w = 1}); + uint16_t trunk2 = haloo3d_obj_addvertex(tree, + (struct vec4){ .x = TREETRUNKWIDTH, .y = -1, .z = -TREETRUNKWIDTH, .w = 1}); + uint16_t trunk3 = haloo3d_obj_addvertex(tree, + (struct vec4){.x = 0, .y = -1, .z = TREETRUNKWIDTH, .w = 1}); + // clang-format on + + haloo3d_facei face; + fastface(face, treeuv, top, tree1, tree3); + haloo3d_obj_addface(tree, face); + fastface(face, treeuv, top, tree3, tree2); + haloo3d_obj_addface(tree, face); + fastface(face, treeuv, top, tree2, tree1); + haloo3d_obj_addface(tree, face); + fastface(face, trunkuv, top, trunk1, trunk3); + haloo3d_obj_addface(tree, face); + fastface(face, trunkuv, top, trunk3, trunk2); + haloo3d_obj_addface(tree, face); + fastface(face, trunkuv, top, trunk2, trunk1); + haloo3d_obj_addface(tree, face); + haloo3d_obj_shrinktofit(tree); +} diff --git a/terrain.c b/terrain.c index c2e348a..6c6800d 100644 --- a/terrain.c +++ b/terrain.c @@ -36,7 +36,7 @@ #define INIT_NEARCLIP 0.01 #define INIT_FARCLIP 100.0 -#define MAXCHUNKPERFRAME 50 +#define MAXCHUNKPERFRAME PLBOXEDGE // Generation consts / things for looks #define LANDCOL 0xF6D2 // 0xF4E1 @@ -69,57 +69,10 @@ int fps = 60; #define PALETTEKEY "palette" #define TREEKEY "tree" -void fastface(haloo3d_facei face, uint16_t tex, uint16_t v0, uint16_t v1, - uint16_t v2) { - face[0].texi = tex; - face[1].texi = tex; - face[2].texi = tex; - face[0].posi = v0; - face[1].posi = v1; - face[2].posi = v2; -} +#include "commonobj.c" static int gen_terrain_seed = 0; -void gen_tree_model(haloo3d_obj *tree) { - haloo3d_obj_resetfixed(tree, 12, 24); - struct vec3 treecol = haloo3d_gen_paletteuv(TREECOL); - int treeuv = haloo3d_obj_addvtexture(tree, treecol); - struct vec3 trunkcol = haloo3d_gen_paletteuv(TREETRUNKCOL); - int trunkuv = haloo3d_obj_addvtexture(tree, trunkcol); - uint16_t top = haloo3d_obj_addvertex( - tree, (struct vec4){.x = 0, .y = 1, .z = 0, .w = 1}); - uint16_t tree1 = haloo3d_obj_addvertex( - tree, (struct vec4){.x = -1, .y = TREEBOTTOM, .z = -1, .w = 1}); - uint16_t tree2 = haloo3d_obj_addvertex( - tree, (struct vec4){.x = 1, .y = TREEBOTTOM, .z = -1, .w = 1}); - uint16_t tree3 = haloo3d_obj_addvertex( - tree, (struct vec4){.x = 0, .y = TREEBOTTOM, .z = 1, .w = 1}); - uint16_t trunk1 = haloo3d_obj_addvertex( - tree, (struct vec4){ - .x = -TREETRUNKWIDTH, .y = -1, .z = -TREETRUNKWIDTH, .w = 1}); - uint16_t trunk2 = haloo3d_obj_addvertex( - tree, (struct vec4){ - .x = TREETRUNKWIDTH, .y = -1, .z = -TREETRUNKWIDTH, .w = 1}); - uint16_t trunk3 = haloo3d_obj_addvertex( - tree, (struct vec4){.x = 0, .y = -1, .z = TREETRUNKWIDTH, .w = 1}); - - haloo3d_facei face; - fastface(face, treeuv, top, tree1, tree3); - haloo3d_obj_addface(tree, face); - fastface(face, treeuv, top, tree3, tree2); - haloo3d_obj_addface(tree, face); - fastface(face, treeuv, top, tree2, tree1); - haloo3d_obj_addface(tree, face); - fastface(face, trunkuv, top, trunk1, trunk3); - haloo3d_obj_addface(tree, face); - fastface(face, trunkuv, top, trunk3, trunk2); - haloo3d_obj_addface(tree, face); - fastface(face, trunkuv, top, trunk2, trunk1); - haloo3d_obj_addface(tree, face); - haloo3d_obj_shrinktofit(tree); -} - void gen_terrain(struct vec3i pos, haloo3d_obj *model, haloo3d_easystore *storage) { eprintf("Generating terrain at %d,%d\n", pos.x, pos.z); @@ -155,28 +108,74 @@ void gen_terrain(struct vec3i pos, haloo3d_obj *model, uint16_t tl = i + CHUNKVSIZE; uint16_t tr = i + CHUNKVSIZE + 1; haloo3d_facei face; + uint16_t faces[2]; fastface(face, landuv, bl, br, tl); - haloo3d_obj_addface(model, face); + faces[0] = haloo3d_obj_addface(model, face); fastface(face, landuv, br, tr, tl); - haloo3d_obj_addface(model, face); - struct vec4 corn = model->vertices[bl]; - if (corn.y > 0.1) { - float scale = 0.1 + RANDF() * 0.05; - float height = 0.5 + RANDF() * 0.5; - // for (int b = 1; b <= 4; b++) { - haloo3d_obj_addobj( - model, tree, - (struct vec3){.x = corn.x, .y = corn.y + scale * height, .z = corn.z}, - (struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP, - (struct vec3){.x = scale, .y = scale * height, .z = scale}); - //} - // haloo3d_obj_addobj( - // model, tree, - // (struct vec3){ - // .x = corn.x, .y = corn.y + scale * height * 2, .z = corn.z}, - // (struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP, - // (struct vec3){.x = scale, .y = scale * height, .z = scale}); + faces[1] = haloo3d_obj_addface(model, face); + struct vec3 triedge[2][3]; + for (int t = 0; t < 2; t++) { + for (int v = 0; v < 3; v++) { + // NOTE: WE SWTICH Y AND Z TO MAKE THE EDGE FUNCTION WORK + triedge[t][v].x = model->vertices[model->faces[faces[t]][v].posi].x; + triedge[t][v].y = model->vertices[model->faces[faces[t]][v].posi].z; + triedge[t][v].z = model->vertices[model->faces[faces[t]][v].posi].y; + } } + // Generate up to 4 trees per cell (usually not) + for (int t = 0; t < 4; t++) { + // We know that the first vec in the first triangle is bl, which is our + // base to start generating trees + struct vec2 treepos = { + .x = triedge[0][0].x + 0.5 * (t & 1) + 0.3 * RANDF(), + .y = triedge[0][0].y - (0.5 * (t >> 1) + 0.3 * RANDF())}; + mfloat_t we[3]; + for (int i = 0; i < 2; i++) { + we[0] = haloo3d_edgefunc(triedge[i][1].v, triedge[i][2].v, treepos.v); + we[1] = haloo3d_edgefunc(triedge[i][2].v, triedge[i][0].v, treepos.v); + we[2] = haloo3d_edgefunc(triedge[i][0].v, triedge[i][1].v, treepos.v); + if (we[0] < 0 || we[1] < 0 || we[2] < 0) { + // eprintf("BAD: %f,%f, (%f, %f)\n", treepos.x, treepos.y, + // model->vertices[bl].x, model->vertices[bl].z); + continue; + } + // We know this is the triangle to check. Let's figure out a y. + // NOTE: WE SWTICH Y AND Z TO MAKE THE EDGE FUNCTION WORK + float y = we[0] * triedge[i][0].z + we[1] * triedge[i][1].z + + we[2] * triedge[i][2].z; + // eprintf("TREE Y: %f\n", y); + // For now, only generate trees if they're above a certain height + if (RANDF() < y) { + float scale = 0.1 + RANDF() * 0.05; + float height = 0.5 + RANDF() * 0.5; + haloo3d_obj_addobj( + model, tree, + (struct vec3){ + .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}); + } + } + } + + // struct vec4 corn = model->vertices[bl]; + // if (corn.y > 0.1) { + // float scale = 0.1 + RANDF() * 0.05; + // float height = 0.5 + RANDF() * 0.5; + // // for (int b = 1; b <= 4; b++) { + // haloo3d_obj_addobj( + // model, tree, + // (struct vec3){.x = corn.x, .y = corn.y + scale * height, .z = + // corn.z}, (struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP, (struct + // vec3){.x = scale, .y = scale * height, .z = scale}); + // //} + // // haloo3d_obj_addobj( + // // model, tree, + // // (struct vec3){ + // // .x = corn.x, .y = corn.y + scale * height * 2, .z = corn.z}, + // // (struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP, + // // (struct vec3){.x = scale, .y = scale * height, .z = scale}); + // } } // haloo3d_obj_addobj( // model, tree,