From ecdc896e088f7c2a54b5d39c2056b4aed2b3e32c Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Sun, 25 Dec 2016 18:59:42 +0930 Subject: [PATCH] Fix bone direction parallel with y axis. --- README.md | 5 ++++- data/bmesh_test_1.h | 6 ++++-- src/bmesh.c | 41 ++++++++++++++++++++++++++++++++--------- src/render.cpp | 1 + 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2890f0d7..566e8d89 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,10 @@ I created the test nodes's geometry information from Blender. Here is the render *Generate Mesh* -When I am implementing the B-Mesh algorithm, I am also think in the future, how to create a library of bunch of initial base models. There is a paper [the Skeleton of a Closed 3D Shape](http://www1.idc.ac.il/icgf/GraphicsSeminar2006/DCGskeleton06.pdf) described how to generate skeleton from mesh, this is the reverse progress of what I am doing, I think it can resolve the problem of insufficient initial base models, I can generate from tons of existed models. +When I am implementing the B-Mesh algorithm, I am also think in the future, how to create a library of bunch of initial base models. There is a paper [the Skeleton of a Closed 3D Shape](http://www1.idc.ac.il/icgf/GraphicsSeminar2006/DCGskeleton06.pdf) described how to generate skeleton from mesh, this is the reverse progress of what I am doing, I think it can resolve the problem of insufficient initial base models, I can generate from tons of existed models. +*Convex Hull* +After finish the rotation at the two connected bones, I need implement [3D Convex Hull](https://en.wikipedia.org/wiki/Convex_hull) algorithm at joint ball. + - [ ] Export Wavefront .obj - [ ] Render B-Mesh result - [ ] Design UI for monster parts configuration diff --git a/data/bmesh_test_1.h b/data/bmesh_test_1.h index de88444f..5e55b3dd 100644 --- a/data/bmesh_test_1.h +++ b/data/bmesh_test_1.h @@ -3,10 +3,12 @@ const float bmeshTest1Balls[][6] = { {2, -0.91403, 0.77069, 0.62299, 0.5}, {3, 2.25224, 0.74973, 0.85115, 0.5}, {4, 0, 0, 0, 0.8, 1}, {5, 0.00920, -0.66115, -2.04601, 0.5}, - {6, 0.01726, -0.88224, -2.87471, 0.2} + {6, 0.01726, -0.88224, -2.87471, 0.2}, + {7, 0, -2, 0.00, 0.2}, + {8, -0.3, -2.8, 0.13, 0.5}, }; const int bmeshTest1Bones[][2] = { {0, 2}, {2, 4}, {4, 3}, {3, 1}, - {4, 5}, {5, 6} + {4, 5}, {5, 6}, {4, 7}, {7, 8} }; diff --git a/src/bmesh.c b/src/bmesh.c index 767aa5ce..b81ad1df 100644 --- a/src/bmesh.c +++ b/src/bmesh.c @@ -205,6 +205,20 @@ static int bmeshGenerateBallCrossSection(bmesh *bm, bmeshBall *ball, return 0; }*/ +static void generateYZfromBoneDirection(vec3 *boneDirection, + vec3 *localYaxis, vec3 *localZaxis) { + vec3 worldYaxis = {0, 1, 0}; + vec3 worldXaxis = {1, 0, 0}; + if (0 == vec3Angle(boneDirection, &worldYaxis)) { + vec3CrossProduct(&worldXaxis, boneDirection, localYaxis); + } else { + vec3CrossProduct(&worldYaxis, boneDirection, localYaxis); + } + vec3Normalize(localYaxis); + vec3CrossProduct(localYaxis, boneDirection, localZaxis); + vec3Normalize(localZaxis); +} + static int bmeshGenerateInbetweenBallsBetween(bmesh *bm, int firstBallIndex, int secondBallIndex) { float step; @@ -215,6 +229,7 @@ static int bmeshGenerateInbetweenBallsBetween(bmesh *bm, vec3 boneDirection; vec3 normalizedBoneDirection; vec3 worldYaxis = {0, 1, 0}; + vec3 worldXaxis = {1, 0, 0}; bmeshBall *firstBall = bmeshGetBall(bm, firstBallIndex); bmeshBall *secondBall = bmeshGetBall(bm, secondBallIndex); bmeshBall *newBall; @@ -227,10 +242,20 @@ static int bmeshGenerateInbetweenBallsBetween(bmesh *bm, vec3Sub(&firstBall->position, &secondBall->position, &boneDirection); normalizedBoneDirection = boneDirection; vec3Normalize(&normalizedBoneDirection); - vec3CrossProduct(&worldYaxis, &boneDirection, &localYaxis); - vec3Normalize(&localYaxis); - vec3CrossProduct(&localYaxis, &boneDirection, &localZaxis); - vec3Normalize(&localZaxis); + generateYZfromBoneDirection(&boneDirection, + &localYaxis, &localZaxis); + + glColor3f(0.0, 0.0, 0.0); + drawDebugPrintf("<%f,%f,%f> <%f,%f,%f> <%f,%f,%f>", + localYaxis.x, + localYaxis.y, + localYaxis.z, + localZaxis.x, + localZaxis.y, + localZaxis.z, + boneDirection.x, + boneDirection.y, + boneDirection.z); distance = vec3Length(&boneDirection); if (distance > BMESH_STEP_DISTANCE) { @@ -425,11 +450,8 @@ static int bmeshSweepFrom(bmesh *bm, bmeshBall *parent, bmeshBall *ball) { ball->boneDirection = parent->boneDirection; vec3RotateAlong(&ball->boneDirection, rotateAngle, &rotateAxis, &ball->boneDirection); - vec3CrossProduct(&worldYaxis, &ball->boneDirection, &ball->localYaxis); - vec3Normalize(&ball->localYaxis); - vec3CrossProduct(&ball->localYaxis, &ball->boneDirection, - &ball->localZaxis); - vec3Normalize(&ball->localZaxis); + generateYZfromBoneDirection(&ball->boneDirection, + &ball->localYaxis, &ball->localZaxis); } else { // TODO: } @@ -470,6 +492,7 @@ static bmeshBall *bmeshFindBallForConvexHull(bmesh *bm, bmeshBall *root, if (!child) { return ball; } + ball->radius = 0; return bmeshFindBallForConvexHull(bm, root, child); } diff --git a/src/render.cpp b/src/render.cpp index e113d6ed..2895fab4 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -258,6 +258,7 @@ void Render::paintGL() { bmeshGenerateInbetweenBalls(bm); bmeshSweep(bm); + bmeshStitch(bm); } drawBmeshBallRecursively(bm, bmeshGetRootBall(bm));