From 587104651c89beeeb382e86777e0a1b437d7c3b4 Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Sun, 12 Jan 2025 19:29:29 +0400 Subject: [PATCH] Halfedge test --- game/game.odin | 32 +++++++++++++++++++++++++++++++- game/halfedge/debug.odin | 5 ----- game/halfedge/halfedge.odin | 8 ++++---- game/physics/bvh/bvh.odin | 2 +- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/game/game.odin b/game/game.odin index db312ef..a9fe5d6 100644 --- a/game/game.odin +++ b/game/game.odin @@ -472,7 +472,6 @@ draw :: proc() { car_body := physics.get_body(&world.physics_scene, runtime_world.car_handle) car_model := assets.get_model(&g_mem.assetman, "assets/toyota_corolla_ae86_trueno.glb") - car_halfedge := halfedge.mesh_from_vertex_index_list(car_model.meshes[0].vertices, car_model.meshes[0].indices, context.temp_allocator) mesh_col: bvh.Collision hit_mesh_idx := -1 @@ -577,6 +576,36 @@ draw :: proc() { } } } + + if mesh_col.hit { + mesh := car_model.meshes[hit_mesh_idx] + vertices := (cast([^]rl.Vector3)mesh.vertices)[:mesh.vertexCount] + indices := mesh.indices[:mesh.triangleCount * 3] + car_halfedge := halfedge.mesh_from_vertex_index_list(vertices, indices, context.temp_allocator) + + face_idx := halfedge.Face_Index(mesh_col.prim) + face := car_halfedge.faces[face_idx] + first_edge_idx := face.edge + + first := true + cur_edge_idx := first_edge_idx + for first || cur_edge_idx != first_edge_idx { + first = false + edge := car_halfedge.edges[cur_edge_idx] + cur_edge_idx = edge.next + + if edge.twin >= 0 { + twin_edge := car_halfedge.edges[edge.twin] + face := twin_edge.face + + i1, i2, i3 := indices[face * 3 + 0], indices[face * 3 + 1], indices[face * 3 + 2] + v1, v2, v3 := vertices[i1], vertices[i2], vertices[i3] + + rl.DrawTriangle3D(v1, v2, v3, rl.RED) + } + } + } + } } @@ -589,6 +618,7 @@ draw :: proc() { if g_mem.editor { rl.DrawText("Editor", 5, 5, 8, rl.ORANGE) + rl.DrawText( fmt.ctprintf( "mesh: %v, aabb tests: %v, tri tests: %v", diff --git a/game/halfedge/debug.odin b/game/halfedge/debug.odin index 6bd7ece..084bcb1 100644 --- a/game/halfedge/debug.odin +++ b/game/halfedge/debug.odin @@ -1,7 +1,2 @@ package halfedge -import rl "vendor:raylib" - -debug_draw_halfedge_mesh :: proc(mesh: ^Mesh) { - -} diff --git a/game/halfedge/halfedge.odin b/game/halfedge/halfedge.odin index 26d8932..df56ef8 100644 --- a/game/halfedge/halfedge.odin +++ b/game/halfedge/halfedge.odin @@ -1,7 +1,5 @@ package halfedge -import "core:container/small_array" - Vec3 :: [3]f32 Vertex :: struct { @@ -14,7 +12,7 @@ Face :: struct { } Vertex_Index :: distinct u16 -Face_Index :: distinct i16 +Face_Index :: distinct u16 Edge_Index :: distinct i16 Half_Edge :: struct { @@ -55,7 +53,7 @@ mesh_from_vertex_index_list :: proc( triangle_num := len(indices) / 3 for i in 0 ..< triangle_num { i1, i2, i3 := indices[i * 3 + 0], indices[i * 3 + 1], indices[i * 3 + 2] - v1, v2, v3 := vertices[i1], vertices[i2], vertices[i3] + // v1, v2, v3 := vertices[i1], vertices[i2], vertices[i3] e1, e2, e3 := i * 3 + 0, i * 3 + 1, i * 3 + 2 faces[i].edge = Edge_Index(e1) @@ -98,4 +96,6 @@ mesh_from_vertex_index_list :: proc( } } } + + return mesh } diff --git a/game/physics/bvh/bvh.odin b/game/physics/bvh/bvh.odin index 323e81c..8ec1d10 100644 --- a/game/physics/bvh/bvh.odin +++ b/game/physics/bvh/bvh.odin @@ -334,7 +334,7 @@ internal_ray_tri_test :: proc(ray: Ray, mesh: Mesh, tri: u16, col: ^Collision) { v1, v2, v3 := mesh.vertices[i1], mesh.vertices[i2], mesh.vertices[i3] col.triangle_tests += 1 - rl.DrawTriangle3D(v1, v2, v3, debug.int_to_color(i32(tri) + 1)) + // rl.DrawTriangle3D(v1, v2, v3, debug.int_to_color(i32(tri) + 1)) t, _, barycentric, ok := collision.intersect_ray_triangle( {ray.origin, ray.origin + ray.dir},