diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2023-12-23 21:13:31 +0100 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2023-12-23 21:13:31 +0100 |
commit | 761f81161f8132271d235387466afc15bbedf96e (patch) | |
tree | 946c5ba93e9a96ec5376a240cff52bbf4216c540 | |
parent | 35ad38ef2f1e2488264585abaaf6aa64ae9d37a5 (diff) | |
download | cuddles-761f81161f8132271d235387466afc15bbedf96e.tar.xz |
round cheese
-rw-r--r-- | stage3/cheese_demo.c | 154 |
1 files changed, 105 insertions, 49 deletions
diff --git a/stage3/cheese_demo.c b/stage3/cheese_demo.c index e0127cc..11d8541 100644 --- a/stage3/cheese_demo.c +++ b/stage3/cheese_demo.c @@ -4,7 +4,7 @@ #include "rng.h" #include "clock.h" -static u32 *make_cheese_texture(u32 tex_w, u32 tex_h) +static u32 *make_cheese_texture(u32 tex_w, u32 tex_h, u32 density_min, u32 density_max) { u32 *texture = malloc(tex_h * tex_w * sizeof *texture); for (u32 y = 0; y < tex_h; y++) @@ -12,7 +12,7 @@ static u32 *make_cheese_texture(u32 tex_w, u32 tex_h) texture[y*tex_w+x] = 0xFFFFDE74; } - int holes = 25 + rand() % 5; + int holes = density_min + rand() % (density_max-density_min+1); for (int i = 0; i < holes; i++) { u32 xo = rand() % tex_w; u32 yo = rand() % tex_h; @@ -35,59 +35,111 @@ static u32 *make_cheese_texture(u32 tex_w, u32 tex_h) return texture; } -void cheese_demo() +typedef struct { + usize num_verts; + vertex *vertices; + texture *textures; + u32 *texture; + float transform[4][4]; +} cheese; + +static cheese make_cheese_model(u32 segments, float slice, u32 tex_w, u32 tex_h) { - cheese3d_ctx ctx = cheese3d_create_default(0xFF000000); + static u32 crust_top = 0xFFFFD35F; + static u32 crust_back = 0xFFF2B71B; + + usize num_verts = 12*segments+12; + vertex *vertices = malloc(sizeof *vertices * num_verts); + texture *textures = malloc(sizeof *textures * num_verts); + + float angle = 0.0; + float seg_delta = slice/segments; + + float x = cos(angle); + float z = sin(angle); + + for (usize i = 0; i < segments; i++) { + angle += seg_delta; + float new_x = cos(angle); + float new_z = sin(angle); + + vertex seg_verts[12] = { + // top face + VERT( 0.0, +0.5, 0.0, 0.0, 0.0), + VERT( x, +0.5, z, 0.0, 0.0), + VERT(new_x, +0.5, new_z, 0.0, 0.0), + // bottom face (🥺) + VERT( 0.0, -0.5, 0.0, 0.0, 0.0), + VERT(new_x, -0.5, new_z, 0.0, 0.0), + VERT( x, -0.5, z, 0.0, 0.0), + // back face + QUAD( + VERT( x, +0.5, z, 0.0, 0.0), + VERT( x, -0.5, z, 0.0, 0.0), + VERT(new_x, -0.5, new_z, 0.0, 0.0), + VERT(new_x, +0.5, new_z, 0.0, 0.0) + ) + }; + + for (usize j = 0; j < 12; j++) vertices[i*12+j] = seg_verts[j]; + for (usize j = 0; j < 6; j++) textures[i*12+j] = (texture) { 1, 1, &crust_top }; + for (usize j = 6; j < 12; j++) textures[i*12+j] = (texture) { 1, 1, &crust_back }; + + x = new_x; + z = new_z; + } + + float start_x = cos(0.0); + float start_z = sin(0.0); + + float end_x = cos(slice); + float end_z = sin(slice); - const u32 tex_w = 500; - const u32 tex_h = 375; - - u32 crust_top = 0xFFFFD35F; - u32 crust_back = 0xFFF2B71B; - u32 *crust_right = make_cheese_texture(tex_w, tex_h); - u32 *crust_left = make_cheese_texture(tex_w, tex_h); - - texture textures[24]; - for (int i = 0; i < 6; i++) textures[i] = (texture) { 1, 1, &crust_top }; - for (int i = 6; i < 12; i++) textures[i] = (texture) { 1, 1, &crust_back }; - for (int i = 12; i < 18; i++) textures[i] = (texture) { tex_w, tex_h, crust_right }; - for (int i = 18; i < 24; i++) textures[i] = (texture) { tex_w, tex_h, crust_left }; - - // triangles are counter-clockwise - vertex prism[24] = { - // top face - VERT(-0.5, +0.5, -0.5, 0.0, 0.0), - VERT(+0.5, +0.5, -0.5, 0.0, 0.0), - VERT( 0.0, +0.5, +0.5, 0.0, 0.0), - // bottom face (🥺) - VERT(-0.5, -0.5, -0.5, 0.0, 0.0), - VERT( 0.0, -0.5, +0.5, 0.0, 0.0), - VERT(+0.5, -0.5, -0.5, 0.0, 0.0), - // back face + vertex sides[12] = { + // left face QUAD( - VERT(-0.5, +0.5, -0.5, 0.0, 0.0), - VERT(-0.5, -0.5, -0.5, 0.0, 0.0), - VERT(+0.5, -0.5, -0.5, 0.0, 0.0), - VERT(+0.5, +0.5, -0.5, 0.0, 0.0) + VERT( 0.0, +0.5, 0.0, 0.5, 0.0), + VERT( 0.0, -0.5, 0.0, 0.5, 1.0), + VERT(start_x, -0.5, start_z, 0.0, 1.0), + VERT(start_x, +0.5, start_z, 0.0, 0.0) ), // right face QUAD( - VERT(+0.5, +0.5, -0.5, 0.0, 0.0), - VERT(+0.5, -0.5, -0.5, 0.0, 1.0), - VERT( 0.0, -0.5, +0.5, 1.0, 1.0), - VERT( 0.0, +0.5, +0.5, 1.0, 0.0) - ), - // left face - QUAD( - VERT( 0.0, +0.5, +0.5, 1.0, 0.0), - VERT( 0.0, -0.5, +0.5, 1.0, 1.0), - VERT(-0.5, -0.5, -0.5, 0.0, 1.0), - VERT(-0.5, +0.5, -0.5, 0.0, 0.0) + VERT( end_x, +0.5, end_z, 1.0, 0.0), + VERT( end_x, -0.5, end_z, 1.0, 1.0), + VERT( 0.0, -0.5, 0.0, 0.5, 1.0), + VERT( 0.0, +0.5, 0.0, 0.5, 0.0) ), }; - float angle = 0; + u32 *tex = make_cheese_texture(tex_w*2, tex_h, 40, 50); + u32 off = segments*12; + for (usize i = 0; i < 12; i++) vertices[off+i] = sides[i]; + for (usize i = 0; i < 12; i++) textures[off+i] = (texture) { tex_w*2, tex_h, tex }; + + cheese ch = { + .num_verts = num_verts, + .vertices = vertices, + .textures = textures, + .texture = tex, + .transform = {{ 0.0 }}, + }; + mat_translate(ch.transform, (float []) { -0.5, 0.0, 0.0 }); + + float tmp[4][4]; + mat_rot_y(tmp, slice/2.0); + mat_mul(ch.transform, tmp); + + return ch; +} + +void cheese_demo() +{ + cheese3d_ctx ctx = cheese3d_create_default(0xFF000000); + cheese ch = make_cheese_model(8, rad(60), 500, 375); + + float angle = 0; u64 time = clock_monotonic(); for (;;) { @@ -111,15 +163,19 @@ void cheese_demo() mat_rot_y(tmp, rad(angle)); mat_mul(transform, tmp); - mat_scale(tmp, (float []){ 3.5, 3.0, 4.0 }); + mat_scale(tmp, (float []){ 4.0, 3.0, 4.0 }); mat_mul(transform, tmp); + mat_mul(transform, ch.transform); + cheese3d_clear(ctx, true, true); - cheese3d_render(ctx, 24, prism, textures, transform); + cheese3d_render(ctx, ch.num_verts, ch.vertices, ch.textures, transform); cheese3d_display(ctx); } cheese3d_destroy(ctx); - free(crust_right); - free(crust_left); + + free(ch.textures); + free(ch.vertices); + free(ch.texture); } |