Working on ps2 for real (terrain)

This commit is contained in:
Carlos Sanchez 2024-09-23 23:39:34 -04:00
parent 0bc14e3ea8
commit e72d10925c
5 changed files with 98 additions and 69 deletions

View File

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
set -e
if [ "$#" -lt "1" ]; then if [ "$#" -lt "1" ]; then
echo "ERROR: You must pass the program name! For instance, to build maze.elf," echo "ERROR: You must pass the program name! For instance, to build maze.elf,"
echo "pass in just 'maze'" echo "pass in just 'maze'"

3
modeleditor/go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitlumen.sh/haloopdy/3dtoys/modeleditor
go 1.23.0

9
modeleditor/main.go Normal file
View File

@ -0,0 +1,9 @@
package main
import (
"fmt"
)
func main() {
fmt.Printf("Program starting\n")
}

BIN
modeleditor/modeleditor Executable file

Binary file not shown.

147
terrain.c
View File

@ -36,19 +36,34 @@
#include <time.h> #include <time.h>
// #define LIMITEDPRINT // #define LIMITEDPRINT
// #define NOPRINT
#ifdef _EE #ifdef _EE
#define WIDTH 160 #define WIDTH 160
#define HEIGHT 120 #define HEIGHT 120
#define SCREENSCALE 4 #define SCREENSCALE 4
#define VIEWDISTANCE 2 // MAX IS AROUND 12
#define MAXTREEPERCELL 4
#define MAXTREEACROSS 2
#define CHUNKINITFACES (H3D_OBJ_MAXFACES / 2)
#define CHUNKINITVERTS (H3D_OBJ_MAXVERTICES / 2)
#define INIT_DITHEREND 999
#define INIT_DITHERSTART 999
#else #else
#define WIDTH 480 #define WIDTH 480
#define HEIGHT 300 #define HEIGHT 300
#define SCREENSCALE 2 #define SCREENSCALE 2
#define VIEWDISTANCE 10 // MAX IS AROUND 12
#define MAXTREEPERCELL 9
#define MAXTREEACROSS 3
#define CHUNKINITFACES H3D_OBJ_MAXFACES
#define CHUNKINITVERTS H3D_OBJ_MAXVERTICES
#define INIT_DITHEREND H3DVF(VIEWDISTANCE - 1.0)
#define INIT_DITHERSTART H3DVF(INIT_DITHEREND * 0.5)
#endif #endif
#define SWIDTH (WIDTH * SCREENSCALE) #define SWIDTH (WIDTH * SCREENSCALE)
#define SHEIGHT (HEIGHT * SCREENSCALE) #define SHEIGHT (HEIGHT * SCREENSCALE)
#define AVGWEIGHT 0.85 #define AVGWEIGHT H3DVF(0.85)
#define DEFAULTUP \ #define DEFAULTUP \
{ .x = 0, .y = 1, .z = 0 } { .x = 0, .y = 1, .z = 0 }
// Lookvec for objects which all face forward along with the player // Lookvec for objects which all face forward along with the player
@ -57,16 +72,15 @@
// How big each chunk is (x and y, square). MUST BE POWER OF 2 - 1 // How big each chunk is (x and y, square). MUST BE POWER OF 2 - 1
#define CHUNKSIZE 7 // This is the width in tiles #define CHUNKSIZE 7 // This is the width in tiles
#define CHUNKSCALE (1.0 / CHUNKSIZE) #define CHUNKSCALE H3DVF(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 10 // MAX IS AROUND 12
#define PLBOXEDGE (VIEWDISTANCE * 2 + 1) #define PLBOXEDGE (VIEWDISTANCE * 2 + 1)
#define MAXCHUNKPERFRAME PLBOXEDGE #define MAXCHUNKPERFRAME PLBOXEDGE
// Generation consts / things for looks // Generation consts / things for looks
#define COLOREDTERRAIN // Some cost, not much #define COLOREDTERRAIN // Some cost, not much
#define LANDHEIGHT 4.0 #define LANDHEIGHT H3DVF(4.0)
#define LANDCOL 0xF7C3 // 0xF4E1 #define LANDCOL 0xF7C3 // 0xF4E1
#define FLANDCOL 0xF260 // 0xF3A0 // 0xF4E1 #define FLANDCOL 0xF260 // 0xF3A0 // 0xF4E1
#define UWLANDCOL 0xF026 #define UWLANDCOL 0xF026
@ -81,40 +95,36 @@
#define TOWERCOL 0xF111 #define TOWERCOL 0xF111
#define LIGHTCOL 0xFF00 #define LIGHTCOL 0xFF00
#define SANDCOL 0xFDD8 #define SANDCOL 0xFDD8
#define TOWERLOW 0.755 // small changes to this make a huge difference #define TOWERLOW H3DVF(0.755) // small changes to this make a huge difference
#define TOWERLIGHTMINSIZE (CHUNKSCALE * 0.1) #define TOWERLIGHTMINSIZE H3DVF(CHUNKSCALE * 0.1)
#define TOWERLIGHTMAXSIZE (CHUNKSCALE * 0.3) #define TOWERLIGHTMAXSIZE H3DVF(CHUNKSCALE * 0.3)
#define TOWERLIGHTMINTRANS 0 #define TOWERLIGHTMINTRANS H3DVF(0)
#define TOWERLIGHTMAXTRANS 0.4 #define TOWERLIGHTMAXTRANS H3DVF(0.4)
#define TOWERLIGHTRATE 1.5 #define TOWERLIGHTRATE H3DVF(1.5)
#define SANDSTART 0.085 #define SANDSTART H3DVF(0.085)
#define TREEBOTTOM -0.333 #define TREEBOTTOM H3DVF(-0.333)
#define TREETRUNKWIDTH 0.2 #define TREETRUNKWIDTH H3DVF(0.20)
#define SEATRANS 0.75 #define SEATRANS H3DVF(0.5)
#define CLOUDTRANSMIN 0.1 #define CLOUDTRANSMIN H3DVF(0.1)
#define CLOUDTRANSMAX 0.2 #define CLOUDTRANSMAX H3DVF(0.2)
#define CLOUDHEIGHT 2.0 #define CLOUDHEIGHT H3DVF(1 + VIEWDISTANCE * 0.1)
#define CLOUDSTRETCH 2 #define CLOUDSTRETCH H3DVF(2)
#define CLOUDCHANCE 0.1 #define CLOUDCHANCE H3DVF(0.1)
#define INIT_LIGHTPITCH (MPI_2 * 1.65) #define INIT_LIGHTPITCH H3DVF(MPI_2 * 1.65)
#define INIT_LIGHTYAW (MPI_2 * 0.65) #define INIT_LIGHTYAW H3DVF(MPI_2 * 0.65)
#define INIT_MINLIGHT 0.2 #define INIT_MINLIGHT H3DVF(0.2)
#define INIT_DITHEREND (VIEWDISTANCE - 1.0) #define MAXTREECLOSENESS H3DVF(0.2)
#define INIT_DITHERSTART (INIT_DITHEREND * 0.5)
#define MAXTREEPERCELL 9
#define MAXTREEACROSS 3
#define MAXTREECLOSENESS 0.2
// Player things // Player things
#define INIT_FOV 90.0 #define INIT_FOV H3DVF(90.0)
#define INIT_CAMPITCH (MPI_2 * 1.3) // MPI_2 * 1.6 // 1.575 #define INIT_CAMPITCH H3DVF(MPI_2 * 1.3) // MPI_2 * 1.6 // 1.575
#define INIT_PLAYERHEIGHT 0.65 #define INIT_PLAYERHEIGHT H3DVF(0.65)
#define INIT_NEARCLIP 0.01 #define INIT_NEARCLIP H3DVF(0.010)
#define INIT_FARCLIP 100.0 #define INIT_FARCLIP H3DVF(100.0)
#define INIT_PLAYERSPEED \ #define INIT_PLAYERSPEED \
{ .x = 0, .y = 0, .z = -1.5 } { .x = 0, .y = 0, .z = -1.5 }
#define RANDF() ((float)rand() / RAND_MAX) #define RANDF() ((float)rand() / H3DVF(RAND_MAX))
#define OFLOOR(v) (floor(fabs(v)) * ((v) < 0 ? -1 : 1)) #define OFLOOR(v) (floor(fabs(v)) * ((v) < 0 ? -1 : 1))
// Some globals you can mess around with potentially // Some globals you can mess around with potentially
@ -152,9 +162,9 @@ static inline mfloat_t calc_barycentric(mfloat_t *point, mfloat_t *values,
void gen_tower(haloo3d_obj *model, haloo3d_obj *tower, tecs *ecs, void gen_tower(haloo3d_obj *model, haloo3d_obj *tower, tecs *ecs,
render_context *ctx, struct vec3i chunk, struct vec3 pos) { render_context *ctx, struct vec3i chunk, struct vec3 pos) {
const float scale = 0.5; const float scale = 0.5;
pos.x += 0.5; pos.x += H3DVF(0.5);
pos.y += scale; pos.y += scale;
pos.z -= 0.5; pos.z -= H3DVF(0.5);
haloo3d_obj_addobj(model, tower, pos, (struct vec3)DEFAULTLOOK, haloo3d_obj_addobj(model, tower, pos, (struct vec3)DEFAULTLOOK,
(struct vec3)DEFAULTUP, (struct vec3)DEFAULTUP,
(struct vec3){.x = scale, .y = scale, .z = scale}); (struct vec3){.x = scale, .y = scale, .z = scale});
@ -175,7 +185,7 @@ void gen_tower(haloo3d_obj *model, haloo3d_obj *tower, tecs *ecs,
.flatdither = 0.2, .flatdither = 0.2,
.texture = haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY), .texture = haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY),
.scale = {.x = CHUNKSCALE, .y = CHUNKSCALE, .z = CHUNKSCALE}, .scale = {.x = CHUNKSCALE, .y = CHUNKSCALE, .z = CHUNKSCALE},
.lighting = NULL, //&ecs->chunklight, .lighting = NULL,
.stoplighting = 0, .stoplighting = 0,
.model = light, .model = light,
.cullbackface = 0, .cullbackface = 0,
@ -187,17 +197,19 @@ void gen_cloud(render_context *ctx, tecs *ecs, struct vec3i pos) {
haloo3d_obj *cloud = haloo3d_easystore_getobj(&ecs->storage, CLOUDKEY); haloo3d_obj *cloud = haloo3d_easystore_getobj(&ecs->storage, CLOUDKEY);
ecs_eid cloudid = tecs_newentity(ecs, 0); ecs_eid cloudid = tecs_newentity(ecs, 0);
ECS_SETCOMPONENT(ecs, cloudid, ecs_playergarbage){}; ECS_SETCOMPONENT(ecs, cloudid, ecs_playergarbage){};
ECS_SETCOMPONENT(ecs, cloudid, ECS_SETCOMPONENT(ecs, cloudid, ecs_placement){
ecs_placement){.up = DEFAULTUP, .up = DEFAULTUP,
.lookvec = DEFAULTLOOK, .lookvec = DEFAULTLOOK,
.pos = {.x = pos.x + RANDF(), .pos = {.x = pos.x + RANDF(),
.y = CLOUDHEIGHT + 0.5 * RANDF(), .y = CLOUDHEIGHT + H3DVF(0.5) * RANDF(),
.z = pos.z - 0.9}}; .z = pos.z - 0.9}};
ECS_SETCOMPONENT(ecs, cloudid, ecs_object){ ECS_SETCOMPONENT(ecs, cloudid, ecs_object){
.flatdither = CLOUDTRANSMIN + (CLOUDTRANSMAX - CLOUDTRANSMIN) * RANDF(), .flatdither = CLOUDTRANSMIN + (CLOUDTRANSMAX - CLOUDTRANSMIN) * RANDF(),
.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 = H3DVF(1) + RANDF(),
.lighting = NULL, //&ecs->chunklight, .y = H3DVF(1.0 / CLOUDSTRETCH),
.z = 1},
.lighting = NULL,
.stoplighting = 0, .stoplighting = 0,
.model = cloud, .model = cloud,
.cullbackface = 0, .cullbackface = 0,
@ -215,23 +227,22 @@ uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model, tecs *ecs,
flowers[0] = haloo3d_easystore_getobj(storage, REDFLOWERKEY); flowers[0] = haloo3d_easystore_getobj(storage, REDFLOWERKEY);
flowers[1] = haloo3d_easystore_getobj(storage, YELLOWFLOWERKEY); 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, CHUNKINITFACES, CHUNKINITVERTS);
#define LCOLSTEP 8 #define LCOLSTEP 8
// uint16_t sandcol = COMCOL(model, 0xFFF7); // uint16_t sandcol = COMCOL(model, 0xFFF7);
uint16_t landcols[(LCOLSTEP + 1) * 2]; uint16_t landcols[(LCOLSTEP + 1) * 2];
for (int i = 0; i <= LCOLSTEP; i++) { for (int i = 0; i <= LCOLSTEP; i++) {
landcols[i] = landcols[i] = COMCOL(
COMCOL(model, haloo3d_col_lerp(LANDCOL, FLANDCOL, model, haloo3d_col_lerp(LANDCOL, FLANDCOL, (float)i / H3DVF(LCOLSTEP)));
(float)i / LCOLSTEP)); // 0xF2C0);
landcols[i + LCOLSTEP + 1] = landcols[i + LCOLSTEP + 1] =
// 0xFDE6 // 0xFDE6
COMCOL(model, haloo3d_col_lerp(SANDCOL, UWLANDCOL, COMCOL(model, haloo3d_col_lerp(SANDCOL, UWLANDCOL,
(float)i / LCOLSTEP)); // 0xF2C0); (float)i / H3DVF(LCOLSTEP)));
} }
fnl_state ns = fnlCreateState(); fnl_state ns = fnlCreateState();
ns.noise_type = FNL_NOISE_OPENSIMPLEX2; ns.noise_type = FNL_NOISE_OPENSIMPLEX2;
ns.seed = gen_terrain_seed; ns.seed = gen_terrain_seed;
ns.frequency = 0.025; ns.frequency = H3DVF(0.025);
ns.fractal_type = FNL_FRACTAL_RIDGED; ns.fractal_type = FNL_FRACTAL_RIDGED;
float noisex = pos.x * CHUNKSIZE; float noisex = pos.x * CHUNKSIZE;
float noisey = -pos.z * CHUNKSIZE; float noisey = -pos.z * CHUNKSIZE;
@ -269,8 +280,8 @@ uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model, tecs *ecs,
float lowys[2]; float lowys[2];
float highys[2]; float highys[2];
for (int t = 0; t < 2; t++) { for (int t = 0; t < 2; t++) {
lowys[t] = 999; lowys[t] = H3DVF(999);
highys[t] = -999; highys[t] = H3DVF(-999);
for (int v = 0; v < 3; v++) { for (int v = 0; v < 3; v++) {
int ofs = t * 3; int ofs = t * 3;
// NOTE: WE SWTICH Y AND Z TO MAKE THE EDGE FUNCTION WORK // NOTE: WE SWTICH Y AND Z TO MAKE THE EDGE FUNCTION WORK
@ -290,7 +301,7 @@ uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model, tecs *ecs,
if (highys[t] < SANDSTART) { if (highys[t] < SANDSTART) {
landcol = LCOLSTEP + 1 + LCOLSTEP * fabs(highys[t]); landcol = LCOLSTEP + 1 + LCOLSTEP * fabs(highys[t]);
} else { } else {
landcol = LCOLSTEP * fabs(lowys[t]) * (1 + nlayer) * 0.5; landcol = LCOLSTEP * fabs(lowys[t]) * (H3DVF(1) + nlayer) * H3DVF(0.5);
} }
for (int v = 0; v < 3; v++) { for (int v = 0; v < 3; v++) {
model->faces[faces[t]][v].texi = landcols[landcol]; model->faces[faces[t]][v].texi = landcols[landcol];
@ -303,19 +314,19 @@ uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model, tecs *ecs,
// 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
struct vec2 treepos = { struct vec2 treepos = {
.x = triedge[0].x + .x = triedge[0].x + H3DVF(1.0 / MAXTREEACROSS) *
(1.0 / MAXTREEACROSS) * ((t % MAXTREEACROSS) +
((t % MAXTREEACROSS) + (1.0 - MAXTREECLOSENESS) * RANDF()), (H3DVF(1.0) - MAXTREECLOSENESS) * RANDF()),
.y = triedge[0].y - .y = triedge[0].y - (H3DVF(1.0 / MAXTREEACROSS) *
((1.0 / MAXTREEACROSS) * ((int)(t / MAXTREEACROSS) + ((int)(t / MAXTREEACROSS) +
(1.0 - MAXTREECLOSENESS) * RANDF()))}; (H3DVF(1.0) - MAXTREECLOSENESS) * RANDF()))};
float y = calc_barycentric(treepos.v, heights, triedge, 2); float y = calc_barycentric(treepos.v, heights, triedge, 2);
float pick = RANDF(); // pow(RANDF(), 2); float pick = RANDF(); // pow(RANDF(), 2);
float nlayer = fnlGetNoise2D(&ns, noisex + treepos.x, noisey + treepos.y); float nlayer = fnlGetNoise2D(&ns, noisex + treepos.x, noisey + treepos.y);
if (pick < y * (1 + nlayer) * 0.5) { if (pick < y * (H3DVF(1) + nlayer) * H3DVF(0.5)) {
// Generate tree // Generate tree
float scale = 0.1 + RANDF() * 0.05; float scale = H3DVF(0.1) + RANDF() * H3DVF(0.05);
float height = 0.5 + RANDF() * 0.5; float height = H3DVF(0.5) + RANDF() * H3DVF(0.5);
haloo3d_obj_addobj( haloo3d_obj_addobj(
model, tree, model, tree,
(struct vec3){ (struct vec3){
@ -323,14 +334,16 @@ uint16_t gen_terrain(struct vec3i pos, haloo3d_obj *model, tecs *ecs,
(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});
treecount++; treecount++;
} else if ((rand() & 3) == 0 && y < 0.4 && y > 0 && RANDF() < -nlayer) { } else if ((rand() & 3) == 0 && y < H3DVF(0.4) && y > H3DVF(0) &&
RANDF() < -nlayer) {
// Generate flower of random-ish color // Generate flower of random-ish color
float scale = 0.04; float scale = H3DVF(0.04);
float vertscale = scale * (1 - 0.5 * RANDF()); float vertscale = scale * (H3DVF(1) - H3DVF(0.5) * RANDF());
haloo3d_obj_addobj( haloo3d_obj_addobj(
&nl_model, flowers[nlayer > -0.9], &nl_model, flowers[nlayer > H3DVF(-0.9)],
(struct vec3){ (struct vec3){.x = treepos.x,
.x = treepos.x, .y = y + vertscale * 0.5, .z = treepos.y}, .y = y + vertscale * H3DVF(0.5),
.z = treepos.y},
(struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP, (struct vec3)DEFAULTLOOK, (struct vec3)DEFAULTUP,
(struct vec3){.x = scale, .y = vertscale, .z = scale}); (struct vec3){.x = scale, .y = vertscale, .z = scale});
} }
@ -865,6 +878,7 @@ int main() { // int argc, char **argv) {
// Scale 3D into final buffer // Scale 3D into final buffer
haloo3d_fb_fill(&screen, &context.window); haloo3d_fb_fill(&screen, &context.window);
#ifndef NOPRINT
// clang-format off // clang-format off
haloo3d_print(&pt, haloo3d_print(&pt,
"Pframe: %05.2f (%05.2f) DT: %0.3f\n" "Pframe: %05.2f (%05.2f) DT: %0.3f\n"
@ -881,7 +895,8 @@ int main() { // int argc, char **argv) {
playerpos->pos.x, playerpos->pos.z, playerpos->pos.x, playerpos->pos.z,
#endif #endif
ecs.totaldrawn); ecs.totaldrawn);
// clang-format on // clang-format on
#endif
// Finally, actually put buffer onto screen // Finally, actually put buffer onto screen
haloo3d_easytimer_start(&sdltimer); haloo3d_easytimer_start(&sdltimer);