Fixed everything? Time for terrain!

This commit is contained in:
Carlos Sanchez 2024-09-14 04:34:19 -04:00
parent 082a084971
commit c0f1685cbb
3 changed files with 26 additions and 24 deletions

@ -1 +1 @@
Subproject commit cb50aaccff0328f21b7c4b9a54ab59ab6b6800ac
Subproject commit 2edcfd6d8340abdd26670f4ac5d911de6e65d7be

View File

@ -27,9 +27,9 @@
// { .x = 1.0, .y = 1.0, .z = 1.0 }
#define CHUNKSIZE 8 // How big each chunk is (x and y, square)
#define CHUNKSCALE 1.0 / CHUNKSIZE
#define VIEWDISTANCE 5
#define PLBOXEDGE VIEWDISTANCE * 2 + 1
#define MAXCHUNKPERFRAME 10
#define VIEWDISTANCE 10
#define PLBOXEDGE (VIEWDISTANCE * 2 + 1)
#define MAXCHUNKPERFRAME 100
// These are initial values but there may be ways to change it
#define CAM_INITPITCH MPI_2
@ -46,7 +46,7 @@
{ .x = 0, .y = 0, .z = -0.5 }
// Some globals you can mess around with potentially
int fps = 30;
int fps = 60;
#define PALETTEKEY "palette"
@ -60,8 +60,8 @@ void fastface(haloo3d_facei face, uint16_t tex, uint16_t v0, uint16_t v1,
face[2].posi = v2;
}
void gen_terrain(struct vec2i pos, haloo3d_obj *model) {
eprintf("Generating terrain at %d,%d\n", pos.x, pos.y);
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 * CHUNKSIZE * 4,
CHUNKSIZE * CHUNKSIZE * 4);
@ -69,7 +69,7 @@ void gen_terrain(struct vec2i pos, haloo3d_obj *model) {
// haloo3d_gen_paletteuv(0xF26F, model->vtexture[model->numvtextures++].v);
struct vec3 seacol = haloo3d_gen_paletteuv(0xF000 | (rand() & 0xFFF));
int seauv = haloo3d_obj_addvtexture(model, seacol);
eprintf("SEAUV: %f,%f\n", seacol.x, seacol.y);
// eprintf("SEAUV: %f,%f\n", seacol.x, seacol.y);
// 0xF26F));
// clang-format off
// REMEMBER: NEGATIVE Z IS FORWARD
@ -84,14 +84,14 @@ void gen_terrain(struct vec2i pos, haloo3d_obj *model) {
fastface(face, seauv, br, tr, tl);
haloo3d_obj_addface(model, face);
model->numfaces += 2;
haloo3d_obj_shrinktofit(model);
// haloo3d_obj_shrinktofit(model);
}
// Generate the entity/components/terrain for the chunk at the given
// coordinates.
// NOTE: chunks are per x/y tile (terrain inside is just scaled
// down to fit inside the 1x1 box)
void gen_chunk(render_context *ctx, tecs *ecs, struct vec2i pos) {
void gen_chunk(render_context *ctx, tecs *ecs, struct vec3i pos) {
// eprintf("Generating chunk at %d,%d\n", pos.x, pos.y);
ecs_eid id = tecs_newentity(ecs, 0);
ecs_playergarbage garbage;
@ -104,13 +104,13 @@ void gen_chunk(render_context *ctx, tecs *ecs, struct vec2i pos) {
ECS_SETCOMPONENT(ecs, id,
ecs_placement){.up = DEFAULTUP,
.lookvec = DEFAULTLOOK,
.pos = {.x = pos.x, .y = 0, .z = pos.y}};
.pos = {.x = pos.x, .y = pos.y, .z = pos.z}};
// TODO: an easy place for optimization (if it's even needed)
// 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},
.lighting = NULL, //&ecs->chunklight,
.lighting = &ecs->chunklight,
.model = model,
.cullbackface = 1,
.context = {ctx},
@ -118,7 +118,9 @@ void gen_chunk(render_context *ctx, tecs *ecs, struct vec2i pos) {
}
void player_chunkload(ecs_placement *ppos, render_context *ctx, tecs *ecs) {
struct vec2i pchunk = {.x = floor(ppos->pos.x), .y = floor(ppos->pos.z)};
struct vec3i pchunk = {.x = floor(ppos->pos.x),
.y = floor(ppos->pos.y),
.z = floor(ppos->pos.z)};
// This is our little array of "filled chunks" around the player. We
// use this to figure out what to generate
ecs_eid surround[PLBOXEDGE][PLBOXEDGE] = {0};
@ -135,7 +137,7 @@ void player_chunkload(ecs_placement *ppos, render_context *ctx, tecs *ecs) {
// If this object is outside the view distance, we no longer have access to
// it
if (fabs(floor(placement->pos.x) - pchunk.x) > VIEWDISTANCE ||
fabs(floor(placement->pos.y) - pchunk.y) > VIEWDISTANCE) {
fabs(floor(placement->pos.z) - pchunk.z) > VIEWDISTANCE) {
// OK, reduce the chunk refcount, since we're no longer close to it
obj->contextcount--;
}
@ -143,22 +145,22 @@ void player_chunkload(ecs_placement *ppos, render_context *ctx, tecs *ecs) {
if (ECS_HASCOMPONENT(ecs, matches[i], ecs_chunk)) {
ecs_chunk *chunk = &ECS_GETCOMPONENT(ecs, matches[i], ecs_chunk);
int surx = chunk->pos.x - pchunk.x + VIEWDISTANCE;
int sury = chunk->pos.y - pchunk.y + VIEWDISTANCE;
if (surx >= 0 && sury >= 0 && surx < PLBOXEDGE && sury < PLBOXEDGE) {
int surz = chunk->pos.z - pchunk.z + VIEWDISTANCE;
if (surx >= 0 && surz >= 0 && surx < PLBOXEDGE && surz < PLBOXEDGE) {
// eprintf("SUR: %d %d\n", surx, sury);
surround[sury][surx] = 1; // matches[i];
surround[surz][surx] = 1; // matches[i];
}
}
}
int chunkgen = 0;
// We now have our view radius box and which chunks are not loaded. Up to
// some amount, let's load them
for (int y = pchunk.y - VIEWDISTANCE; y <= pchunk.y + VIEWDISTANCE; y++) {
for (int x = pchunk.x - VIEWDISTANCE; x <= pchunk.x + PLBOXEDGE; x++) {
for (int z = pchunk.z - VIEWDISTANCE; z <= pchunk.z + VIEWDISTANCE; z++) {
for (int x = pchunk.x - VIEWDISTANCE; x <= pchunk.x + VIEWDISTANCE; x++) {
int surx = x - pchunk.x + VIEWDISTANCE;
int sury = y - pchunk.y + VIEWDISTANCE;
if (!surround[sury][surx]) {
gen_chunk(ctx, ecs, (struct vec2i){.x = x, .y = y});
int surz = z - pchunk.z + VIEWDISTANCE;
if (!surround[surz][surx]) {
gen_chunk(ctx, ecs, (struct vec3i){.x = x, .y = 0, .z = z});
//.y = pchunk.y - VIEWDISTANCE + y});
if (++chunkgen >= MAXCHUNKPERFRAME) {
break;
@ -375,7 +377,7 @@ int main() { // int argc, char **argv) {
haloo3d_easytimer_init(&sdltimer, AVGWEIGHT);
render_context context;
context.windowclear = 0xFF00;
context.windowclear = 0xF333;
context.nearclip = INIT_NEARCLIP;
context.farclip = INIT_FARCLIP;
context.fov = INIT_FOV;

View File

@ -94,7 +94,7 @@ typedef struct {
// should be integer aligned. Not necessarily 1:1 with world
// coordinates?
typedef struct {
struct vec2i pos;
struct vec3i pos;
// uint8_t generation; // The type(?) of generation (idk honestly)
} ecs_chunk;