diff --git a/Makefile b/Makefile index f7d50ee..604a0ac 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,8 @@ HEADERS_src_l2 := $(HEADERS_gen_l1_5) $(call find_headers,l2) #HEADERS_gen_l2 := $(HEADERS_src_l2) gen/l2/dorothy.txt HEADERS_gen_l2 := gen/l2/dorothy.txt +HEADERS_src_l3 := $(HEADERS_gen_l2) $(call find_headers,l3) + cflags := -Wall -Wextra -Werror=implicit-function-declaration -Werror=return-type -Wno-unused-parameter \ --std=c99 -g -ggdb -O0 \ -fno-trapping-math -D_POSIX_C_SOURCE=200112L -D_GNU_SOURCE @@ -145,6 +147,14 @@ out/l3/r4: src/l3/r4/R4.hs src/l2/allie/Allie.hs $(full_allie_obj) run_r4: out/l3/r4 ./out/l3/r4 +out/l3/r4b: src/l3/r4/r4.c $(HEADERS_src_l3) $(l_wl_protocols) $(ASSETS_gen_l_adele) + mkdir -p out/l3 + $(cc) $(cflags) -o $@ $< $(xdg_shell_private_c) -lvulkan -lm -lxkbcommon -lwayland-client -lpng -lfreetype + +.PHONY: run_r4b +run_r4b: out/l3/r4b + ./out/l3/r4b + .PHONY: clean clean: rm -rf gen out \ No newline at end of file diff --git a/src/l1/system/fileio.h b/src/l1/system/fileio.h index a65291c..ce32fcb 100644 --- a/src/l1/system/fileio.h +++ b/src/l1/system/fileio.h @@ -56,6 +56,7 @@ void write_file_by_path(VecU8 path, SpanU8 content){ if (fwrite(content.data, 1, content.len, fd) < content.len) { abortf("fwrite\n"); } + VecU8_drop(path); fclose(fd); } diff --git a/src/l2/anne/r4.h b/src/l2/anne/r4.h index 958b1c4..a264477 100644 --- a/src/l2/anne/r4.h +++ b/src/l2/anne/r4.h @@ -842,21 +842,79 @@ void r4_asset_gen_generic_mesh_one_fourth_of_a_cylinder_2(U64 w, U64 r, U64 k) { VecU8_fmt("l2/textures/log_%u_%u_%u_NORMAL.png", w, r, k)); } +#define pow2_14 16384 + +/* P : Vec>*/ +void r4_asset_gen_generic_mesh_horizontal_polygon(const VecVecs64vec2* P, mat3x2 tex_trans, VecU8 save_path){ + VecMarieHoleAndVertexId triangulation = marie_polygon_hole_removal_triangulation(P); + VecU64 hole_pref_vert_sz = VecU64_new_zeroinit(P->len + 1); + for (size_t h = 0; h < P->len; h++) { + hole_pref_vert_sz.buf[h + 1] = hole_pref_vert_sz.buf[h] + P->buf[h].len; + } + U64 tvn = hole_pref_vert_sz.buf[P->len]; + VecGenericMeshVertexInc vertices = VecGenericMeshVertexInc_new_zeroinit(tvn); + for (size_t h = 0; h < P->len; h++) { + for (size_t i = 0; i < P->buf[h].len; i++) { + size_t vi = hole_pref_vert_sz.buf[h] + i; + s64vec2 pos = P->buf[h].buf[i]; + vec2 pos_float_plane = {(float)pos.x / pow2_14, (float)pos.y / pow2_14}; + vec3 pos_float = {pos_float_plane.x, 0, -pos_float_plane.y}; + *VecGenericMeshVertexInc_mat(&vertices, vi) = (GenericMeshVertexInc){.pos = pos_float, + .tex = mat3x2_mul_vec3(tex_trans, vec2_and_one(pos_float_plane))}; + } + } + + VecU32 indexes = VecU32_new_zeroinit(triangulation.len); + + for (size_t ii = 0; ii < triangulation.len; ii++) { + MarieHoleAndVertexId p = triangulation.buf[ii]; + assert(p.hole_id < P->len); + assert(p.vertex_in_hole_id < P->buf[p.hole_id].len); + U64 vi = hole_pref_vert_sz.buf[p.hole_id] + p.vertex_in_hole_id; + assert(vi < tvn); + indexes.buf[ii] = (U32)vi; + } + alice_write_generic_mesh_to_file((GenericMeshTopology){.vertices = vertices, .indexes = indexes}, save_path); + + VecU64_drop(hole_pref_vert_sz); + VecMarieHoleAndVertexId_drop(triangulation); +} + +/* #.(q) */ +VecVecs64vec2 generate_funny_polygon(){ + Vecs64vec2 A = Vecs64vec2_new_zeroinit(60); + for (int i = 0; i < 60; i++) { + float x = cosf((float)i * 2 * M_PIf / 60) * 2; + float y = sinf((float)i * 2 * M_PIf / 60); + A.buf[i] = (s64vec2){(S64)roundf(x * pow2_14), (S64)roundf(y * pow2_14)}; + } + VecVecs64vec2 P = VecVecs64vec2_new(); + VecVecs64vec2_append(&P, A); + return P; +} + /* We are on l2 */ int gen_assets_for_r4() { - mkdir_nofail("l2/models"); - mkdir_nofail("l2/textures"); - mkdir_nofail("l2/textures/r4"); - r4_asset_gen_generic_mesh_one_fourth_of_a_cylinder_2(10, 2, 6); - alice_write_shiny_mesh_to_file(generate_shiny_cube((vec3){0.6f, 0.6f, 0.7f}), vcstr("l2/models/cube.AliceShinyMesh")); - alice_write_shiny_mesh_to_file(generate_shiny_lamp(0.3f, 0.13f, 0.19f), vcstr("l2/models/lamp.AliceShinyMesh")); - r4_asset_gen_generic_mesh_quad(10, 10, vcstr("l2/models/quad.AliceGenericMesh")); - r4_asset_gen_generic_mesh_cylinder(200, 0.4f, 0.17f, 30, vcstr("l2/models/puck.AliceGenericMesh"), - vcstr("l2/textures/puck_TEMPLATE.png"), vcstr("l2/textures/puck_NORMAL.png")); - r4_asset_gen_generic_mesh_cylinder(100, 0.04f, 1.5f, 4, vcstr("l2/models/stick.AliceGenericMesh"), - vcstr("l2/textures/stick_TEMPLATE.png"), vcstr("l2/textures/stick_NORMAL.png")); - r4_generate_flat_normal_map(vcstr("l2/textures/flat_NORMAL.png")); - generate_single_pixel_gray_tex(vcstr("l2/textures/no_SPECULAR.png"), 0); + // mkdir_nofail("l2/models"); + // mkdir_nofail("l2/textures"); + // mkdir_nofail("l2/textures/r4"); + // r4_asset_gen_generic_mesh_one_fourth_of_a_cylinder_2(10, 2, 6); + // alice_write_shiny_mesh_to_file(generate_shiny_cube((vec3){0.6f, 0.6f, 0.7f}), vcstr("l2/models/cube.AliceShinyMesh")); + // alice_write_shiny_mesh_to_file(generate_shiny_lamp(0.3f, 0.13f, 0.19f), vcstr("l2/models/lamp.AliceShinyMesh")); + // r4_asset_gen_generic_mesh_quad(10, 10, vcstr("l2/models/quad.AliceGenericMesh")); + // r4_asset_gen_generic_mesh_cylinder(200, 0.4f, 0.17f, 30, vcstr("l2/models/puck.AliceGenericMesh"), + // vcstr("l2/textures/puck_TEMPLATE.png"), vcstr("l2/textures/puck_NORMAL.png")); + // r4_asset_gen_generic_mesh_cylinder(100, 0.04f, 1.5f, 4, vcstr("l2/models/stick.AliceGenericMesh"), + // vcstr("l2/textures/stick_TEMPLATE.png"), vcstr("l2/textures/stick_NORMAL.png")); + // r4_generate_flat_normal_map(vcstr("l2/textures/flat_NORMAL.png")); + // generate_single_pixel_gray_tex(vcstr("l2/textures/no_SPECULAR.png"), 0); + { /* Just a test */ + VecVecs64vec2 P = generate_funny_polygon(); + r4_asset_gen_generic_mesh_horizontal_polygon(&P, (mat3x2){.x.x = 1, .y.y = 1}, + vcstr("l2/models/floor1.AliceGenericMesh")); + VecVecs64vec2_drop(P); + } + return 0; } diff --git a/src/l2/marie/shape_geom.h b/src/l2/marie/shape_geom.h index d4e8b96..200f777 100644 --- a/src/l2/marie/shape_geom.h +++ b/src/l2/marie/shape_geom.h @@ -84,8 +84,8 @@ void marie_ear_cutting_triangulation_check_vertex(Spans64vec2 P, const VecMarieE } for (U64 i = xnn; i != xp; i = rem->buf[i].next) { s64vec2 S = P.data[i]; - if (marie_precise_surface(A, B, S) >= 0 || - marie_precise_surface(B, C, S) >= 0 || marie_precise_surface(C, A, S) >= 0) { + if (marie_precise_surface(A, B, S) >= 0 && + marie_precise_surface(B, C, S) >= 0 && marie_precise_surface(C, A, S) >= 0) { return; } } @@ -132,6 +132,8 @@ VecU64 marie_polygon_ear_cutting_triangulation(Spans64vec2 P){ n--; } + VecU64_drop(ear_queue); + VecMarieEarCuttingTriangulVertState_drop(vertices); return triangles; } @@ -186,6 +188,7 @@ VecMarieHoleAndVertexId marie_polygon_hole_removal(const VecVecs64vec2* P){ VecU64 rightest_vertex = VecU64_new_zeroinit(HN); for (size_t hole = 1; hole < HN; hole++) { + printf("C1 hole = %lu\n", hole); U64 ans = UINT64_MAX; size_t hs = P->buf[hole].len; assert(hs >= 3); @@ -216,6 +219,7 @@ VecMarieHoleAndVertexId marie_polygon_hole_removal(const VecVecs64vec2* P){ VecVecVecMarieHoleAndVertexId connections = VecVecVecMarieHoleAndVertexId_new_reserved(HN); for (size_t hole = 0; hole < HN; hole++) { + printf("C2 hole = %lu\n", hole); size_t hs = P->buf[hole].len; VecVecVecMarieHoleAndVertexId_append(&connections, VecVecMarieHoleAndVertexId_new_reserved(hs)); for (size_t p = 0; p < hs; p++) { @@ -224,6 +228,7 @@ VecMarieHoleAndVertexId marie_polygon_hole_removal(const VecVecs64vec2* P){ } VecMarieHoleAndVertexId parent_vertex_of_hole = VecMarieHoleAndVertexId_new_zeroinit(HN); for (size_t hole = 1; hole < HN; hole++) { + printf("C3 hole = %lu\n", hole); size_t a_hs = P->buf[hole].len; U64 ai = rightest_vertex.buf[hole]; s64vec2 a = P->buf[hole].buf[ai]; @@ -273,6 +278,7 @@ VecMarieHoleAndVertexId marie_polygon_hole_removal(const VecVecs64vec2* P){ } for (size_t hole = 0; hole < HN; hole++) { + printf("C4 hole = %lu\n", hole); size_t hs = P->buf[hole].len; assert(hole == 0 || connections.buf[hole].buf[rightest_vertex.buf[hole]].len >= 1); for (size_t ai = 0; ai < hs; ai++) { @@ -287,12 +293,13 @@ VecMarieHoleAndVertexId marie_polygon_hole_removal(const VecVecs64vec2* P){ assert(vertex_connections->len >= 1); } } - for (size_t hole = 0; hole < HN; hole++) { + for (size_t hole = 1; hole < HN; hole++) { + printf("C5 hole = %lu\n", hole); // todo: remove debug vivod U64 a = rightest_vertex.buf[hole]; MarieHoleAndVertexId b = parent_vertex_of_hole.buf[hole]; VecMarieHoleAndVertexId_append(&connections.buf[hole].buf[a], b); } - + // todo: debug infinite loop VecVecU64 progress = VecVecU64_new_reserved(HN); for (size_t h = 0; h < HN; h++) { VecVecU64_append(&progress, VecU64_new_zeroinit(P->buf[h].len)); @@ -308,7 +315,7 @@ VecMarieHoleAndVertexId marie_polygon_hole_removal(const VecVecs64vec2* P){ break; assert(*p < conn->len); VecMarieHoleAndVertexId_append(&detour, cur); - cur = conn->buf[*(p++)]; + cur = conn->buf[(*p)++]; } VecU64_drop(rightest_vertex); diff --git a/src/l3/r4/r4.c b/src/l3/r4/r4.c index 2d2cf0a..980c917 100644 --- a/src/l3/r4/r4.c +++ b/src/l3/r4/r4.c @@ -9,15 +9,6 @@ AliceGenericMeshPath AliceGenericMeshPath_for_log(SpanU8 root_dir, U64 w, U64 r, }; } -AliceGenericMeshPath AliceGenericMeshPath_for_puck(){ - return (AliceGenericMeshPath){ - .topology_path = VecU8_fmt("gen/l2/models/puck.AliceGenericMesh"), - .diffuse_texture_path = VecU8_fmt("src/l3/textures/puck_diffuse.png"), - .normal_texture_path = VecU8_fmt("gen/l2/textures/puck_NORMAL.png"), - .specular_texture_path = VecU8_fmt("src/l3/textures/puck_specular.png"), - }; -} - vec3 project_dir_onto_plane_xz(vec3 v){ vec2 xz = vec2_normalize((vec2){v.x, v.z}); return (vec3){xz.x, 0, xz.y}; @@ -147,7 +138,23 @@ int main(){ .model_t = marie_translation_mat4((vec3){5.f, -3, 12.f}), }); - ListNodeAliceGenericMeshHand* model_puck = Alice_add_generic_mesh(st.alice, AliceGenericMeshPath_for_puck()); + ListNodeAliceGenericMeshHand* floor1_mesh = Alice_add_generic_mesh(st.alice, (AliceGenericMeshPath){ + .topology_path = vcstr("gen/l2/models/floor1.AliceGenericMesh"), + .diffuse_texture_path = vcstr("src/l3/textures/wood.png"), + .normal_texture_path = vcstr("gen/l2/textures/flat_NORMAL.png"), + .specular_texture_path = vcstr("gen/l2/textures/no_SPECULAR.png"), + }); + AliceGenericMeshHand_resize_instance_arr(alice, &floor1_mesh->el, 1); + AliceGenericMeshHand_set_inst(&floor1_mesh->el, 0, (GenericMeshInstanceInc){ + .model_t = marie_translation_mat4((vec3){0, 0, 0}), + }); + + ListNodeAliceGenericMeshHand* model_puck = Alice_add_generic_mesh(st.alice, (AliceGenericMeshPath){ + .topology_path = vcstr("gen/l2/models/puck.AliceGenericMesh"), + .diffuse_texture_path = vcstr("src/l3/textures/puck_diffuse.png"), + .normal_texture_path = vcstr("gen/l2/textures/puck_NORMAL.png"), + .specular_texture_path = vcstr("src/l3/textures/puck_specular.png"), + }); AliceGenericMeshHand_resize_instance_arr(st.alice, &model_puck->el, 100); for (int X = 0; X < 10; X++) { for (int Z = 0; Z < 10; Z++) { diff --git a/src/l3/textures/wood.png b/src/l3/textures/wood.png new file mode 100644 index 0000000..df6b9b8 Binary files /dev/null and b/src/l3/textures/wood.png differ