From 167a97e66a67c510b042e05fa2d48a2a9b1f08b1 Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Sat, 31 Dec 2016 21:34:56 +0930 Subject: [PATCH] Fix inbetween mesh normal. --- src/bmesh.c | 17 ++++++++++++++--- src/convexhull.c | 8 ++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/bmesh.c b/src/bmesh.c index 7c75cfa7..94433113 100644 --- a/src/bmesh.c +++ b/src/bmesh.c @@ -821,11 +821,22 @@ void calculateBallQuad(bmeshBall *ball, quad *q) { vec3Add(&q->pt[3], &z, &q->pt[3]); } -static void drawWallsBetweenQuads(quad *q1, quad *q2) { +static void drawWallsBetweenQuads(vec3 *origin, quad *q1, quad *q2) { int i; + vec3 normal; + vec3 o2v; for (i = 0; i < 4; ++i) { quad wall = {{q1->pt[i], q2->pt[i], q2->pt[(i + 1) % 4], q1->pt[(i + 1) % 4]}}; + vec3Normal(&wall.pt[0], &wall.pt[1], &wall.pt[2], &normal); + vec3Sub(&wall.pt[0], origin, &o2v); + if (vec3DotProduct(&o2v, &normal) < 0) { + int j; + quad oldWall = wall; + for (j = 0; j < 4; ++j) { + wall.pt[j] = oldWall.pt[3 - j]; + } + } drawQuad(&wall); } } @@ -845,7 +856,7 @@ static int bmeshGenerateInbetweenMeshFrom(bmesh *bm, bmeshBall *parent, if (parent && parent->radius > 0) { quad parentFace; calculateBallQuad(parent, &parentFace); - drawWallsBetweenQuads(&parentFace, ¤tFace); + drawWallsBetweenQuads(&ball->position, &parentFace, ¤tFace); child = bmeshGetBallFirstChild(bm, ball, &iterator); if (!child) { bmeshBall fakeParentParent = *parent; @@ -857,7 +868,7 @@ static int bmeshGenerateInbetweenMeshFrom(bmesh *bm, bmeshBall *parent, vec3Lerp(&fakeParentParent.position, &fakeParent.position, 2, &fakeBall.position); calculateBallQuad(&fakeBall, &childFace); - drawWallsBetweenQuads(¤tFace, &childFace); + drawWallsBetweenQuads(&fakeBall.position, ¤tFace, &childFace); if (vec3Distance(&ball->position, &fakeBall.position) >= ball->radius) { drawQuad(&childFace); diff --git a/src/convexhull.c b/src/convexhull.c index e028de4b..3a47b082 100644 --- a/src/convexhull.c +++ b/src/convexhull.c @@ -573,10 +573,10 @@ int convexHullMergeTriangles(convexHull *hull) { f1->vertexNum = 4; f2->vertexNum = 0; //if (edgeIndex >= 12 && edgeIndex <= 12) { - drawDebugPoint((vec3 *)arrayGetItem( - hull->vertexArray, e->p1), edgeIndex); - drawDebugPoint((vec3 *)arrayGetItem( - hull->vertexArray, e->p2), edgeIndex); + // drawDebugPoint((vec3 *)arrayGetItem( + // hull->vertexArray, e->p1), edgeIndex); + // drawDebugPoint((vec3 *)arrayGetItem( + // hull->vertexArray, e->p2), edgeIndex); //} } }