Fix points ordering

This commit is contained in:
hodasemi 2023-05-15 09:04:57 +02:00
parent 87cf72bbe5
commit 9e9254b04c
2 changed files with 20 additions and 1 deletions

View file

@ -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,

View file

@ -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()