From 9e9254b04c5149efb21d43d58ffc71c5dd96b1bf Mon Sep 17 00:00:00 2001 From: hodasemi Date: Mon, 15 May 2023 09:04:57 +0200 Subject: [PATCH] Fix points ordering --- src/global_state.rs | 2 +- src/polygon.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/global_state.rs b/src/global_state.rs index f504f3c..c385c09 100644 --- a/src/global_state.rs +++ b/src/global_state.rs @@ -29,7 +29,7 @@ impl GlobalState { window_extent: Vec2::default(), polygon_started: false, view_2d: true, - extrusion_height: 20.0, + extrusion_height: 50.0, offscreen_image, offscreen_image_size, diff --git a/src/polygon.rs b/src/polygon.rs index 74b6900..3edde6a 100644 --- a/src/polygon.rs +++ b/src/polygon.rs @@ -145,6 +145,8 @@ impl Polygon { } } + self.order_points(); + // create triangle mesh let triangle_mesh = self.create_triangulated_mesh(); @@ -272,6 +274,23 @@ impl Polygon { } } + fn order_points(&mut self) { + let n = Vec3::Z; + + let v1 = (self.points[0] - self.points[1]).normalize().extend(0.0); + let v2 = (self.points[1] - self.points[2]).normalize().extend(0.0); + + let c1 = v1.cross(n); + + let d = c1.dot(v2); + + // dot product is bigger than 0 if normal and connection line are facing in the same direction + // reverse the order of points, thus the face of the 3d object is directed to the outside + if d >= 0.0 { + self.points = self.points.iter().cloned().rev().collect(); + } + } + fn check_all_for_proximity(&self, point: Vec2) -> bool { self.points .iter()