From da6ea393f9123cd061df2c48a51e5123b5b41ac7 Mon Sep 17 00:00:00 2001 From: katajisto Date: Tue, 8 Jul 2025 23:27:44 +0300 Subject: [PATCH] Meshgen port complete, does not work correctly --- src/editor/trile_editor.jai | 5 +- src/meshgen.jai | 94 +++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 36 deletions(-) diff --git a/src/editor/trile_editor.jai b/src/editor/trile_editor.jai index ff8e481..54e337f 100644 --- a/src/editor/trile_editor.jai +++ b/src/editor/trile_editor.jai @@ -43,6 +43,10 @@ current_trile : Trile; #scope_file apply_tool_to_trixel :: (x: s64, y: s64, z: s64) { + //temptest: + print("Testing meshgen!\n"); + generate_mesh_matias(*current_trile); + if current_tool == .PAINT { current_trile.trixels[x][y][z].material.color = current_color; } @@ -262,7 +266,6 @@ draw_trile :: () { hovered_trixel_x = x; hovered_trixel_y = y; hovered_trixel_z = z; - print("%\n", hit.normal); min_distance = hit.distance; } } diff --git a/src/meshgen.jai b/src/meshgen.jai index 02f224f..cfe16e9 100644 --- a/src/meshgen.jai +++ b/src/meshgen.jai @@ -340,59 +340,83 @@ a_is_inside_b :: (a: Quad, b: Quad) -> bool { return false; } -/* -void removeQuad(Quad quad, std::set& quadSet){ - auto parentIt = quadSet.lower_bound(quad); - while(!isInsideOther(quad,*parentIt)){ - parentIt++; //The "parent"quad should always be found before reaching set end +remove_quad :: (quad: Quad, quadSet: *Quad_Set) { + parent : Quad; + for k,v : quadSet { + if v < quad then continue; + if a_is_inside_b(quad, v) { + table_remove(quadSet, v); + parent = v; + break; + } } - Quad parent = *parentIt; - quadSet.erase(parentIt); + + topLeft : Vector2 = .{quad.bottomLeft.x,quad.topRight.y}; + bottomRight : Vector2 = .{quad.topRight.x,quad.bottomLeft.y}; - Vector2 topLeft = {quad.bottomLeft.x,quad.topRight.y}; - Vector2 bottomRight = {quad.topRight.x,quad.bottomLeft.y}; + if quad.topRight.x != parent.topRight.x { + table_add(quadSet, .{parent.topRight,bottomRight}, true); + } - if(quad.topRight.x != parent.topRight.x){ - quadSet.insert({parent.topRight,bottomRight}); + if quad.topRight.y != parent.topRight.y { + table_add(quadSet, .{.{quad.topRight.x,parent.topRight.y},.{parent.bottomLeft.x,quad.topRight.y}}, true); } - if(quad.topRight.y != parent.topRight.y){ - quadSet.insert({quad.topRight.x,parent.topRight.y,parent.bottomLeft.x,quad.topRight.y}); + if quad.bottomLeft.y != parent.bottomLeft.y { + table_add(quadSet, .{.{parent.topRight.x,quad.bottomLeft.y},.{quad.bottomLeft.x,parent.bottomLeft.y}}, true); } - if(quad.bottomLeft.y != parent.bottomLeft.y){ - quadSet.insert({parent.topRight.x,quad.bottomLeft.y,quad.bottomLeft.x,parent.bottomLeft.y}); - } - if(quad.bottomLeft.x != parent.bottomLeft.x){ - quadSet.insert({topLeft,parent.bottomLeft}); + if quad.bottomLeft.x != parent.bottomLeft.x { + table_add(quadSet, .{topLeft,parent.bottomLeft}, true); } } -void removeNotSeen(Trile *trilept, Quads& quads){ - for(int x = 0; x < 16; x++) { - for(int y = 0; y < 16; y++){ - for(int z = 0; z < 16; z++) { - // Check for "air" contact on each side of the trixel. - char exposure = SidesWithAirExposure(trilept, x, y, z); - for(TrileSide side : TrileSideValues){ - if( !(side & exposure)){ - int *ux,*uy,*uz; - changePerspective(&x,&y,&z,ux,uy,uz,side); - removeQuad(Quad{(float)*ux+1,(float)*uy+1,(float)*ux,(float)*uy},quads[side][*uz]); +remove_not_seen :: (trilept: *Trile, quads: *Quads) { + for x: 0..15 { + for y: 0..15 { + for z: 0..15 { + exposure: u8 = sides_with_air_exposure(trilept, x, y, z); + for side: trileSideValues { + if !((cast(u8)side) & exposure) { + ux, uy, uz := change_perspective(*x, *y, *z, side); + remove_quad(.{.{cast(float)ux.*+1, cast(float)uy.*+1}, .{cast(float)ux.*, cast(float)uy.*}}, *quads.*[side_to_quad_list_index(side)][uz.*]); } } - } } } } -void generateOptimizedQuadMesh(Trile *trilept, std::vector& vecs, std::vector& normals){ - Quads quads; - initQuads(quads); - removeNotSeen(trilept,quads); - quadsToVecs(quads,vecs,normals); +generate_optimized_quad_mesh :: (trilept: *Trile, vecs: *[..]float, normals: *[..]float) { + quads : Quads; + init_quads(*quads); + remove_not_seen(trilept, *quads); + quads_to_vecs(*quads, vecs, normals); } +Pool :: #import "Pool"; +meshgenpool : Pool.Pool; + +generate_mesh_matias :: (trileptr : *Trile) { + Pool.set_allocators(*meshgenpool); + new_context := context; + new_context.allocator = .{Pool.pool_allocator_proc, *meshgenpool}; + push_context new_context { + triangleVecs : [..]float; + triangleNorms : [..]float; + quadVecs : [..]float; + quadNorms : [..]float; + + generate_basic_quad_mesh(trileptr,*quadVecs,*quadNorms); + print("Quad vecs size: %\n", quadVecs.count); + + quads_to_triangles(*quadVecs, *triangleVecs,*quadNorms, *triangleNorms); + + print("Triangle vecs size: %\n", triangleVecs.count); + } + Pool.reset(*meshgenpool); +} + +/* Mesh GenerateMeshMatias(Trile *trileptr){ std::vector quadVecs;